Build Vue's <Transition>: enter and leave class timing, duration detection, cancellation, and out-in and in-out modes

hardAbout 65 minutesUpdated CodingProvuetransitionanimationcss-transitionsstate-machine
Not solved

Share this question

Problem

Vue's <Transition> does not animate anything itself. It adds and removes CSS classes at exactly the right moments and works out when the browser has finished, so CSS does the animation. Getting those moments right is subtle: the start class must be painted before it is removed, the end must be detected from computed styles, a leave that is interrupted by an enter must be cancelled cleanly, and switching between two elements needs out-in and in-out modes. Build the engine.

const hooks = createTransition({ name: "fade" });
hooks.beforeEnter(el); // class="fade-enter-from fade-enter-active"
container.appendChild(el);
hooks.enter(el);
// two frames later: class="fade-enter-active fade-enter-to"
// after transitionend (or the timeout): class=""

Ref. UR-1463 · © UIRound · uiround.com/questions/build-vues-transition-enter-and-leave-class-timing-duration-detection-cancellation-and-out-in-and-in-out-modes

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment