refactor(presence): enhance slot handling and attribute forwarding
This commit is contained in:
@@ -3,9 +3,14 @@ export interface PresenceProps {
|
||||
present: boolean;
|
||||
forceMount?: boolean;
|
||||
}
|
||||
|
||||
export default {
|
||||
inheritAttrs: false,
|
||||
};
|
||||
</script>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { Slot } from '../primitive/Slot';
|
||||
import { usePresence } from './usePresence';
|
||||
|
||||
const {
|
||||
@@ -23,6 +28,7 @@ defineExpose({ present: isPresent });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<!-- @vue-expect-error ref is forwarded to slot -->
|
||||
<slot v-if="forceMount || present || isPresent" :ref="setRef" :present="isPresent" />
|
||||
<Slot v-if="forceMount || isPresent" :ref="setRef" v-bind="$attrs">
|
||||
<slot :present="isPresent" />
|
||||
</Slot>
|
||||
</template>
|
||||
|
||||
@@ -203,6 +203,67 @@ describe('Presence', () => {
|
||||
expect(slotPresent).toBe(false);
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it('forwards attrs to slot child', () => {
|
||||
const wrapper = mount(Presence, {
|
||||
props: { present: true },
|
||||
attrs: { class: 'fade', 'data-testid': 'box' },
|
||||
slots: { default: () => h('div', 'content') },
|
||||
});
|
||||
|
||||
const div = wrapper.find('div');
|
||||
expect(div.classes()).toContain('fade');
|
||||
expect(div.attributes('data-testid')).toBe('box');
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it('forwards style to slot child', () => {
|
||||
const wrapper = mount(Presence, {
|
||||
props: { present: true },
|
||||
attrs: { style: 'color: red' },
|
||||
slots: { default: () => h('div', 'content') },
|
||||
});
|
||||
|
||||
expect(wrapper.find('div').attributes('style')).toContain('color: red');
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it('merges attrs when child already has attrs', () => {
|
||||
const wrapper = mount(Presence, {
|
||||
props: { present: true },
|
||||
attrs: { class: 'outer' },
|
||||
slots: { default: () => h('div', { class: 'inner' }, 'content') },
|
||||
});
|
||||
|
||||
const div = wrapper.find('div');
|
||||
expect(div.classes()).toContain('outer');
|
||||
expect(div.classes()).toContain('inner');
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it('does not render attrs when not present', () => {
|
||||
const wrapper = mount(Presence, {
|
||||
props: { present: false },
|
||||
attrs: { class: 'fade' },
|
||||
slots: { default: () => h('div', 'content') },
|
||||
});
|
||||
|
||||
expect(wrapper.find('div').exists()).toBe(false);
|
||||
wrapper.unmount();
|
||||
});
|
||||
|
||||
it('forwards attrs with forceMount', () => {
|
||||
const wrapper = mount(Presence, {
|
||||
props: { present: false, forceMount: true },
|
||||
attrs: { class: 'fade', 'data-testid': 'forced' },
|
||||
slots: { default: () => h('div', 'content') },
|
||||
});
|
||||
|
||||
const div = wrapper.find('div');
|
||||
expect(div.classes()).toContain('fade');
|
||||
expect(div.attributes('data-testid')).toBe('forced');
|
||||
wrapper.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
describe('usePresence (animation)', () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { SetupContext } from 'vue';
|
||||
import { cloneVNode, warn } from 'vue';
|
||||
import type { SetupContext } from 'vue';
|
||||
import { getRawChildren } from '../utils/getRawChildren';
|
||||
|
||||
type FunctionalComponentContext = Omit<SetupContext, 'expose'>;
|
||||
|
||||
Reference in New Issue
Block a user