动漫通知
从侧边拖影冲进来的通知,在一团灰尘里滑停并落下滑停拟声字,离开时缩小消失;variant="cut-in" 则像必杀技一样,在一条斜切的切入色带后面砸进来,关闭时沿一刀被斩成两半。
适合使用的场景
- 在游戏或动漫页面确认一个操作:已保存、已发送、已解锁
- 带一个可选操作(例如撤销)的短消息
注意事项
- 一次一条;用 open 与 onOpenChange 控制,不是队列
- 标题请简短;拟声字落在卡片上缘之上
- 开启减少动态效果时一个特效都不画,状态照常改变,不看特效也能使用
交给你的 agent 安装
一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。
用 CLI 安装
使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。
$ npx shadcn@latest add https://sunui.synvize.com/r/anime-toast.json或手动安装
- 1. 安装依赖。
$ npm install clsx tailwind-merge - 2. 如果还没有,在
lib/utils.ts添加cn辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。anime-toast.tsx"use client"; import { useEffect, useRef, useState, type ReactNode } from "react"; import { add, at, calm, clamp, inn, later, lines, onFrame, paletteOf, puff, sfx, slash, Spring, ticks, useBackLayer, type Layer, type Palette, type SfxLang } from "@/components/ui/anime-fx"; import { cn } from "@/lib/utils"; export type AnimeToastProps = { open: boolean; onOpenChange: (open: boolean) => void; title: ReactNode; description?: ReactNode; action?: { label: string; onClick: () => void }; /** Dismiss label for the close button */ closeLabel?: string; /** Milliseconds before it leaves by itself; paused while hovered, focused or the tab is hidden. 0 keeps it open. */ duration?: number; /** Lettering language; defaults to <html lang> */ lang?: SfxLang; /** Show the sound-effect word when it lands */ lettering?: boolean; /** * "skid" (default) streaks in from the side and skids to a stop, then shrinks away. * "cut-in" slams in behind a slanted cut-in band like a super move, and closing it slices it in half. */ variant?: "skid" | "cut-in"; className?: string; }; const SHELL = "border-[3px] border-foreground bg-card shadow-[4px_5px_0_color-mix(in_oklch,var(--sunui-spot,var(--primary))_38%,var(--card))]"; const out = (t: number) => 1 - (1 - t) ** 3; /** The cut runs from 62% down the left edge to 38% down the right edge. */ const CUT = { l: 0.62, r: 0.38 }; /** A slanted band that sweeps across the card's row, then thins away: shadow face, a lit streak, the brand outline. */ function cutIn(P: Palette, el: HTMLElement, layer: Layer | null, life = 0.42) { // The card is still at scale 0 here, so take its size from layout and its centre from the box const b = el.getBoundingClientRect(); const cx = b.left + scrollX + b.width / 2; const x0 = cx - el.offsetWidth / 2 - 90; const x1 = cx + el.offsetWidth / 2 + 90; const cy = b.top + scrollY + b.height / 2; const h = el.offsetHeight * 0.9; const lean = h * 0.5; add(life, (g, k) => { const grow = out(clamp(k / 0.35)); const thin = 1 - inn(clamp((k - 0.45) / 0.55)); const hh = (h / 2) * thin; if (hh < 0.5) return; const right = x0 + (x1 - x0) * grow; const band = (top: number, bot: number) => { const p = new Path2D(); p.moveTo(x0 + lean, top); p.lineTo(right + lean, top); p.lineTo(right - lean, bot); p.lineTo(x0 - lean, bot); p.closePath(); return p; }; const all = band(cy - hh, cy + hh); g.fillStyle = P.shade; g.fill(all); // The lit streak stays inside the band's outline g.save(); g.clip(all); g.fillStyle = P.fill; g.fill(band(cy - hh * 0.55, cy - hh * 0.18)); g.restore(); g.strokeStyle = P.line; g.lineWidth = 2; g.stroke(all); }, layer); } /** The slash along the cut, a little past both edges. */ function cut(P: Palette, el: HTMLElement) { const b = el.getBoundingClientRect(); const ext = 46; const dy = (b.height * (CUT.r - CUT.l)) / b.width; slash(P, b.left + scrollX - ext, b.top + scrollY + b.height * CUT.l - dy * ext, b.right + scrollX + ext, b.top + scrollY + b.height * CUT.r + dy * ext); } /** * A notification that streaks in from the side as a smear, skids to a stop in a puff of dust with a skid word, and leaves by * shrinking away; or, with variant="cut-in", slams in behind a cut-in band and is sliced in half when closed. Its live region is always on the page, so the message is announced; the timer pauses on hover, focus and * when the tab is hidden, and Escape closes it. */ export function AnimeToast({ open, onOpenChange, title, description, action, closeLabel = "Dismiss", duration = 4200, lang, lettering = true, variant = "skid", className }: AnimeToastProps) { const card = useRef<HTMLDivElement>(null); const halves = useRef<(HTMLDivElement | null)[]>([]); const [shown, setShown] = useState(open); const [sliced, setSliced] = useState(false); // The cut-in band goes behind the card, so the card slams in over it const back = useBackLayer(150); const x = useRef(new Spring(1, 260, 22)); const s = useRef(new Spring(1, 320, 24)); const stop = useRef<(() => void) | null>(null); const held = useRef(false); const paint = () => { const el = card.current; if (!el) return; const v = x.current.v; // Smear while it travels: stretched along the move and leaning into it const smear = 1 + Math.min(0.5, Math.abs(v) * 0.08); el.style.transform = `translateX(${(x.current.x * 120).toFixed(2)}%) skewX(${Math.max(-16, Math.min(0, v * 3)).toFixed(1)}deg) scale(${(smear * s.current.x).toFixed(3)}, ${((1 / Math.sqrt(smear)) * s.current.x).toFixed(3)})`; }; // Enter: slide in from the right and land; leave: shrink away, then unmount useEffect(() => { if (open) setShown(true); }, [open]); useEffect(() => { if (!shown) return; const el = card.current; stop.current?.(); if (open) { if (calm()) { x.current.x = x.current.to = 0; s.current.x = s.current.to = 1; paint(); return; } setSliced(false); if (variant === "cut-in" && el) { // Hidden until the band has crossed, then it slams in a size too big and springs down to rest x.current.x = x.current.to = 0; s.current.x = s.current.to = 0; paint(); const P = paletteOf(el); cutIn(P, el, back.layer()); const s2 = new Spring(0, 520, 20); let go = false; later(150, () => { go = true; s2.x = 1.22; s2.to = 1; // Still scaled to 0 at this moment: place things from the layout size around the centre const c = at(el); const left = c.x - el.offsetWidth / 2; const top = c.y - el.offsetHeight / 2; ticks(P, left - 12, c.y, { dir: Math.PI, n: 3, r: 10, len: 14 }); if (lettering) sfx(P, "swish", left + 46, top - 30, { size: 46, lang, rot: -0.12, life: 0.8 }); }); stop.current = onFrame((dt) => { if (!go) return; s2.step(dt); s.current.x = s2.x; paint(); if (s2.still) { stop.current?.(); stop.current = null; } }); return () => { stop.current?.(); stop.current = null; }; } x.current.x = 1; x.current.to = 0; s.current.x = s.current.to = 1; let landed = false; stop.current = onFrame((dt) => { x.current.step(dt); paint(); if (!landed && el && x.current.x < 0.03) { landed = true; const P = paletteOf(el); const c = at(el, 0, 1); // Dust off the trailing corner, outside the card, so it never covers the text puff(P, c.x - 8, c.y - 8, { size: 9, n: 5, vx: -26, vy: -10, life: 0.6 }); lines(P, at(el, 1, 0.5).x + 8, at(el, 1, 0.5).y, { n: 4, dir: 0, len: 30, spread: 14, life: 0.28 }); const top = at(el, 0, 0); if (lettering) sfx(P, "skid", top.x + 46, top.y - 30, { size: 46, lang, rot: -0.1, life: 0.85 }); } if (x.current.still) { stop.current?.(); stop.current = null; } }); } else if (variant === "cut-in" && el && !calm()) { // Slice: the stroke crosses, the card splits along it, and the halves slide apart along the cut and shrink away cut(paletteOf(el), el); setSliced(true); let t = 0; const ang = Math.atan2(CUT.r - CUT.l, 1); stop.current = onFrame((dt) => { t += dt; const k = clamp((t - 0.07) / 0.4); const d = out(k) * 26; const sc = 1 - inn(clamp((k - 0.35) / 0.65)); halves.current.forEach((h, i) => { if (!h) return; const sign = i ? -1 : 1; const tx = Math.cos(ang) * d * sign; const ty = Math.sin(ang) * d * sign + (i ? 5 : -5) * out(k); h.style.transform = `translate(${tx.toFixed(2)}px, ${ty.toFixed(2)}px) rotate(${(sign * out(k) * 2.5).toFixed(2)}deg) scale(${sc.toFixed(3)})`; }); if (k >= 1) { stop.current?.(); stop.current = null; setShown(false); setSliced(false); } }); } else { s.current.to = 0; stop.current = onFrame((dt) => { s.current.step(dt); paint(); if (s.current.x < 0.04) { stop.current?.(); stop.current = null; setShown(false); } }); if (calm()) setShown(false); } return () => { stop.current?.(); stop.current = null; }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [open, shown, variant]); // Escape closes it from anywhere while it is open useEffect(() => { if (!open) return; const esc = (e: KeyboardEvent) => e.key === "Escape" && onOpenChange(false); window.addEventListener("keydown", esc); return () => window.removeEventListener("keydown", esc); }, [open, onOpenChange]); // Auto-dismiss, paused while held (hover, focus) or while the tab is hidden useEffect(() => { if (!open || !duration) return; let left = duration; let last = performance.now(); const t = window.setInterval(() => { const now = performance.now(); if (!held.current && document.visibilityState === "visible") left -= now - last; last = now; if (left <= 0) onOpenChange(false); }, 100); return () => window.clearInterval(t); }, [open, duration, onOpenChange]); const body = (live: boolean) => ( <> <div className="min-w-0 flex-1"> <p className="text-[15px] font-extrabold italic">{title}</p> {description && <p className="mt-0.5 text-sm text-muted-foreground">{description}</p>} </div> {action && ( <button type="button" tabIndex={live ? undefined : -1} onClick={() => { action.onClick(); onOpenChange(false); }} className="shrink-0 rounded-[10px] border-[2.5px] border-foreground px-3 py-1.5 text-sm font-bold outline-none focus-visible:ring-[3px] focus-visible:ring-[var(--sunui-spot,var(--primary))]" > {action.label} </button> )} <button type="button" tabIndex={live ? undefined : -1} aria-label={closeLabel} onClick={() => onOpenChange(false)} className="-mr-1 grid size-8 shrink-0 place-items-center rounded-lg text-muted-foreground outline-none hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-[var(--sunui-spot,var(--primary))]" > <svg viewBox="0 0 24 24" aria-hidden className="size-4" fill="none" stroke="currentColor" strokeWidth={2.4} strokeLinecap="round"> <path d="M6 6l12 12M18 6L6 18" /> </svg> </button> </> ); return ( <div role="status" aria-live="polite" className="pointer-events-none fixed inset-x-0 bottom-0 z-50 isolate flex justify-center p-4 sm:justify-end sm:p-6"> {/* Always mounted, so its layer is registered before a cut-in needs it */} <canvas {...back.props} /> {shown && ( <div className="pointer-events-auto relative w-full max-w-sm"> <div ref={card} onPointerEnter={() => (held.current = true)} onPointerLeave={() => (held.current = false)} onFocus={() => (held.current = true)} onBlur={() => (held.current = false)} className={cn("flex w-full items-start gap-3 rounded-[14px] px-4 py-3.5 will-change-transform", variant === "cut-in" ? "origin-center" : "origin-bottom-left", SHELL, sliced && "invisible", className)} style={{ transform: variant === "cut-in" ? "scale(0)" : "translateX(120%)" }} > {body(true)} </div> {/* The two halves of a sliced card: copies for the eye only, clipped along the cut */} {sliced && [`polygon(0 0, 100% 0, 100% ${CUT.r * 100}%, 0 ${CUT.l * 100}%)`, `polygon(0 ${CUT.l * 100}%, 100% ${CUT.r * 100}%, 100% 100%, 0 100%)`].map((clip, i) => ( <div key={i} aria-hidden inert ref={(n) => { halves.current[i] = n; }} className={cn("absolute inset-0 flex items-start gap-3 rounded-[14px] px-4 py-3.5 will-change-transform", SHELL, className)} style={{ clipPath: clip }} > {body(false)} </div> ))} </div> )} </div> ); }
相似区块
- Pro


