Animate Primitives with CSS keyframes or the JavaScript animation library of your choice.
Adding animation to Primitives should feel similar to any other component, but there are some caveats noted here in regards to exiting animations with JS animation libraries.
The simplest way to animate Primitives is with CSS.
You can use CSS animation to animate both mount and unmount phases. The latter is possible because the Primitives will suspend unmount while your animation plays out.
When many stateful Primitives are hidden from view, they are actually removed from the React Tree, and their elements removed from the DOM. JavaScript animation libraries need control of the unmounting phase, so we provide the forceMount
prop on many components to allow consumers to delegate the mounting and unmounting of children based on the animation state determined by those libraries.
For example, if you want to use React Spring to animate a Dialog
, you would do so by conditionally rendering the dialog Overlay
and Content
parts based on the animation state from one of its hooks like useTransition
: