Overview
ThePresence component enables animation of component mount and unmount transitions. It keeps components in the DOM during exit animations and provides hooks for advanced animation control.
Import
Presence Component
Props
boolean
required
Whether the component should be present in the DOM.
React.ReactElement | ((props: RenderPropContext) => React.ReactElement)
required
A single React element with a ref prop, or a render function that receives presence context.
'animation' | 'transition'
default:"'animation'"
The type of CSS animation to use. Use ‘animation’ for CSS animations and ‘transition’ for CSS transitions.
boolean
default:"false"
When true, forces the component to always be mounted regardless of the present state.
() => void
Callback invoked when the exit animation completes.
string
Additional CSS class names to apply.
usePresence Hook
Options
boolean
required
Whether the component should be present.
'animation' | 'transition'
default:"'animation'"
The type of CSS animation being used.
() => void
Callback invoked when exit completes.
Returns
boolean
Whether the element is currently present in the state machine.
boolean
Whether the element is present or has completed its transition.
boolean
Whether a transition should start.
React.Ref<HTMLElement>
Ref to attach to the animated element.
{ getPresenceProps: (props) => props }
Prop getter function for applying presence attributes.
Type Definitions
Data Attributes
The component adds these data attributes for styling:data-present: “true” when the element is presentdata-present-or-transition-complete: “true” when present or transition is completedata-state: Current state (“mounted”, “unmountSuspended”, “unmounted”)data-transition: “active” or “inactive” (only whenvariant="transition")
Usage Examples
Basic Animation
CSS Transitions
Render Function
Exit Callback
Force Mount
Using the Hook
Conditional Content Based on State
Notes
- The component uses a state machine internally to manage mount/unmount states
- For
variant="animation", it detects animation name changes to determine when to suspend unmounting - For
variant="transition", it listens totransitionrunandtransitionendevents - The child element must accept a
refprop - Only one child element is allowed (or a render function that returns one element)
- The component sets
animationFillMode: 'forwards'during exit to prevent flashing - Based on Radix UI’s Presence implementation