Skip to content

Animation ​

Animate a component with CSS transitions, CSS animations or a JavaScript animation library.

A part that shows and hides, such as a popup, a backdrop or a panel, keeps its element in the DOM until its leave ends. Put the part in a Vue <Transition>. Vue adds the classes of the <Transition> when the element enters and leaves.

CSS transitions ​

Give the hidden style to the enter-from and leave-to classes, and the transition to the element.

Use a transition where you can. A transition that stops midway goes back from where it is, with no jump. For example, the user can close a popup before it is fully open.

vue
<template>
  <Popover.Portal>
    <Popover.Positioner>
      <Transition name="popup">
        <Popover.Popup class="Popup">…</Popover.Popup>
      </Transition>
    </Popover.Positioner>
  </Popover.Portal>
</template>
css
.Popup {
  transform-origin: var(--transform-origin);
  transition:
    transform 150ms,
    opacity 150ms;
}

.popup-enter-from,
.popup-leave-to {
  opacity: 0;
  transform: scale(0.9);
}

Without keepMounted, a Portal part renders its content only while the popup is open or leaves. Put the <Transition> in the portal, around the part. A <Transition> around a Portal part animates nothing, because the portal has no root element.

The popup and the backdrop of Dialog, Alert Dialog, Popover, Menu, Context Menu, Menubar, Tooltip, Date Picker, Date Range Picker and Time Picker show their element one update after they mount. So their <Transition> runs the enter with no appear. Do not give it appear: in a virtual DOM app, Vue then calls the JavaScript hooks of the enter twice. In a disabled portal, a popup that is open at the first render shows at once, as in the server HTML, with no enter. The popup of Preview Card, Combobox, Autocomplete, Navigation Menu and Drawer mounts at each open, so give its <Transition> appear.

When one of these popups opens again during its leave, it is the same element. Vue calls leave-cancelled, and a transition goes back from where it is. The backdrop does the same. Do not read the layout of the element, such as offsetWidth, in leave-cancelled or before-enter. During these hooks, the element has display: none, and a read of the layout ends its transition. The enter then starts from the enter-from class.

While an element leaves, the element that takes its place can already be in the DOM. To remove the element that leaves from the flow, give the leave-active class position: absolute.

When a panel opens again during its leave, the new panel enters after the leave ends.

Do not put a <Transition> around a part that renders the element slot. The root of the part is then the fragment of the slot, and Vue warns that it cannot animate it. Give the part keepMounted, and animate your element from its state, as in Animate a popup that stays in the DOM.

CSS animations ​

Give the animations to the enter-active and leave-active classes. Give the enter and the leave each their own keyframes.

css
@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
}

@keyframes scaleOut {
  to {
    opacity: 0;
    transform: scale(0.9);
  }
}

.popup-enter-active {
  animation: scaleIn 250ms ease-out;
}

.popup-leave-active {
  animation: scaleOut 250ms ease-in;
}

An animation cannot go back from where it is. When a popup opens again during its leave, the leave animation stops, and the enter animation starts from its first keyframe. With one keyframes name and reverse, the browser keeps the animation at its time and turns its direction, so the popup jumps to another point.

Content that stays in the DOM ​

With keepMounted, a closed popup or panel stays in the DOM with the hidden and inert attributes. Vue runs no <Transition> for it. Animate it with CSS on [hidden]. Give [hidden] a display value, so that the browser does not hide the element at once, and give it the closed style.

css
.Popup {
  transition: opacity 150ms;
}

.Popup[hidden] {
  display: block;
  opacity: 0;
}

With inert, the user cannot reach the closed element with the pointer or the focus.

JavaScript animations ​

A JavaScript animation library, such as Motion for Vue (the package motion-v), runs in the JavaScript hooks of the <Transition>. With :css="false", the leave ends when the leave hook calls done. Use @after-enter and @after-leave for code that runs after an animation.

Animate a popup that leaves the DOM ​

Without keepMounted, the popup of Popover is in the DOM only while the popover is open or leaves.

  1. Give the popup its closed style in the style sheet.
  2. Put Popover.Popup in a <Transition> with :css="false".
  3. In the enter hook, animate to the open style with animate of motion-v. In the leave hook, animate to the closed style. Each animation starts from the current style, so an open during the leave goes back from where it is.
  4. Call done when the animation ends. The popup then leaves the DOM.
vue
<script setup lang="ts">
import { Popover } from '@vase-ui/vue/popover';
import { animate } from 'motion-v';
import styles from './index.module.css';

function enter(element: Element, done: () => void) {
  void animate(element, { opacity: 1, scale: 1 }).then(done);
}

function leave(element: Element, done: () => void) {
  void animate(element, { opacity: 0, scale: 0.8 }).then(done);
}
</script>

<template>
  <Popover.Root>
    <Popover.Trigger :class="styles.Trigger">Trigger</Popover.Trigger>
    <Popover.Portal>
      <Popover.Positioner :class="styles.Positioner" :side-offset="8">
        <Transition :css="false" @enter="enter" @leave="leave">
          <Popover.Popup :class="styles.Popup" aria-label="Animated popover">Popup</Popover.Popup>
        </Transition>
      </Popover.Positioner>
    </Popover.Portal>
  </Popover.Root>
</template>
css
.Trigger {
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.25rem;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-pressed] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Positioner {
  width: var(--positioner-width);
  height: var(--positioner-height);
  max-width: var(--available-width);
}

.Popup {
  box-sizing: border-box;
  padding: 0.75rem 1rem;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-size: 0.875rem;
  line-height: 1.25rem;
  transform-origin: var(--transform-origin);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  opacity: 0;
  transform: scale(0.8);
  outline: none;

  width: var(--popup-width, auto);
  height: var(--popup-height, auto);
  max-width: 500px;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }
}

<AnimatePresence> of motion-v renders a Vue <Transition> or <TransitionGroup>, so it takes the same place: in the portal, around the popup.

Animate a popup that stays in the DOM ​

With keepMounted on Popover.Portal, the popup stays in the DOM. Render motion.div in the element slot of Popover.Popup, and compute animate from state.open. Give initial the value false, because the popup mounts closed. A rule of the style sheet keeps the closed popup visible while its exit runs.

vue
<script setup lang="ts">
import { Popover } from '@vase-ui/vue/popover';
import { motion } from 'motion-v';
import type { MotionProps } from 'motion-v';
import styles from './index.module.css';
</script>

<template>
  <Popover.Root>
    <Popover.Trigger :class="styles.Trigger">Trigger</Popover.Trigger>
    <Popover.Portal keep-mounted>
      <Popover.Positioner :class="styles.Positioner" :side-offset="8">
        <Popover.Popup :class="styles.Popup" aria-label="Animated popover">
          <template #element="{ props, elementRef, state }">
            <motion.div
              v-bind="props as MotionProps"
              :ref="elementRef"
              :initial="false"
              :animate="{ opacity: state.open ? 1 : 0, scale: state.open ? 1 : 0.8 }"
            >
              Popup
            </motion.div>
          </template>
        </Popover.Popup>
      </Popover.Positioner>
    </Popover.Portal>
  </Popover.Root>
</template>
css
.Trigger {
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 2rem;
  padding: 0 0.75rem;
  margin: 0;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1.25rem;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &[data-pressed] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Positioner {
  width: var(--positioner-width);
  height: var(--positioner-height);
  max-width: var(--available-width);
}

.Popup {
  box-sizing: border-box;
  padding: 0.75rem 1rem;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-size: 0.875rem;
  line-height: 1.25rem;
  transform-origin: var(--transform-origin);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  width: var(--popup-width, auto);
  height: var(--popup-height, auto);
  max-width: 500px;
  outline: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }
}

/* The closed popup has `hidden`, and it stays shown while its exit animation plays. `inert` keeps
   each pointer and the focus from it. */
.Popup[hidden] {
  display: block;
}

Animate a Select popup ​

The popup of Select mounts at the first focus of the trigger, the first open or the first autofill. Then it stays in the DOM, and it shows and hides its own element. Put a <Transition> around Select.Popup, with no appear. The hooks of the <Transition> then run at each open and each close. As for Popover, give the popup its closed style in the style sheet, and animate from the current style.

vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Select } from '@vase-ui/vue/select';
import { animate } from 'motion-v';
import styles from './index.module.css';

const fonts = [
  { label: 'Select font', value: null },
  { label: 'Sans-serif', value: 'sans' },
  { label: 'Serif', value: 'serif' },
  { label: 'Monospace', value: 'mono' },
  { label: 'Cursive', value: 'cursive' },
];

const ICON_PROPS = {
  width: '16',
  height: '16',
  viewBox: '0 0 16 16',
  style: { display: 'block' },
};

const CaretUpDownIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, fill: 'currentColor', ...attrs }, [
    h('path', { d: 'M11 10H5l3 3.5zm0-4H5l3-3.5z' }),
  ]);

// The popup stays in the DOM, so the hooks of the `<Transition>` run at each open and close.
function enter(element: Element, done: () => void) {
  void animate(element, { opacity: 1, scale: 1 }, { duration: 0.15 }).then(done);
}

function leave(element: Element, done: () => void) {
  void animate(element, { opacity: 0, scale: 0.8 }, { duration: 0.15 }).then(done);
}

const CheckIcon: FunctionalComponent = (_, { attrs }) =>
  h('svg', { ...ICON_PROPS, fill: 'none', stroke: 'currentColor', ...attrs }, [
    h('path', { d: 'm2.5 8.5 4 4 7-9' }),
  ]);
</script>

<template>
  <Select.Root :items="fonts">
    <Select.Trigger aria-label="Font" :class="styles.Select">
      <Select.Value :class="styles.Value" />
      <Select.Icon>
        <CaretUpDownIcon />
      </Select.Icon>
    </Select.Trigger>
    <Select.Portal>
      <Select.Positioner :class="styles.Positioner" :side-offset="4">
        <Transition :css="false" @enter="enter" @leave="leave">
          <Select.Popup :class="styles.Popup">
            <Select.ScrollUpArrow :class="styles.ScrollUpArrow" />
            <Select.List :class="styles.List">
              <Select.Item
                v-for="{ label, value } in fonts"
                :key="label"
                :value="value"
                :class="styles.Item"
                v-slot="{ state }"
              >
                <span v-if="state.selected" :class="styles.ItemIndicator">
                  <CheckIcon />
                </span>
                <Select.ItemText :class="styles.ItemText">{{ label }}</Select.ItemText>
              </Select.Item>
            </Select.List>
            <Select.ScrollDownArrow :class="styles.ScrollDownArrow" />
          </Select.Popup>
        </Transition>
      </Select.Positioner>
    </Select.Portal>
  </Select.Root>
</template>
css
.Select {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  height: 2rem;
  padding-left: 0.5rem;
  padding-right: 0.25rem;
  margin: 0;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;
  min-width: 9rem;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &[data-pressed] {
    background-color: oklch(97% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(26.9% 0 0deg);
    }
  }

  &:active {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Value[data-placeholder] {
  color: oklch(55.6% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: oklch(70.8% 0 0deg);
  }
}

.Positioner {
  outline: none;
  z-index: 10;
  -webkit-user-select: none;
  user-select: none;
}

.Popup {
  box-sizing: border-box;
  outline: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  background-color: white;
  background-clip: padding-box;
  color: oklch(14.5% 0 0deg);
  min-width: var(--anchor-width);
  transform-origin: var(--transform-origin);
  box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
  opacity: 0;
  transform: scale(0.8);

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
    box-shadow: none;
  }

  &[data-side='none'] {
    min-width: calc(var(--anchor-width) + 1.75rem);
  }
}

.List {
  box-sizing: border-box;
  position: relative;
  padding-block: 0.25rem;
  overflow-y: auto;
  max-height: var(--available-height);
  scroll-padding-block: 1.5rem;
}

.Arrow {
  display: flex;

  &[data-side='top'] {
    bottom: -8px;
    rotate: 180deg;
  }

  &[data-side='bottom'] {
    top: -8px;
    rotate: 0deg;
  }

  &[data-side='left'] {
    right: -13px;
    rotate: 90deg;
  }

  &[data-side='right'] {
    left: -13px;
    rotate: -90deg;
  }
}

.ArrowFill {
  fill: white;

  @media (prefers-color-scheme: dark) {
    fill: oklch(14.5% 0 0deg);
  }
}

.ArrowOuterStroke {
  fill: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    fill: white;
  }
}

.ArrowInnerStroke {
  fill: white;

  @media (prefers-color-scheme: dark) {
    fill: oklch(14.5% 0 0deg);
  }
}

.Item {
  box-sizing: border-box;
  outline: 0;
  font-size: 0.875rem;
  line-height: 1.25rem;
  padding-block: 0.375rem;
  padding-left: 0.625rem;
  padding-right: 1rem;
  display: grid;
  gap: 0.5rem;
  align-items: center;
  grid-template-columns: 1rem 1fr;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;

  &[data-highlighted] {
    background-color: oklch(14.5% 0 0deg);
    color: white;

    @media (prefers-color-scheme: dark) {
      background-color: white;
      color: oklch(14.5% 0 0deg);
    }
  }
}

.ItemIndicator {
  grid-column-start: 1;
}

.ItemText {
  grid-column-start: 2;
}

.ScrollUpArrow,
.ScrollDownArrow {
  width: 100%;
  background-color: white;
  z-index: 1;
  text-align: center;
  cursor: default;
  height: 1rem;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;

  @media (prefers-color-scheme: dark) {
    background-color: oklch(14.5% 0 0deg);
  }

  &::before {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
  }
}

.ScrollUpArrow {
  top: 0;

  &[data-side='none'] {
    &::before {
      top: -100%;
    }
  }
}

.ScrollDownArrow {
  bottom: 0;

  &[data-side='none'] {
    &::before {
      bottom: -100%;
    }
  }
}

End the exit yourself ​

With :css="false", the element stays in the DOM until you call done of the leave hook. Call it when your animation ends.

To wait for something before the popup starts to close, call cancel() in your openChange listener at the close. When the wait ends, set the value of v-model:open to false.

Differences from Base UI ​

Base UIVase UI
data-starting-styleThe enter-from class of <Transition>.
data-ending-styleThe leave-to class of <Transition>.
transitionStatus in the stateThe classes and the JavaScript hooks of <Transition>.
The part keeps a closing popup mounted until its animations end<Transition> keeps the element in the DOM until its transition or animation ends.
onOpenChangeComplete@after-enter and @after-leave of <Transition>.
actionsRef.unmount() and eventDetails.preventUnmountOnClose()done of the leave hook, with :css="false".
[data-open] and [data-closed] for a CSS animationThe enter-active and leave-active classes of <Transition>. With keepMounted, [hidden] on the closed element.