SunUI
创始会员$79 终身更新,1月11日截止领取名额
免费动漫作画点按

动漫标签页

凸起的标签用弹簧滑到新的标签,途中随速度拉长,落地时有点缀楔形、晃动弧线,标签栏下方扬起灰尘。

适合使用的场景

  • 在几个视图间切换:简介、技能、装备;详情、评价、规格
  • 游戏或动漫网站的角色与道具页

注意事项

  • 两到五个短标签放得下,不会滚动
  • 没有拟声字:标签经常切换,所以落地保持小
  • 开启减少动态效果时一个特效都不画,状态照常改变,不看特效也能使用

交给你的 agent 安装

一次复制就包含安装命令、使用时机、props 与设计规则。粘贴到 Claude Code、Cursor 或 Codex 即可。

用 CLI 安装

使用 shadcn CLI。源码会复制到你的项目中,每一行都由你掌控。

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

或手动安装

  1. 1. 安装依赖。
    $ npm install clsx tailwind-merge
  2. 2. 如果还没有,在 lib/utils.ts 添加 cn 辅助函数。没有用 shadcn/ui?再加上 sunui-theme.css 提供颜色变量(做法)。然后复制组件代码。
    anime-tabs.tsx
    "use client";
    
    import { useEffect, useId, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
    
    import { at, onFrame, paletteOf, puff, Spring, ticks, wob } from "@/components/ui/anime-fx";
    import { cn } from "@/lib/utils";
    
    export type AnimeTab = { value: string; label: string; content: ReactNode };
    
    export type AnimeTabsProps = {
      tabs: AnimeTab[];
      value?: string;
      defaultValue?: string;
      onValueChange?: (value: string) => void;
      /** Accessible name of the tab list */
      label: string;
      className?: string;
    };
    
    /**
     * Tabs where the raised tab slides to the new one on a spring, stretching with speed, and lands with accent wedges,
     * wobble marks and a puff. Keyboard follows the tabs pattern: arrow keys, Home and End move and select.
     */
    export function AnimeTabs({ tabs, value, defaultValue, onValueChange, label, className }: AnimeTabsProps) {
      const id = useId();
      const [own, setOwn] = useState(defaultValue ?? tabs[0]?.value);
      const current = value ?? own;
      const list = useRef<HTMLDivElement>(null);
      const pill = useRef<HTMLSpanElement>(null);
      const buttons = useRef<Record<string, HTMLButtonElement | null>>({});
      const x = useRef(new Spring(0, 360, 26));
      const w = useRef(new Spring(0, 360, 26));
      const stop = useRef<(() => void) | null>(null);
      const first = useRef(true);
    
      const paint = (stretch = 1) => {
        if (!pill.current) return;
        pill.current.style.width = `${w.current.x}px`;
        pill.current.style.transform = `translateX(${x.current.x}px) scaleX(${stretch.toFixed(3)})`;
      };
    
      useLayoutEffect(() => {
        const b = buttons.current[current];
        const host = list.current;
        if (!b || !host) return;
        const left = b.offsetLeft;
        const width = b.offsetWidth;
        // First paint and resizes: jump there; a change of tab: travel on the spring and land with effects
        if (first.current) {
          first.current = false;
          x.current.x = x.current.to = left;
          w.current.x = w.current.to = width;
          paint();
          return;
        }
        x.current.to = left;
        w.current.to = width;
        const dir = left > x.current.x ? 1 : -1;
        let landed = false;
        stop.current?.();
        stop.current = onFrame((dt) => {
          x.current.step(dt);
          w.current.step(dt);
          paint(1 + Math.min(0.25, Math.abs(x.current.v) / 4000));
          if (!landed && Math.abs(x.current.x - left) < 3 && pill.current) {
            landed = true;
            const P = paletteOf(pill.current);
            const c = at(pill.current);
            ticks(P, c.x, c.y - c.h / 2 - 2, { n: 3, r: 4, len: 10, fan: 0.5 });
            wob(P, c.x + dir * (c.w / 2 + 6), c.y, { dir, r: 9, life: 0.35 });
            // Dust below the tab list, so it never drifts over the tab labels
            puff(P, c.x - dir * (c.w / 2), c.y + c.h / 2 + 12, { size: 6, n: 4, vx: -dir * 22, vy: 4, life: 0.5 });
          }
          if (x.current.still && w.current.still) {
            stop.current?.();
            stop.current = null;
            paint();
          }
        });
      }, [current]);
      useEffect(() => () => stop.current?.(), []);
    
      const select = (v: string) => {
        if (value === undefined) setOwn(v);
        onValueChange?.(v);
      };
    
      const key = (e: KeyboardEvent, i: number) => {
        const n = tabs.length;
        const next = e.key === "ArrowRight" ? (i + 1) % n : e.key === "ArrowLeft" ? (i - 1 + n) % n : e.key === "Home" ? 0 : e.key === "End" ? n - 1 : -1;
        if (next < 0) return;
        e.preventDefault();
        select(tabs[next].value);
        buttons.current[tabs[next].value]?.focus();
      };
    
      return (
        <div className={cn("w-full", className)}>
          <div ref={list} role="tablist" aria-label={label} className="relative inline-flex rounded-[14px] border-[3px] border-foreground bg-card p-1">
            <span
              ref={pill}
              aria-hidden
              className="absolute top-1 bottom-1 left-0 origin-center rounded-[10px] border-[3px] border-foreground bg-card shadow-[3px_4px_0_color-mix(in_oklch,var(--sunui-spot,var(--primary))_38%,var(--card))] will-change-transform"
            />
            {tabs.map((t, i) => (
              <button
                key={t.value}
                ref={(el) => void (buttons.current[t.value] = el)}
                id={`${id}-t-${t.value}`}
                type="button"
                role="tab"
                aria-selected={current === t.value}
                aria-controls={`${id}-p-${t.value}`}
                tabIndex={current === t.value ? 0 : -1}
                onClick={() => select(t.value)}
                onKeyDown={(e) => key(e, i)}
                className={cn(
                  "relative z-10 rounded-[10px] px-4 py-2 text-sm whitespace-nowrap outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-[var(--sunui-spot,var(--primary))]",
                  current === t.value ? "font-extrabold italic" : "font-semibold text-muted-foreground hover:text-foreground",
                )}
              >
                {t.label}
              </button>
            ))}
          </div>
          {tabs.map((t) => (
            <div key={t.value} id={`${id}-p-${t.value}`} role="tabpanel" aria-labelledby={`${id}-t-${t.value}`} hidden={current !== t.value} tabIndex={0} className="mt-5 outline-none focus-visible:ring-[3px] focus-visible:ring-[var(--sunui-spot,var(--primary))] rounded-lg">
              {t.content}
            </div>
          ))}
        </div>
      );
    }