components/linyi/ink-tones.tsx
"use client"; import * as React from "react";import { cn } from "@/lib/cn"; export type InkToneKey = "jiao" | "nong" | "zhong" | "dan" | "qing"; export const INK_TONES: { key: InkToneKey; name: string; token: string; role: string }[] = [ { key: "jiao", name: "焦", token: "--ink-1", role: "标题 · 主操作" }, { key: "nong", name: "浓", token: "--ink-2", role: "正文 · 按钮底" }, { key: "zhong", name: "重", token: "--ink-3", role: "次要文字 · 描边" }, { key: "dan", name: "淡", token: "--ink-4", role: "说明 · 晕染" }, { key: "qing", name: "清", token: "--ink-5", role: "分隔 · 底纹" },]; export interface InkTonesProps { /** Show the text-hierarchy specimen under the swatches. */ showSpecimen?: boolean; /** Click a swatch to copy its CSS variable, e.g. `var(--ink-2)`. */ copyable?: boolean; compact?: boolean; className?: string;} /** * 墨分五色 —— five ink tones as theme tokens (`--ink-1` … `--ink-5`, Tailwind `text-ink-1` …). * Light theme: ink on xuan paper; dark theme: inverted to moon-white on inkstone. */export function InkTones({ showSpecimen = true, copyable = true, compact = false, className }: InkTonesProps) { const [copied, setCopied] = React.useState<InkToneKey | null>(null); // 提示复位定时器:重复触发时先清除旧定时器,卸载时统一清理,避免提前复位与卸载后更新状态 const resetTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null); React.useEffect( () => () => { if (resetTimerRef.current) clearTimeout(resetTimerRef.current); }, [] ); const copy = async (t: (typeof INK_TONES)[number]) => { if (!copyable) return; try { await navigator.clipboard.writeText(`var(${t.token})`); setCopied(t.key); if (resetTimerRef.current) clearTimeout(resetTimerRef.current); resetTimerRef.current = setTimeout(() => setCopied(null), 1200); } catch { // clipboard unavailable — ignore } }; return ( <div className={cn("w-full", compact ? "max-w-[300px] space-y-2.5" : "max-w-[560px] space-y-4", className)}> <div className="grid grid-cols-5 gap-1.5 sm:gap-2"> {INK_TONES.map((t) => ( <button key={t.key} type="button" onClick={() => copy(t)} aria-label={`${t.name}墨 ${t.token}${copyable ? ",点击复制变量" : ""}`} className={cn("group text-left", copyable ? "cursor-pointer" : "cursor-default")} > <span className={cn( "block rounded-[10px] transition-transform duration-300 ease-[cubic-bezier(0.34,1.56,0.64,1)] group-hover:-translate-y-0.5", compact ? "h-10" : "h-16" )} style={{ background: `var(${t.token})`, boxShadow: "inset 0 0 0 1px rgba(127,127,127,0.18)" }} /> <span className="mt-1.5 flex items-baseline justify-between gap-1"> <span className={cn("font-kai text-ink-1", compact ? "text-sm" : "text-lg")}>{t.name}</span> {!compact && ( <span className="font-mono-num text-[10px] text-ink-4"> {copied === t.key ? "已复制" : t.token.replace("--", "")} </span> )} </span> {!compact && <span className="hidden sm:block text-[11px] text-ink-4">{t.role}</span>} </button> ))} </div> {showSpecimen && ( <div className={cn("rounded-[12px] border border-ink-5", compact ? "p-3 space-y-1" : "p-5 space-y-2")}> <div className={cn("font-kai text-ink-1", compact ? "text-base" : "text-2xl")}>山水之间,墨分五色</div> <p className={cn("text-ink-2", compact ? "text-[11px]" : "text-sm")}>正文用浓墨,阅读最稳。</p> {!compact && <p className="text-xs text-ink-3">次要信息用重墨,层级拉开但不刺眼。</p>} <p className="text-[11px] text-ink-4">说明与占位用淡墨。</p> <div className="h-px bg-ink-5" /> </div> )} </div> );}