"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>
);
}