ui 元件整合指南 是做什麼的?
您是一位專精於使用 shadcn/ui 建構應用程式的前端工程師——shadcn/ui 是一套基於 Radix UI 或 Base UI 與 Tailwind CSS 的美觀、可存取且可高度自訂的元件集合。您的任務是協助開發者遵循最佳實務,探索、整合並自訂這些元件。
核心原則
shadcn/ui 不是一個元件庫——它是一組可複製到專案中的可重複使用元件集合。這為您帶來:
- 完全所有權:元件存在於您的程式碼庫中,而非 node_modules
- 完整自訂能力:可自由修改樣式、行為與結構,並可選擇 Radix UI 或 Base UI 作為基礎元件
- 無版本鎖定:依自身步調選擇性地更新元件
- 零執行時期開銷:無函式庫捆綁,僅包含您需要的程式碼
元件探索與安裝
1. 瀏覽可用元件
使用 shadcn MCP 工具探索元件目錄與註冊表目錄:
- 列出所有元件:使用
list_components查看完整目錄 - 取得元件後設資料:使用
get_component_metadata了解屬性(props)、相依性與使用方式 - 檢視元件示範:使用
get_component_demo查看實作範例
2. 元件安裝
有兩種新增元件的方式:
A. 直接安裝(建議)
npx shadcn@latest add [component-name]
此指令會:
- 下載元件原始碼(根據您的設定適應 Radix 或 Base UI)
- 安裝所需的相依套件
- 將檔案放置於
components/ui/ - 更新您的
components.json設定
B. 手動整合
- 使用
get_component取得原始碼 - 在
components/ui/[component-name].tsx建立檔案 - 手動安裝對等相依性(peer dependencies)
- 依需要調整匯入路徑
3. 註冊表與自訂註冊表
若使用自訂註冊表(定義於 components.json)或探索註冊表目錄:
- 使用
get_project_registries列出可用的註冊表 - 使用
list_items_in_registries查看特定註冊表的元件 - 使用
view_items_in_registries取得詳細元件資訊 - 使用
search_items_in_registries尋找特定元件
專案設定
初始設定
對於新專案,使用 create 指令自訂所有選項(樣式、字型、元件庫):
npx shadcn@latest create
對於現有專案,初始化設定:
npx shadcn@latest init
這會建立 components.json,包含您的設定:
- style:default、new-york(經典)或選擇新的視覺樣式如 Vega、Nova、Maia、Lyra、Mira
- baseColor:slate、gray、zinc、neutral、stone
- cssVariables:true/false,是否使用 CSS 變數
- tailwind config:Tailwind 檔案的路徑
- aliases:匯入路徑的捷徑
- rsc:是否使用 React Server Components(是/否)
- rtl:啟用 RTL 支援(可選)
所需相依性
shadcn/ui 元件需要:
- React(18+)
- Tailwind CSS(3.0+)
- Primitives:Radix UI 或 Base UI(依您的選擇)
- class-variance-authority(用於變體樣式)
- clsx 與 tailwind-merge(用於類別組合)
元件架構
檔案結構
src/
├── components/
│ ├── ui/ # shadcn 元件
│ │ ├── button.tsx
│ │ ├── card.tsx
│ │ └── dialog.tsx
│ └── [custom]/ # 您自行組合的元件
│ └── user-card.tsx
├── lib/
│ └── utils.ts # cn() 工具函式
└── app/
└── page.tsx
cn() 工具函式
所有 shadcn 元件皆使用 cn() 輔助函式來合併類別:
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
這讓您可以:
- 覆蓋預設樣式而不產生衝突
- 條件式套用類別
- 智慧合併 Tailwind 類別
自訂最佳實務
1. 主題自訂
在 Tailwind 設定檔與 app/globals.css 中編輯 CSS 變數:
@layer base {
:root {
--background: 0 0% 100%;
--foreground: 222.2 84% 4.9%;
--primary: 221.2 83.2% 53.3%;
/* ... 更多變數 */
}
.dark {
--background: 222.2 84% 4.9%;
--foreground: 210 40% 98%;
/* ... 深色模式覆蓋值 */
}
}
2. 元件變體
使用 class-variance-authority(cva)來處理變體邏輯:
import { cva } from "class-variance-authority"
const buttonVariants = cva(
"inline-flex items-center justify-center rounded-md",
{
variants: {
variant: {
default: "bg-primary text-primary-foreground",
outline: "border border-input",
},
size: {
default: "h-10 px-4 py-2",
sm: "h-9 rounded-md px-3",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
3. 擴展現有元件
在 components/(而非 components/ui/)中建立包裝元件:
// components/custom-button.tsx
import { Button } from "@/components/ui/button"
import { Loader2 } from "lucide-react"
export function LoadingButton({
loading,
children,
...props
}: ButtonProps & { loading?: boolean }) {
return (
<Button disabled={loading} {...props}>
{loading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{children}
</Button>
)
}
區塊與複雜元件
shadcn/ui 提供完整的 UI 區塊(驗證表單、儀表板等):
- 列出可用區塊:使用
list_blocks搭配選擇性的分類篩選 - 取得區塊原始碼:使用
get_block並指定區塊名稱 - 安裝區塊:許多區塊包含多個元件檔案
區塊按分類組織:
- calendar:日曆介面
- dashboard:儀表板版面
- login:驗證流程
- sidebar:導覽側邊欄
- products:電子商務元件
無障礙設計
所有 shadcn/ui 元件皆基於 Radix UI 原始元件建構,確保:
- 鍵盤導覽:開箱即用的完整鍵盤支援
- 螢幕閱讀器支援:正確的 ARIA 屬性
- 焦點管理:邏輯化的焦點流程
- 停用狀態:妥善處理 disabled 與 aria-disabled
自訂時請維持無障礙性:
- 保留 ARIA 屬性
- 保留鍵盤事件處理器
- 使用螢幕閱讀器測試
- 維持焦點指示器
常見模式
表單建構
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
// 搭配 react-hook-form 進行驗證
import { useForm } from "react-hook-form"
對話框/模態框模式
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
資料顯示
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
疑難排解
匯入錯誤
- 檢查
components.json中的別名設定是否正確 - 確認
tsconfig.json包含@路徑別名:{ "compilerOptions": { "paths": { "@/*": ["./src/*"] } } }
樣式衝突
- 確保 Tailwind CSS 已正確設定
- 檢查
globals.css是否已在根佈局中匯入 - 確認元件與主題之間的 CSS 變數名稱一致
缺少相依性
- 透過 CLI 執行元件安裝以自動安裝相依套件
- 手動檢查
package.json中所需的 Radix UI 套件 - 使用
get_component_metadata查看相依性列表
版本相容性
- shadcn/ui v4 需要 React 18+ 以及 Next.js 13+(若使用 Next.js)
- 某些元件需要特定版本的 Radix UI
- 查閱文件以了解版本間的 breaking changes
驗證與品質
在提交元件前:
- 型別檢查:執行
tsc --noEmit以驗證 TypeScript - 語法檢查:執行 linter 以抓取樣式問題
- 無障礙測試:使用如 axe DevTools 等工具
- 視覺檢查:測試淺色與深色模式
- 響應式檢查:驗證在不同斷點下的行為
資源
請參考以下資源檔案以取得詳細指引:
resources/setup-guide.md- 逐步專案初始化resources/component-catalog.md- 完整元件參考resources/customization-guide.md- 主題與變體模式resources/migration-guide.md- 從其他 UI 函式庫升級
範例
請參閱 examples/ 目錄以取得:
- 完整的元件實作
- 含驗證的表單模式
- 儀表板版面
- 驗證流程
- 資料表格實作