PC02▸🧭 Navigation · 導航▸層級
Breadcrumb
麵包屑
難度 ★★★★★標籤 breadcrumb · 層級 · 省略3 min read
模式簡述
Breadcrumb:路徑追蹤。讓使用者知道自己在哪、可以跳回任一層級。深層內容必備。
Claude Code Prompt
參考:PC02 breadcrumb 組件:Breadcrumb.tsx Breadcrumb:路徑追蹤。讓使用者知道自己在哪、可以跳回任一層級。深層內容必備。 限制: - 層級太深沒省略 - 最後一項做成連結 - 分隔符用 / 太硬
完整程式碼
"use client";
import { ChevronRight } from "lucide-react";
type State = "short" | "long";
const SHORT = ["Home", "Projects", "Frontend Lab"];
const LONG = [
"Home",
"Workspace",
"Studio",
"Projects",
"2026",
"Frontend Lab",
"Pattern PA01",
];
export function Breadcrumb({ state = "short" }: { state?: State }) {
const items = state === "long" ? LONG : SHORT;
const display: (string | "…")[] =
items.length > 4
? [items[0], "…", items[items.length - 2], items[items.length - 1]]
: items;
return (
<nav className="w-full max-w-md">
<ol className="flex items-center gap-1 flex-wrap font-mono text-xs">
{display.map((it, i) => {
const isLast = i === display.length - 1;
return (
<li key={i} className="flex items-center gap-1">
{it === "…" ? (
<span className="text-muted-foreground/60">…</span>
) : isLast ? (
<span className="text-foreground">{it}</span>
) : (
<a href="#" className="text-muted-foreground hover:text-lab-accent">
{it}
</a>
)}
{!isLast && <ChevronRight className="h-3 w-3 text-muted-foreground/40" />}
</li>
);
})}
</ol>
{state === "long" && (
<p className="mt-3 font-mono text-[10px] text-muted-foreground/60">
▸ 7 層被收摺成 4 個顯示節點
</p>
)}
</nav>
);
}何時用
- ✅ 檔案系統、CMS、有清楚階層的網站
- ✅ 電商的分類層級
- ✅ 知識庫的章節導航
何時別用
- ❌ 扁平結構網站(只有兩層沒必要)
- ❌ 手機優先的介面(會太擠)
- ❌ 使用者已經有 sidebar nav 顯示位置
我踩過的坑
-
層級太深沒省略:> 4 層就用 …省略中間,保留首末。
-
最後一項做成連結:當前頁不該是 link,是普通文字。
-
分隔符用 / 太硬:用 ChevronRight 圖示更現代,或用 / 但顏色更淡。
相關模式
- PC03
- PC04