Actions · Material 3: Buttons, Icon buttons
Button
Starts an action. One component covers every emphasis (primary to link), every intent (tone), icon-only buttons, and buttons that show their own result or need a hold.
Examples and variants
Variants
Emphasis from highest to lowest. Use one primary per area.
function ButtonVariants() {
return (
<div className="flex flex-wrap items-center justify-center gap-3">
<Button>Primary</Button>
<Button variant="secondary">Secondary</Button>
<Button variant="outline">Outline</Button>
<Button variant="ghost">Ghost</Button>
<Button variant="link">Link</Button>
</div>
)
}Sizes and icons
sm, md (default), lg. Icons use data-icon to tighten the padding on their side.
function ButtonSizes() {
return (
<div className="flex flex-wrap items-center justify-center gap-3">
<Button size="sm">Small</Button>
<Button size="md">Medium</Button>
<Button size="lg">Large</Button>
<Button>
Continue
<ArrowRightIcon data-icon="inline-end" />
</Button>
</div>
)
}Disabled
Filled variants share --button-container-disabled; ghost stays transparent. Labels use --button-label-disabled, kept at 3:1 instead of fading.
function ButtonDisabled() {
return (
<div className="flex flex-wrap items-center justify-center gap-3">
<Button disabled>Primary</Button>
<Button variant="secondary" disabled>
Secondary
</Button>
<Button variant="outline" disabled>
Outline
</Button>
<Button variant="ghost" disabled>
Ghost
</Button>
<Button tone="danger" disabled>
Delete
</Button>
</div>
)
}Tones
Intent on any variant. Danger only for destructive actions.
function ButtonTones() {
return (
<div className="flex flex-wrap items-center justify-center gap-3">
<Button tone="danger">Delete</Button>
<Button tone="danger" variant="ghost">
<TrashIcon data-icon="inline-start" />
Remove
</Button>
<Button tone="success">Approved</Button>
</div>
)
}Status
The button shows its own result: dots, then a check, in the same width. Prefer this to a toast.
function ButtonStatus() {
const [status, setStatus] = useState<"idle" | "loading" | "success">("idle")
return (
<Button
status={status}
statusLabel={{ success: "Saved" }}
onClick={() => {
setStatus("loading")
const wait = tokenMs("--duration-fill")
window.setTimeout(() => setStatus("success"), wait * 2)
window.setTimeout(() => setStatus("idle"), wait * 4)
}}
>
Save changes
</Button>
)
}Hold to confirm
The click only happens after a full hold. For big, irreversible losses.
function ButtonHold() {
const [deleted, setDeleted] = useState(false)
return deleted ? (
<Button variant="secondary" onClick={() => setDeleted(false)}>
Deleted. Reset
</Button>
) : (
<Button tone="danger" confirm="hold" onClick={() => setDeleted(true)}>
Hold to delete
</Button>
)
}Icon buttons
size="icon-sm | icon | icon-lg". Always give an aria-label.
function IconButtons() {
return (
<div className="flex items-center justify-center gap-3">
<Button size="icon-sm" variant="ghost" aria-label="Like">
<HeartIcon />
</Button>
<Button size="icon" variant="secondary" aria-label="Add">
<PlusIcon />
</Button>
<Button size="icon-lg" aria-label="Add">
<PlusIcon />
</Button>
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/button.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install @base-ui/react class-variance-authority cn lucide-reactCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/button.tsxbutton
components/ui/button.tsx"use client" import { useRef, useState } from "react" import { Button as ButtonPrimitive } from "@base-ui/react/button" import { CheckIcon, XIcon } from "lucide-react" import { tokenMs } from "@/lib/motion/token-ms" import { cva, type VariantProps } from "class-variance-authority" import { cn } from "cn" // Minimal: no strokes on controls (DESIGN.md rule 11), token sizes, token focus ring, // liquid press squash instead of shadcn's translate-y nudge. // Every visual spec reads a component token (styles/tokens/components.css, --button-*). // Sizes: sm | md | lg (default = md). Variants: primary (default) | secondary | outline | ghost | link. const filled = { primary: "bg-(--button-primary-container) text-(--button-primary-label) hover:bg-(--button-primary-container-hover) active:bg-(--button-primary-container-pressed)", secondary: "bg-(--button-secondary-container) text-(--button-secondary-label) hover:bg-(--button-secondary-container-hover) active:bg-(--button-secondary-container-pressed) aria-expanded:bg-(--button-secondary-container-hover)", } const buttonVariants = cva( "liquid-press group/button relative inline-flex shrink-0 items-center justify-center overflow-hidden rounded-(--button-corner) border-solid border-(length:--button-stroke-width) border-(--stroke-color-default) bg-clip-padding text-(length:--button-label-size) [font-weight:var(--button-label-weight)] whitespace-nowrap outline-none select-none focus-visible:shadow-(--focus-ring) disabled:pointer-events-none disabled:bg-(--button-container-disabled) disabled:text-(--button-label-disabled) disabled:shadow-none aria-invalid:shadow-(--focus-ring-invalid) [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-(--button-icon-size)", { variants: { variant: { primary: filled.primary, default: filled.primary, secondary: filled.secondary, outline: "bg-(--button-outline-container) text-(--button-secondary-label) shadow-(--button-outline-elevation) hover:bg-(--button-outline-container-hover) active:bg-(--button-outline-container-pressed) aria-expanded:bg-(--button-outline-container-hover)", ghost: "text-(--button-secondary-label) hover:bg-(--button-ghost-container-hover) active:bg-(--button-ghost-container-pressed) aria-expanded:bg-(--button-ghost-container-hover) disabled:bg-transparent", destructive: "", link: "text-(--button-link-label) underline-offset-4 hover:underline disabled:bg-transparent", }, size: { xs: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-xs) gap-(--button-gap-sm) px-(--button-padding-x-xs) text-(length:--button-label-size-sm) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)", sm: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-sm) gap-(--button-gap-sm) px-(--button-padding-x-sm) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)", md: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-md) gap-(--button-gap) px-(--button-padding-x-md) has-data-[icon=inline-end]:pe-(--button-padding-x-sm) has-data-[icon=inline-start]:ps-(--button-padding-x-sm)", default: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-md) gap-(--button-gap) px-(--button-padding-x-md) has-data-[icon=inline-end]:pe-(--button-padding-x-sm) has-data-[icon=inline-start]:ps-(--button-padding-x-sm)", lg: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-lg) gap-(--button-gap) px-(--button-padding-x-lg) has-data-[icon=inline-end]:pe-(--button-padding-x-md) has-data-[icon=inline-start]:ps-(--button-padding-x-md) [&_svg:not([class*='size-'])]:size-(--button-icon-size-lg)", icon: "size-(--icon-button-size-md) rounded-(--icon-button-corner)", "icon-xs": "size-(--button-height-xs) rounded-(--icon-button-corner) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)", "icon-sm": "size-(--icon-button-size-sm) rounded-(--icon-button-corner) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)", "icon-md": "size-(--icon-button-size-md) rounded-(--icon-button-corner)", "icon-lg": "size-(--icon-button-size-lg) rounded-(--icon-button-corner) [&_svg:not([class*='size-'])]:size-(--button-icon-size-lg)", }, tone: { neutral: "", danger: "", success: "", }, }, // tone: colour intent on any variant (component-model.md §7). destructive = tone danger. compoundVariants: [ { tone: "danger", variant: ["primary", "default", "secondary", "outline", "destructive"], className: "bg-(--button-danger-container) text-(--button-danger-label) shadow-none hover:bg-(--button-danger-container-hover) active:bg-(--button-danger-container-pressed) [--focus-ring-color:var(--color-danger)]", }, { tone: "danger", variant: ["ghost", "link"], className: "text-(--button-danger-label) hover:bg-(--button-danger-container) active:bg-(--button-danger-container-hover) [--focus-ring-color:var(--color-danger)]", }, { tone: "success", variant: ["primary", "default", "secondary", "outline"], className: "bg-(--button-success-container) text-(--button-success-label) shadow-none hover:bg-(--button-success-container-hover) active:bg-(--button-success-container-pressed)", }, ], defaultVariants: { variant: "primary", size: "md", tone: "neutral", }, } ) type Wash = { id: number; x: number; y: number } type Status = "idle" | "loading" | "success" | "error" type ButtonProps = ButtonPrimitive.Props & VariantProps<typeof buttonVariants> & { /** * The button shows its own result inside a fixed width: spinner, then a * check or a cross. Prefer this to a toast (component-model.md §5). */ status?: Status /** "hold": the click only happens after holding for --duration-fill (pointer, Space or Enter). */ confirm?: "hold" /** Screen reader text for the success and error states. */ statusLabel?: { success?: string; error?: string } /** @deprecated use status="loading" */ loading?: boolean /** @deprecated use status="success" */ success?: boolean } function Button({ className, variant = "primary", size = "md", tone = "neutral", status: statusProp, confirm, statusLabel, loading = false, success = false, children, onClick, onPointerUp, onPointerDown, onPointerLeave, onKeyDown, onKeyUp, ...props }: ButtonProps) { const status: Status = statusProp ?? (loading ? "loading" : success ? "success" : "idle") const busy = status === "loading" const [wash, setWash] = useState<Wash | null>(null) const [held, setHeld] = useState(false) const holdTimer = useRef(0) const allowClick = useRef(false) const self = useRef<HTMLElement | null>(null) const resolvedTone = variant === "destructive" ? "danger" : tone function startHold(element: HTMLElement) { if (confirm !== "hold" || busy) return self.current = element setHeld(true) window.clearTimeout(holdTimer.current) holdTimer.current = window.setTimeout(() => { setHeld(false) allowClick.current = true self.current?.click() allowClick.current = false }, tokenMs("--duration-fill", element)) } function stopHold() { if (confirm !== "hold") return setHeld(false) window.clearTimeout(holdTimer.current) } function handleClick(event: Parameters<NonNullable<ButtonProps["onClick"]>>[0]) { if (busy || (confirm === "hold" && !allowClick.current)) { event.preventDefault() return } onClick?.(event) } function handlePointerUp(event: Parameters<NonNullable<ButtonProps["onPointerUp"]>>[0]) { onPointerUp?.(event) stopHold() if (event.defaultPrevented || busy || confirm === "hold") return if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return const motion = event.currentTarget.closest<HTMLElement>("[data-motion]")?.dataset.motion if (motion === "calm" || motion === "still") return const rect = event.currentTarget.getBoundingClientRect() setWash({ id: Date.now(), x: event.clientX - rect.left, y: event.clientY - rect.top, }) } const showResult = status === "success" || status === "error" return ( <ButtonPrimitive data-slot="button" data-icon={typeof size === "string" && size.startsWith("icon") ? "" : undefined} data-tone={resolvedTone} data-status={status} data-confirm={confirm} aria-busy={busy || undefined} aria-disabled={busy || undefined} {...props} className={cn( buttonVariants({ variant, size, tone: resolvedTone, className }), status === "error" && "animate-[isotope-nudge_var(--duration-fast)_var(--ease-out)]" )} onClick={handleClick} onPointerUp={handlePointerUp} onPointerDown={(event) => { onPointerDown?.(event) if (event.button === 0) startHold(event.currentTarget) }} onPointerLeave={(event) => { onPointerLeave?.(event) stopHold() }} onKeyDown={(event) => { onKeyDown?.(event) if (confirm === "hold" && (event.key === " " || event.key === "Enter") && !event.repeat) { startHold(event.currentTarget) } }} onKeyUp={(event) => { onKeyUp?.(event) if (event.key === " " || event.key === "Enter") stopHold() }} > {confirm === "hold" ? ( <span aria-hidden data-held={held ? "" : undefined} className="hold-fill pointer-events-none absolute inset-y-0 left-0 w-full bg-current opacity-20" /> ) : null} <span className={cn( "button-label relative inline-flex items-center justify-center gap-[inherit]", (busy || showResult) && "opacity-0 blur-(--motion-blur-enter) transition-[opacity,filter] duration-(--duration-fast) ease-(--ease-out)" )} > {children} {confirm === "hold" ? <span className="sr-only">(hold to confirm)</span> : null} </span> {showResult ? ( <span className="button-success absolute inset-0 inline-flex items-center justify-center" role="status"> <span className="sr-only"> {status === "success" ? statusLabel?.success ?? "Done" : statusLabel?.error ?? "Failed"} </span> {status === "success" ? <CheckIcon aria-hidden /> : <XIcon aria-hidden />} </span> ) : null} {busy ? ( <span className="absolute inset-0 inline-flex items-center justify-center gap-inline-xs"> <span className="sr-only">Loading</span> {[0, 1, 2].map((dot) => ( <span key={dot} aria-hidden className="size-inline-xs rounded-full bg-current animate-[isotope-pulse_var(--duration-pulse)_var(--ease-out)_infinite]" style={{ animationDelay: `calc(var(--duration-stagger) * ${dot})` }} /> ))} </span> ) : null} {wash ? ( <span key={wash.id} aria-hidden onAnimationEnd={() => setWash(null)} className="pointer-events-none absolute size-[calc(var(--size-control-lg)*4)] rounded-full bg-current animate-[isotope-wash_var(--duration-settle)_var(--ease-out)_both]" style={{ left: wash.x, top: wash.y }} /> ) : null} </ButtonPrimitive> ) } export { Button, buttonVariants } export type { ButtonProps }
Imports
What the examples on this page import:
import { ArrowRightIcon, HeartIcon, PlusIcon, TrashIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { tokenMs } from "@/lib/motion/token-ms"Anatomy
- 1Container · Pill shape, --button-corner; colour from the variant and tone.
- 2Label · --button-label-size, weight 500. Fades and blurs out when status shows.
- 3Icon (optional) · --button-icon-size; data-icon="inline-start | inline-end".
- 4Status layer · Dots, check or cross, centred over the label.
- 5Hold fill · Fills left to right during confirm="hold".
- 6Wash · A soft ripple from the press point (liquid only).
Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "primary" | "secondary" | "outline" | "ghost" | "link" | "primary" | Emphasis. destructive is kept as an alias of tone="danger". |
size | "xs" | "sm" | "md" | "lg" | "icon-xs" | "icon-sm" | "icon" | "icon-lg" | "md" | Height and padding from --button-height-* and --button-padding-x-*. |
tone | "neutral" | "danger" | "success" | "neutral" | Colour intent on any variant. |
status | "idle" | "loading" | "success" | "error" | "idle" | Shows the result in place. loading sets aria-busy. |
statusLabel | { success?: string; error?: string } | — | Screen reader text for the result. |
confirm | "hold" | — | Requires holding for --button-hold-duration (pointer, Space or Enter). |
render | ReactElement | — | Base UI composition: render as a link or inside another trigger. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--button-height-xs | var(--size-control-xs) | 24px | same |
--button-height-sm | var(--size-control-sm) | 32px | same |
--button-height-md | var(--size-control-md) | 40px | same |
--button-height-lg | var(--size-control-lg) | 48px | same |
--button-padding-x-xs | var(--space-inset-control-x-sm) | 8px | same |
--button-padding-x-sm | var(--space-inset-control-x) | 12px | same |
--button-padding-x-md | var(--space-inset-control-x-lg) | 16px | same |
--button-padding-x-lg | var(--space-inset-control-x-xl) | 20px | same |
--button-gap | var(--space-gap-inline-sm) | 8px | same |
--button-gap-sm | var(--space-gap-inline-xs) | 4px | same |
--button-corner | var(--shape-full) | 999px | same |
--button-corner-pressed | var(--button-corner) | 999px | same |
--button-stroke-width | var(--stroke-width-control) | 0px | same |
--button-label-size | var(--type-label-size) | 13px | same |
--button-label-weight | 500 | 500 | same |
--button-icon-size | var(--size-icon-md) | 16px | same |
--button-icon-size-sm | var(--size-icon-sm) | 14px | same |
--button-icon-size-lg | var(--size-icon-lg) | 20px | same |
--button-label-size-sm | var(--type-caption-size) | 11px | same |
--button-primary-container | var(--color-brand) | oklch(0.18 0.008 260) | oklch(0.98 0 0) |
--button-primary-container-hover | var(--color-brand-hover) | oklch(0.28 0.008 260) | oklch(0.9 0 0) |
--button-primary-container-pressed | var(--color-brand-pressed) | oklch(0.14 0.008 260) | oklch(0.82 0 0) |
--button-primary-label | var(--color-fg-on-accent) | oklch(0.97 0.004 95) | oklch(0.14 0 0) |
--button-secondary-container | var(--surface-container) | oklch(0.965 0.005 95) | oklch(0.12 0 0) |
--button-secondary-label | var(--on-surface) | oklch(0.22 0.01 260) | oklch(1 0 0) |
--button-secondary-container-hover | color-mix(in oklch, var(--button-secondary-container), var(--button-secondary-label) var(--state-hover-mix)) | color-mix(in oklch, var(--button-secondary-container), var(--button-secondary-label) var(--state-hover-mix)) | same |
--button-secondary-container-pressed | color-mix(in oklch, var(--button-secondary-container), var(--button-secondary-label) var(--state-pressed-mix)) | color-mix(in oklch, var(--button-secondary-container), var(--button-secondary-label) var(--state-pressed-mix)) | same |
--button-outline-container | var(--surface-container-lowest) | oklch(1 0 0) | oklch(0.14 0 0) |
--button-outline-container-hover | color-mix(in oklch, var(--button-outline-container), var(--button-secondary-label) var(--state-hover-mix)) | color-mix(in oklch, var(--button-outline-container), var(--button-secondary-label) var(--state-hover-mix)) | same |
--button-outline-container-pressed | color-mix(in oklch, var(--button-outline-container), var(--button-secondary-label) var(--state-pressed-mix)) | color-mix(in oklch, var(--button-outline-container), var(--button-secondary-label) var(--state-pressed-mix)) | same |
--button-outline-elevation | var(--elevation-1) | 0 1px 1px -0.5px oklch(0.22 0.01 260 / 7%), 0 2px 3px -1px oklch(0.22 0.01 260 / 10%), 0 4px 8px -4px oklch(0.22 0.01 260 / 6%) | 0 1px 2px oklch(0 0 0 / 35%), 0 2px 10px oklch(0 0 0 / 25%) |
--button-ghost-container-hover | color-mix(in oklch, transparent, var(--button-secondary-label) var(--state-hover-mix)) | color-mix(in oklch, transparent, var(--button-secondary-label) var(--state-hover-mix)) | same |
--button-ghost-container-pressed | color-mix(in oklch, transparent, var(--button-secondary-label) var(--state-pressed-mix)) | color-mix(in oklch, transparent, var(--button-secondary-label) var(--state-pressed-mix)) | same |
--button-danger-container | var(--color-danger-subtle) | oklch(0.55 0.2 25 / 10%) | oklch(0.68 0.18 22 / 16%) |
--button-danger-label | var(--color-danger-fg) | oklch(0.48 0.18 25) | oklch(0.78 0.14 22) |
--button-danger-container-hover | color-mix(in oklch, var(--button-danger-container), var(--button-danger-label) var(--state-hover-mix)) | color-mix(in oklch, var(--button-danger-container), var(--button-danger-label) var(--state-hover-mix)) | same |
--button-danger-container-pressed | color-mix(in oklch, var(--button-danger-container), var(--button-danger-label) var(--state-pressed-mix)) | color-mix(in oklch, var(--button-danger-container), var(--button-danger-label) var(--state-pressed-mix)) | same |
--button-success-container | var(--color-success-subtle) | color-mix(in oklch, var(--ref-success) 10%, transparent) | color-mix(in oklch, var(--ref-success) 16%, transparent) |
--button-success-label | var(--color-success-fg) | oklch(0.48 0.13 150) | oklch(0.8 0.15 150) |
--button-success-container-hover | color-mix(in oklch, var(--button-success-container), var(--button-success-label) var(--state-hover-mix)) | color-mix(in oklch, var(--button-success-container), var(--button-success-label) var(--state-hover-mix)) | same |
--button-success-container-pressed | color-mix(in oklch, var(--button-success-container), var(--button-success-label) var(--state-pressed-mix)) | color-mix(in oklch, var(--button-success-container), var(--button-success-label) var(--state-pressed-mix)) | same |
--button-container-disabled | var(--color-bg-disabled) | color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent) | same |
--button-label-disabled | var(--color-fg-disabled) | oklch(0.62 0.01 95) | oklch(0.53 0 0) |
--button-link-label | var(--color-fg-link) | oklch(0.22 0.01 260) | oklch(1 0 0) |
--button-press-scale | var(--motion-scale-press) | 0.96 | same |
--button-hold-duration | var(--duration-fill) | 700ms | same |
--icon-button-size-sm | var(--size-control-sm) | 32px | same |
--icon-button-size-md | var(--size-control-md) | 40px | same |
--icon-button-size-lg | var(--size-control-lg) | 48px | same |
--icon-button-corner | var(--shape-full) | 999px | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Hover | Container colour shifts. No lift. | --duration-micro |
| Press | Squashes, then settles with a slight overshoot. | --button-press-scale, --ease-bounce |
| Release | A wash ripples out from the press point and fades. | --duration-settle |
| Status | Label blurs out; dots rise in; the check pops. | --duration-fast, --spring-bouncy |
| Hold | Fill tracks the hold 1:1 and rewinds if released early. | --button-hold-duration |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- A real <button> with Base UI focus and keyboard behaviour.
- Icon-only buttons need aria-label (and a Tooltip when the icon isn't obvious).
- status="loading" sets aria-busy and aria-disabled; results are announced with statusLabel.
- confirm="hold" works with Space and Enter, and tells screen readers to hold.
- Focus ring: --focus-ring, 3:1 on every surface.
Usage
Do
- One primary button per area.
- Label with a verb: Save, Invite, Delete project.
- Use status for save, send and pay instead of a toast.
Don’t
- Don't use tone="danger" for anything reversible.
- Don't put two primary buttons side by side.
- Don't use a button to navigate; use a link.
Code
"use client"
import { useRef, useState } from "react"
import { Button as ButtonPrimitive } from "@base-ui/react/button"
import { CheckIcon, XIcon } from "lucide-react"
import { tokenMs } from "@/lib/motion/token-ms"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
// Minimal: no strokes on controls (DESIGN.md rule 11), token sizes, token focus ring,
// liquid press squash instead of shadcn's translate-y nudge.
// Every visual spec reads a component token (styles/tokens/components.css, --button-*).
// Sizes: sm | md | lg (default = md). Variants: primary (default) | secondary | outline | ghost | link.
const filled = {
primary:
"bg-(--button-primary-container) text-(--button-primary-label) hover:bg-(--button-primary-container-hover) active:bg-(--button-primary-container-pressed)",
secondary:
"bg-(--button-secondary-container) text-(--button-secondary-label) hover:bg-(--button-secondary-container-hover) active:bg-(--button-secondary-container-pressed) aria-expanded:bg-(--button-secondary-container-hover)",
}
const buttonVariants = cva(
"liquid-press group/button relative inline-flex shrink-0 items-center justify-center overflow-hidden rounded-(--button-corner) border-solid border-(length:--button-stroke-width) border-(--stroke-color-default) bg-clip-padding text-(length:--button-label-size) [font-weight:var(--button-label-weight)] whitespace-nowrap outline-none select-none focus-visible:shadow-(--focus-ring) disabled:pointer-events-none disabled:bg-(--button-container-disabled) disabled:text-(--button-label-disabled) disabled:shadow-none aria-invalid:shadow-(--focus-ring-invalid) [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-(--button-icon-size)",
{
variants: {
variant: {
primary: filled.primary,
default: filled.primary,
secondary: filled.secondary,
outline:
"bg-(--button-outline-container) text-(--button-secondary-label) shadow-(--button-outline-elevation) hover:bg-(--button-outline-container-hover) active:bg-(--button-outline-container-pressed) aria-expanded:bg-(--button-outline-container-hover)",
ghost: "text-(--button-secondary-label) hover:bg-(--button-ghost-container-hover) active:bg-(--button-ghost-container-pressed) aria-expanded:bg-(--button-ghost-container-hover) disabled:bg-transparent",
destructive: "",
link: "text-(--button-link-label) underline-offset-4 hover:underline disabled:bg-transparent",
},
size: {
xs: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-xs) gap-(--button-gap-sm) px-(--button-padding-x-xs) text-(length:--button-label-size-sm) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)",
sm: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-sm) gap-(--button-gap-sm) px-(--button-padding-x-sm) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)",
md: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-md) gap-(--button-gap) px-(--button-padding-x-md) has-data-[icon=inline-end]:pe-(--button-padding-x-sm) has-data-[icon=inline-start]:ps-(--button-padding-x-sm)",
default: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-md) gap-(--button-gap) px-(--button-padding-x-md) has-data-[icon=inline-end]:pe-(--button-padding-x-sm) has-data-[icon=inline-start]:ps-(--button-padding-x-sm)",
lg: "active:not-disabled:rounded-(--button-corner-pressed) h-(--button-height-lg) gap-(--button-gap) px-(--button-padding-x-lg) has-data-[icon=inline-end]:pe-(--button-padding-x-md) has-data-[icon=inline-start]:ps-(--button-padding-x-md) [&_svg:not([class*='size-'])]:size-(--button-icon-size-lg)",
icon: "size-(--icon-button-size-md) rounded-(--icon-button-corner)",
"icon-xs": "size-(--button-height-xs) rounded-(--icon-button-corner) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)",
"icon-sm": "size-(--icon-button-size-sm) rounded-(--icon-button-corner) [&_svg:not([class*='size-'])]:size-(--button-icon-size-sm)",
"icon-md": "size-(--icon-button-size-md) rounded-(--icon-button-corner)",
"icon-lg": "size-(--icon-button-size-lg) rounded-(--icon-button-corner) [&_svg:not([class*='size-'])]:size-(--button-icon-size-lg)",
},
tone: {
neutral: "",
danger: "",
success: "",
},
},
// tone: colour intent on any variant (component-model.md §7). destructive = tone danger.
compoundVariants: [
{
tone: "danger",
variant: ["primary", "default", "secondary", "outline", "destructive"],
className:
"bg-(--button-danger-container) text-(--button-danger-label) shadow-none hover:bg-(--button-danger-container-hover) active:bg-(--button-danger-container-pressed) [--focus-ring-color:var(--color-danger)]",
},
{
tone: "danger",
variant: ["ghost", "link"],
className: "text-(--button-danger-label) hover:bg-(--button-danger-container) active:bg-(--button-danger-container-hover) [--focus-ring-color:var(--color-danger)]",
},
{
tone: "success",
variant: ["primary", "default", "secondary", "outline"],
className: "bg-(--button-success-container) text-(--button-success-label) shadow-none hover:bg-(--button-success-container-hover) active:bg-(--button-success-container-pressed)",
},
],
defaultVariants: {
variant: "primary",
size: "md",
tone: "neutral",
},
}
)
type Wash = { id: number; x: number; y: number }
type Status = "idle" | "loading" | "success" | "error"
type ButtonProps = ButtonPrimitive.Props &
VariantProps<typeof buttonVariants> & {
/**
* The button shows its own result inside a fixed width: spinner, then a
* check or a cross. Prefer this to a toast (component-model.md §5).
*/
status?: Status
/** "hold": the click only happens after holding for --duration-fill (pointer, Space or Enter). */
confirm?: "hold"
/** Screen reader text for the success and error states. */
statusLabel?: { success?: string; error?: string }
/** @deprecated use status="loading" */
loading?: boolean
/** @deprecated use status="success" */
success?: boolean
}
function Button({
className,
variant = "primary",
size = "md",
tone = "neutral",
status: statusProp,
confirm,
statusLabel,
loading = false,
success = false,
children,
onClick,
onPointerUp,
onPointerDown,
onPointerLeave,
onKeyDown,
onKeyUp,
...props
}: ButtonProps) {
const status: Status = statusProp ?? (loading ? "loading" : success ? "success" : "idle")
const busy = status === "loading"
const [wash, setWash] = useState<Wash | null>(null)
const [held, setHeld] = useState(false)
const holdTimer = useRef(0)
const allowClick = useRef(false)
const self = useRef<HTMLElement | null>(null)
const resolvedTone = variant === "destructive" ? "danger" : tone
function startHold(element: HTMLElement) {
if (confirm !== "hold" || busy) return
self.current = element
setHeld(true)
window.clearTimeout(holdTimer.current)
holdTimer.current = window.setTimeout(() => {
setHeld(false)
allowClick.current = true
self.current?.click()
allowClick.current = false
}, tokenMs("--duration-fill", element))
}
function stopHold() {
if (confirm !== "hold") return
setHeld(false)
window.clearTimeout(holdTimer.current)
}
function handleClick(event: Parameters<NonNullable<ButtonProps["onClick"]>>[0]) {
if (busy || (confirm === "hold" && !allowClick.current)) {
event.preventDefault()
return
}
onClick?.(event)
}
function handlePointerUp(event: Parameters<NonNullable<ButtonProps["onPointerUp"]>>[0]) {
onPointerUp?.(event)
stopHold()
if (event.defaultPrevented || busy || confirm === "hold") return
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return
const motion = event.currentTarget.closest<HTMLElement>("[data-motion]")?.dataset.motion
if (motion === "calm" || motion === "still") return
const rect = event.currentTarget.getBoundingClientRect()
setWash({
id: Date.now(),
x: event.clientX - rect.left,
y: event.clientY - rect.top,
})
}
const showResult = status === "success" || status === "error"
return (
<ButtonPrimitive
data-slot="button"
data-icon={typeof size === "string" && size.startsWith("icon") ? "" : undefined}
data-tone={resolvedTone}
data-status={status}
data-confirm={confirm}
aria-busy={busy || undefined}
aria-disabled={busy || undefined}
{...props}
className={cn(
buttonVariants({ variant, size, tone: resolvedTone, className }),
status === "error" && "animate-[isotope-nudge_var(--duration-fast)_var(--ease-out)]"
)}
onClick={handleClick}
onPointerUp={handlePointerUp}
onPointerDown={(event) => {
onPointerDown?.(event)
if (event.button === 0) startHold(event.currentTarget)
}}
onPointerLeave={(event) => {
onPointerLeave?.(event)
stopHold()
}}
onKeyDown={(event) => {
onKeyDown?.(event)
if (confirm === "hold" && (event.key === " " || event.key === "Enter") && !event.repeat) {
startHold(event.currentTarget)
}
}}
onKeyUp={(event) => {
onKeyUp?.(event)
if (event.key === " " || event.key === "Enter") stopHold()
}}
>
{confirm === "hold" ? (
<span
aria-hidden
data-held={held ? "" : undefined}
className="hold-fill pointer-events-none absolute inset-y-0 left-0 w-full bg-current opacity-20"
/>
) : null}
<span
className={cn(
"button-label relative inline-flex items-center justify-center gap-[inherit]",
(busy || showResult) &&
"opacity-0 blur-(--motion-blur-enter) transition-[opacity,filter] duration-(--duration-fast) ease-(--ease-out)"
)}
>
{children}
{confirm === "hold" ? <span className="sr-only">(hold to confirm)</span> : null}
</span>
{showResult ? (
<span className="button-success absolute inset-0 inline-flex items-center justify-center" role="status">
<span className="sr-only">
{status === "success" ? statusLabel?.success ?? "Done" : statusLabel?.error ?? "Failed"}
</span>
{status === "success" ? <CheckIcon aria-hidden /> : <XIcon aria-hidden />}
</span>
) : null}
{busy ? (
<span className="absolute inset-0 inline-flex items-center justify-center gap-inline-xs">
<span className="sr-only">Loading</span>
{[0, 1, 2].map((dot) => (
<span
key={dot}
aria-hidden
className="size-inline-xs rounded-full bg-current animate-[isotope-pulse_var(--duration-pulse)_var(--ease-out)_infinite]"
style={{ animationDelay: `calc(var(--duration-stagger) * ${dot})` }}
/>
))}
</span>
) : null}
{wash ? (
<span
key={wash.id}
aria-hidden
onAnimationEnd={() => setWash(null)}
className="pointer-events-none absolute size-[calc(var(--size-control-lg)*4)] rounded-full bg-current animate-[isotope-wash_var(--duration-settle)_var(--ease-out)_both]"
style={{ left: wash.x, top: wash.y }}
/>
) : null}
</ButtonPrimitive>
)
}
export { Button, buttonVariants }
export type { ButtonProps }