Repository Public
1"use client";2import Link from "next/link";3import { useState, type ButtonHTMLAttributes, type ReactNode } from "react";4import { useI18n } from "@/i18n";5import { cx, type Tone } from "./cx";6import { Icon, type IconName } from "./Icon";78type Variant = "secondary" | "primary" | "ghost" | "danger";9type Size = "sm" | "md" | "lg";1011export interface ButtonProps extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "className"> {12 variant?: Variant;13 size?: Size;14 icon?: IconName;15 loading?: boolean;16 block?: boolean;17}1819const btnClass = (variant: Variant, size: Size, extra?: string | false) =>20 cx("mcc-btn", variant !== "secondary" && `mcc-btn-${variant}`, size !== "md" && `mcc-btn-${size}`, extra);2122export function Button({ variant = "secondary", size = "md", icon, loading, block, children, disabled, type = "button", ...rest }: ButtonProps) {23 return (24 <button {...rest} type={type} disabled={disabled || loading} aria-busy={loading || undefined} className={btnClass(variant, size, block && "mcc-btn-block")}>25 {loading ? <Icon name="refresh" size={size === "sm" ? 13 : 15} className="mcc-spin" /> : icon ? <Icon name={icon} size={size === "sm" ? 13 : 15} /> : null}26 {children}27 </button>28 );29}3031/** Icon-only button: the accessible name is mandatory and doubles as the tooltip. */32export function IconButton({ icon, label, variant = "ghost", size = "md", tip = true, ...rest }: Omit<ButtonProps, "children" | "icon"> & { icon: IconName; label: string; tip?: boolean }) {33 const { loading, disabled, type = "button", ...other } = rest;34 const button = (35 <button {...other} type={type} disabled={disabled || loading} aria-label={label} className={btnClass(variant, size, "mcc-btn-icon")}>36 <Icon name={loading ? "refresh" : icon} size={size === "sm" ? 14 : 16} className={loading ? "mcc-spin" : undefined} />37 </button>38 );39 return tip ? <span className="mcc-tip below" data-tip>{button}<span role="tooltip" aria-hidden="true">{label}</span></span> : button;40}4142export function LinkButton({ href, variant = "secondary", size = "md", icon, children, external }: { href: string; variant?: Variant; size?: Size; icon?: IconName; children: ReactNode; external?: boolean }) {43 const content = <>{icon ? <Icon name={icon} size={size === "sm" ? 13 : 15} /> : null}{children}</>;44 if (external) return <a href={href} className={btnClass(variant, size)} target="_blank" rel="noopener noreferrer">{content}</a>;45 return <Link href={href} className={btnClass(variant, size)}>{content}</Link>;46}4748const TONE_ICON: Record<Tone, IconName> = { ok: "ok", warning: "warning", critical: "critical", info: "info", stale: "stale", offline: "offline", maintenance: "wrench", neutral: "unknown" };4950/** Filled state badge: tone colour + distinct icon + text. */51export function Badge({ tone = "neutral", icon, children }: { tone?: Tone; icon?: IconName | false; children: ReactNode }) {52 return (53 <span className={cx("mcc-badge", `tone-${tone}`, icon === false && "mcc-badge-plain")}>54 {icon === false ? null : <Icon name={icon ?? TONE_ICON[tone]} size={13} />}55 {children}56 </span>57 );58}5960/** Inline state: coloured icon whose shape depends on the tone, followed by the label (never colour alone). */61export function StateDot({ tone, label, icon, subtle, hideLabel }: { tone: Tone; label: string; icon?: IconName; subtle?: boolean; hideLabel?: boolean }) {62 return (63 <span className={cx("mcc-state", `tone-${tone}`, subtle && "subtle")}>64 <Icon name={icon ?? TONE_ICON[tone]} size={14} />65 <span className={hideLabel ? "sr-only" : "label"}>{label}</span>66 </span>67 );68}6970export function Count({ value, tone }: { value: number | string; tone?: Tone }) {71 return <span className={cx("mcc-count", tone && `tone-${tone}`)}>{value}</span>;72}73export function Tag({ children }: { children: ReactNode }) {74 return <span className="mcc-tag">{children}</span>;75}76export function Kbd({ children }: { children: ReactNode }) {77 return <kbd className="mcc-kbd">{children}</kbd>;78}79export function Mono({ children, title }: { children: ReactNode; title?: string }) {80 return <span className="mono" title={title}>{children}</span>;81}8283export function Skeleton({ width, height, label }: { width?: "25" | "40" | "60" | "80"; height?: "lg" | "xl" | "row"; label?: string }) {84 return <span className={cx("mcc-skel", width && `w-${width}`, height && `h-${height}`)} role={label ? "status" : undefined} aria-label={label} aria-hidden={label ? undefined : true} />;85}86export function SkeletonRows({ rows = 6 }: { rows?: number }) {87 const { t } = useI18n();88 return (89 <div role="status" aria-label={t("common.loading")} className="mcc-table-wrap">90 {Array.from({ length: rows }, (_, i) => <span key={i} className="mcc-skel h-row" aria-hidden="true" />)}91 </div>92 );93}9495/** `focusable`: makes the wrapper a tab stop — use it around disabled controls so the reason is reachable by keyboard. */96export function Tooltip({ content, children, placement, focusable }: { content: ReactNode; children: ReactNode; placement?: "below" | "end"; focusable?: boolean }) {97 return (98 <span className={cx("mcc-tip", placement)} tabIndex={focusable ? 0 : undefined}>99 {children}100 <span role="tooltip">{content}</span>101 </span>102 );103}104105/** Copies text and confirms through a polite live region. */106export function CopyButton({ text, label, size = "sm", children }: { text: string; label?: string; size?: Size; children?: ReactNode }) {107 const { t } = useI18n();108 const [state, setState] = useState<"idle" | "done" | "failed">("idle");109 const copy = async () => {110 try {111 await navigator.clipboard.writeText(text);112 setState("done");113 } catch {114 setState("failed");115 }116 window.setTimeout(() => setState("idle"), 2000);117 };118 const name = label ?? t("common.copy");119 return (120 <>121 {children ? (122 <Button size={size} icon={state === "done" ? "check" : "copy"} onClick={copy}>{children}</Button>123 ) : (124 <IconButton size={size} icon={state === "done" ? "check" : "copy"} label={name} onClick={copy} />125 )}126 <span className="sr-only" role="status">{state === "done" ? t("common.copied") : state === "failed" ? t("common.copyFailed") : ""}</span>127 </>128 );129}130131export function KeyValue({ items, inline }: { items: { label: string; value: ReactNode }[]; inline?: boolean }) {132 if (inline) {133 return (134 <dl className="mcc-kv inline">135 {items.map((it) => (136 <div key={it.label}><dt className="caps">{it.label}</dt><dd>{it.value}</dd></div>137 ))}138 </dl>139 );140 }141 return (142 <dl className="mcc-kv">143 {items.map((it) => (144 <div key={it.label} className="mcc-kv-row">145 <dt>{it.label}</dt>146 <dd>{it.value}</dd>147 </div>148 ))}149 </dl>150 );151}152153export function Stat({ label, value, unit, context }: { label: string; value: ReactNode; unit?: string; context?: ReactNode }) {154 return (155 <div className="mcc-stat">156 <span className="caps">{label}</span>157 <span className="value">{value}{unit ? <small>{unit}</small> : null}</span>158 {context ? <span className="context">{context}</span> : null}159 </div>160 );161}162