Motion helpers
CSS-transition helpers for components that animate without motion/react: easing strings, interaction durations, reduced-motion checks and the size morph FancyButton uses.
Motion for code that animates without motion/react: CSS and the Web Animations API. easeOut, easeInOut and easeSpring are CSS easing strings and duration holds milliseconds for a press, a release, a hover, an entrance, an exit and a morph. useSizeMorph({ axis }) returns a ref: the element's width or height then glides whenever its content changes size, which is how a button follows a label that changes. prefersReducedMotion() reads the reader's setting at the moment you call it, in the browser, so call it in a handler or an effect and never while rendering; useSizeMorph already honours it. For motion/react springs and durations, use @khaeli/motion-tokens.
Width that follows its label
"use client"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import { useSizeMorph } from "@/lib/motion"
export default function MotionWidthThatFollowsItsLabel() {
const [following, setFollowing] = useState(false)
// The button's width glides to fit each new label. With reduced motion it changes at once.
const morph = useSizeMorph<HTMLElement>({ axis: "width" })
return (
<div className="flex items-center gap-2">
<Button
ref={morph}
variant="outline"
aria-pressed={following}
className="data-morphing:overflow-hidden"
onClick={() => setFollowing(!following)}
>
{following ? "Following this project" : "Follow"}
</Button>
<span className="text-muted-foreground">Atlas redesign</span>
</div>
)
}
Height that follows its content
Release 4.2
Twelve tasks shipped.
"use client"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import { duration, easeInOut, useSizeMorph } from "@/lib/motion"
export default function MotionHeightThatFollowsItsContent() {
const [open, setOpen] = useState(false)
// `duration` and `easing` are optional: 300ms and the strong ease-out unless you say otherwise.
const morph = useSizeMorph<HTMLDivElement>({ axis: "height", duration: duration.morph, easing: easeInOut })
return (
<div className="flex w-full max-w-sm flex-col items-start gap-3">
<div ref={morph} className="w-full overflow-hidden rounded-xl border border-border p-3">
<p className="font-medium">Release 4.2</p>
<p className="text-muted-foreground">Twelve tasks shipped.</p>
{open ? (
<ul id="motion-release-details" className="mt-2 list-disc pl-5">
<li>Offline drafts</li>
<li>Faster search</li>
<li>Export to CSV</li>
</ul>
) : null}
</div>
<Button
variant="outline"
aria-expanded={open}
aria-controls={open ? "motion-release-details" : undefined}
onClick={() => setOpen(!open)}
>
{open ? "Hide the changes" : "Show the changes"}
</Button>
</div>
)
}
Eases with the Web Animations API
easeOuteaseInOuteaseSpring
"use client"
import { useRef, useState } from "react"
import { Button } from "@/components/ui/button"
import { duration, easeInOut, easeOut, easeSpring, prefersReducedMotion } from "@/lib/motion"
const curves = [
{ name: "easeOut", easing: easeOut },
{ name: "easeInOut", easing: easeInOut },
{ name: "easeSpring", easing: easeSpring },
]
// The two ends of a track, as `translate` values: the track is a size container.
const START = "0px"
const END = "calc(100cqw - 100%)"
export default function MotionEasesWithTheWebAnimationsApi() {
const [moved, setMoved] = useState(false)
const dots = useRef<(HTMLElement | null)[]>([])
function play() {
const [from, to] = moved ? [END, START] : [START, END]
dots.current.forEach((dot, index) => {
dot?.animate(
// Reduced motion: the dot is at its new place at once and fades in there.
prefersReducedMotion() ? [{ opacity: 0 }, { opacity: 1 }] : [{ translate: from }, { translate: to }],
{ duration: duration.morph, easing: curves[index]?.easing },
)
})
setMoved(!moved)
}
return (
<div className="flex w-full max-w-sm flex-col gap-4">
<ul className="flex flex-col gap-2">
{curves.map(({ name }, index) => (
<li key={name} className="grid grid-cols-[6rem_1fr] items-center gap-3">
<code className="font-mono text-sm text-muted-foreground">{name}</code>
<div className="rounded-control bg-muted p-1">
<div className="@container">
<div
ref={(node) => {
dots.current[index] = node
}}
style={{ translate: moved ? END : START }}
className="size-6 rounded-small bg-primary"
/>
</div>
</div>
</li>
))}
</ul>
<Button variant="outline" className="self-start" onClick={play}>
Play
</Button>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/motionbunx --bun shadcn@latest add @khaeli/motionInstalls with
Nothing else. This item needs no other Registry item and no npm package.
Accessible name
- A control whose label changes keeps one meaning: say the state with
aria-pressedoraria-expanded, not with the label alone.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/motion.json