Skip to main content
Presence is a component that helps you animate elements in and out of the DOM. It keeps elements mounted during exit animations and provides a present state to child components, enabling smooth mount and unmount animations.

Installation

Component

Presence

Props

boolean
required
Whether the component should be present (mounted) in the DOM. When false, the component remains mounted until exit animations complete.
React.ReactElement | ((props: { present: boolean }) => React.ReactElement)
required
The element to animate. Can be a React element or a render function that receives present state.

Usage

Basic Fade Animation

Using Render Function

Slide Animation

Scale Animation

Multiple Animations

Conditional Content Based on State

How It Works

Presence uses a state machine with three states:
  1. mounted: Component is present and visible
  2. unmountSuspended: Component is animating out (exit animation)
  3. unmounted: Component is removed from DOM

State Transitions

  • When present changes from false to true: unmounted → mounted
  • When present changes from true to false:
    • If CSS animation detected: mounted → unmountSuspended
    • When animation ends: unmountSuspended → unmounted
    • If no animation: mounted → unmounted (immediate)

Animation Detection

The component:
  • Reads computed styles to detect CSS animations
  • Listens to animationend, animationcancel, and animationstart events
  • Waits for animations to complete before unmounting
  • Handles animation interruptions (e.g., when present changes mid-animation)

Important Notes

The component detects CSS animations by reading the animation-name computed style. Make sure your exit animations have a different animation-name than entry animations.
If the element has display: none or no animation defined, it will unmount immediately when present becomes false.
The component uses useLayoutEffect to synchronously detect animation changes before the browser paints, preventing flashing.
When using the render function pattern, the present prop lets you conditionally render content or apply different styles during entry and exit.

Best Practices

  1. Always define exit animations - Without them, the component unmounts immediately
  2. Use different animation names - Entry and exit animations should have distinct names
  3. Set animation-fill-mode - The component sets forwards during exit to prevent flashing
  4. Test animation timing - Ensure animations complete before state changes occur

Browser Compatibility

The component relies on:
  • CSS Animations API
  • getComputedStyle()
  • Animation events (animationend, animationcancel, animationstart)
These are supported in all modern browsers.