SunUI
創始會員NT$2,490 終身更新,1月11日截止取得席位
免費動漫作畫載入

動漫載入

沒有數字可顯示時的等待指示:一條墨色塊在軌道裡用彈簧來回衝刺,隨速度拉長,每到一端就揚起灰塵與晃動弧線。

適合使用的時機

  • 配對、載入關卡,或任何不知道要等多久的等待
  • 遊戲或動漫頁面上的短暫等待

注意事項

  • 知道進度時改用 anime-progress
  • 掛著就一直動;等待結束時請移除

交給你的 agent 安裝

一次複製就包含安裝指令、使用時機、props 與設計規則。貼進 Claude Code、Cursor 或 Codex 即可。

用 CLI 安裝

使用 shadcn CLI。原始碼會複製到你的專案裡,每一行都由你掌握。

$ npx shadcn@latest add https://sunui.synvize.com/r/anime-loader.json

或手動安裝

  1. 1. 安裝相依套件。
    $ npm install clsx tailwind-merge
  2. 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>
      );
    }