Skip to content

Instantly share code, notes, and snippets.

@Christilut
Last active January 8, 2019 09:29
Show Gist options
  • Save Christilut/14ec9792640e4b52c1f236b2c8461d67 to your computer and use it in GitHub Desktop.
Save Christilut/14ec9792640e4b52c1f236b2c8461d67 to your computer and use it in GitHub Desktop.
Vue component for dynamic height show/hide transitioning
.expand-enter-active,
.expand-leave-active {
transition: height 0.4s ease;
overflow: hidden;
}
.expand-enter,
.expand-leave-to {
height: 0;
}
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);
},
})
<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