Last active
January 8, 2019 09:29
-
-
Save Christilut/14ec9792640e4b52c1f236b2c8461d67 to your computer and use it in GitHub Desktop.
Vue component for dynamic height show/hide transitioning
This file contains hidden or 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
.expand-enter-active, | |
.expand-leave-active { | |
transition: height 0.4s ease; | |
overflow: hidden; | |
} | |
.expand-enter, | |
.expand-leave-to { | |
height: 0; | |
} |
This file contains hidden or 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
Vue.component('transition-expand', { | |
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 = null; | |
element.style.position = null; | |
element.style.visibility = null; | |
element.style.height = 0; | |
/* eslint-enable */ | |
setTimeout(() => { | |
// 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; | |
setTimeout(() => { | |
// eslint-disable-next-line no-param-reassign | |
element.style.height = 0; | |
}); | |
}, | |
}, | |
}; | |
return createElement('transition', data, context.children); | |
}, | |
}) |
This file contains hidden or 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
<transition-expand> | |
<div class="answer" v-if='expanded'> | |
<div class="html" v-html="answer"></div> | |
</div> | |
</transition-expand> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment