tailwind-theme-builder
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTailwind Theme Builder
Tailwind 主题构建工具
Set up a fully themed Tailwind v4 + shadcn/ui project with dark mode. Produces configured CSS, theme provider, and working component library.
搭建一个带有暗黑模式的、完整主题化的Tailwind v4 + shadcn/ui项目。生成已配置的CSS、主题提供器和可用的组件库。
Workflow
操作流程
Step 1: Install Dependencies
步骤1:安装依赖项
bash
pnpm add tailwindcss @tailwindcss/vite
pnpm add -D @types/node tw-animate-css
pnpm dlx shadcn@latest initbash
pnpm add tailwindcss @tailwindcss/vite
pnpm add -D @types/node tw-animate-css
pnpm dlx shadcn@latest initDelete v3 config if it exists
Delete v3 config if it exists
rm -f tailwind.config.ts
undefinedrm -f tailwind.config.ts
undefinedStep 2: Configure Vite
步骤2:配置Vite
Copy or add the Tailwind plugin:
assets/vite.config.tstypescript
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import path from 'path'
export default defineConfig({
plugins: [react(), tailwindcss()],
resolve: { alias: { '@': path.resolve(__dirname, './src') } }
})复制或添加Tailwind插件:
assets/vite.config.tstypescript
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
import path from 'path'
export default defineConfig({
plugins: [react(), tailwindcss()],
resolve: { alias: { '@': path.resolve(__dirname, './src') } }
})Step 3: Four-Step CSS Architecture (Mandatory)
步骤3:四步式CSS架构(必填)
This exact order is required. Skipping steps breaks the theme.
src/index.css:
css
@import "tailwindcss";
@import "tw-animate-css";
/* 1. Define CSS variables at root (NOT inside @layer base) */
:root {
--background: hsl(0 0% 100%);
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(221.2 83.2% 53.3%);
--primary-foreground: hsl(210 40% 98%);
/* ... all semantic tokens */
}
.dark {
--background: hsl(222.2 84% 4.9%);
--foreground: hsl(210 40% 98%);
--primary: hsl(217.2 91.2% 59.8%);
--primary-foreground: hsl(222.2 47.4% 11.2%);
}
/* 2. Map variables to Tailwind utilities */
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--color-primary: var(--primary);
--color-primary-foreground: var(--primary-foreground);
}
/* 3. Apply base styles (NO hsl() wrapper here) */
@layer base {
body {
background-color: var(--background);
color: var(--foreground);
}
}Result: , etc. work automatically. Dark mode switches via class — no variants needed for semantic colours.
bg-backgroundtext-primary.darkdark:必须严格遵循此顺序,跳过步骤会导致主题失效。
src/index.css:
css
@import "tailwindcss";
@import "tw-animate-css";
/* 1. Define CSS variables at root (NOT inside @layer base) */
:root {
--background: hsl(0 0% 100%);
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(221.2 83.2% 53.3%);
--primary-foreground: hsl(210 40% 98%);
/* ... all semantic tokens */
}
.dark {
--background: hsl(222.2 84% 4.9%);
--foreground: hsl(210 40% 98%);
--primary: hsl(217.2 91.2% 59.8%);
--primary-foreground: hsl(222.2 47.4% 11.2%);
}
/* 2. Map variables to Tailwind utilities */
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--color-primary: var(--primary);
--color-primary-foreground: var(--primary-foreground);
}
/* 3. Apply base styles (NO hsl() wrapper here) */
@layer base {
body {
background-color: var(--background);
color: var(--foreground);
}
}效果: 、 等工具类可自动生效。暗黑模式通过类切换——语义化颜色无需使用变体。
bg-backgroundtext-primary.darkdark:Step 4: Set Up Dark Mode
步骤4:设置暗黑模式
Copy to your components directory, then wrap your app:
assets/theme-provider.tsxtypescript
import { ThemeProvider } from '@/components/theme-provider'
ReactDOM.createRoot(document.getElementById('root')!).render(
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<App />
</ThemeProvider>
)Add a theme toggle:
bash
pnpm dlx shadcn@latest add dropdown-menuSee for the ModeToggle component.
references/dark-mode.md将复制到你的组件目录,然后包裹应用:
assets/theme-provider.tsxtypescript
import { ThemeProvider } from '@/components/theme-provider'
ReactDOM.createRoot(document.getElementById('root')!).render(
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<App />
</ThemeProvider>
)添加主题切换器:
bash
pnpm dlx shadcn@latest add dropdown-menu查看获取ModeToggle组件的实现。
references/dark-mode.mdStep 5: Configure components.json
步骤5:配置components.json
json
{
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "slate",
"cssVariables": true
}
}"config": ""json
{
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "slate",
"cssVariables": true
}
}"config": ""Critical Rules
关键规则
Always:
- Wrap colours with in
hsl()/:root.dark - Use to map all CSS variables
@theme inline - Use plugin (NOT PostCSS)
@tailwindcss/vite - Delete if it exists
tailwind.config.ts
Never:
- Put /
:rootinside.dark@layer base - Use (v4 doesn't support nested @theme)
.dark { @theme { } } - Double-wrap:
hsl(var(--background)) - Use with
@applyclasses (use@layer baseinstead)@utility
必须遵守:
- 在/
:root中使用.dark包裹颜色值hsl() - 使用映射所有CSS变量
@theme inline - 使用插件(而非PostCSS)
@tailwindcss/vite - 如果存在则删除该文件
tailwind.config.ts
绝对禁止:
- 将/
:root放入.dark内部@layer base - 使用(v4不支持嵌套@theme)
.dark { @theme { } } - 双重包裹:
hsl(var(--background)) - 在类中使用
@layer base(改用@apply)@utility
Common Errors
常见错误
| Symptom | Cause | Fix |
|---|---|---|
| Missing | Add |
| Colours all black/white | Double | Use |
| Dark mode not switching | Missing ThemeProvider | Wrap app in |
| Build fails | | Delete the file |
| Animation errors | Using | Install |
| v4 breaking change | Use |
See for detailed error explanations with sources.
references/common-gotchas.md| 症状 | 原因 | 修复方案 |
|---|---|---|
| 缺少 | 添加 |
| 颜色全为黑/白 | 双重 | 使用 |
| 暗黑模式无法切换 | 缺少ThemeProvider | 用 |
| 构建失败 | 存在 | 删除该文件 |
| 动画报错 | 使用了 | 改用 |
自定义类中 | v4的破坏性更新 | 用 |
查看获取带GitHub来源的详细错误说明。
references/common-gotchas.mdAsset Files
资源文件
Copy from directory:
assets/- — Complete CSS with all colour variables
index.css - — shadcn/ui v4 config
components.json - — Vite + Tailwind plugin
vite.config.ts - — Dark mode provider
theme-provider.tsx - —
utils.tsutilitycn()
从目录复制以下文件:
assets/- — 包含所有颜色变量的完整CSS文件
index.css - — shadcn/ui v4配置文件
components.json - — Vite + Tailwind插件配置
vite.config.ts - — 暗黑模式提供器
theme-provider.tsx - —
utils.ts工具函数cn()
Reference Files
参考文件
- — 8 documented errors with GitHub sources
references/common-gotchas.md - — Complete dark mode implementation
references/dark-mode.md - — Deep dive into 4-step pattern
references/architecture.md - — v3 to v4 migration
references/migration-guide.md
- — 8个带GitHub来源的已记录错误
references/common-gotchas.md - — 完整的暗黑模式实现方案
references/dark-mode.md - — 四步模式的深度解析
references/architecture.md - — v3到v4的迁移指南
references/migration-guide.md