Slab
Slab

Guide

Animate opening and closing

Popovers, menus and dialogs vanish the moment they close, as their native elements do. Two CSS features animate them in and out anyway, with no script.

01 Why closing doesn't animate

A closed Popover isn't displayed, and neither is a closed Dialog, Menu or Select, because the native element inside each one isn't. So there's nothing for a transition to start from when it opens, and nothing left to see by the time it would finish closing. Two CSS features fix that. @starting-style gives an element that's just been shown a style to transition from. transition-behavior: allow-discrete lets display transition, so a closing element stays shown until its other transitions end. Anything in the top layer, like a popover or a modal dialog, transitions overlay the same way, so it stays on top while it closes.

02 Key it on the open state

Every element that opens has :state(open), from the moment it opens until the moment it closes, however it got there. So it's the same three rules each time. The part gets its closed style and its transitions. :state(open) on the host gives the part its open style. And @starting-style gives the open part its closed style again, as where to start from. [open] works too on everything but Toast, since open reflects, but :state(open) is on every one.

  • ::part(popover)

    Popover, Menu, Context Menu, Select, Date Picker and Date Range Picker.

  • ::part(listbox)

    Combobox, whose Options open in a popover beside the input.

  • ::part(tooltip) and ::part(card)

    Tooltip and Hover Card.

  • ::part(dialog)

    Dialog, Alert Dialog, Drawer and Command Palette. A modal one has a ::backdrop to animate too.

  • ::part(details)::details-content

    Disclosure, and each Disclosure in an Accordion.

  • ::part(content)

    Navigation Menu Item's links. They're in the page, not the top layer, so there's no overlay to transition.

03 A popover

It fades in and drops into place, and does the same in reverse when it closes, whether that was a click on its anchor, a click outside, or Escape. Swap the tag and the part for any element in the first four tiles above.

Ana and Chen are setting forms. Ben brings the pump at 6:30.
CSS
slab-popover::part(popover) {
  opacity: 0;
  translate: 0 -0.5rem;
  transition:
    opacity 150ms ease-out,
    translate 150ms ease-out,
    display 150ms allow-discrete,
    overlay 150ms allow-discrete;
}

slab-popover:state(open)::part(popover) {
  opacity: 1;
  translate: none;
}

@starting-style {
  slab-popover:state(open)::part(popover) {
    opacity: 0;
    translate: 0 -0.5rem;
  }
}
HTML
<slab-popover>
  <button slot="anchor" type="button">Who's on site?</button>
  Ana and Chen are setting forms. Ben brings the pump at 6:30.
</slab-popover>

display and overlay take the same duration as the rest, so the popover is shown and on top until the fade has finished.

04 A dialog and its backdrop

A modal Dialog's backdrop is a pseudo-element of its dialog part, and transitions on its own. Give it the same display and overlay transitions, or it's gone the moment the dialog starts to close.

Forms Thursday, pour Friday at 7, cure through the 23rd.

CSS
slab-dialog::part(dialog) {
  opacity: 0;
  scale: 0.95;
  transition:
    opacity 200ms ease-out,
    scale 200ms ease-out,
    display 200ms allow-discrete,
    overlay 200ms allow-discrete;
}

slab-dialog:state(open)::part(dialog) {
  opacity: 1;
  scale: none;
}

slab-dialog::part(dialog)::backdrop {
  background-color: rgb(0 0 0 / 0);
  transition:
    background-color 200ms ease-out,
    display 200ms allow-discrete,
    overlay 200ms allow-discrete;
}

slab-dialog:state(open)::part(dialog)::backdrop {
  background-color: rgb(0 0 0 / 0.5);
}

@starting-style {
  slab-dialog:state(open)::part(dialog) {
    opacity: 0;
    scale: 0.95;
  }

  slab-dialog:state(open)::part(dialog)::backdrop {
    background-color: rgb(0 0 0 / 0);
  }
}
HTML
<button type="button" data-show-modal="animated-dialog">Pour schedule</button>

<slab-dialog id="animated-dialog" label="Pour schedule">
  <p>Forms Thursday, pour Friday at 7, cure through the 23rd.</p>

  <form method="dialog">
    <button>Close</button>
  </form>
</slab-dialog>

Drawer works the same way. Slide it in with translate instead of scale, from the edge it opens on.

05 A disclosure

Disclosure is a <details>, and what it hides is that element's ::details-content, which closes with content-visibility: hidden rather than display: none. So that's what transitions as a discrete property. To grow it from nothing to its height, it needs a size a transition can reach: calc-size(auto, size), which browsers that don't support it drop for plain auto, opening and closing at once as they would without the transition.

4,000 PSI concrete with a water reducer, poured at a 4-inch slump.
CSS
slab-disclosure::part(details)::details-content {
  block-size: 0;
  overflow: clip;
  transition:
    block-size 250ms ease-out,
    content-visibility 250ms allow-discrete;
}

slab-disclosure[open]::part(details)::details-content,
slab-disclosure::part(details):open::details-content {
  block-size: auto;
  block-size: calc-size(auto, size);
}
HTML
<slab-disclosure label="What's in the mix?">
  4,000 PSI concrete with a water reducer, poured at a 4-inch slump.
</slab-disclosure>

There's no @starting-style here, because a closed <details> keeps its content's box, so the transition already has a start. The second selector is for Safari, which doesn't restyle ::details-content through the host when it's opened by a click. :open matches the <details> itself.

06 Less motion

Someone who asks for less motion still needs to see that something opened. Keep the fade and leave the movement out of transition, so it's in place at once. Or turn the transition off altogether. Slab leaves the choice to you, as it does every other visual one. The examples above keep the fade.

styles.css
@media (prefers-reduced-motion: reduce) {
  slab-popover::part(popover) {
    transition:
      opacity 150ms ease-out,
      display 150ms allow-discrete,
      overlay 150ms allow-discrete;
  }

  slab-dialog::part(dialog) {
    transition:
      opacity 200ms ease-out,
      display 200ms allow-discrete,
      overlay 200ms allow-discrete;
  }
}

07 Where it works

@starting-style and transition-behavior are in every current browser: Chrome and Edge 117, Safari 17.5 and Firefox 129. overlay is only in Chrome and Edge so far. In Safari and Firefox, a closing popover or modal dialog leaves the top layer at once. It still fades out, but anything in the page that was under it can show on top of it as it does. calc-size() is only in Chrome and Edge too, so elsewhere a Disclosure opens and closes at once. Older browsers skip what they don't know and open and close at once, the way Slab does with no styles at all.

08 Toast

A Toast animates in the same way, through ::part(region) and :state(open). It can't animate out. Its content is taken out of the region as it closes, which is what makes a screenreader announce it as new the next time it's shown, so there's nothing left to fade.