Side drawers
Five side drawers that slide in from the horizontal edges: right, left, right inset, left inset and corner. 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-sideDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/drawer-side.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"
/* Side drawer family: 5 decorative drawers opening FROM THE SIDE. The essentials
drawer was vertical (bottom/top); this family slides in from the horizontal edges
(right/left). 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 positioned at the edge (z-50). It
closes on a backdrop click, Escape or the close button (X). The panel takes focus
on open. The slide runs through AnimatePresence; only a fade under
reduced-motion. Color comes ONLY from tokens, with transparency via color-mix. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
/* The edge that determines the slide direction. Right and left are full-height drawers; bottom is used only for the corner card rising from below. */
type Edge = "right" | "left" | "bottom"
const widthSize: Record<StyledSize, string> = {
sm: "w-72",
md: "w-80",
lg: "w-96",
xl: "w-[32rem]",
}
/* On a panel with an inset margin a 100% translation leaves a sliver the size of that margin; an extra 2rem is added to clear the gap completely. It does no harm on a flush panel either, it just slides a little further away. */
function slideOffset(edge: Edge) {
switch (edge) {
case "right":
return { x: "calc(100% + 2rem)" }
case "left":
return { x: "calc(-100% - 2rem)" }
case "bottom":
return { y: "calc(100% + 2rem)" }
}
}
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
/* Konum + koseler (or. "inset-y-0 right-0 rounded-l-2xl"). */
anchorClassName: string
panelClassName: string
backdropClassName: string
}
/* Shared shell: trigger plus an AnimatePresence backdrop plus a panel positioned at the edge. edge decides the slide direction, anchorClassName the position and corners. */
function DrawerShell({
edge,
anchorClassName,
panelClassName,
backdropClassName,
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 enter = slideOffset(edge)
return (
<div data-slot="styled-drawer" 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. */}
{/* data-slot is not passed to cloneElement: ButtonHTMLAttributes does not accept an unknown `data-*` key (TS2769), and overriding the consumer's own slot would be wrong anyway. It is preserved on the default button. */}
{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"
data-slot="styled-drawer-trigger"
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={reduce ? { opacity: 0 } : enter}
animate={reduce ? { opacity: 1 } : { x: 0, y: 0 }}
exit={reduce ? { opacity: 0 } : enter}
transition={
reduce
? { duration: 0 }
: { type: "spring", stiffness: 320, damping: 34 }
}
className={cn(
"fixed flex flex-col gap-4 p-6 outline-none",
anchorClassName,
widthSize[size],
panelClassName,
className
)}
>
<div className="flex items-start justify-between gap-4">
{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 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)]"
/* Right: sag kenardan tam boy kayar, ic koseler rounded-l-2xl, solda kenarlik. */
export function RightDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="right"
backdropClassName={solidBackdrop}
anchorClassName="inset-y-0 right-0 rounded-l-2xl"
panelClassName="border-l border-border bg-card text-card-foreground shadow-xl"
/>
)
}
/* Left: sol kenardan tam boy kayar, ic koseler rounded-r-2xl, sagda kenarlik. */
export function LeftDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="left"
backdropClassName={solidBackdrop}
anchorClassName="inset-y-0 left-0 rounded-r-2xl"
panelClassName="border-r border-border bg-card text-card-foreground shadow-xl"
/>
)
}
/* Right inset: sagdan m-4 boslukla yuzen panel, tum koseler rounded-2xl. */
export function RightInsetDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="right"
backdropClassName={solidBackdrop}
anchorClassName="inset-y-4 right-4 rounded-2xl"
panelClassName="border border-border bg-card text-card-foreground shadow-xl"
/>
)
}
/* Left inset: soldan m-4 boslukla yuzen panel, tum koseler rounded-2xl. */
export function LeftInsetDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="left"
backdropClassName={solidBackdrop}
anchorClassName="inset-y-4 left-4 rounded-2xl"
panelClassName="border border-border bg-card text-card-foreground shadow-xl"
/>
)
}
/* Corner: a card sitting in the bottom right corner that grows with its content; it slides up from below. Not full height - capped at max-h-[70vh] and scrolls when the content needs it. */
export function CornerDrawer(props: DrawerProps) {
return (
<DrawerShell
{...props}
edge="bottom"
backdropClassName={solidBackdrop}
anchorClassName="bottom-4 right-4 max-h-[70vh] rounded-2xl"
panelClassName="border border-border bg-card text-card-foreground shadow-2xl"
/>
)
}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.
Right
Slides in flush against the right edge, full height.
import { RightDrawer } from "@/components/ui/drawer-side"
<RightDrawer title="Drawer title">A self-contained side drawer that slides in from the edge. Click the backdrop or press Escape to close.</RightDrawer>Left
Slides in flush against the left edge, full height.
import { LeftDrawer } from "@/components/ui/drawer-side"
<LeftDrawer title="Drawer title">A self-contained side drawer that slides in from the edge. Click the backdrop or press Escape to close.</LeftDrawer>Right inset
Floats off the right edge with an m-4 gap and rounded-2xl corners.
import { RightInsetDrawer } from "@/components/ui/drawer-side"
<RightInsetDrawer title="Drawer title">A self-contained side drawer that slides in from the edge. Click the backdrop or press Escape to close.</RightInsetDrawer>Left inset
Floats off the left edge with an m-4 gap and rounded-2xl corners.
import { LeftInsetDrawer } from "@/components/ui/drawer-side"
<LeftInsetDrawer title="Drawer title">A self-contained side drawer that slides in from the edge. Click the backdrop or press Escape to close.</LeftInsetDrawer>Corner
A compact bottom-right corner card that rises from the corner.
import { CornerDrawer } from "@/components/ui/drawer-side"
<CornerDrawer title="Drawer title">A self-contained side drawer that slides in from the edge. Click the backdrop or press Escape to close.</CornerDrawer>ai2 Side drawers: 5 styled variations on the token system
The ai2 Side drawers are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around side drawers that slide in from the horizontal edges. 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 slides the drawer in from its edge; 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 slide is skipped and the drawer fades or shows instantly.
What is in the ai2 Side drawers?
5 exports in one file: Right, Left, Right inset, Left inset and Corner. 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 slides the drawer in from its edge; the backdrop fades.
- Reduced-motion aware: Under prefers-reduced-motion, the slide is skipped and the drawer fades or shows instantly.
- 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 Side 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.