/** * Button - every button shape of the kit in one component. * * primary - accent-filled CTA (the sheet action button): radius md, white * text. `fullWidth` gives the full-bleed footer CTA. * secondary - neutral surface, hairline border (the idle chip aesthetic). * tonal - soft tint fill + matching text (lighter than primary). Tints the * accent by default; pass `toneColor` (hex) to match a context * (e.g. a banner passes its own tone). * icon - round icon button. `tone` covers the flavours in use: * surface (header button: card + shadow + hairline), * accent (send), accentSoft (a softer accent), muted (the "…" menu). * fab - large round accent floating action button (the "+" FAB). * * Interaction: hover + pressed feedback is colour-agnostic (CSS `filter` * brightness + a press `scale`), driven by POINTER events so press works on * touch as well as mouse. None of it applies while disabled. * * primary / secondary accept `leftIcon` / `rightIcon` to sit an icon beside the * label in a flex row (gap from the SP token); the icon-only `icon` variant is * unchanged. * * All colours come from the theme; sizes/radii from tokens. */ import { useState } from "react"; import type { CSSProperties, ReactNode } from "react"; import { FS, FW, R, SP } from "@rukin/tokens"; import { useTheme } from "./theme"; import type { Theme } from "./theme"; export type ButtonVariant = "primary" | "secondary" | "tonal" | "icon" | "fab"; export type IconTone = "surface" | "accent" | "accentSoft" | "muted"; export type ButtonScale = "sm" | "md" | "lg"; // Text-variant buttons centre their content with flex, not with the default // block layout: a lone icon child (no text) otherwise sits on the text baseline // and renders ~2px above the true centre of the button. Text is unaffected - // a single line centres identically either way. // inline-flex, not flex: a block-level flex would stretch an ordinary button to // the full width of its container, while width here is set by fullWidth alone. const CENTERED = { display: "inline-flex", alignItems: "center", justifyContent: "center" } as const; // Size scale for text buttons (primary / secondary / tonal): padding + FS + gap. // `md` is the historical default look; sm / lg step down / up from it. const TEXT_SCALE: Record = { sm: { padding: "8px 14px", fontSize: FS.sm, gap: SP[1] }, md: { padding: "14px 20px", fontSize: FS.base, gap: SP[2] }, lg: { padding: "18px 28px", fontSize: FS.lg, gap: SP[3] }, }; export type ButtonProps = { children: ReactNode; variant?: ButtonVariant; onClick?: () => void; /** primary / secondary: stretch to the container width (the footer CTA). */ fullWidth?: boolean; /** icon: square size in px (38 in a header, 40 in a sheet). */ size?: number; /** primary / secondary / tonal: text-button size scale (default md). */ scale?: ButtonScale; /** icon: colour flavour. */ tone?: IconTone; /** tonal: base colour of the tint (hex). Defaults to the theme accent. */ toneColor?: string; /** primary / secondary: icon rendered before the label. */ leftIcon?: ReactNode; /** primary / secondary: icon rendered after the label. */ rightIcon?: ReactNode; title?: string; disabled?: boolean; style?: CSSProperties; }; function iconToneStyle(C: Theme["C"], tone: IconTone): CSSProperties { switch (tone) { case "accent": return { background: C.accent, color: "#fff", border: "none" }; case "accentSoft": return { background: C.accentSoft, color: C.accent, border: "none" }; case "muted": return { background: C.canvas, color: C.muted, border: "none" }; case "surface": default: return { background: C.card, color: C.ink, boxShadow: C.shadow, border: `1px solid ${C.dark ? C.hairline : "transparent"}`, }; } } export function Button({ children, variant = "primary", onClick, fullWidth, size = 38, scale = "md", tone = "surface", toneColor, leftIcon, rightIcon, title, disabled, style, }: ButtonProps) { const { C } = useTheme(); const [hover, setHover] = useState(false); const [pressed, setPressed] = useState(false); const sc = TEXT_SCALE[scale]; let base: CSSProperties; if (variant === "icon") { base = { width: size, height: size, borderRadius: R.pill, padding: 0, flexShrink: 0, display: "inline-flex", alignItems: "center", justifyContent: "center", cursor: "pointer", ...iconToneStyle(C, tone), }; } else if (variant === "fab") { base = { width: 54, height: 54, borderRadius: R.pill, border: "none", background: C.accent, color: "#fff", fontSize: 28, fontWeight: FW.regular, lineHeight: 1, cursor: "pointer", display: "inline-flex", alignItems: "center", justifyContent: "center", boxShadow: `0 6px 18px ${C.accent}59`, }; } else if (variant === "secondary") { base = { width: fullWidth ? "100%" : undefined, border: `1.5px solid ${C.hairline}`, borderRadius: R.md, ...CENTERED, padding: sc.padding, fontSize: sc.fontSize, fontWeight: FW.semibold, background: C.card, color: C.ink, cursor: "pointer", transition: "filter .15s, transform .1s", }; } else if (variant === "tonal") { // Soft tint fill + matching text - lighter than primary's solid fill. // Default tone is the theme accent (accentSoft is already theme-tuned); // a custom `toneColor` gets an ~13% alpha tint so it can match a context. const toneBase = toneColor ?? C.accent; const tintBg = toneColor ? `${toneColor}22` : C.accentSoft; base = { width: fullWidth ? "100%" : undefined, border: "none", borderRadius: R.md, ...CENTERED, padding: sc.padding, fontSize: sc.fontSize, fontWeight: FW.semibold, background: tintBg, color: toneBase, cursor: "pointer", transition: "filter .15s, transform .1s", }; } else { // primary base = { width: fullWidth ? "100%" : undefined, border: "none", borderRadius: R.md, ...CENTERED, padding: sc.padding, fontSize: sc.fontSize, fontWeight: FW.semibold, background: C.accent, color: "#fff", cursor: "pointer", transition: "filter .15s, transform .1s", }; } // Colour-agnostic hover / press feedback (skip while disabled). const interactive = !disabled ? { filter: pressed ? "brightness(0.9)" : hover ? "brightness(0.96)" : undefined, transform: pressed ? "scale(0.98)" : undefined, } : {}; const clearInteract = () => { setHover(false); setPressed(false); }; const usesIconRow = (variant === "primary" || variant === "secondary" || variant === "tonal") && (leftIcon || rightIcon); const label = usesIconRow ? ( {leftIcon} {children} {rightIcon} ) : ( children ); return ( ); }