Hướng Dẫn Setup shadcn/ui trong React App: Từ Cài Đặt Đến Sử Dụng Components
Tổng Quan về shadcn/ui
shadcn/ui là một collection of copy-and-paste components được xây dựng trên Radix UI và Tailwind CSS. Khác với các UI libraries truyền thống, shadcn/ui không phải là một npm package mà là một hệ thống components có thể copy và customize theo nhu cầu project.
Lợi Ích Chính
- Full Control: Components được copy vào project, bạn sở hữu hoàn toàn code
- Customizable: Dễ dàng modify theo design system riêng
- Modern Stack: Built with Radix UI, Tailwind CSS, và TypeScript
- Accessibility: Components tuân thủ WAI-ARIA guidelines
- Developer Experience: Excellent TypeScript support và IntelliSense
Yêu Cầu Hệ Thống
- React: 18+ với TypeScript support
- Node.js: 20.19+ hoặc mới hơn
- Package Manager: pnpm, npm, yarn, hoặc bun
- Build Tool: Vite (khuyến nghị), Next.js, hoặc React Router
Cài Đặt shadcn/ui với Vite
Bước 2: Cài Đặt và Cấu Hình Tailwind CSS
shadcn/ui sử dụng Tailwind CSS v4 với Vite plugin mới:
Bước 3: Cấu Hình TypeScript Paths
Cần cấu hình path aliases cho TypeScript để import components dễ dàng.
Cập nhật tsconfig.json:
{
"files": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.node.json"
}
],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}
Cập nhật tsconfig.app.json:
{
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": [
"src"
]
}
Bước 4: Cấu Hình Vite Plugin
Cài đặt Node.js types và cập nhật Vite config:
pnpm add -D @types/node
Cập nhật vite.config.ts:
import path from "path"
import tailwindcss from "@tailwindcss/vite"
import react from "@vitejs/plugin-react"
import { defineConfig } from "vite"
// https://vite.dev/config/
export default defineConfig({
plugins: [
react(),
tailwindcss()
],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
},
},
})
Bước 5: Khởi Tạo shadcn/ui
Chạy CLI command để setup shadcn/ui:
pnpm dlx shadcn@latest init
Bạn sẽ được hỏi một số câu hỏi configuration:
✔ Which color would you like to use as base color? › Neutral
✔ Where is your global CSS file? › src/index.css
✔ Do you want to use CSS variables for colors? › yes
✔ Are you using a custom tailwind prefix? › (leave empty)
✔ Where is your tailwind.config.js located? › tailwind.config.ts
✔ Configure the import alias for components? › @/components
✔ Configure the import alias for utils? ›. @/lib/utils
✔ Are you using React Server Components? › no
Bước 6: Verification Setup
Sau khi chạy shadcn init, một số files sẽ được tạo:
components.json- Configuration filesrc/lib/utils.ts- Utility functionstailwind.config.ts- Tailwind configuration
Kiểm tra components.json:
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "new-york",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "tailwind.config.ts",
"css": "src/index.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui"
}
}
Kiểm tra src/lib/utils.ts:
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Yêu cầu đăng nhập
Vui lòng đăng nhập để truy cập nội dung này
Additional Resources
Course Guide
Comprehensive PDF guide with examples
GitHub Repository
Example code for all lessons
Discussion
Have a question about this lesson? Post it here and get answers from instructors and peers.
