Skip to content

Instantly share code, notes, and snippets.

@motion-work
Last active April 29, 2020 14:12
Show Gist options
  • Save motion-work/bf6200aefe1098ca3edcaac632f38f61 to your computer and use it in GitHub Desktop.
Save motion-work/bf6200aefe1098ca3edcaac632f38f61 to your computer and use it in GitHub Desktop.
TransitionExpand.vue
<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>
@motion-work
Copy link
Author

motion-work commented Mar 4, 2020

then use it like that:

<TransitionExpand> <div v-if="open"> ... </div> </TransitionExpand>

@okaufmann
Copy link

great <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment