Line data Source code
1 : <script setup lang="ts">
2 : import { computed, onMounted, ref } from 'vue'
3 :
4 : // From: https://stackoverflow.com/a/71426342/22392721
5 : interface Props {
6 : duration?: number
7 : easingEnter?: string
8 : easingLeave?: string
9 : opacityClosed?: number
10 : opacityOpened?: number
11 : disable?: boolean
12 : }
13 :
14 : const {
15 : duration = 150,
16 : easingEnter = 'ease-in-out',
17 : easingLeave = 'ease-in-out',
18 : opacityClosed = 0,
19 : opacityOpened = 1,
20 : disable
21 : } = defineProps<Props>()
22 :
23 2355 : const closed = '0px'
24 2355 :
25 657 : const isMounted = ref(false)
26 657 : onMounted(() => (isMounted.value = true))
27 :
28 657 : const animationDuration = computed(() =>
29 : isMounted.value && !disable ? duration : 0
30 : )
31 :
32 : interface initialStyle {
33 : height: string
34 : width: string
35 : position: string
36 : visibility: string
37 : overflow: string
38 : paddingTop: string
39 : paddingBottom: string
40 : borderTopWidth: string
41 : borderBottomWidth: string
42 : marginTop: string
43 : marginBottom: string
44 : }
45 :
46 171 : function getElementStyle(element: HTMLElement) {
47 171 : return {
48 : height: element.style.height,
49 : width: element.style.width,
50 : position: element.style.position,
51 : visibility: element.style.visibility,
52 : overflow: element.style.overflow,
53 : paddingTop: element.style.paddingTop,
54 : paddingBottom: element.style.paddingBottom,
55 : borderTopWidth: element.style.borderTopWidth,
56 : borderBottomWidth: element.style.borderBottomWidth,
57 : marginTop: element.style.marginTop,
58 : marginBottom: element.style.marginBottom
59 : }
60 : }
61 :
62 94 : function prepareElement(element: HTMLElement, initialStyle: initialStyle) {
63 94 : const { width } = getComputedStyle(element)
64 94 : element.style.width = width
65 94 : element.style.position = 'absolute'
66 94 : element.style.visibility = 'hidden'
67 94 : element.style.height = ''
68 94 : const { height } = getComputedStyle(element)
69 94 : element.style.width = initialStyle.width
70 94 : element.style.position = initialStyle.position
71 94 : element.style.visibility = initialStyle.visibility
72 94 : element.style.height = closed
73 94 : element.style.overflow = 'hidden'
74 94 : return initialStyle.height && initialStyle.height !== closed
75 : ? initialStyle.height
76 : : height
77 : }
78 :
79 171 : function animateTransition(
80 : element: HTMLElement,
81 : initialStyle: initialStyle,
82 : done: () => void,
83 : keyframes: Keyframe[] | PropertyIndexedKeyframes | null,
84 : options?: number | KeyframeAnimationOptions
85 : ) {
86 171 : const animation = element.animate(keyframes, options)
87 171 : element.style.height = initialStyle.height
88 171 : animation.onfinish = () => {
89 148 : element.style.overflow = initialStyle.overflow
90 148 : done()
91 : }
92 : }
93 :
94 171 : function getEnterKeyframes(height: string, initialStyle: initialStyle) {
95 171 : return [
96 : {
97 : height: closed,
98 : opacity: opacityClosed,
99 : paddingTop: closed,
100 : paddingBottom: closed,
101 : borderTopWidth: closed,
102 : borderBottomWidth: closed,
103 : marginTop: closed,
104 : marginBottom: closed
105 : },
106 : {
107 : height,
108 : opacity: opacityOpened,
109 : paddingTop: initialStyle.paddingTop,
110 : paddingBottom: initialStyle.paddingBottom,
111 : borderTopWidth: initialStyle.borderTopWidth,
112 : borderBottomWidth: initialStyle.borderBottomWidth,
113 : marginTop: initialStyle.marginTop,
114 : marginBottom: initialStyle.marginBottom
115 : }
116 : ]
117 : }
118 :
119 94 : function enterTransition(element: Element, done: () => void) {
120 94 : const HTMLElement = element as HTMLElement
121 94 : const initialStyle = getElementStyle(HTMLElement)
122 : const height = prepareElement(HTMLElement, initialStyle)
123 : const keyframes = getEnterKeyframes(height, initialStyle)
124 : const options = { duration: animationDuration.value, easing: easingEnter }
125 94 : animateTransition(HTMLElement, initialStyle, done, keyframes, options)
126 : }
127 :
128 77 : function leaveTransition(element: Element, done: () => void) {
129 77 : const HTMLElement = element as HTMLElement
130 77 : const initialStyle = getElementStyle(HTMLElement)
131 77 : const { height } = getComputedStyle(HTMLElement)
132 77 : HTMLElement.style.height = height
133 77 : HTMLElement.style.overflow = 'hidden'
134 : const keyframes = getEnterKeyframes(height, initialStyle).reverse()
135 : const options = { duration: animationDuration.value, easing: easingLeave }
136 77 : animateTransition(HTMLElement, initialStyle, done, keyframes, options)
137 : }
138 657 : </script>
139 :
140 : <template>
141 732 : <Transition :css="false" @enter="enterTransition" @leave="leaveTransition">
142 : <slot />
143 : </Transition>
144 : </template>
|