components/linyi/seal-badge.tsx
motion
"use client"; import * as React from "react";import { motion } from "motion/react";import { cn } from "@/lib/cn"; export type SealVariant = "zhu" | "bai";export type SealShape = "square" | "round" | "rect"; export interface SealBadgeProps { /** 1–4 characters for square/round seals (read right column first); any short text for rect. */ text: string; /** zhu 朱文(红字) · bai 白文(红底留白字) */ variant?: SealVariant; shape?: SealShape; /** Height in px. */ size?: number; /** Seal ink colour; defaults to the theme's `--seal-ink`. */ color?: string; /** Re-stamp with a press animation when clicked. */ stampable?: boolean; onStamp?: () => void; className?: string; "aria-label"?: string;} /** * 朱印徽标 —— small carved-seal badges for status, tiers and prices. * Inline SVG: displaced edges + uneven seal-paste coverage, rendered once (static filter). */export function SealBadge({ text, variant = "zhu", shape = "square", size = 56, color = "var(--seal-ink)", stampable = false, onStamp, className, "aria-label": ariaLabel,}: SealBadgeProps) { const id = React.useId().replace(/:/g, ""); const [stamps, setStamps] = React.useState(0); const h = size; const w = shape === "rect" ? Math.max(size * 1.7, size * 0.62 * Math.max(2, [...text].length)) : size; const chars = [...text]; const glyphFill = variant === "bai" ? "var(--ink-paper)" : color; const fs = shape === "rect" ? h * 0.5 : chars.length > 2 ? h * 0.34 : chars.length === 2 ? h * 0.38 : h * 0.56; const glyph = (c: string, x: number, y: number, key: React.Key) => ( <text key={key} x={x} y={y} dominantBaseline="central" textAnchor="middle" fontSize={fs} fontWeight={700} fill={glyphFill} style={{ fontFamily: "var(--font-kai-stack)" }}> {c} </text> ); const glyphs = shape === "rect" ? [glyph(text, w / 2, h / 2, "t")] : chars.length >= 4 ? chars.slice(0, 4).map((c, i) => glyph(c, i < 2 ? w * 0.7 : w * 0.3, i % 2 === 0 ? h * 0.32 : h * 0.7, i)) : chars.length === 3 ? [glyph(chars[0], w * 0.7, h * 0.5, 0), glyph(chars[1], w * 0.3, h * 0.32, 1), glyph(chars[2], w * 0.3, h * 0.7, 2)] : chars.map((c, i) => glyph(c, w / 2, chars.length === 1 ? h / 2 : i === 0 ? h * 0.31 : h * 0.71, i)); const stroke = Math.max(1.6, h * 0.05); const frame = shape === "round" ? ( <circle cx={w / 2} cy={h / 2} r={h / 2 - stroke} fill={variant === "bai" ? color : "none"} stroke={color} strokeWidth={variant === "bai" ? 0 : stroke} /> ) : ( <rect x={stroke / 2 + 1} y={stroke / 2 + 1} width={w - stroke - 2} height={h - stroke - 2} rx={shape === "rect" ? h * 0.16 : h * 0.05} fill={variant === "bai" ? color : "none"} stroke={color} strokeWidth={variant === "bai" ? 0 : stroke} /> ); const svg = ( <motion.svg key={stamps} width={w} height={h} viewBox={`0 0 ${w} ${h}`} initial={stamps === 0 ? false : { scale: 1.35, opacity: 0, rotate: -6 }} animate={{ scale: 1, opacity: 1, rotate: -2 }} transition={{ type: "spring", stiffness: 520, damping: 24, mass: 0.7 }} style={{ overflow: "visible", display: "block" }} aria-hidden="true" > <defs> <filter id={`seal-${id}`} x="-8%" y="-8%" width="116%" height="116%"> <feTurbulence type="fractalNoise" baseFrequency="0.85" numOctaves="2" seed="4" result="n" /> <feDisplacementMap in="SourceGraphic" in2="n" scale={Math.max(1.4, h * 0.04)} xChannelSelector="R" yChannelSelector="G" result="d" /> <feTurbulence type="fractalNoise" baseFrequency="0.16" numOctaves="2" seed="11" result="blot" /> <feColorMatrix in="blot" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -2.6 0 0 0 2.42" result="m" /> <feComposite in="d" in2="m" operator="in" /> </filter> </defs> <g filter={`url(#seal-${id})`}> {frame} {glyphs} </g> </motion.svg> ); const label = ariaLabel ?? `印:${text}`; if (!stampable) { return ( <span role="img" aria-label={label} className={cn("inline-flex", className)}> {svg} </span> ); } return ( <button type="button" aria-label={`${label},点击盖章`} onClick={() => { setStamps((n) => n + 1); onStamp?.(); }} className={cn("inline-flex cursor-pointer rounded-[4px]", className)} > {svg} </button> );}