- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Kbd
- Label
- Menubar
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
- Typography
选择适合你起点的方案。
使用 shadcn/create
构建你的预设
打开 shadcn/create 并以可视化方式构建你的预设。选择样式、颜色、字体、图标等。
打开 shadcn/create
创建项目
点击 Create Project,选择你的包管理器,然后复制生成的命令。
生成的命令看起来大致如下:
pnpm dlx shadcn@latest init --preset [CODE] --template astro
最终命令会包含你选择的选项,例如 --base、--monorepo 或 --rtl。
添加组件
向你的项目添加 Card 组件:
pnpm dlx shadcn@latest add card
如果你创建了 monorepo,请在 apps/web 目录中运行命令,或者从仓库根目录指定 workspace:
pnpm dlx shadcn@latest add card -c apps/web
上面的命令会把 Card 组件添加到你的项目中。之后你可以像这样导入它:
---
import Layout from "@/layouts/main.astro"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
---
<Layout>
<Card className="max-w-sm">
<CardHeader>
<CardTitle>项目概览</CardTitle>
<CardDescription>
跟踪你的 Astro 应用的进展和最近活动。
</CardDescription>
</CardHeader>
<CardContent>
你的设计系统已经就绪。开始构建下一个组件吧。
</CardContent>
</Card>
</Layout>如果你创建了 monorepo,请改为更新 apps/web/src/pages/index.astro,并从 @workspace/ui/components/card 导入。monorepo 布局中的 apps/web/src/layouts/main.astro 已经为你导入了 @workspace/ui/globals.css。
使用 CLI
创建项目
运行 init 命令以为新的 Astro 项目创建脚手架。按照提示配置项目:base、preset、monorepo 等。
pnpm dlx shadcn@latest init -t astro
对于 monorepo 项目,请使用 --monorepo 标志:
pnpm dlx shadcn@latest init -t astro --monorepo
添加组件
向你的项目添加 Card 组件:
pnpm dlx shadcn@latest add card
如果你创建了 monorepo,请在 apps/web 目录中运行命令,或者从仓库根目录指定 workspace:
pnpm dlx shadcn@latest add card -c apps/web
上面的命令会把 Card 组件添加到你的项目中。之后你可以像这样导入它:
---
import Layout from "@/layouts/main.astro"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
---
<Layout>
<Card className="max-w-sm">
<CardHeader>
<CardTitle>项目概览</CardTitle>
<CardDescription>
跟踪你的 Astro 应用的进展和最近活动。
</CardDescription>
</CardHeader>
<CardContent>
你的设计系统已经就绪。开始构建下一个组件吧。
</CardContent>
</Card>
</Layout>如果你创建了 monorepo,请改为更新 apps/web/src/pages/index.astro,并从 @workspace/ui/components/card 导入。monorepo 布局中的 apps/web/src/layouts/main.astro 已经为你导入了 @workspace/ui/globals.css。
现有项目
创建项目
如果你需要一个新的 Astro 项目,请先创建一个。否则,跳过此步骤。
pnpm create astro@latest astro-app -- --template with-tailwindcss --install --add react --git
这条命令会为你配置 Tailwind CSS 和 React 集成。如果你要在旧版或自定义的 Astro 应用中添加 shadcn/ui,请先确保两者都已配置好。
Tailwind starter 会通过 src/layouts/main.astro 加载全局样式表。请保留这个布局,或者确保你的页面导入了 @/styles/global.css。
编辑 tsconfig.json 文件
如果你的项目已经配置了 @/* 别名,可以跳过此步骤。
在 tsconfig.json 文件中添加以下代码以解析路径:
{
"compilerOptions": {
// ...
"baseUrl": ".",
"paths": {
"@/*": [
"./src/*"
]
}
// ...
}
}运行 CLI
运行 shadcn init 命令,为你的项目设置 shadcn/ui:
pnpm dlx shadcn@latest init
添加组件
现在你可以开始向项目中添加组件了。
pnpm dlx shadcn@latest add button
上面的命令会把 Button 组件添加到你的项目中。之后你可以像这样导入它:
---
import Layout from "@/layouts/main.astro"
import { Button } from "@/components/ui/button"
---
<Layout>
<div class="grid h-screen place-items-center content-center">
<Button>Button</Button>
</div>
</Layout>