动漫加载
没有数字可显示时的等待指示:一条墨色块在轨道里用弹簧来回冲刺,随速度拉长,每到一端就扬起灰尘与晃动弧线。
适合使用的场景
- 匹配、加载关卡,或任何不知道要等多久的等待
- 游戏或动漫页面上的短暂等待
注意事项
- 知道进度时改用 anime-progress
- 挂载期间会一直动;等待结束时请移除
交给你的 agent 安装
一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。
用 CLI 安装
使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。
$ npx shadcn@latest add https://sunui.synvize.com/r/anime-loader.json或手动安装
- 1. 安装依赖。
$ npm install clsx tailwind-merge - 2. 如果还没有,在
lib/utils.ts添加cn辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。anime-loader.tsx"use client"; import { useEffect, useRef, useState } from "react"; import { at, calm, later, onFrame, paletteOf, puff, Spring, wob } from "@/components/ui/anime-fx"; import { cn } from "@/lib/utils"; export type AnimeLoaderProps = { /** Read out to screen readers and shown under the bar unless hideLabel */ label?: string; hideLabel?: boolean; 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))]"; /** * A waiting indicator for when there is no number to show: an ink slug charges back and forth inside a track on a spring, * stretching with speed, and kicks up dust with wobble marks at each end. With reduced motion it is a still half bar. */ export function AnimeLoader({ label = "Loading", hideLabel = false, className }: AnimeLoaderProps) { const track = useRef<HTMLDivElement>(null); const slug = useRef<HTMLSpanElement>(null); const [still, setStill] = useState(false); useEffect(() => { if (calm()) { setStill(true); return; } const s = new Spring(0, 90, 7); s.to = 1; let turned = false; const stop = onFrame((dt) => { const x = s.step(dt); const el = slug.current; const host = track.current; if (!el || !host) return; const travel = host.clientWidth - el.offsetWidth - 6; const stretch = 1 + Math.min(0.6, Math.abs(s.v) * 0.18); el.style.transform = `translateX(${(Math.max(0, Math.min(1, x)) * travel).toFixed(2)}px) scaleX(${stretch.toFixed(3)})`; // Turn around just before the end, so it overshoots into the wall and kicks up dust if (!turned && Math.abs(x - s.to) < 0.08) { turned = true; const P = paletteOf(host); const end = at(host, s.to, 0.5); const dir = s.to === 1 ? 1 : -1; wob(P, end.x + dir * 4, end.y, { dir, r: 9, life: 0.35 }); puff(P, end.x - dir * 6, end.y + end.h / 2, { size: 6, n: 4, vx: -dir * 20, vy: -12, life: 0.55 }); later(140, () => { s.to = s.to === 1 ? 0 : 1; turned = false; }); } }); return stop; }, []); return ( <div role="status" className={cn("w-full max-w-xs", className)}> <div ref={track} className={cn("relative h-5 overflow-hidden rounded-full p-[3px]", SHELL)}> <span ref={slug} aria-hidden className={cn("block h-full w-1/4 origin-center rounded-full bg-[var(--sunui-spot,var(--primary))] will-change-transform", still && "w-1/2")} /> </div> <p className={cn("mt-2 text-[13px] font-bold", hideLabel && "sr-only")}>{label}</p> </div> ); }




