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

目錄捲動追蹤

會標出目前正在讀哪一段的目錄,品牌專色的指示條在項目間滑動。

適合使用的時機

  • 長的商品說明、尺寸與保養頁、FAQ 或政策頁

注意事項

  • offset 設成固定 header 高度再多一點,才會點亮正確的項目
  • 每個項目的 id 都要對應頁面上元素的 id

交給你的 agent 安裝

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

用 CLI 安裝

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

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

或手動安裝

  1. 1. 安裝相依套件。
    $ npm install clsx tailwind-merge framer-motion
  2. 2. 如果還沒有,在 lib/utils.ts 加上 cn 輔助函式。沒有用 shadcn/ui?再加上 sunui-theme.css 提供顏色變數(做法)。然後複製元件程式碼。
    toc-scrollspy.tsx
    "use client";
    
    import { motion, useReducedMotion } from "framer-motion";
    import { useEffect, useId, useState, type ReactNode, type RefObject } from "react";
    
    import { cn } from "@/lib/utils";
    
    const springs = {
      snappy: { type: "spring", stiffness: 520, damping: 34 },
    } as const;
    
    export interface TocItem {
      /** id of the matching section element */
      id: string;
      label: ReactNode;
      /** 1 is the main heading, 2 is indented */
      level?: 1 | 2;
    }
    
    export interface ScrollSpyOptions {
      /** Pass this when used inside a scrollable container; defaults to the viewport */
      container?: RefObject<HTMLElement | null>;
      /** A section counts as "being read" when its top is within this many px of the scroll area top, usually the fixed header height plus a bit */
      offset?: number;
    }
    
    /**
     * Returns the id of the section being read: the last section whose top has scrolled past offset.
     * At the bottom it always returns the last one, so a short final section can still become active.
     */
    export function useScrollSpy(ids: string[], { container, offset = 96 }: ScrollSpyOptions = {}) {
      const [active, setActive] = useState<string | null>(ids[0] ?? null);
      const key = ids.join("|");
    
      useEffect(() => {
        const root = container?.current ?? null;
        const target: HTMLElement | Window = root ?? window;
        let raf = 0;
        const measure = () => {
          raf = 0;
          const top = root ? root.getBoundingClientRect().top : 0;
          const atEnd = root ? root.scrollTop + root.clientHeight >= root.scrollHeight - 2 : window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 2;
          let current = ids[0] ?? null;
          for (const id of ids) {
            const el = document.getElementById(id);
            if (el && el.getBoundingClientRect().top - top <= offset) current = id;
          }
          if (atEnd && ids.length) current = ids[ids.length - 1];
          setActive(current);
        };
        const onScroll = () => {
          if (!raf) raf = requestAnimationFrame(measure);
        };
        measure();
        target.addEventListener("scroll", onScroll, { passive: true });
        window.addEventListener("resize", onScroll);
        return () => {
          cancelAnimationFrame(raf);
          target.removeEventListener("scroll", onScroll);
          window.removeEventListener("resize", onScroll);
        };
        // eslint-disable-next-line react-hooks/exhaustive-deps
      }, [key, container, offset]);
    
      return active;
    }
    
    export interface TocScrollspyProps extends ScrollSpyOptions {
      items: TocItem[];
      title?: ReactNode;
      /** Smooth scroll on click (jumps instantly with reduced motion) */
      smooth?: boolean;
      onNavigate?: (id: string) => void;
      className?: string;
    }
    
    /** Brand accent: follows the theme's --primary by default; buyers set --sunui-spot to use their own brand color. Used only for selection, offers and current progress */
    const spotBg = "bg-[color:var(--sunui-spot,var(--primary))]";
    
    export function TocScrollspy({ items, title, container, offset = 96, smooth = true, onNavigate, className }: TocScrollspyProps) {
      const reduce = useReducedMotion();
      const group = useId();
      const active = useScrollSpy(
        items.map((i) => i.id),
        { container, offset },
      );
    
      const go = (id: string) => {
        const el = document.getElementById(id);
        if (!el) return;
        const root = container?.current;
        const behavior: ScrollBehavior = smooth && !reduce ? "smooth" : "auto";
        if (root) {
          root.scrollTo({ top: root.scrollTop + el.getBoundingClientRect().top - root.getBoundingClientRect().top - offset + 8, behavior });
        } else {
          window.scrollTo({ top: window.scrollY + el.getBoundingClientRect().top - offset + 8, behavior });
        }
        el.setAttribute("tabindex", "-1");
        el.focus({ preventScroll: true });
        onNavigate?.(id);
      };
    
      return (
        <nav aria-label={typeof title === "string" ? title : "Table of contents"} className={cn("text-sm", className)}>
          {title && <p className="mb-3 text-xs font-medium uppercase tracking-[0.12em] text-muted-foreground">{title}</p>}
          <ul className="relative border-l border-foreground/10">
            {items.map((item) => {
              const on = item.id === active;
              return (
                <li key={item.id} className="relative">
                  {on && (
                    <motion.span
                      layoutId={`${group}-bar`}
                      transition={reduce ? { duration: 0 } : springs.snappy}
                      className={cn("absolute -left-px inset-y-0 w-0.5", spotBg)}
                    />
                  )}
                  <a
                    href={`#${item.id}`}
                    aria-current={on ? "location" : undefined}
                    onClick={(e) => {
                      e.preventDefault();
                      go(item.id);
                    }}
                    className={cn(
                      "block py-1.5 pr-2 outline-offset-2 transition-colors focus-visible:outline-2",
                      item.level === 2 ? "pl-7" : "pl-4",
                      on ? "font-medium text-foreground" : "text-muted-foreground hover:text-foreground",
                    )}
                  >
                    {item.label}
                  </a>
                </li>
              );
            })}
          </ul>
        </nav>
      );
    }