Skip to the content

useExitTransition

useExitTransition keeps an element mounted until its CSS exit transition finishes. When open turns false, the element re-renders with data-state="closing" — the hidden state of the motion presets — and unmounts on transitionend. Modal, Sheet, Tooltip and the toasts use it.
Hooks

Signature

const { mounted, state, ref } = useExitTransition(open: boolean, opt?: { property?: string });
Name
Type
Description
open
boolean
Whether the element is requested.
opt.property
string
Only a transitionend for this CSS property completes the exit.
mounted
boolean
Render the element while true; turns false after the exit.
state
'open' | 'closing'
Mirror it as data-state={state} on the animated element.
ref
(node: HTMLElement | null) => void
Attach it to the element whose transition drives the unmount.
Scrolls sideways inside the table. The page does not.

Example

import { Flex } from '@apollion-dsi/core/containers/flex';
import { useExitTransition } from '@apollion-dsi/core/hooks';

function Banner({ open, children }) {
  const { mounted, state, ref } = useExitTransition(open);

  if (!mounted) return null;
  return (
    <Flex ref={ref} motion="slide-up" data-state={state}>
      {children}
    </Flex>
  );
}

Pitfalls

  • When the element's computed transition time is zero (or it has no transition), it unmounts immediately.
  • Attach ref to the element that carries the transition. Transitions on its children do not count.
  • transitionend can be lost; a timeout at the computed duration plus 50 ms completes the exit anyway.
  • A closing element that carries a live region (role="status") should get aria-hidden while state is 'closing', so it is not announced on its way out.
  • It handles exits only. Enter animations come from CSS @starting-style in the presets.

See also