Button
A button for an action: save, run, delete. Six variants set how much attention it asks for, and a loading state covers the wait without moving anything. For a choice between views use the segmented control; for navigation use a link with the button classes.
Install
npx shadcn@latest add https://thrumui.dev/r/button.jsonAdds components/ui/button.tsx. Nothing else in your project changes.
import { buttonClass } from "@/components/ui/button";Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "primary" | "secondary" | "outline" | "ghost" | "destructive" | "link" | Default "primary" | How much the button asks for attention. Use one primary button per view. |
size | "sm" | "md" | "lg" | "icon" | Default "md" | sm is 28 pixels tall, md 32, lg 40. icon is a 32 pixel square for one icon and needs an aria-label. |
loading | boolean | Default false | Shows a spinner over the label and ignores presses. The button keeps its width, its name and the focus, and is announced as busy. |
disabled | boolean | Default false | The native attribute: the button leaves the tab order and cannot be pressed. |
type | "button" | "submit" | "reset" | Default "button" | The native type. Set submit inside a form. |
className | string | – | Extra classes, merged last. w-full makes it fill its container. |
...props | ComponentProps<"button"> | – | Every other button attribute and event handler is passed through. |
buttonClass() | ({ variant, size }) => string | – | A second export: the same classes for an element that is not a button, such as a link. |
Motion and sound
Motion
- Background, border and text colour change over 150 ms on hover.
- A press scales the button to 97% and back over 150 ms. A disabled or loading button does not move.
- The spinner turns once a second while loading is set.
- Reduced motion: colours and scale change at once and the spinner stands still.
Sound
Silent. This component makes no sound.
Accessibility
- It is a native button element, so its role, name and keyboard behaviour come from the browser.
- While loading it carries aria-busy and aria-disabled and keeps its text as its name: the label is only made transparent.
- The focus ring is 2 pixels of the theme's ring colour, 2 pixels away from the button, so it shows on any background.
- A button with only an icon needs an aria-label.
- Every size is at least 28 pixels tall, above the 24 pixel minimum for pointer targets.
| Keys | Action |
|---|---|
| Tab | Focus the button. A loading button stays focusable; a disabled one is skipped. |
| Enter / Space | Press the button. Nothing happens while it is loading. |
Source
"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
export type ButtonVariant = "primary" | "secondary" | "outline" | "ghost" | "destructive" | "link";
export type ButtonSize = "sm" | "md" | "lg" | "icon";
export type ButtonProps = ComponentProps<"button"> & {
/** How much the button asks for attention. One `primary` per view. */
variant?: ButtonVariant;
/** `sm` is 28 pixels tall, `md` 32, `lg` 40. `icon` is a 32 pixel square for a single icon, and needs an `aria-label`. */
size?: ButtonSize;
/** Shows a spinner over the label and ignores presses. The button keeps its width, its name and the focus. */
loading?: boolean;
};
const BASE =
"relative inline-flex shrink-0 cursor-pointer items-center justify-center rounded-md border border-transparent font-medium whitespace-nowrap select-none " +
"transition-[background-color,border-color,color,scale] duration-150 ease-out motion-reduce:transition-none " +
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring active:scale-[0.97] " +
"disabled:cursor-not-allowed disabled:opacity-50 disabled:active:scale-100 aria-disabled:cursor-not-allowed aria-disabled:active:scale-100";
const VARIANT: Record<ButtonVariant, string> = {
primary: "bg-primary text-primary-foreground hover:bg-primary/85",
secondary: "border-border/60 bg-secondary text-secondary-foreground hover:bg-secondary/70",
outline: "border-input bg-background text-foreground hover:bg-accent hover:text-accent-foreground",
ghost: "text-foreground hover:bg-accent hover:text-accent-foreground",
destructive: "bg-destructive text-background hover:bg-destructive/85",
link: "text-foreground underline underline-offset-4 hover:decoration-2",
};
const SIZE: Record<ButtonSize, string> = {
sm: "h-7 px-2.5 text-xs",
md: "h-8 px-3 text-sm",
lg: "h-10 px-4 text-sm",
icon: "size-8 text-sm",
};
/** How each variant takes a glass material: coloured glass, clear glass, or none (a ghost and a link have no body). */
export const BUTTON_SURFACE: Record<ButtonVariant, "accent" | "control" | undefined> = {
primary: "accent",
destructive: "accent",
secondary: "control",
outline: "control",
ghost: undefined,
link: undefined,
};
/** The classes of a button, for an element that is not one: a link that should look like a button. */
export function buttonClass({ variant = "primary", size = "md" }: { variant?: ButtonVariant; size?: ButtonSize } = {}): string {
return cn(BASE, VARIANT[variant], SIZE[size]);
}
/**
* A button. It is a native `<button>`, so forms, the keyboard and screen readers treat it as one.
* While `loading` it stays focusable and says it is busy, so a reader does not lose their place.
*/
export function Button({ variant = "primary", size = "md", loading = false, type = "button", className, children, onClick, ...props }: ButtonProps) {
return (
<button
data-slot="button"
data-surface={BUTTON_SURFACE[variant]}
type={type}
aria-disabled={loading || undefined}
aria-busy={loading || undefined}
className={cn(buttonClass({ variant, size }), className)}
onClick={(event) => {
// Not `disabled`: that would drop the focus to the page in the middle of a submit.
if (loading) event.preventDefault();
else onClick?.(event);
}}
{...props}
>
{loading && (
<span className="absolute inset-0 grid place-items-center">
<svg viewBox="0 0 24 24" className="size-4 animate-spin motion-reduce:animate-none" fill="none" stroke="currentColor" strokeWidth={2} aria-hidden="true">
<path d="M12 3.75a8.25 8.25 0 1 0 8.25 8.25" />
</svg>
</span>
)}
<span className={cn("inline-flex items-center gap-2", loading && "opacity-0")}>{children}</span>
</button>
);
}Files and dependencies
- Adds
components/ui/button.tsx- Packages
- None
- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.