Button
A button, or an anchor when you give it an href. Four variants, three sizes, and a loading state that does not shift the layout.
Copy source
Requires: Nothing. Any bundler that understands .module.css.
This is the component, complete. Copy the files below into a ui/ folder at your project root and keep the folder structure — the imports between them are relative, so the paths have to come along. Each component imports its own .module.css, so its class names are scoped to it and cannot collide with your application's.
ui/components/Button/Button.tsximport { cx } from "../../internal/cx";
import styles from "./button.module.css";
export type ButtonVariant = "primary" | "secondary" | "ghost" | "danger";
export type ButtonSize = "sm" | "md" | "lg";
/** Whichever element a branch renders, so the ref is the union of both. */
type ButtonNode = HTMLButtonElement | HTMLAnchorElement;
interface BaseProps {
variant?: ButtonVariant;
size?: ButtonSize;
/** Swaps the label for a spinner and blocks activation. Width stays stable. */
loading?: boolean;
fullWidth?: boolean;
/** Rendered before the label. Use a 16px square svg. */
iconStart?: React.ReactNode;
/** Rendered after the label. */
iconEnd?: React.ReactNode;
className?: string;
children?: React.ReactNode;
/** Points at the rendered `<a>` or `<button>`, whichever this instance is. */
ref?: React.Ref<ButtonNode>;
}
type ButtonAsButton = BaseProps &
Omit<React.ComponentProps<"button">, keyof BaseProps> & {
href?: undefined;
};
type ButtonAsLink = BaseProps &
Omit<React.ComponentProps<"a">, keyof BaseProps> & {
href: string;
};
export type ButtonProps = ButtonAsButton | ButtonAsLink;
const VARIANT_CLASS = {
primary: styles.variantPrimary,
secondary: styles.variantSecondary,
ghost: styles.variantGhost,
danger: styles.variantDanger,
} satisfies Record<ButtonVariant, string | undefined>;
const SIZE_CLASS = {
sm: styles.sizeSm,
md: styles.sizeMd,
lg: styles.sizeLg,
} satisfies Record<ButtonSize, string | undefined>;
/** Everything both forms need to paint themselves, already destructured. */
interface Chrome {
loading: boolean;
iconStart?: React.ReactNode;
iconEnd?: React.ReactNode;
children?: React.ReactNode;
}
function Spinner(): React.JSX.Element {
return (
<span aria-hidden="true" className={styles.spinner}>
<svg viewBox="0 0 16 16" width="16" height="16" fill="none">
<rect
x="1.5"
y="1.5"
width="13"
height="13"
stroke="currentColor"
strokeWidth="2"
strokeDasharray="26 26"
strokeDashoffset="13"
/>
</svg>
</span>
);
}
function Content({
loading,
iconStart,
iconEnd,
children,
}: Chrome): React.JSX.Element {
return (
<>
{loading ? <Spinner /> : null}
{iconStart ? <span className={styles.icon}>{iconStart}</span> : null}
<span className={styles.label}>{children}</span>
{iconEnd ? <span className={styles.icon}>{iconEnd}</span> : null}
</>
);
}
function classesFor(
variant: ButtonVariant,
size: ButtonSize,
loading: boolean,
fullWidth: boolean,
className: string | undefined,
): string {
return cx(
styles.button,
VARIANT_CLASS[variant],
SIZE_CLASS[size],
fullWidth && styles.full,
loading && styles.loading,
className,
);
}
// A callback ref is assignable to both `<a ref>` and `<button ref>`; a
// `Ref<A | B>` narrows to neither, which would need a cast.
function refSetter<T>(ref: React.Ref<T> | undefined): (node: T | null) => void {
return (node) => {
if (typeof ref === "function") ref(node);
else if (ref !== null && ref !== undefined) ref.current = node;
};
}
type ButtonAnchorProps = Omit<ButtonAsLink, "ref"> & {
ref?: React.Ref<HTMLAnchorElement>;
};
function ButtonAnchor({
ref,
...props
}: ButtonAnchorProps): React.JSX.Element {
const {
variant = "secondary",
size = "md",
loading = false,
fullWidth = false,
iconStart,
iconEnd,
className,
children,
href,
...anchorRest
} = props;
const classes = classesFor(variant, size, loading, fullWidth, className);
return (
<a
ref={ref}
href={href}
className={classes}
aria-busy={loading || undefined}
aria-disabled={loading || undefined}
tabIndex={loading ? -1 : undefined}
{...anchorRest}
>
<Content loading={loading} iconStart={iconStart} iconEnd={iconEnd}>
{children}
</Content>
</a>
);
}
type ButtonElementProps = Omit<ButtonAsButton, "ref"> & {
ref?: React.Ref<HTMLButtonElement>;
};
function ButtonElement({
ref,
...props
}: ButtonElementProps): React.JSX.Element {
const {
variant = "secondary",
size = "md",
loading = false,
fullWidth = false,
iconStart,
iconEnd,
className,
children,
type,
disabled,
...buttonRest
} = props;
const classes = classesFor(variant, size, loading, fullWidth, className);
return (
<button
ref={ref}
type={type ?? "button"}
className={classes}
disabled={disabled || loading}
aria-busy={loading || undefined}
{...buttonRest}
>
<Content loading={loading} iconStart={iconStart} iconEnd={iconEnd}>
{children}
</Content>
</button>
);
}
// The union is narrowed once, on `typeof href`, so each branch owns exactly one
// element's props. `refSetter` is what lets the union ref reach either element.
export function Button(props: ButtonProps): React.JSX.Element {
if (typeof props.href === "string") {
const { ref, ...anchorProps } = props;
return <ButtonAnchor {...anchorProps} ref={refSetter(ref)} />;
}
const { ref, ...buttonProps } = props;
return <ButtonElement {...buttonProps} ref={refSetter(ref)} />;
}ui/components/Button/button.module.css/* Button — square, ruled, and flat. The only motion is a colour change. */
.button {
--button-height: var(--fr-control-md);
--button-pad: var(--fr-space-4);
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
gap: var(--fr-space-2);
min-height: var(--button-height);
padding: 0 var(--button-pad);
border: var(--fr-rule-width) solid transparent;
border-radius: var(--fr-radius);
font-family: var(--fr-font-sans);
font-size: var(--fr-text-body);
font-weight: var(--fr-weight-semibold);
letter-spacing: -0.005em;
line-height: 1;
text-decoration: none;
white-space: nowrap;
cursor: pointer;
user-select: none;
transition:
background-color var(--fr-duration-fast) var(--fr-ease),
color var(--fr-duration-fast) var(--fr-ease),
border-color var(--fr-duration-fast) var(--fr-ease);
}
.button:focus-visible {
outline: var(--fr-focus-width) solid var(--fr-accent);
outline-offset: var(--fr-focus-offset);
}
/* The primary button is filled with the accent, so a ring in the same colour
would be invisible against its own edge. Focus marks it in ink instead. */
.variantPrimary:focus-visible,
.variantDanger:focus-visible {
outline-color: var(--fr-ink);
}
/* Sizes */
.sizeSm {
--button-height: var(--fr-control-sm);
--button-pad: var(--fr-space-3);
font-size: var(--fr-text-small);
}
.sizeLg {
--button-height: var(--fr-control-lg);
--button-pad: var(--fr-space-5);
font-size: 1rem;
}
.full {
width: 100%;
}
/* Variants: primary is the strongest call to action, secondary the workhorse,
ghost has no chrome until hovered, danger is for destructive confirmation. */
.variantPrimary {
background: var(--fr-accent);
color: var(--fr-accent-ink);
border-color: var(--fr-accent);
}
.variantPrimary:hover:not(:disabled) {
background: var(--fr-accent-hover);
border-color: var(--fr-accent-hover);
/* Restated because the base layer's `a:hover` outranks this class, and a link
button would otherwise repaint its label to the hover background. */
color: var(--fr-accent-ink);
}
.variantPrimary:active:not(:disabled) {
background: var(--fr-ink);
border-color: var(--fr-ink);
color: var(--fr-paper);
}
.variantSecondary {
background: var(--fr-surface);
color: var(--fr-ink);
border-color: var(--fr-rule-strong);
}
.variantSecondary:hover:not(:disabled) {
background: var(--fr-surface-sunken);
/* See the note on primary. */
color: var(--fr-ink);
}
.variantSecondary:active:not(:disabled) {
background: var(--fr-ink);
color: var(--fr-paper);
}
.variantGhost {
background: transparent;
color: var(--fr-ink);
border-color: transparent;
}
.variantGhost:hover:not(:disabled) {
background: var(--fr-surface-sunken);
/* See the note on primary. */
color: var(--fr-ink);
}
.variantGhost:active:not(:disabled) {
background: var(--fr-rule-soft);
}
.variantDanger {
background: var(--fr-danger);
color: var(--fr-danger-ink);
border-color: var(--fr-danger);
}
.variantDanger:hover:not(:disabled) {
background: var(--fr-ink);
border-color: var(--fr-ink);
color: var(--fr-paper);
}
/* Disabled and loading */
.button:disabled,
.button[aria-disabled="true"] {
cursor: not-allowed;
background: var(--fr-disabled-surface);
color: var(--fr-disabled-ink);
border-color: var(--fr-disabled-surface);
}
.variantGhost:disabled,
.variantGhost[aria-disabled="true"] {
background: transparent;
}
.loading {
cursor: progress;
}
/* The label keeps its box while loading, so the button never resizes. */
.loading .label {
opacity: 0.75;
}
.icon {
display: inline-flex;
align-items: center;
flex: none;
}
.icon > svg {
width: 1rem;
height: 1rem;
}
.spinner {
display: inline-flex;
flex: none;
animation: fr-spin 700ms linear infinite;
}
@keyframes fr-spin {
to {
transform: rotate(360deg);
}
}
@media (prefers-reduced-motion: reduce) {
.spinner {
animation-duration: 2s;
}
}ui/internal/cx.tsexport type ClassValue = string | false | null | undefined;
// The only part of `clsx` this library needs, without the dependency.
export function cx(...values: ClassValue[]): string {
const out: string[] = [];
for (const value of values) {
if (value) out.push(value);
}
return out.join(" ");
}Every component reads from these two files. Copy them once, on your first component, and reuse them thereafter.
ui/styles/tokens.css
/* Every ground is white and every value below cleared AA on it; dark mode
re-derives rather than inverting. Check contrast again before changing a value. */
:root {
/* The only radius token. Do not override per component. */
--fr-radius: 0px;
/* Core surfaces — flat by default; separation comes from rules, not fills. */
--fr-paper: #ffffff;
--fr-surface: #ffffff;
--fr-surface-sunken: #f5f5f5;
--fr-ink: #262626;
--fr-ink-muted: #525252;
--fr-ink-subtle: #737373;
/* Rules — soft divides, rule bounds, strong marks a control edge. A boundary
is a hairline tone; only controls and active markers earn the ink one. */
--fr-rule: #e5e5e5;
--fr-rule-strong: #262626;
--fr-rule-soft: #ededed;
/* Accent: Emerald. Reserved for interaction, never decoration. `bright` is
ornament only — marks and arrows, never a label or a fill that carries one. */
--fr-accent: #047857;
--fr-accent-ink: #ffffff;
--fr-accent-soft: #ecfdf5;
--fr-accent-hover: #065f46;
--fr-accent-bright: #10b981;
/* Status. Success stays a true green rather than a second emerald, so a badge
tone never reads as the accent. */
--fr-danger: #a81f24;
--fr-danger-soft: #fbeaeb;
--fr-danger-ink: #ffffff;
--fr-success: #15803d;
--fr-success-soft: #f0fdf4;
--fr-success-ink: #ffffff;
--fr-warning: #7a5312;
--fr-warning-soft: #f8efdf;
--fr-warning-ink: #ffffff;
/* Inverse surfaces */
--fr-inverse-surface: #262626;
--fr-inverse-ink: #ffffff;
/* Disabled */
--fr-disabled-surface: #f0f0f0;
--fr-disabled-ink: #737373;
/* Hard shadow: no blur, ever. */
--fr-shadow-hard: 3px 3px 0 var(--fr-rule-strong);
--fr-shadow-hard-sm: 2px 2px 0 var(--fr-rule-strong);
/* No font ships with the library, so this is the platform's own UI face. */
--fr-font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue",
Arial, sans-serif;
--fr-font-mono: ui-monospace, "SF Mono", "Menlo", "Consolas", monospace;
--fr-text-display: 3.5rem;
--fr-text-h1: 2.5rem;
--fr-text-h2: 1.75rem;
--fr-text-h3: 1.25rem;
--fr-text-body: 0.9375rem;
--fr-text-small: 0.8125rem;
--fr-text-micro: 0.6875rem;
--fr-leading-display: 1.02;
--fr-leading-heading: 1.15;
--fr-leading-body: 1.6;
--fr-tracking-micro: 0.08em;
--fr-tracking-display: -0.02em;
--fr-weight-regular: 400;
--fr-weight-medium: 500;
--fr-weight-semibold: 600;
--fr-weight-bold: 700;
/* Spacing scale — 4px base */
--fr-space-0: 0;
--fr-space-1: 0.25rem;
--fr-space-2: 0.5rem;
--fr-space-3: 0.75rem;
--fr-space-4: 1rem;
--fr-space-5: 1.5rem;
--fr-space-6: 2rem;
--fr-space-7: 3rem;
--fr-space-8: 4rem;
--fr-space-9: 6rem;
/* Sizing */
--fr-control-sm: 1.75rem;
--fr-control-md: 2.25rem;
--fr-control-lg: 2.75rem;
--fr-rule-width: 1px;
--fr-rule-width-strong: 2px;
--fr-focus-width: 2px;
--fr-focus-offset: 2px;
/* Motion — short and linear; this library does not bounce. */
--fr-duration-fast: 90ms;
--fr-duration-base: 140ms;
--fr-ease: cubic-bezier(0.2, 0, 0, 1);
/* Layers */
--fr-z-sticky: 10;
--fr-z-overlay: 100;
--fr-z-dialog: 101;
/* Layout */
--fr-container: 78rem;
--fr-measure: 68ch;
}
/* Dark re-derives rather than inverting: the ground stays near-black and the
accent lifts so it still clears AA (#34D399 on #0A0A0A is 10.33:1). */
[data-theme="dark"] {
--fr-paper: #0a0a0a;
--fr-surface: #141414;
--fr-surface-sunken: #1c1c1c;
--fr-ink: #f5f5f5;
--fr-ink-muted: #a3a3a3;
--fr-ink-subtle: #7a7a7a;
--fr-rule: #3a3a3a;
--fr-rule-strong: #f5f5f5;
--fr-rule-soft: #2a2a2a;
--fr-accent: #34d399;
--fr-accent-ink: #022c22;
--fr-accent-soft: #052e22;
--fr-accent-hover: #6ee7b7;
--fr-accent-bright: #34d399;
--fr-danger: #fca5a5;
--fr-danger-soft: #2e1214;
--fr-danger-ink: #240708;
--fr-success: #86efac;
--fr-success-soft: #0d2415;
--fr-success-ink: #04150c;
--fr-warning: #e2b262;
--fr-warning-soft: #2a2011;
--fr-warning-ink: #1c1305;
--fr-inverse-surface: #f5f5f5;
--fr-inverse-ink: #0a0a0a;
--fr-disabled-surface: #1f1f1f;
--fr-disabled-ink: #6a6a6a;
--fr-shadow-hard: 3px 3px 0 #f5f5f5;
--fr-shadow-hard-sm: 2px 2px 0 #f5f5f5;
}
/* Opt-in inverse region: use for a closing band or a deliberate contrast strip. */
[data-theme="inverse"] {
--fr-paper: #171717;
--fr-surface: #171717;
--fr-surface-sunken: #242424;
--fr-ink: #ffffff;
--fr-ink-muted: #b5b5b5;
--fr-ink-subtle: #8a8a8a;
--fr-rule: #3d3d3d;
--fr-rule-strong: #ffffff;
--fr-rule-soft: #2e2e2e;
--fr-accent: #34d399;
--fr-accent-ink: #022c22;
--fr-accent-soft: #052e22;
--fr-accent-hover: #6ee7b7;
--fr-accent-bright: #34d399;
--fr-inverse-surface: #ffffff;
--fr-inverse-ink: #171717;
--fr-shadow-hard: 3px 3px 0 #ffffff;
--fr-shadow-hard-sm: 2px 2px 0 #ffffff;
}
@media (prefers-reduced-motion: reduce) {
:root {
--fr-duration-fast: 0ms;
--fr-duration-base: 0ms;
}
}ui/styles/base.css
/* Only what a module cannot scope: the page reset. Everything a component owns
lives in its .module.css, so no global class here can collide with yours. */
*,
*::before,
*::after {
box-sizing: border-box;
}
html {
-webkit-text-size-adjust: 100%;
text-size-adjust: 100%;
}
body {
margin: 0;
font-family: var(--fr-font-sans);
font-size: var(--fr-text-body);
line-height: var(--fr-leading-body);
color: var(--fr-ink);
background: var(--fr-paper);
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
h1,
h2,
h3,
h4,
h5,
h6,
p,
figure,
blockquote,
dl,
dd {
margin: 0;
}
ul,
ol {
margin: 0;
padding: 0;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-weight: var(--fr-weight-semibold);
line-height: var(--fr-leading-heading);
letter-spacing: -0.02em;
text-wrap: balance;
}
a {
color: var(--fr-accent);
text-decoration-thickness: 1px;
text-underline-offset: 2px;
}
a:hover {
color: var(--fr-accent-hover);
}
img,
svg,
video,
canvas {
display: block;
max-width: 100%;
}
button,
input,
select,
textarea {
font: inherit;
color: inherit;
letter-spacing: inherit;
}Then import the component from its file. A page that holds state needs "use client".
/* app/layout.tsx — once, before your own CSS */
import "../ui/styles/tokens.css";
import "../ui/styles/base.css";// app/page.tsx
"use client";
import { Button } from "../ui/components/Button/Button";Variants
primary is filled with Emerald and is the one action the user came to do. On a page with several actions, exactly one should be primary.
That is the accent's one seat on any view with a primary action: the focus ring, the active rail item, and the links all move to ink or rule, so the green means "do this" and nothing else.
secondary is the default and the workhorse: white surface, a 1px ink rule so it reads as a control.
ghost has no chrome until hover. Use it for tertiary actions and for buttons inside an already-ruled container.
danger is a solid red fill, reserved for the confirming action in a destructive flow. It is never used for a close or cancel — those are ghost.
Sizes
The three heights are shared with Input and Select — 28px, 36px, and 44px. A medium button and a medium input have the same height and rule weight, so they align in a row without a wrapper.
With icons
Icons are passed as nodes and sized to 16px. The component does not ship an icon set — every icon is yours, which keeps the library free of a 400KB dependency for eleven glyphs.
Disabled and loading
Loading swaps in a square spinner and sets disabled plus aria-busy. The label stays in the DOM and keeps its box, so the button does not resize when the state flips — which is the difference between a calm interface and a jittery one.
Full width
As a link
Passing href renders an <a> with identical styling. This matters: a navigation that looks like a button should still be a link, so it can be opened in a new tab, middle-clicked, and announced as a link.
<Button href="/docs/components/input">Read the Input docs</Button>The component cannot infer intent, so choose deliberately: if it navigates, pass href; if it acts, leave it as a button.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | 'primary' | 'secondary' | 'ghost' | 'danger' | secondary | Visual weight and intent. |
| size | 'sm' | 'md' | 'lg' | md | Control height, shared with Input and Select. |
| loading | boolean | false | Shows a spinner, disables the button, and sets aria-busy. Width is preserved. |
| fullWidth | boolean | false | Stretches the button to its container. |
| iconStart | ReactNode | — | Node rendered before the label. Sized to 16px. |
| iconEnd | ReactNode | — | Node rendered after the label. Sized to 16px. |
| href | string | — | When present, renders an anchor instead of a button. |
| type | 'button' | 'submit' | 'reset' | button | Only for the button form. Defaults to button so it never submits a form by accident. |
All other props are forwarded to the underlying <button> or <a>.
Accessibility
- Renders
<button type="button">by default, so dropping one into a form does not submit it. - Loading sets
disabledandaria-busy="true"; the disabled state is announced and activation is blocked at the element level, not by a click guard. - An
hrefbutton becomes a real anchor, witharia-disabledandtabIndex={-1}while loading rather than a silently dead link. - Focus is a 2px ring at 2px offset, square, on every variant. Primary and danger are already filled with their accent, so their ring is ink — otherwise the ring would vanish against the fill.
- Disabled buttons use
--fr-disabled-surfaceand--fr-disabled-ink, which hold AA contrast against the surrounding surface.
Do and don't
danger for cancel or close. Those are not destructive.loading instead of disabling on submit, so the user knows work is happening.href and get an anchor.