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 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.
.Popup { box-sizing: border-box; padding: 1rem 1.5rem; background-color: canvas; transform-origin: var(--transform-origin); transition: transform 150ms, opacity 150ms;
&[data-starting-style], &[data-ending-style] { opacity: 0; transform: scale(0.9); }}
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 the style applied before a component becomes hidden.
@keyframes scaleIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: scale(1); }}
@keyframes scaleOut { from { opacity: 1; transform: scale(1); } to { opacity: 0; transform: scale(0.9); }}
.Popup[data-open] { animation: scaleIn 250ms ease-out;}
.Popup[data-closed] { animation: scaleOut 250ms ease-in;}
JavaScript animations
JavaScript animation libraries such as Motion require control of the mounting and unmounting lifecycle of components.
Most Base UI components are unmounted when hidden. These components usually provide the keepMounted
prop to allow JavaScript animation libraries to take control.