Skip to content
GitHub11 stars

A guide to animating the primitives.

The primitives can be animated with CSS transitions or CSS animations. Each part renders data attributes for its states, and a few attributes that exist only for animation.

CSS transitions

Use these attributes to transition a part as it appears and disappears:

  • [data-starting-style] is the style to transition from, as the part appears.
  • [data-ending-style] is the style to transition to, as the part disappears.

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, a transition takes it smoothly back to its closed state without an abrupt jump.

app.css
.popover-popup {
  transform-origin: var(--transform-origin);
  transition: transform 150ms, opacity 150ms;
}

.popover-popup[data-starting-style],
.popover-popup[data-ending-style] {
  opacity: 0;
  transform: scale(0.9);
}

CSS animations

Use these attributes to run CSS animations as a part appears and disappears:

  • [data-open] is present while the part is shown.
  • [data-closed] is present while the part is hidden, and while it animates out.
app.css
@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);
  }
}

.popover-popup[data-open] {
  animation: scaleIn 250ms ease-out;
}

.popover-popup[data-closed] {
  animation: scaleOut 250ms ease-in forwards;
}

The styled components animate this way, with the animate-in and animate-out utilities of tw-animate-css: data-open:animate-in data-open:zoom-in-95 data-closed:animate-out data-closed:zoom-out-95.

Exit animations

A popup that closes stays in the DOM until its exit animation has finished:

  • Blazor renders data-closed and data-ending-style on it right away and makes it inert, and its trigger gets aria-expanded="false", so assistive technology and stray clicks already see a closed popup.
  • The browser waits for the popup's own animations and transitions to finish, then Blazor removes it. Infinite animations inside it, such as a spinner, are ignored, and a timeout based on the computed durations is the safety net.
  • Opening it again while it animates out cancels the exit and keeps the same element.
  • OnOpenChangeComplete on the root runs once the animation has finished, with the new open state.

Set KeepMounted on the popup's Portal part to keep it in the DOM, hidden, while it's closed. Panels that open and close in place, such as an accordion's, take KeepMounted on the panel or the root.

Animating height

Accordion and Collapsible panels set their content's height as a CSS variable, so you can transition the height between zero and the content's size:

app.css
.accordion-panel {
  height: var(--accordion-panel-height);
  overflow: hidden;
  transition: height 150ms ease-out;
}

.accordion-panel[data-starting-style],
.accordion-panel[data-ending-style] {
  height: 0;
}

Reduced motion

The primitives play whatever animation you give them, whatever the reduced-motion setting. To open and close instantly for people who ask for less motion, turn the animation off in a media query, or with Tailwind's motion-reduce: variant:

app.css
@media (prefers-reduced-motion: reduce) {
  .popover-popup {
    transition: none;
    animation: none;
  }
}