Motion drawers
Five bottom-edge drawers with distinct entrances: slide, spring, fade, scale and overshoot. Each is self-contained (no radix or vaul), sized, token-driven, and closes on backdrop click or Escape.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/drawer-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/drawer-motion.tsx"use client"
import * as React from "react"
import { motion, AnimatePresence, useReducedMotion } from "motion/react"
import { X } from "lucide-react"
import { cn } from "@/lib/utils"
/* Motion drawer family: 5 showy bottom drawers. They all open upward from the
BOTTOM edge; the panel itself is plain (bg-card) and the DIFFERENCE is the
character of the enter/exit animation. Each export is a complete drawer: internal
open/closed state (uncontrolled defaultOpen or controlled open/onOpenChange), NO
radix/vaul/portal - a self-contained fixed backdrop + a fixed panel pinned to the
edge. It closes on a backdrop click, Escape or the close button. The panel takes
focus on open. AnimatePresence handles enter/exit; under reduced motion ALL
transforms drop away and only an opacity fade remains. Color comes ONLY from
tokens, via alpha color-mix. The essentials mechanism is preserved: sliding from
the bottom edge via data-slot + slideOffset. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
type Edge = "bottom"
const panelHeight: Record<StyledSize, string> = {
sm: "h-[35vh]",
md: "h-[50vh]",
lg: "h-[68vh]",
xl: "h-[82vh]",
}
const edgeAnchor: Record<Edge, string> = {
bottom: "inset-x-0 bottom-0",
}
/* The offset the panel is pushed past the edge by when closed. +y for the bottom edge. */
function slideOffset(edge: Edge) {
switch (edge) {
case "bottom":
return { y: "100%" }
}
}
/* The enter/exit definition each export carries. Combined with slideOffset. */
interface DrawerMotion {
initial: Record<string, number | string>
animate: Record<string, number | string>
exit: Record<string, number | string>
transition: Record<string, unknown>
}
interface DrawerProps {
size?: StyledSize
trigger?: React.ReactNode
title?: React.ReactNode
children?: React.ReactNode
className?: string
open?: boolean
defaultOpen?: boolean
onOpenChange?: (o: boolean) => void
}
/* Controlled/uncontrolled acik durum yonetimi. */
function useControllableOpen(
open: boolean | undefined,
defaultOpen: boolean | undefined,
onOpenChange: ((o: boolean) => void) | undefined
) {
const [uncontrolled, setUncontrolled] = React.useState(defaultOpen ?? false)
const isControlled = open !== undefined
const value = isControlled ? open : uncontrolled
const setValue = React.useCallback(
(next: boolean) => {
if (!isControlled) setUncontrolled(next)
onOpenChange?.(next)
},
[isControlled, onOpenChange]
)
return [value, setValue] as const
}
interface ShellProps extends DrawerProps {
edge: Edge
slot: string
panelClassName: string
backdropClassName: string
drawerMotion: DrawerMotion
}
/* Shared shell: trigger plus an AnimatePresence backdrop plus panel. drawerMotion carries the enter/exit animation supplied from outside; it differs in every export. The Essentials slideOffset mechanism is preserved: the closed position is pushed past the edge. */
function DrawerShell({
edge,
slot,
panelClassName,
backdropClassName,
drawerMotion,
size = "md",
trigger,
title,
children,
className,
open,
defaultOpen,
onOpenChange,
}: ShellProps) {
const reduce = useReducedMotion()
const [isOpen, setOpen] = useControllableOpen(open, defaultOpen, onOpenChange)
const panelRef = React.useRef<HTMLDivElement>(null)
React.useEffect(() => {
if (!isOpen) return
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") setOpen(false)
}
window.addEventListener("keydown", onKey)
return () => window.removeEventListener("keydown", onKey)
}, [isOpen, setOpen])
const sizeClass = cn("w-full", panelHeight[size])
const offset = slideOffset(edge)
/* Reduced motion: every transform is dropped, only the opacity fade remains. */
const fade = {
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { duration: 0 },
}
const active = reduce
? fade
: {
initial: { ...offset, ...drawerMotion.initial },
animate: { x: 0, y: 0, ...drawerMotion.animate },
exit: { ...offset, ...drawerMotion.exit },
transition: drawerMotion.transition,
}
return (
<div data-slot={slot} className="inline-flex">
{/* The ARIA state lives on the REAL trigger. There used to be a
`<span role="button" tabIndex={-1}>` wrapper, which produced two
buttons in the AX tree: one carrying the state (not focusable) and
one that took focus (stateless). A screen-reader user never heard
"has dialog" or "expanded" - measured with the CDP AX tree. */}
{React.isValidElement<React.ButtonHTMLAttributes<HTMLButtonElement>>(trigger) ? (
React.cloneElement(trigger, {
"aria-haspopup": "dialog",
"aria-expanded": isOpen,
onClick: (event: React.MouseEvent<HTMLButtonElement>) => {
trigger.props.onClick?.(event)
setOpen(true)
},
})
) : (
<button
type="button"
aria-haspopup="dialog"
aria-expanded={isOpen}
onClick={() => setOpen(true)}
className="inline-flex h-9 shrink-0 select-none items-center justify-center gap-2 rounded-lg border border-border bg-secondary px-4 text-sm font-medium text-secondary-foreground outline-none whitespace-nowrap transition-[color,background-color] duration-(--motion-fast) ease-(--motion-ease) hover:bg-surface-3 focus-visible:ring-[3px] focus-visible:ring-ring/50 [&_i]:text-base [&_i]:leading-none [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0"
>
{trigger ?? "Open"}
</button>
)}
<AnimatePresence>
{isOpen ? (
<div className="fixed inset-0 z-50">
<motion.div
data-slot="styled-drawer-backdrop"
onClick={() => setOpen(false)}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: reduce ? 0 : 0.2, ease: [0.4, 0, 0.2, 1] }}
className={cn("absolute inset-0", backdropClassName)}
/>
<motion.div
ref={panelRef}
data-slot="styled-drawer-panel"
role="dialog"
aria-modal="true"
tabIndex={-1}
onAnimationComplete={() => panelRef.current?.focus()}
initial={active.initial}
animate={active.animate}
exit={active.exit}
transition={active.transition}
className={cn(
"fixed flex flex-col shadow-lg outline-none",
edgeAnchor[edge],
sizeClass,
panelClassName,
className
)}
>
{/* Tutamac: alt cekmecenin okunabilir gorsel imzasi. */}
<div className="flex shrink-0 justify-center pt-3 pb-1">
<span className="h-1.5 w-12 rounded-full bg-[color-mix(in_oklab,var(--color-foreground)_25%,transparent)]" />
</div>
<div className="flex shrink-0 items-start justify-between gap-4 px-6 pt-1">
{title ? (
<h2 data-slot="styled-drawer-title" className="text-base font-semibold text-foreground">
{title}
</h2>
) : (
<span />
)}
<button
type="button"
onClick={() => setOpen(false)}
aria-label="Close"
className="relative -m-1 inline-flex size-7 shrink-0 items-center justify-center rounded-md text-muted-foreground outline-none transition-[color,background-color] duration-(--motion-fast) ease-(--motion-ease) hover:bg-surface-3 hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50 after:absolute after:-inset-2 [&_i]:text-base [&_i]:leading-none [&_svg]:size-4"
>
<X />
</button>
</div>
<div className="min-h-0 flex-1 overflow-auto px-6 py-4 text-sm text-muted-foreground">
{children}
</div>
</motion.div>
</div>
) : null}
</AnimatePresence>
</div>
)
}
const solidBackdrop = "bg-[color-mix(in_oklab,var(--color-foreground)_50%,transparent)]"
const panelLook = "rounded-t-2xl border-t border-border bg-card text-card-foreground"
/* Slide: duz kayma, yumusak ease. Overshoot yok, sabit sureli tween. */
export function SlideDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="bottom"
slot="styled-drawer"
backdropClassName={solidBackdrop}
panelClassName={panelLook}
drawerMotion={{
initial: {},
animate: {},
exit: {},
transition: { type: "tween", duration: 0.32, ease: [0.4, 0, 0.2, 1] },
}}
/>
)
}
/* Spring: a springy entrance that settles with a slight overshoot. */
export function SpringDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="bottom"
slot="styled-drawer"
backdropClassName={solidBackdrop}
panelClassName={panelLook}
drawerMotion={{
initial: {},
animate: {},
exit: {},
transition: { type: "spring", stiffness: 300, damping: 26 },
}}
/>
)
}
/* Fade: kayma ile opacity fade birlikte; panel kayarken belirir. */
export function FadeDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="bottom"
slot="styled-drawer"
backdropClassName={solidBackdrop}
panelClassName={panelLook}
drawerMotion={{
initial: { opacity: 0 },
animate: { opacity: 1 },
exit: { opacity: 0 },
transition: { type: "tween", duration: 0.36, ease: [0.16, 1, 0.3, 1] },
}}
/>
)
}
/* Scale: kayarken hafif buyume; kapalikken az kuculmus gelir. */
export function ScaleDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="bottom"
slot="styled-drawer"
backdropClassName={solidBackdrop}
panelClassName={panelLook}
drawerMotion={{
initial: { scale: 0.94 },
animate: { scale: 1 },
exit: { scale: 0.94 },
transition: { type: "spring", stiffness: 260, damping: 28 },
}}
/>
)
}
/* Overshoot: agresif yay, dusuk damping ile kenardan gecip geri seker. */
export function OvershootDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="bottom"
slot="styled-drawer"
backdropClassName={solidBackdrop}
panelClassName={panelLook}
drawerMotion={{
initial: {},
animate: {},
exit: {},
transition: { type: "spring", stiffness: 520, damping: 13 },
}}
/>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Slide
Slides up with a smooth, even ease.
import { SlideDrawer } from "@/components/ui/drawer-motion"
<SlideDrawer title="Drawer title">A self-contained bottom drawer with a distinct entrance. Click the backdrop or press Escape to close.</SlideDrawer>Spring
Springs up with a gentle overshoot.
import { SpringDrawer } from "@/components/ui/drawer-motion"
<SpringDrawer title="Drawer title">A self-contained bottom drawer with a distinct entrance. Click the backdrop or press Escape to close.</SpringDrawer>Fade
Slides and fades in opacity together.
import { FadeDrawer } from "@/components/ui/drawer-motion"
<FadeDrawer title="Drawer title">A self-contained bottom drawer with a distinct entrance. Click the backdrop or press Escape to close.</FadeDrawer>Scale
Scales up slightly while it slides in.
import { ScaleDrawer } from "@/components/ui/drawer-motion"
<ScaleDrawer title="Drawer title">A self-contained bottom drawer with a distinct entrance. Click the backdrop or press Escape to close.</ScaleDrawer>Overshoot
Overshoots the edge and springs back aggressively.
import { OvershootDrawer } from "@/components/ui/drawer-motion"
<OvershootDrawer title="Drawer title">A self-contained bottom drawer with a distinct entrance. Click the backdrop or press Escape to close.</OvershootDrawer>ai2 Motion drawers: 5 styled variations on the token system
The ai2 Motion drawers are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around self-contained bottom-edge drawers with distinct entrances. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion animates a distinct even slide, gentle spring, slide-plus-fade, scale or aggressive overshoot on enter and exit; the backdrop fades. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the transforms are skipped and the panel fades instead.
What is in the ai2 Motion drawers?
5 exports in one file: Slide, Spring, Fade, Scale and Overshoot. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion animates a distinct even slide, gentle spring, slide-plus-fade, scale or aggressive overshoot on enter and exit; the backdrop fades.
- Reduced-motion aware: Under prefers-reduced-motion, the transforms are skipped and the panel fades instead.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Motion drawers sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.