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