动漫复制
一行文字加一个复制按钮,用在安装命令、邀请码与推荐链接。
适合使用的场景
- 游戏或活动页的邀请码、推荐链接、礼品码
- 只会复制一次的安装命令或 API 密钥
注意事项
- 太长的文字会以省略号截断,完整内容放在 title;复制的永远是完整文字
- 剪贴板需要安全页面(https 或 localhost),其他情况会改为选中文字
- 开启减少动态效果时一个特效都不画,标签照常改变
交给你的 agent 安装
一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。
用 CLI 安装
使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。
$ npx shadcn@latest add https://sunui.synvize.com/r/anime-copy.json或手动安装
- 1. 安装依赖。
$ npm install clsx tailwind-merge - 2. 如果还没有,在
lib/utils.ts添加cn辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。anime-copy.tsx"use client"; import { useEffect, useRef, useState } from "react"; import { at, later, onFrame, paletteOf, sfx, slash, Spring, ticks, type SfxLang } from "@/components/ui/anime-fx"; import { cn } from "@/lib/utils"; export type AnimeCopyProps = { /** What is shown and copied */ text: string; /** The button's label */ label?: string; /** The button's label after copying */ copiedLabel?: string; /** The button's label when the clipboard refused and the text was selected instead */ selectedLabel?: string; /** Called after the text is on the clipboard */ onCopy?: (text: string) => void; /** Back to ready this many milliseconds after copying */ resetAfter?: number; /** Lettering language; defaults to <html lang> */ lang?: SfxLang; /** Show the sound-effect word on the cut */ lettering?: boolean; className?: string; }; const SHELL = "border-[3px] border-foreground bg-card text-foreground shadow-[4px_5px_0_color-mix(in_oklch,var(--sunui-spot,var(--primary))_38%,var(--card))]"; async function write(text: string) { try { await navigator.clipboard.writeText(text); return true; } catch { return false; } } /** * A line of text with a copy button: an install command, an invite code, a referral link. Hover or focus the button and two * wedges point at it; click and one stroke cleaves across the whole bar, the bar recoils, the button inverts for a frame and a * click word lands; the button settles on a check. If the clipboard refuses, the text is selected so the person can copy it. */ export function AnimeCopy({ text, label = "Copy", copiedLabel = "Copied", selectedLabel = "Selected", onCopy, resetAfter = 1800, lang, lettering = true, className }: AnimeCopyProps) { const bar = useRef<HTMLDivElement>(null); const code = useRef<HTMLElement>(null); const btn = useRef<HTMLButtonElement>(null); const [state, setState] = useState<"ready" | "copied" | "selected">("ready"); const [inverted, setInverted] = useState(false); const st = useRef({ x: new Spring(0, 700, 16), off: null as null | (() => void), seq: 0, aimed: false }); useEffect(() => { const s = st.current; return () => { s.off?.(); s.off = null; }; }, []); // Anticipation: two wedges point at the button, once per hover or focus const aim = () => { const el = btn.current; const s = st.current; if (!el || s.aimed || state !== "ready") return; s.aimed = true; const P = paletteOf(el); const c = at(el, 0.5, 1); ticks(P, c.x, c.y + 18, { dir: -Math.PI / 2, n: 2, r: 4, len: 12, fan: 0.9, life: 0.38 }); }; // Recoil: the bar is knocked along the cut and springs back const recoil = () => { const s = st.current; s.x.v = -260; if (s.off) return; s.off = onFrame((dt) => { const x = s.x.step(dt); if (bar.current) bar.current.style.transform = s.x.still ? "" : `translate(${x.toFixed(2)}px, ${(x * 0.35).toFixed(2)}px)`; if (s.x.still) { s.off?.(); s.off = null; } }); }; const copy = async () => { const el = bar.current; const b = btn.current; const s = st.current; if (!el || !b) return; const ok = await write(text); const n = ++s.seq; if (!ok) { // The clipboard refused (an insecure page, a denied permission): select the text instead const range = document.createRange(); if (code.current) range.selectNodeContents(code.current); const sel = window.getSelection(); sel?.removeAllRanges(); sel?.addRange(range); setState("selected"); } else { setState("copied"); onCopy?.(text); const P = paletteOf(el); const r = el.getBoundingClientRect(); // The cleave: a stroke across the whole bar, rising left to right, a little past both ends slash(P, r.left + scrollX - 48, r.top + scrollY + r.height * 1.05, r.right + scrollX + 48, r.top + scrollY - r.height * 0.05, { width: 12, life: 0.36 }); recoil(); setInverted(true); later(180, () => n === s.seq && setInverted(false)); const c = at(b, 0.5, 0); if (lettering) sfx(P, "click", c.x - 4, c.y - 28, { size: 44, lang, rot: -0.1, life: 0.75 }); ticks(P, c.x + 26, c.y - 4, { dir: -0.7, n: 2, r: 4, len: 13 }); } window.setTimeout(() => { if (n !== s.seq) return; setState("ready"); s.aimed = false; }, resetAfter); }; const shown = state === "copied" ? copiedLabel : state === "selected" ? selectedLabel : label; return ( <div ref={bar} className={cn("flex w-full max-w-md items-center gap-3 rounded-[14px] py-2 pr-2 pl-4 will-change-transform", SHELL, className)}> <code ref={code} className="min-w-0 flex-1 truncate font-mono text-[14px] font-semibold" title={text}> {text} </code> <button ref={btn} type="button" onClick={copy} onPointerEnter={aim} onFocus={aim} onPointerLeave={() => state === "ready" && (st.current.aimed = false)} className={cn( "inline-flex shrink-0 items-center gap-1.5 rounded-[10px] border-[2.5px] border-foreground bg-card px-3.5 py-1.5 text-[14px] leading-none font-extrabold italic select-none touch-manipulation", "outline-none focus-visible:ring-[3px] focus-visible:ring-[var(--sunui-spot,var(--primary))] focus-visible:ring-offset-2 focus-visible:ring-offset-background", state === "copied" && "border-[var(--sunui-spot,var(--primary))]", inverted && "bg-foreground text-background", )} > <svg viewBox="0 0 24 18" aria-hidden className="h-[13px] overflow-visible transition-[width] duration-200" style={{ width: state === "copied" ? 16 : 0 }}> <path d="M3 9.5l6 6L21 2.5" fill="none" stroke="var(--sunui-success, currentColor)" strokeWidth={4} strokeLinecap="round" strokeLinejoin="round" pathLength={1} strokeDasharray={1} strokeDashoffset={state === "copied" ? 0 : 1} style={{ transition: "stroke-dashoffset 240ms cubic-bezier(.2,.8,.2,1)" }} /> </svg> {shown} </button> <span role="status" aria-live="polite" className="sr-only"> {state === "ready" ? "" : shown} </span> </div> ); }
相似区块
- Pro
- Pro



