Accordion: animate content height on open/close #1225
-
Hey guys! Is it possible to animate accordion item content height on toggle? I know how to animate the node, but how can I listen to toggle event for each item? Thank you! |
Beta Was this translation helpful? Give feedback.
Answered by
bogdan0083
Feb 6, 2024
Replies: 1 comment
-
I managed to figure out how to animate accordion item content height by myself.
<Transition @enter="onEnter" @leave="onLeave">
<div
v-bind="api.getItemContentProps({ value: item.title })"
class="text-xs overflow-hidden"
ref="itemContentRefs"
v-if="api.value.includes(item.title)"
>
<div class="py-3 px-4">
{{ item.content }}
</div>
</div>
</Transition>
const onTransitionEnd = (node, done) => {
node.classList.remove("accordion-transitioning");
node.removeEventListener("transitionend", onTransitionEnd, false);
done();
};
const onEnter = (node, done) => {
const h = node.clientHeight;
node.style.height = "0px";
setTimeout(() => {
node.classList.add("accordion-transitioning");
node.style.height = h + "px";
node.addEventListener("transitionend", () => onTransitionEnd(node, done));
}, 0);
};
const onLeave = (node, done) => {
const h = node.clientHeight;
node.style.height = h + "px";
setTimeout(() => {
node.classList.add("accordion-transitioning");
node.style.height = "0px";
node.addEventListener("transitionend", () => onTransitionEnd(node, done));
}, 0);
}; The code is quite repetitive and not very DRY, but it works just fine! I hope someone finds it useful 😄 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
bogdan0083
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I managed to figure out how to animate accordion item content height by myself.
Transition
component. Please note that I also addedv-if
to make sure transitionenter/leave
functions will be triggered:onEnter
andonLeave
: