Core components / Buttons
Button
Buttons start actions. Use one orange Primary per view for the main action, Secondary for everything alongside it, and Ink when a dark button suits the moment better.
Code
The component itself, ready to drop into your project. Switch between React, Vue and plain HTML; every version uses the same Tailwind classes on Ochre's tokens.
"use client";
import * as React from "react";
import { cx } from "@/components/ochre/utils";
export type ButtonVariant = "primary" | "secondary" | "tertiary" | "ink" | "danger" | "link" | "link-subtle";
export type ButtonSize = "s" | "m" | "l" | "xl" | "xxl";
export type ButtonShape = "rounded" | "pill";
type Common = {
variant?: ButtonVariant;
size?: ButtonSize;
leadingIcon?: React.ReactNode;
trailingIcon?: React.ReactNode;
loading?: boolean;
iconOnly?: boolean;
fullWidth?: boolean;
// Pill rounds the ends fully: Pill button, and Round icon button with iconOnly.
shape?: ButtonShape;
};
export type ButtonProps = Common &
(
| (React.ButtonHTMLAttributes<HTMLButtonElement> & { href?: undefined })
| (React.AnchorHTMLAttributes<HTMLAnchorElement> & { href: string })
);
// One ladder for every control: S 32, M 40 (desktop default), L 48 (touch), XL 56, XXL 64.
// Exported so the other buttons (toggle, shortcut, count, button group) sit on exactly the same ladder.
export const buttonSizes: Record<ButtonSize, string> = {
s: "h-8 gap-1.5 px-3 text-[13px] leading-[18px] [&_svg]:size-3.5",
m: "h-10 gap-2 px-3.5 text-[14px] leading-5 [&_svg]:size-4",
l: "h-12 gap-2 px-4 text-[16px] leading-6 [&_svg]:size-[18px]",
xl: "h-14 gap-2.5 px-5 text-[18px] leading-7 [&_svg]:size-5",
xxl: "h-16 gap-3 px-6 text-[20px] leading-7 [&_svg]:size-6",
};
export const buttonRadii: Record<ButtonSize, string> = {
s: "rounded-8",
m: "rounded-10",
l: "rounded-12",
xl: "rounded-12",
xxl: "rounded-16",
};
export const buttonIconOnlySizes: Record<ButtonSize, string> = {
s: "w-8 px-0",
m: "w-10 px-0",
l: "w-12 px-0",
xl: "w-14 px-0",
xxl: "w-16 px-0",
};
const variants: Record<ButtonVariant, string> = {
primary: "bg-action-primary text-action-on-primary shadow-xs hover:bg-action-primary-hover",
secondary:
"border border-border-default bg-surface-card text-action-on-secondary shadow-xs hover:border-border-hover hover:bg-action-secondary-hover",
tertiary: "text-text-default hover:bg-state-hover active:bg-state-pressed",
ink: "bg-action-ink text-action-on-ink shadow-xs hover:bg-action-ink-hover",
danger: "bg-action-danger text-action-on-danger shadow-xs hover:bg-action-danger-hover focus-visible:shadow-focus-danger",
link: "!h-auto !px-0 text-text-accent underline-offset-4 hover:text-text-accent-hover hover:underline",
"link-subtle": "!h-auto !px-0 text-text-muted underline-offset-4 hover:text-text-default hover:underline",
};
export function Button(props: ButtonProps) {
const {
variant = "primary",
size = "m",
leadingIcon,
trailingIcon,
loading = false,
iconOnly = false,
fullWidth = false,
shape = "rounded",
className,
children,
...rest
} = props;
const isLink = variant === "link" || variant === "link-subtle";
const classes = cx(
"relative inline-flex shrink-0 select-none items-center justify-center whitespace-nowrap font-medium",
// Colours change quickly; the press rides the snappy spring and springs back on release.
"transition-ui outline-none focus-visible:shadow-focus",
!isLink && "press",
"disabled:pointer-events-none disabled:border-transparent disabled:bg-action-disabled disabled:text-action-on-disabled disabled:shadow-none",
"aria-disabled:pointer-events-none [&[aria-disabled=true]:not([data-loading])]:opacity-50",
"data-[loading=true]:pointer-events-none",
buttonSizes[size],
shape === "pill" ? "rounded-full" : buttonRadii[size],
iconOnly && buttonIconOnlySizes[size],
variants[variant],
isLink && "disabled:bg-transparent",
fullWidth && "w-full",
className,
);
// Loading never changes the width. With a leading icon, the spinner crossfades into its place;
// without one, the label fades out where it stands and the spinner fades in over it.
const fade = "transition-[opacity,scale] duration-[var(--motion-base)] ease-[var(--motion-ease-out)]";
const overlay = loading && !leadingIcon;
const content = (
<>
{leadingIcon ? (
<span className="grid shrink-0 place-items-center [&>*]:col-start-1 [&>*]:row-start-1">
<span className={cx("inline-flex", fade, loading ? "scale-50 opacity-0" : "scale-100 opacity-100")}>{leadingIcon}</span>
{loading ? (
<span className={cx("inline-flex", fade, "starting:scale-50 starting:opacity-0")}>
<Spinner />
</span>
) : null}
</span>
) : null}
{!iconOnly && children != null && (
<span className={cx("truncate", fade, overlay ? "opacity-0" : "opacity-100")}>{children}</span>
)}
{!iconOnly && trailingIcon ? (
<span className={cx("inline-flex shrink-0", fade, overlay ? "opacity-0" : "opacity-100")}>{trailingIcon}</span>
) : null}
{overlay ? (
<span className={cx("absolute inset-0 grid place-items-center", fade, "starting:scale-50 starting:opacity-0")}>
<Spinner />
</span>
) : null}
</>
);
if ("href" in rest && rest.href !== undefined) {
const anchor = rest as React.AnchorHTMLAttributes<HTMLAnchorElement>;
return (
<a {...anchor} className={classes} aria-busy={loading || undefined} data-loading={loading || undefined}>
{content}
</a>
);
}
const button = rest as React.ButtonHTMLAttributes<HTMLButtonElement>;
return (
<button
type="button"
{...button}
// A loading button keeps its colour and ignores clicks, rather than turning grey.
aria-disabled={loading || button["aria-disabled"] || undefined}
onClick={loading ? (e) => e.preventDefault() : button.onClick}
aria-busy={loading || undefined}
data-loading={loading || undefined}
className={classes}
>
{content}
</button>
);
}
function Spinner() {
return (
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true" className="animate-spin">
<circle cx="12" cy="12" r="9" stroke="currentColor" strokeOpacity="0.25" strokeWidth="2.5" />
<path d="M21 12a9 9 0 0 0-9-9" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" />
</svg>
);
}
Installation
With the @ochre registry set up, one command adds the component and what it imports (utils). Or copy the code above into components/ochre/button.tsx.
npx shadcn@latest add @ochre/buttonExamples
Sizes
One size ladder for every control, icon buttons included. M is the desktop default; L and up are for touch.
import { Settings } from "lucide-react";
import { Button } from "@/components/ochre/button";
const sizes = ["s", "m", "l", "xl", "xxl"] as const;
export default function ButtonSizes() {
return (
<div className="grid justify-items-center gap-4">
<div className="flex flex-wrap items-end justify-center gap-3">
<Button size="s">Small</Button>
<Button size="m">Medium</Button>
<Button size="l">Large</Button>
<Button size="xl">Extra large</Button>
<Button size="xxl">Huge</Button>
</div>
<div className="flex flex-wrap items-end justify-center gap-3">
{sizes.map((size) => (
<Button key={size} size={size} variant="secondary" iconOnly leadingIcon={<Settings />} aria-label={`Settings, ${size}`} />
))}
</div>
</div>
);
}
With icons
Icons sit either side of the label. Icon buttons come in the five solid variants and need an aria-label.
import { ArrowRight, Pencil, Plus, Rocket, Settings, Share2, Trash2 } from "lucide-react";
import { Button } from "@/components/ochre/button";
export default function ButtonIcons() {
return (
<div className="grid justify-items-center gap-4">
<div className="flex flex-wrap items-center justify-center gap-3">
<Button leadingIcon={<Plus />}>New project</Button>
<Button variant="secondary" trailingIcon={<ArrowRight />}>
Continue
</Button>
</div>
<div className="flex flex-wrap items-center justify-center gap-3">
<Button iconOnly leadingIcon={<Pencil />} aria-label="Edit" />
<Button variant="secondary" iconOnly leadingIcon={<Share2 />} aria-label="Share" />
<Button variant="tertiary" iconOnly leadingIcon={<Settings />} aria-label="Settings" />
<Button variant="ink" iconOnly leadingIcon={<Rocket />} aria-label="Deploy" />
<Button variant="danger" iconOnly leadingIcon={<Trash2 />} aria-label="Delete" />
</div>
</div>
);
}
Loading and disabled
Loading keeps the width and swaps the leading icon for a spinner.
import { Button } from "@/components/ochre/button";
export default function ButtonStates() {
return (
<div className="flex flex-wrap items-center gap-3">
<Button loading>Saving</Button>
<Button variant="secondary" loading>
Uploading
</Button>
<Button disabled>Save changes</Button>
<Button variant="secondary" disabled>
Preview
</Button>
</div>
);
}
Motion
Built on Ochre's ease-out curves. With reduced motion switched on, everything still works, it just doesn't move.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | "primary" | "secondary" | "tertiary" | "ink" | "danger" | "link" | "link-subtle" | "primary" | Visual weight. One primary per view. |
| size | "s" | "m" | "l" | "xl" | "xxl" | "m" | 32, 40, 48, 56 or 64 pixels high. M on desktop, L for touch. |
| leadingIcon | ReactNode | – | Icon before the label. Also the icon of an iconOnly button. |
| trailingIcon | ReactNode | – | Icon after the label, usually a chevron or arrow. |
| loading | boolean | false | Shows a spinner in place of the leading icon and disables the button. |
| iconOnly | boolean | false | Square button showing only leadingIcon. Give it an aria-label. |
| fullWidth | boolean | false | Stretches to its container, for stacked mobile actions. |
| shape | "rounded" | "pill" | "rounded" | Pill rounds the ends fully. Pill button and Round icon button are built on it. |
| href | string | – | Renders an <a> with the same styles. |
Usage
Use it for
- Primary for the one action the view exists for: Save, Create, Continue.
- Secondary for the actions beside it, Tertiary for quiet ones in toolbars and dialogs.
- Danger only for the confirming button of something that can't be undone.
- Link inside sentences or for navigation that should read as text.
Avoid
- Two Primary buttons in one view.
- Labels longer than three words, or Title Case: buttons are short verbs in sentence case.
- S buttons on touch screens: use L for mobile, sign-in and checkout.