Animation

Lever UI animates with plain CSS. Anything that opens and closes has data-state="open" or data-state="closed". Give each state an animation.

Code
.details-panel[data-state='open'] {
  animation: fade-in var(--leverui-durations-slow);
}

.details-panel[data-state='closed'] {
  animation: fade-out var(--leverui-durations-moderate);
}

Dialog, Drawer, Menu, Popover, and Tooltip already animate. Use this for your own content, or to change theirs.

Your own content

Wrap it in Presence. Presence sets data-state and keeps the element on the page until its closing animation ends. It animates nothing by itself; the CSS above does.

Code
import { Button, Presence } from '@leverui/components';
import { createSignal } from 'solid-js';
import type { JSX } from '@solidjs/web';

/** Keep the details mounted until their closing animation ends. */
export function AnimatedDetails(): JSX.Element {
  const [open, setOpen] = createSignal(false);

  /** Flip the controlled presence state. */
  function toggle(): void {
    setOpen((current) => !current);
  }

  return (
    <>
      <Button aria-expanded={open()} onClick={toggle}>
        Toggle details
      </Button>
      <Presence class="details-panel" present={open()} unmountOnExit>
        Details
      </Presence>
    </>
  );
}

Enter animation

An open element has data-state="open".

Code
.details-panel[data-state='open'] {
  animation: details-in var(--leverui-durations-slow) var(--leverui-easings-ease-out);
}

Use a built-in keyframe, or write your own under a new name. Reusing a built-in name such as fade-in replaces it for every component.

Code
@keyframes details-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

Exit animation

A closed element has data-state="closed".

Code
.details-panel[data-state='closed'] {
  animation: details-out var(--leverui-durations-moderate) var(--leverui-easings-ease-in);
}
Code
@keyframes details-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

Composing animations

List several keyframes in animation-name to run them together.

Code
.details-panel[data-state='open'] {
  animation-name: fade-in, scale-in;
  animation-duration: var(--leverui-durations-slow);
}

.details-panel[data-state='closed'] {
  animation-name: fade-out, scale-out;
  animation-duration: var(--leverui-durations-faster);
}

Built-in keyframes

KeyframesEffect
fade-in, fade-outFade.
scale-in, scale-outGrow in, shrink out.
slide-from-top, -bottom, -left, -rightSlide in a short way.
slide-to-top, -bottom, -left, -rightSlide out a short way.
expand-height, collapse-height, expand-width, collapse-widthOpen and close to content size. See below.
spin, ping, pulse, bounceLoops.

Add -full to a slide, such as slide-from-bottom-full, to travel the element's full size.

The expand and collapse keyframes read the size from --height or --width. Collapsible sets both on its content.

Timing

Duration variableValue
--leverui-durations-fastest50ms
--leverui-durations-faster100ms
--leverui-durations-fast150ms
--leverui-durations-moderate200ms
--leverui-durations-slow300ms
--leverui-durations-slower400ms
--leverui-durations-slowest500ms

Easings: --leverui-easings-ease-in, -ease-out, -ease-in-out, and -ease-in-smooth.

Loops come ready to use: animation: var(--leverui-animations-spin). Also -ping, -pulse, and -bounce.

Full lists: durations, easings, and animations.

Reduced motion

The built-in Dialog, Drawer, Menu, Popover, and Tooltip animations do not shorten themselves for people who ask for less motion. Add this rule once.

Code
@media (prefers-reduced-motion: reduce) {
  [data-state='open'],
  [data-state='closed'] {
    animation-duration: 1ms;
  }
}
  • Keep the rule on data-state. A rule for everything turns loops such as Spinner into a flicker. Marquee, Skeleton, and indeterminate Progress already handle reduced motion.
  • animation: none also works, and Presence then removes closing content at once. 1ms keeps animationend events firing.
  • No !important needed in ordinary CSS. Inside a cascade layer of your own, order it after the leverui.* layers or add !important.

For your own animations, add a calmer version in the same stylesheet.

Code
.details-panel[data-state='open'] {
  animation-name: slide-from-bottom, fade-in;
  animation-duration: var(--leverui-durations-slow);
}

@media (prefers-reduced-motion: reduce) {
  .details-panel[data-state='open'] {
    animation-name: fade-in;
  }
}