Components
- 手风琴
- 提示
- 警告对话框
- 宽高比
- 头像
- 徽章
- 面包屑导航
- 按钮
- 按钮组
- 日历 Calendar
- 卡片
- Carousel
- 图表 Chart
- 复选框
- 折叠面板
- 组合框
- 命令
- 上下文菜单
- 数据表格 Data Table
- 日期选择器 Date Picker
- 对话框 Dialog
- 抽屉
- 下拉菜单
- Empty
- 字段
- 悬停卡片
- 输入
- 输入组
- 输入 OTP
- 项目
- Kbd
- 标签
- 菜单栏
- 原生选择框
- 导航菜单 Navigation Menu
- 分页
- 弹出框
- 进度 Progress
- 单选框组
- 可调整大小
- 滚动区域 Scroll Area
- 选择框
- 分隔符 Separator
- 侧边栏 Sheet
- 侧边栏 Sidebar
- 骨架屏
- 滑块
- Sonner
- 加载指示器 Spinner
- 开关
- 表格
- 标签页 Tabs
- 文本域
- 吐司
- 切换按钮 Toggle
- 切换组
- 提示 Tooltip
- 排版
Starting fresh? Use shadcn/create to save time. It handles Tailwind, TypeScript, and path alias configuration for you, plus custom themes and presets.
创建项目
首先使用 vite 创建一个新的 React 项目。选择 React + TypeScript 模板:
pnpm create vite@latest
添加 Tailwind CSS
pnpm add tailwindcss @tailwindcss/vite
将 src/index.css 中的所有内容替换为以下内容:
@import "tailwindcss";编辑 tsconfig.json 文件
当前版本的 Vite 将 TypeScript 配置拆分成三个文件,其中两个需要编辑。
在 tsconfig.json 和 tsconfig.app.json 文件的 compilerOptions 部分添加 baseUrl 和 paths 属性:
{
"files": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.node.json"
}
],
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
}
}编辑 tsconfig.app.json 文件
在 tsconfig.app.json 文件中添加以下代码,用于你的 IDE 解析路径:
{
"compilerOptions": {
// ...
"baseUrl": ".",
"paths": {
"@/*": [
"./src/*"
]
}
// ...
}
}更新 vite.config.ts
向 vite.config.ts 中添加以下代码,使你的应用能正确解析路径:
pnpm add -D @types/node
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"),
},
},
})运行 CLI
运行 shadcn 初始化命令来设置你的项目:
pnpm dlx shadcn@latest init
你将被问到一些问题以配置 components.json。
Which color would you like to use as base color? › Neutral添加组件
现在你可以开始向项目中添加组件了。
pnpm dlx shadcn@latest add button
上述命令会将 Button 组件添加到你的项目中。然后你可以这样导入它:
import { Button } from "@/components/ui/button"
function App() {
return (
<div className="flex min-h-svh flex-col items-center justify-center">
<Button>点击我</Button>
</div>
)
}
export default App