vael-ui v0.5.7

Customize motion

Every component that animates has built-in motion and a motion prop. Keep the built-in motion, turn it off, change its values, or run it with GSAP, motion or the Web Animations API.

Built-in motion

Leave motion unset and the component uses its built-in motion. Most apps need nothing more. Set it to false and the component changes state at once: it still opens, closes and removes items, without animating.

<Dialog v-model:open="open" :motion="false">
  Opens and closes instantly.
</Dialog>

To turn motion off everywhere, such as in screenshot tests, set :motion="false" on a ConfigProvider. Under reduced motion, the built-in motion keeps its fades and drops its movement.

What each component takes

KindComponentsProps and keys
Opens and closesBottomSheet, CascadeSelect, Combobox, CommandPalette, ContextMenu, DatePicker, Dialog, Drawer, Menu, Message, PasswordInput, Popover, Select, SplitButton, Tooltip, Tour, TreeSelectmotionbeforeCloseforceMount
Changes in placeAccordion, AnimatedNumber, AnimatedText, Avatar, AvatarGroup, Badge, Breadcrumb, Button, ButtonGroup, Calendar, Card, Checkbox, Chip, Collapsible, ColorPicker, Dial, Dock, Editable, Field, Input, InputNumber, Kbd, Knob, Loader, MenuList, OtpInput, Pagination, Progress, PullToRefresh, Radio, RadioGroup, Rating, Resizable, ScrollArea, SelectButton, Separator, Skeleton, Slider, Sortable, Stepper, SwipeToReveal, Switch, Tabs, Tag, Textarea, Toolbar, Treemotion
Items that come and goToaster (toast), Timeline (item), FileUpload (item), SpeedDial (action), DataTable (row), CommandPalette (items → item), Select, Combobox, CascadeSelect (chips → chip)motionstaggerleaving

Each component exports its own motion type, such as DialogMotion, that lists its parts and states. The part names match its ui keys. For a list, the name in parentheses is the part each item gets.

Change the values

Pass an object to describe the motion as data. initial sets where each part comes in from, and exit sets where it leaves to. The keys are part names, and the values are CSS properties plus x, y and blur.

<script setup lang="ts">
import { Popover } from "vael-ui"
import type { PopoverMotion } from "vael-ui"

const panelMotion: PopoverMotion = {
  initial: { panel: { opacity: 0, y: 8 } },
  exit: { panel: { opacity: 0, y: 4 } },
  spring: { damping: 0.8, response: 0.3 },
}
</script>

<template>
  <Popover open-on-trigger-click :motion="panelMotion">
    <template #trigger>
      <button>Open</button>
    </template>
    <template #default>Popover content</template>
  </Popover>
</template>

For timing, give a spring, or a duration in milliseconds with an easing, not both. A spring’s damping of 1 settles without overshoot, and lower values overshoot. response is roughly the seconds it takes to arrive. Leave out exit and the part fades out with a slight shrink.

Use your own library

When values aren’t enough, add enter(parts, ctx) and leave(parts, ctx) hooks. parts holds the component’s elements by part name. Return what your library returns: a Web Animations Animation, a GSAP tween or timeline, the controls from motion’s animate(), or a promise. The component waits for it before it finishes opening or removes the element.

If the motion is interrupted, such as a close while it’s still opening, the component stops what you returned with kill, stop or cancel, and aborts ctx.signal. Under reduced motion, hooks don’t run. Set reducedMotion: 'run' to run them anyway, and check ctx.reducedMotion yourself.

GSAP

Return a timeline. Here a Dialog fades its overlay and slides its panel:

<script setup lang="ts">
import { ref } from "vue"
import gsap from "gsap"
import { Dialog } from "vael-ui"
import type { DialogMotion } from "vael-ui"

const open = ref(false)

const dialogMotion: DialogMotion = {
  enter: ({ overlay, panel }) =>
    gsap
      .timeline()
      .from(overlay, { opacity: 0, duration: 0.2 })
      .from(panel, { opacity: 0, y: 12, duration: 0.3, ease: "power3.out" }, 0),
  leave: ({ overlay, panel }) =>
    gsap
      .timeline()
      .to(panel, { opacity: 0, y: 8, duration: 0.15, ease: "power2.in" })
      .to(overlay, { opacity: 0, duration: 0.15 }, 0),
}
</script>

<template>
  <Dialog v-model:open="open" :motion="dialogMotion">Dialog content</Dialog>
</template>

motion

Return the controls from animate(), imported from motion-v or motion. Here a Popover panel springs in:

<script setup lang="ts">
import { animate } from "motion-v"
import { Popover } from "vael-ui"
import type { PopoverMotion } from "vael-ui"

const panelMotion: PopoverMotion = {
  enter: ({ panel }) =>
    animate(
      panel,
      { opacity: [0, 1], scale: [0.95, 1] },
      { type: "spring", bounce: 0.2, visualDuration: 0.3 },
    ),
  leave: ({ panel }) => animate(panel, { opacity: 0, scale: 0.95 }, { duration: 0.15 }),
}
</script>

<template>
  <Popover open-on-trigger-click :motion="panelMotion">
    <template #trigger>
      <button>Open</button>
    </template>
    <template #default>Popover content</template>
  </Popover>
</template>

Web Animations API

No library needed: element.animate() returns an Animation. Here a Message slides out to the side when it closes:

<script setup lang="ts">
import { ref } from "vue"
import { Message } from "vael-ui"
import type { MessageMotion } from "vael-ui"

const open = ref(true)

const messageMotion: MessageMotion = {
  enter: ({ root }) =>
    root.animate([{ opacity: 0, transform: "translateY(-4px)" }, { opacity: 1, transform: "none" }], {
      duration: 200,
      easing: "ease-out",
    }),
  leave: ({ root }) =>
    root.animate([{ opacity: 1 }, { opacity: 0, transform: "translateX(24px)" }], {
      duration: 180,
      easing: "ease-in",
    }),
}
</script>

<template>
  <Message v-model:open="open" closable :motion="messageMotion">Changes saved.</Message>
</template>

Lists

Toaster, Timeline, FileUpload, SpeedDial and DataTable take the same motion object for their items. CommandPalette takes it under items, and Select, Combobox and CascadeSelect under chips. Values and hooks apply to each item. A hook gets one item’s parts, such as { toast }, { item }, { row }, { action } or { chip }, plus ctx.index for your own stagger.

Lists take three more keys. stagger is the seconds between items that arrive together. leaving sets what happens to a leaving item’s space: with 'pop', the default, the other items close the gap at once; with 'hold', the item keeps its space until it’s gone. appear: true also animates the items already there when the list mounts.

<FileUpload
  v-model:files="files"
  multiple
  :motion="{ initial: { item: { opacity: 0, y: 6 } }, stagger: 0.04 }"
/>

With hooks, each Timeline step gets its marker, connectors and content:

<script setup lang="ts">
import { gsap } from "gsap"
import { Timeline } from "vael-ui"
import type { TimelineMotion } from "vael-ui"

defineProps<{ events: { id: number; label: string }[] }>()

const steps: TimelineMotion = {
  // A leaving step keeps its space, so the line retracts before the gap closes.
  leaving: "hold",
  enter({ previousConnector: line, marker, content }) {
    gsap.set(marker, { scale: 0.4, opacity: 0 })
    gsap.set(content, { opacity: 0, x: 8 })
    const tl = gsap.timeline()
    if (line) tl.fromTo(line, { scaleY: 0, transformOrigin: "50% 0" }, { scaleY: 1, duration: 0.3 })
    tl.to(marker, { scale: 1, opacity: 1, duration: 0.4, ease: "back.out(2.2)" })
    tl.to(content, { opacity: 1, x: 0, duration: 0.35 }, "<0.05")
    return tl
  },
  leave({ item, marker, content }) {
    const tl = gsap.timeline()
    tl.to([content, marker], { opacity: 0, duration: 0.2 }, 0)
    tl.to(item, { height: 0, overflow: "hidden", duration: 0.3, ease: "power2.inOut" }, 0.2)
    return tl
  },
}
</script>

<template>
  <Timeline :items="events" :item-key="(e) => e.id" :motion="steps">
    <template #item="{ item }">{{ item.label }}</template>
  </Timeline>
</template>

The old list events, such as @item-enter and @item-leave, are gone. Move that code into enter and leave.

beforeClose decides, it doesn’t animate

beforeClose is a guard. Return true to close, false to stay open, or a promise of either. While the promise is pending, the overlay stays open and still. A confirmation takes one line:

<script setup lang="ts">
import { ref } from "vue"
import { Dialog, confirmAction } from "vael-ui"

const open = ref(false)

const confirmDiscard = () =>
  confirmAction({ title: "Discard changes?", confirmLabel: "Discard" }).result
</script>

<template>
  <Dialog v-model:open="open" :before-close="confirmDiscard">Edit form</Dialog>
</template>

The guard receives the reason for closing. It doesn’t run your exit animation: put that in motion.leave. The done() callback is gone.

forceMount

forceMount keeps the content mounted while it’s closed and toggles it with v-show. Use it when the content must stay in the DOM, such as to keep its state or to measure it. The built-in motion doesn’t play on content kept this way, so it shows and hides at once. To animate it, use motion hooks.

One style for the whole app

Pass a style to ConfigProvider to set motion once. Role keys, such as overlay and list, apply to every component of that kind. Component keys, such as dialog and toaster, override their role. A component’s own motion prop overrides both. version: 1 marks the format, so a saved style keeps working in later releases.

<script setup lang="ts">
import { ConfigProvider } from "vael-ui"
import type { MotionStyle } from "vael-ui/motion"

const style: MotionStyle = {
  version: 1,
  spring: { damping: 0.9, response: 0.35 },
  overlay: { initial: { opacity: 0, y: 6 } },
  list: { stagger: 0.04 },
  toaster: { leaving: "hold" },
}
</script>

<template>
  <ConfigProvider :motion="style">
    <RouterView />
  </ConfigProvider>
</template>

Animate your own elements with v-animate

The v-animate directive gives your own elements the same motion. v-animate="{ initial: { opacity: 0, y: 8 } }" plays when the element mounts. When it unmounts, it leaves the way it came, a little quieter and quicker.

Inside a component, add a name: v-animate:rows joins that component’s motion as a target called rows. The element enters and leaves with the panel. Set its values on the element, or under targets.rows in the component’s motion.

<script setup lang="ts">
import { ref } from "vue"
import { Dialog } from "vael-ui"
import { vAnimate } from "vael-ui/motion"

const open = ref(false)
const steps = ["Pick a plan", "Add your team", "Send invites"]
</script>

<template>
  <p v-animate="{ initial: { opacity: 0, y: 8 } }">Welcome back</p>

  <Dialog v-model:open="open">
    <ol>
      <li
        v-for="step in steps"
        :key="step"
        v-animate:rows="{ initial: { opacity: 0, y: 6 }, stagger: 0.03 }"
      >
        {{ step }}
      </li>
    </ol>
  </Dialog>
</template>

Exposed elements

For full control over one part, set :motion="false", put a template ref on the component, and animate its DOM elements: el for the root, plus parts such as checkEl or thumbEl. Each component’s Exposed table lists what it has.

In this example, GSAP pops the check mark in each time the box is checked:

<script setup lang="ts">
import { ref, useTemplateRef, watch } from "vue"
import gsap from "gsap"
import { Checkbox } from "vael-ui"

const checked = ref(false)
const checkbox = useTemplateRef("checkbox")

watch(checked, (isChecked) => {
  if (!isChecked) return
  gsap.fromTo(
    checkbox.value?.checkEl,
    { scale: 0.6, opacity: 0 },
    {
      scale: 1,
      opacity: 1,
      transformOrigin: "50% 50%",
      duration: 0.25,
      clearProps: "all",
    },
  )
})
</script>

<template>
  <Checkbox
    ref="checkbox"
    v-model="checked"
    :motion="false"
    label="Remember me"
  />
</template>