Last active
April 29, 2020 14:12
-
-
Save motion-work/bf6200aefe1098ca3edcaac632f38f61 to your computer and use it in GitHub Desktop.
TransitionExpand.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
export default { | |
name: `TransitionExpand`, | |
functional: true, | |
render(createElement, context) { | |
const data = { | |
props: { | |
name: `expand`, | |
}, | |
on: { | |
afterEnter(element) { | |
// eslint-disable-next-line no-param-reassign | |
element.style.height = `auto`; | |
}, | |
enter(element) { | |
const { width } = getComputedStyle(element); | |
/* eslint-disable no-param-reassign */ | |
element.style.width = width; | |
element.style.position = `absolute`; | |
element.style.visibility = `hidden`; | |
element.style.height = `auto`; | |
/* eslint-enable */ | |
const { height } = getComputedStyle(element); | |
/* eslint-disable no-param-reassign */ | |
element.style.width = ''; | |
element.style.position = ''; | |
element.style.visibility = ''; | |
element.style.height = 0; | |
/* eslint-enable */ | |
// Force repaint to make sure the | |
// animation is triggered correctly. | |
// eslint-disable-next-line no-unused-expressions | |
getComputedStyle(element).height; | |
requestAnimationFrame(() => { | |
// eslint-disable-next-line no-param-reassign | |
element.style.height = height; | |
}); | |
}, | |
leave(element) { | |
const { height } = getComputedStyle(element); | |
// eslint-disable-next-line no-param-reassign | |
element.style.height = height; | |
// Force repaint to make sure the | |
// animation is triggered correctly. | |
// eslint-disable-next-line no-unused-expressions | |
getComputedStyle(element).height; | |
requestAnimationFrame(() => { | |
// eslint-disable-next-line no-param-reassign | |
element.style.height = 0; | |
}); | |
}, | |
}, | |
}; | |
return createElement(`transition`, data, context.children); | |
}, | |
}; | |
</script> | |
<style scoped> | |
* { | |
will-change: height; | |
transform: translateZ(0); | |
backface-visibility: hidden; | |
perspective: 1000px; | |
} | |
</style> | |
<style> | |
.expand-enter-active, | |
.expand-leave-active { | |
transition: height 0.15s ease-in-out; | |
overflow: hidden; | |
} | |
.expand-enter, | |
.expand-leave-to { | |
height: 0; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
then use it like that:
<TransitionExpand> <div v-if="open"> ... </div> </TransitionExpand>