109 lines · 5.5 KBRaw
| 1 | import { CheckIcon, CopyIcon, InfoIcon, WarningCircleIcon } from "../icons"; |
| 2 | import { useEffect, useRef, useState, type ComponentProps, type ReactNode } from "react"; |
| 3 | import { SwapIcon } from "../components/swap-icon"; |
| 4 | import { cn } from "./cn"; |
| 5 | |
| 6 | /** Muted line inks for user-chosen label colours: they mark a category, never a state. */ |
| 7 | const LABEL_INK: Record<string, string> = { |
| 8 | blue: "#7e9ce6", |
| 9 | green: "var(--sage)", |
| 10 | orange: "#d08b4f", |
| 11 | purple: "#a08fd0", |
| 12 | red: "#d9726a", |
| 13 | teal: "#62aaa4", |
| 14 | neutral: "var(--faint)", |
| 15 | }; |
| 16 | |
| 17 | /** The ink of a label colour, for a swatch outside a badge. */ |
| 18 | export function labelInk(color: string): string { |
| 19 | return LABEL_INK[color] ?? LABEL_INK.neutral; |
| 20 | } |
| 21 | |
| 22 | const TONE: Record<string, string> = { |
| 23 | secondary: "bg-hover text-muted", |
| 24 | outline: "text-muted shadow-[inset_0_0_0_1px_var(--line)]", |
| 25 | primary: "bg-accent-tint text-accent-text", |
| 26 | success: "bg-success-tint text-success", |
| 27 | purple: "bg-success-tint text-success", |
| 28 | warning: "bg-needs-tint text-needs-text", |
| 29 | danger: "bg-danger-tint text-danger", |
| 30 | red: "bg-danger-tint text-danger", |
| 31 | }; |
| 32 | |
| 33 | export type BadgeVariant = keyof typeof TONE | keyof typeof LABEL_INK; |
| 34 | |
| 35 | /** A small chip. Status and role badges use tone; label colours show as a dot. */ |
| 36 | export function Badge({ variant = "secondary", className, children, ...props }: { variant?: BadgeVariant | string } & ComponentProps<"span">) { |
| 37 | const ink = !(variant in TONE) ? LABEL_INK[variant] : undefined; |
| 38 | return ( |
| 39 | <span |
| 40 | className={cn( |
| 41 | "caption inline-flex h-[20px] shrink-0 items-center gap-1.5 rounded-chip px-[7px] whitespace-nowrap tabular-nums", |
| 42 | ink ? "text-ink shadow-[inset_0_0_0_1px_var(--line)]" : TONE[variant] ?? TONE.secondary, |
| 43 | className, |
| 44 | )} |
| 45 | {...props} |
| 46 | > |
| 47 | {ink && <span aria-hidden className="size-1.5 rounded-full" style={{ background: ink }} />} |
| 48 | {children} |
| 49 | </span> |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | /** A message inside the page: information, or an error that names the problem and the recovery. */ |
| 54 | export function Banner({ variant = "info", title, description, children, className }: { variant?: "info" | "error" | "success"; title?: ReactNode; description?: ReactNode; children?: ReactNode; className?: string }) { |
| 55 | const error = variant === "error"; |
| 56 | const Icon = error ? WarningCircleIcon : variant === "success" ? CheckIcon : InfoIcon; |
| 57 | return ( |
| 58 | <div role={error ? "alert" : "status"} className={cn("flex gap-3 rounded-control px-3.5 py-3 text-compact", error ? "bg-danger-tint text-ink shadow-[inset_0_0_0_1px_color-mix(in_oklab,var(--danger)_22%,transparent)]" : variant === "success" ? "bg-success-tint text-ink shadow-[inset_0_0_0_1px_color-mix(in_oklab,var(--success)_22%,transparent)]" : "bg-panel text-ink shadow-card", className)}> |
| 59 | <Icon size={18} className={cn("mt-px shrink-0", error ? "text-danger" : variant === "success" ? "text-success" : "text-muted")} aria-hidden /> |
| 60 | <div className="flex min-w-0 flex-col gap-1"> |
| 61 | {title && <p className="m-0 font-semibold">{title}</p>} |
| 62 | {description && <div className={cn("m-0", title ? "text-muted" : "")}>{description}</div>} |
| 63 | {children} |
| 64 | </div> |
| 65 | </div> |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** The empty state of a list or page: a quiet icon, what is missing, and the next step. It enters in stages. */ |
| 70 | export function Empty({ icon, title, description, contents, size = "base", className }: { icon?: ReactNode; title: ReactNode; description?: ReactNode; contents?: ReactNode; size?: "sm" | "base"; className?: string }) { |
| 71 | return ( |
| 72 | <div className={cn("empty-enter flex flex-col items-center gap-4 text-center", size === "sm" ? "px-6 py-8" : "px-8 py-14", className)}> |
| 73 | {icon && <div className="flex size-11 items-center justify-center rounded-panel bg-panel text-muted shadow-card [&_svg]:size-5 [&_svg]:text-muted">{icon}</div>} |
| 74 | <div className="flex max-w-[52ch] flex-col items-center gap-1"> |
| 75 | <h2 className="m-0 text-callout font-semibold text-ink">{title}</h2> |
| 76 | {description && <p className="m-0 text-compact text-muted">{description}</p>} |
| 77 | </div> |
| 78 | {contents && <div className="flex flex-wrap items-center justify-center gap-2">{contents}</div>} |
| 79 | </div> |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** A value to copy, such as a token or a link, with a copy control whose icon cross-fades to a check. */ |
| 84 | export function ClipboardText({ text, size = "sm", className }: { text: string; size?: "sm" | "base"; className?: string }) { |
| 85 | const [copied, setCopied] = useState(false); |
| 86 | const timer = useRef<ReturnType<typeof setTimeout>>(undefined); |
| 87 | useEffect(() => () => clearTimeout(timer.current), []); |
| 88 | const copy = async () => { |
| 89 | try { |
| 90 | await navigator.clipboard.writeText(text); |
| 91 | setCopied(true); |
| 92 | clearTimeout(timer.current); |
| 93 | timer.current = setTimeout(() => setCopied(false), 1600); |
| 94 | } catch { |
| 95 | setCopied(false); |
| 96 | } |
| 97 | }; |
| 98 | return ( |
| 99 | <span className={cn("inline-flex max-w-full items-stretch overflow-hidden rounded-control bg-sunken shadow-[inset_0_0_0_1px_var(--hairline)]", className)}> |
| 100 | <code className={cn("min-w-0 truncate px-2.5 font-mono", size === "base" ? "py-1.5 text-compact" : "py-1 text-xs")}>{text}</code> |
| 101 | <button type="button" onClick={copy} aria-label={copied ? "Copied" : "Copy"} className="pressable flex w-9 shrink-0 cursor-pointer items-center justify-center text-muted shadow-[inset_1px_0_0_var(--hairline)] hover:bg-hover hover:text-ink"> |
| 102 | <SwapIcon active={copied} on={<CheckIcon size={15} className="text-success" />} off={<CopyIcon size={15} />} /> |
| 103 | </button> |
| 104 | <span className="sr-only" role="status"> |
| 105 | {copied ? "Copied" : ""} |
| 106 | </span> |
| 107 | </span> |
| 108 | ); |
| 109 | } |