Get Started
组件
- Accordion
- Alert Dialog
- Alert
- Aspect Ratio
- Avatar
- Badge
- Breadcrumb
- Button Group
- Button
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Drawer
- Dropdown Menu
- Empty
- Field
- Form
- Hover Card
- Input Group
- Input OTP
- Input
- 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 Group
- Toggle
- Tooltip
- Typography
注意: 本指南面向 Remix。如需 React Router 版本,请参见 React Router 指南。
创建项目
先使用 create-remix 创建新的 Remix 项目:
pnpm dlx create-remix@latest my-app
运行 CLI
执行 shadcn 的 init 命令完成初始化:
pnpm dlx shadcn@latest init
配置 components.json
根据提示回答以下问题以生成 components.json:
Which color would you like to use as base color? › Neutral项目结构
注意: 以下结构仅供参考,你可以按需调整文件位置。
- 将 UI 组件放在
app/components/ui目录。 - 业务组件可放在
app/components目录。 app/lib存放工具函数。例如utils.ts中定义了cn辅助函数。- 全局样式位于
app/tailwind.css。
安装 Tailwind CSS
pnpm add -D tailwindcss@latest autoprefixer@latest
随后创建 postcss.config.js 文件:
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}最后在 remix.config.js 中加入以下配置:
/** @type {import('@remix-run/dev').AppConfig} */
export default {
...
tailwind: true,
postcss: true,
...
};在应用中引入 tailwind.css
在 app/root.tsx 中导入 tailwind.css 文件:
import styles from "./tailwind.css?url"
export const links: LinksFunction = () => [
{ rel: "stylesheet", href: styles },
...(cssBundleHref ? [{ rel: "stylesheet", href: cssBundleHref }] : []),
]完成
现在可以开始向项目中添加组件。
pnpm dlx shadcn@latest add button
上述命令会把 Button 组件加入项目,你可以像下面这样导入:
import { Button } from "~/components/ui/button"
export default function Home() {
return (
<div>
<Button>Click me</Button>
</div>
)
}