shadcn-v3
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSkill: shadcn
Skill: shadcn
Scope
范围
- Applies to: Tailwind v4 with shadcn/ui setup, CSS variable architecture, dark mode, theme configuration, component composition patterns, accessibility, form integration
- Does NOT cover: Tailwind v3, PostCSS configuration
- 适用于:Tailwind v4 与 shadcn/ui 配置、CSS 变量架构、暗模式、主题配置、组件组合模式、可访问性、表单集成
- 不涵盖:Tailwind v3、PostCSS 配置
Assumptions
前提条件
- Tailwind CSS v4+
- shadcn/ui latest
- Next.js 16 (this repo); CSS-first Tailwind v4, not Vite
- React 18+ or Next.js 14+
- TypeScript 6+
- Tailwind CSS v4+
- 最新版 shadcn/ui
- Next.js 16(此仓库);采用 CSS-first 的 Tailwind v4,而非 Vite
- React 18+ 或 Next.js 14+
- TypeScript 6+
Principles
原则
- Use to map CSS variables to Tailwind tokens
@theme inline - Use wrapper for color values in
hsl()and:root.dark - Set in
"tailwind.config": ""(empty for v4)components.json - Delete if it exists (v4 uses CSS-based config)
tailwind.config.ts - Use CSS-first Tailwind v4 in the Next app (not )
@tailwindcss/vite - Use utility for conditional classes
cn() - Semantic colors automatically adapt to dark mode (no variants needed)
dark: - Use directive for plugins (not
@pluginor@import)require() - Compose complex components from smaller shadcn primitives
- Extend components via wrapper pattern (don't modify originals)
- Use CVA (class-variance-authority) for variant systems
- Use only when a parent must attach a ref (React 19 does not require it by default)
forwardRef - Leverage Radix UI primitives for built-in accessibility
- 使用 将 CSS 变量映射到 Tailwind tokens
@theme inline - 在 和
:root中使用.dark包装颜色值hsl() - 在 中设置
components.json(v4 下留空)"tailwind.config": "" - 如果 存在,则将其删除(v4 使用基于 CSS 的配置)
tailwind.config.ts - 在 Next 应用中使用 CSS-first 的 Tailwind v4(而不是 )
@tailwindcss/vite - 使用 工具函数处理条件类名
cn() - 语义颜色会自动适应暗模式(无需使用 变体)
dark: - 使用 指令加载插件(而不是
@plugin或@import)require() - 由较小的 shadcn 基础组件组合出复杂组件
- 通过 wrapper pattern 扩展组件(不要直接修改原始组件)
- 使用 CVA(class-variance-authority)实现变体系统
- 仅当父组件必须附加 ref 时才使用 (React 19 默认不再要求使用)
forwardRef - 利用 Radix UI primitives 提供内置可访问性能力
Constraints
约束
MUST
必须
- Wrap color values with in
hsl()and:root.dark - Use to map all CSS variables
@theme inline - Set in
"tailwind.config": ""components.json - Delete if it exists
tailwind.config.ts - Use CSS-first Tailwind v4 in Next apps (in CSS)
@import "tailwindcss"
- 在 和
:root中使用.dark包装颜色值hsl() - 使用 映射所有 CSS 变量
@theme inline - 在 中设置
components.json"tailwind.config": "" - 如果 存在,则将其删除
tailwind.config.ts - 在 Next 应用中使用 CSS-first 的 Tailwind v4(在 CSS 中使用 )
@import "tailwindcss"
SHOULD
建议
- Use semantic color tokens (,
--background, etc.)--foreground - Use utility for conditional classes
cn() - Use directive for plugins
@plugin - Compose components from smaller shadcn primitives
- Use wrapper pattern to extend components (don't modify originals)
- Use CVA for variant systems in custom components
- Use when a parent must attach a ref
forwardRef - Test accessibility with keyboard navigation and screen readers
- Use Radix UI primitives for complex interactions (dialogs, dropdowns, etc.)
- Provide ARIA labels for icon-only buttons and interactive elements
- Use only when the app is Vite-based (not Next.js)
@tailwindcss/vite
- 使用语义颜色 tokens(、
--background等)--foreground - 使用 工具函数处理条件类名
cn() - 使用 指令加载插件
@plugin - 由较小的 shadcn primitives 组合组件
- 使用 wrapper pattern 扩展组件(不要直接修改原始组件)
- 在自定义组件的变体系统中使用 CVA
- 当父组件必须附加 ref 时使用
forwardRef - 使用键盘导航和屏幕阅读器测试可访问性
- 对复杂交互(对话框、下拉菜单等)使用 Radix UI primitives
- 为仅含图标的按钮和可交互元素提供 ARIA 标签
- 仅在应用基于 Vite 时使用 (Next.js 不适用)
@tailwindcss/vite
AVOID
避免
- Putting or
:rootinside.dark@layer base - Using pattern (v4 doesn't support nested @theme)
.dark { @theme { } } - Double-wrapping colors (in body)
hsl(var(--background)) - Using for theme colors
tailwind.config.ts - Using directive (deprecated in v4)
@apply - Using variants for semantic colors
dark: - Using or
@importfor plugins (userequire())@plugin - Modifying base shadcn components directly (use wrapper pattern)
- Building custom dropdowns/dialogs from scratch (use Radix primitives)
- Relying on color alone for state indication
- Skipping accessibility testing
- 将 或
:root放在.dark内@layer base - 使用 模式(v4 不支持嵌套的
.dark { @theme { } })@theme - 对颜色进行双重包装(例如在 body 中使用 )
hsl(var(--background)) - 使用 配置主题颜色
tailwind.config.ts - 使用 指令(v4 中已弃用)
@apply - 对语义颜色使用 变体
dark: - 使用 或
@import加载插件(应使用require())@plugin - 直接修改 shadcn 基础组件(应使用 wrapper pattern)
- 从零开始构建自定义 dropdown 或 dialog(应使用 Radix primitives)
- 仅依靠颜色表示状态
- 跳过可访问性测试
Interactions
交互
- Works with next for App Router setup
- Uses typescript for type safety
- 与 next 配合使用,用于 App Router 配置
- 使用 typescript 确保类型安全
Patterns
模式
CSS Variable Setup
CSS 变量配置
css
/* src/index.css */
@import "tailwindcss";
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
}
:root {
--background: hsl(0 0% 100%);
--foreground: hsl(222.2 84% 4.9%);
}
.dark {
--background: hsl(222.2 84% 4.9%);
--foreground: hsl(210 40% 98%);
}css
/* src/index.css */
@import "tailwindcss";
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
}
:root {
--background: hsl(0 0% 100%);
--foreground: hsl(222.2 84% 4.9%);
}
.dark {
--background: hsl(222.2 84% 4.9%);
--foreground: hsl(210 40% 98%);
}Vite Configuration (Vite apps only)
Vite 配置(仅限 Vite 应用)
typescript
// vite.config.ts — skip for Next.js CSS-first setups
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [react(), tailwindcss()],
})typescript
// vite.config.ts — skip for Next.js CSS-first setups
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [react(), tailwindcss()],
})Components Config
组件配置
json
{
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "slate",
"cssVariables": true
}
}See Templates (including Component Extension) and Architecture for complete setup.
json
{
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "slate",
"cssVariables": true
}
}查看模板(包括组件扩展)和架构,了解完整配置。
References
参考资料
- Architecture - Complete setup pattern
- Dark Mode - Dark mode implementation
- Common Gotchas - Common issues and fixes
- Migration Guide - Migrating from v3
- Component Patterns - Composition, CVA, extension patterns
- Accessibility - ARIA, keyboard navigation, screen readers
- Form Patterns - React Hook Form integration
- 架构 - 完整配置模式
- 暗模式 - 暗模式实现
- 常见问题 - 常见问题与修复方法
- 迁移指南 - 从 v3 迁移
- 组件模式 - 组合、CVA 和扩展模式
- 可访问性 - ARIA、键盘导航和屏幕阅读器
- 表单模式 - React Hook Form 集成