Skip to contents

Animation

A guide to animating Base UI components.

Base UI components can be animated using CSS transitions, CSS animations, or JavaScript animation libraries. Each component provides a number of data attributes to target its states, as well as a few attributes specifically for animation.

CSS transitions

Use the following Base UI attributes for creating transitions when a component becomes visible or hidden:

  • [data-starting-style] corresponds to the initial style to transition from.
  • [data-ending-style] corresponds to the final style to transition to.

Transitions are recommended over CSS animations, because a transition can be smoothly cancelled midway. For example, if the user closes a popup before it finishes opening, with CSS transitions it will smoothly animate to its closed state without any abrupt changes.

popover.css

CSS animations

Use the following Base UI attributes for creating CSS animations when a component becomes visible or hidden:

  • [data-open] corresponds to the style applied when a component becomes visible.
  • [data-closed] corresponds to the style applied before a component becomes hidden.
popover.css

JavaScript animations

JavaScript animation libraries such as Motion require control of the mounting and unmounting lifecycle of components in order for exit animations to play.

Base UI relies on element.getAnimations() to detect if animations have finished on an element. When using Motion, opacity animations are reflected in element.getAnimations(), so Base UI automatically waits for the animation finish before unmounting the component. If opacity isn’t part of your animation (such as in a translating drawer component), you should still animate it using a value close to 1 (such as opacity: 0.9999), so that Base UI can detect the animation.

Animating components unmounted from DOM when closed with Motion

Most popup components like Popover, Dialog, Tooltip, and Menu are unmounted from the DOM when they are closed by default. To animate them with Motion:

  • Make the component controlled with the open prop so <AnimatePresence> can see the state as a child
  • Specify keepMounted on the <Portal> part
  • Use the render prop to compose the <Popup> with motion.div
animated-popover.tsx

Animating components kept in DOM when closed with Motion

Components that specify keepMounted remain rendered in the DOM when they are closed. These elements need a different approach to be animated with Motion:

  • Use the render prop to compose the <Popup> with motion.div
  • Animate the properties based on the open state, avoiding <AnimatePresence>
animated-popover.tsx

Animating Select component with Motion

The Select component is initially unmounted but remains mounted after interaction. To animate it with Motion, a mix of the two previous approaches is needed.

Manual unmounting

For full control, you can manually unmount the component when it’s closed once animations have finished using an actionsRef passed to the <Root>:

manual-unmount.tsx

View Transitions

React 19.3 ships the <ViewTransition> component, which animates DOM changes using the browser’s View Transition API. Base UI components work with it as long as two conditions are met:

  • The update that changes the UI runs inside a Transition. Base UI applies its own state changes as regular updates, so control the state yourself and wrap the update in startTransition.
  • React, not Base UI, removes the elements being animated. Components like <Tabs.Panel> stay mounted while their CSS exit animation plays and are removed a frame later, outside of the Transition, which makes the browser skip the view transition. Render such elements conditionally so they leave the tree within the Transition.

The ::view-transition-* pseudo-elements are rendered on the root element, so their styles must live in a global stylesheet rather than a CSS Module.

When a Transition only changes the elements inside <ViewTransition> boundaries, React excludes the root element from the view transition so the rest of the page stays interactive. Chrome doesn’t paint position: fixed elements during such a transition, so give fixed elements like headers and sidebars their own view-transition-name.

Animating Tabs panels with View Transitions

Render only the active panel inside a <ViewTransition> so React can play its enter and exit animations. Pass the activationDirection from the change event details to addTransitionType to slide the panels toward the newly active tab.

Workspace stats and activity.

Animating list changes with View Transitions

Wrap each list item in a <ViewTransition> so React assigns it a stable view-transition-name. When a control such as <ToggleGroup> changes the order of the list inside a Transition, the items animate to their new positions.

  • index.tsx12 KB
  • logo.svg30 KB
  • package.json2 KB
  • README.md4 KB
  • styles.css7 KB

Popups

Dialog and Popover can animate with <ViewTransition> when open is controlled inside startTransition, the portal uses keepMounted, and the popup and its View Transition boundary are rendered conditionally from open. Keeping the portal mounted lets its DOM container exist before the opening Transition.

For Popover, place <Popover.Positioner> inside the <ViewTransition> boundary so the positioning element participates in the transition.