createTransitionPresence
Keep an element mounted until its CSS exit animation finishes.
createTransitionPresence keeps conditionally rendered elements mounted while their CSS exit animation runs. When open becomes false, present() stays true until the registered elements’ exit animations settle. Render the element with <Show when={present()}> so it unmounts only after the animation ends.
Import#
Signature#
Usage Example#
src/AnimatedDialog.tsx
Details#
- Register each animating element with
registerElement, which returns a release function. Register from the component that renders the element, not inside itsrefcallback, so the owner releases the registration on unmount. The hook listens foranimationstart,animationend, andanimationcancelon the element. present()starts as the initialopen()value. It turnstrueas soon asopenistrue.- With no registered element,
present()turnsfalseas soon asopenisfalse. onExitCompleteruns once the exit settles andpresent()turnsfalse.- Zero-duration and cancelled animations complete the exit immediately, so a missing or interrupted animation does not leave the element mounted.