|
1 |
| -<script lang="ts"> |
| 1 | +<template> |
| 2 | + <Transition v-bind="computedAttrs"> |
| 3 | + <slot /> |
| 4 | + </Transition> |
| 5 | +</template> |
| 6 | + |
| 7 | +<script setup lang="ts"> |
2 | 8 | import type {Booleanish, TransitionMode} from '../../types'
|
3 | 9 | import {isPlainObject} from '../../utils'
|
4 |
| -import {defineComponent, h, PropType, ref, toRef, Transition} from 'vue' |
| 10 | +import {computed, toRef} from 'vue' |
5 | 11 | import {useBooleanish} from '../../composables'
|
6 | 12 |
|
7 |
| -const NO_FADE_PROPS = { |
8 |
| - name: '', |
9 |
| - enterActiveClass: '', |
10 |
| - enterToClass: '', |
11 |
| - leaveActiveClass: '', |
12 |
| - leaveToClass: 'showing', |
13 |
| - enterFromClass: 'showing', |
14 |
| - leaveFromClass: '', |
15 |
| -} |
16 |
| -
|
17 |
| -const FADE_PROPS = { |
18 |
| - ...NO_FADE_PROPS, |
19 |
| - enterActiveClass: 'fade showing', |
20 |
| - leaveActiveClass: 'fade showing', |
| 13 | +interface Props { |
| 14 | + appear?: Booleanish |
| 15 | + mode?: TransitionMode |
| 16 | + noFade?: Booleanish |
| 17 | + transProps?: Record<string, unknown> |
21 | 18 | }
|
22 | 19 |
|
23 |
| -export default defineComponent({ |
24 |
| - props: { |
25 |
| - appear: {type: [Boolean, String] as PropType<Booleanish>, default: false}, |
26 |
| - mode: {type: String as PropType<TransitionMode>, required: false}, |
27 |
| - noFade: {type: [Boolean, String] as PropType<Booleanish>, default: false}, |
28 |
| - transProps: {type: Object, required: false}, |
29 |
| - }, |
30 |
| - setup(props, {slots}) { |
31 |
| - const appearBoolean = useBooleanish(toRef(props, 'appear')) |
32 |
| - const noFadeBoolean = useBooleanish(toRef(props, 'noFade')) |
| 20 | +const props = withDefaults(defineProps<Props>(), { |
| 21 | + appear: false, |
| 22 | + noFade: false, |
| 23 | +}) |
33 | 24 |
|
34 |
| - const transProperties = ref(props.transProps) |
35 |
| - if (!isPlainObject(transProperties.value)) { |
36 |
| - transProperties.value = noFadeBoolean.value ? NO_FADE_PROPS : FADE_PROPS |
37 |
| - if (appearBoolean.value) { |
38 |
| - // Default the appear classes to equal the enter classes |
39 |
| - transProperties.value = { |
40 |
| - ...transProperties.value, |
41 |
| - appear: true, |
42 |
| - appearClass: transProperties.value.enterClass, |
43 |
| - appearActiveClass: transProperties.value.enterActiveClass, |
44 |
| - appearToClass: transProperties.value.enterToClass, |
45 |
| - } |
46 |
| - } |
47 |
| - } |
48 |
| - transProperties.value = { |
49 |
| - mode: props.mode, |
50 |
| - ...transProperties.value, |
51 |
| - // We always need `css` true |
52 |
| - css: true, |
53 |
| - } |
| 25 | +const appearBoolean = useBooleanish(toRef(props, 'appear')) |
| 26 | +const noFadeBoolean = useBooleanish(toRef(props, 'noFade')) |
54 | 27 |
|
55 |
| - return () => |
56 |
| - h( |
57 |
| - Transition, |
58 |
| - {...transProperties.value}, |
59 |
| - { |
60 |
| - default: () => (slots.default ? slots.default() : []), |
61 |
| - } |
62 |
| - ) |
63 |
| - }, |
| 28 | +const fadeProperties = computed(() => { |
| 29 | + const NO_FADE_PROPS = { |
| 30 | + name: '', |
| 31 | + enterActiveClass: '', |
| 32 | + enterToClass: '', |
| 33 | + leaveActiveClass: '', |
| 34 | + leaveToClass: 'showing', |
| 35 | + enterFromClass: 'showing', |
| 36 | + leaveFromClass: '', |
| 37 | + } |
| 38 | + const FADE_PROPS = { |
| 39 | + ...NO_FADE_PROPS, |
| 40 | + enterActiveClass: 'fade showing', |
| 41 | + leaveActiveClass: 'fade showing', |
| 42 | + } |
| 43 | + return noFadeBoolean.value ? NO_FADE_PROPS : FADE_PROPS |
64 | 44 | })
|
| 45 | +
|
| 46 | +const baseProperties = computed(() => ({mode: props.mode, css: true, ...fadeProperties.value})) |
| 47 | +
|
| 48 | +const computedAttrs = computed(() => |
| 49 | + isPlainObject(props.transProps) |
| 50 | + ? { |
| 51 | + // Order matters here since the props.transProps would get overwritten if it came first |
| 52 | + // But the goal of props.transProps is to overwrite base properties |
| 53 | + ...baseProperties.value, |
| 54 | + ...props.transProps, |
| 55 | + } |
| 56 | + : appearBoolean.value |
| 57 | + ? { |
| 58 | + ...baseProperties.value, |
| 59 | + appear: true, |
| 60 | + appearActiveClass: fadeProperties.value.enterActiveClass, |
| 61 | + appearToClass: fadeProperties.value.enterToClass, |
| 62 | + } |
| 63 | + : baseProperties.value |
| 64 | +) |
65 | 65 | </script>
|
0 commit comments