tailwind-v4-shadcn by secondsky/claude-skills
npx skills add https://github.com/secondsky/claude-skills --skill tailwind-v4-shadcn生产环境验证 : WordPress Auditor (https://wordpress-auditor.webfonts.workers.dev) 最后更新 : 2025-12-04 状态 : 生产就绪 ✅
对 AI 助手至关重要 : 如果你是 Claude Code 正在帮助用户设置 Tailwind v4:
reference/common-gotchas.md 中列出的已知问题用户需要执行的操作 : 告诉 Claude 首先检查此技能!
请说:"我正在设置 Tailwind v4 + shadcn/ui - 请先检查 tailwind-v4-shadcn 技能"
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
未激活技能时:
激活技能后:
当技能激活时,所有这些都会自动处理。
bun add tailwindcss @tailwindcss/vite
# 或: npm install tailwindcss @tailwindcss/vite
bun add -d @types/node
# 注意:由于已知的 Bun 兼容性问题,使用 pnpm 进行 shadcn init
# (bunx 存在 "Script not found" 和 postinstall/msw 问题)
pnpm dlx shadcn@latest init
// vite.config.ts
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": {
"config": "", // ← 关键:v4 版本留空
"css": "src/index.css",
"cssVariables": true
}
}
rm tailwind.config.ts # v4 不使用此文件
此模式是强制性的 - 跳过步骤会破坏你的主题。
/* src/index.css */
@import "tailwindcss";
:root {
--background: hsl(0 0% 100%); /* ← 必须使用 hsl() 包装 */
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(221.2 83.2% 53.3%);
/* ... 所有浅色模式颜色 */
}
.dark {
--background: hsl(222.2 84% 4.9%);
--foreground: hsl(210 40% 98%);
--primary: hsl(217.2 91.2% 59.8%);
/* ... 所有深色模式颜色 */
}
关键规则:
@layer base 内部)hsl() 包装.dark 表示深色模式(不要使用 .dark { @theme { } })@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--color-primary: var(--primary);
/* ... 映射所有 CSS 变量 */
}
为何需要此步骤:
bg-background, text-primary)bg-primary 等将不存在@layer base {
body {
background-color: var(--background); /* 此处不要使用 hsl() */
color: var(--foreground);
}
}
关键规则:
var(--background)hsl(var(--background))<div className="bg-background text-foreground">
{/* 不需要 dark: 变体 - 主题会自动切换 */}
</div>
完整实现请查看 reference/dark-mode.md 或使用模板:
// 从以下位置复制:templates/theme-provider.tsx
// src/main.tsx
import { ThemeProvider } from '@/components/theme-provider'
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<App />
</ThemeProvider>
</React.StrictMode>,
)
pnpm dlx shadcn@latest add dropdown-menu
ModeToggle 组件代码请查看 reference/dark-mode.md。
在 :root 和 .dark 中用 hsl() 包装颜色值
--background: hsl(0 0% 100%); /* ✅ 正确 */
使用 @theme inline 映射所有 CSS 变量
@theme inline {
--color-background: var(--background);
}
在 components.json 中设置 "tailwind.config": ""
{ "tailwind": { "config": "" } }
如果存在则删除 tailwind.config.ts
使用 @tailwindcss/vite 插件(而不是 PostCSS)
使用 cn() 处理条件类
import { cn } from "@/lib/utils"
<div className={cn("base", isActive && "active")} />
将 :root 或 .dark 放在 @layer base 内部
/* 错误 */
@layer base {
:root { --background: hsl(...); }
}
使用 .dark { @theme { } } 模式
/* 错误 - v4 不支持嵌套的 @theme */
.dark {
@theme {
--color-primary: hsl(...);
}
}
双重包装颜色
/* 错误 */
body {
background-color: hsl(var(--background));
}
使用 tailwind.config.ts 定义主题颜色
/* 错误 - v4 会忽略此配置 */
export default {
theme: {
extend: {
colors: { primary: 'hsl(var(--primary))' }
}
}
}
使用 @apply 指令(v4 中已弃用)
为语义化颜色使用 dark: 变体
/* 错误 */
<div className="bg-primary dark:bg-primary-dark" />
/* 正确 */
<div className="bg-primary" />
始终使用语义化名称表示颜色:
:root {
--destructive: hsl(0 84.2% 60.2%); /* 红色 - 错误、关键 */
--success: hsl(142.1 76.2% 36.3%); /* 绿色 - 成功状态 */
--warning: hsl(38 92% 50%); /* 黄色 - 警告 */
--info: hsl(221.2 83.2% 53.3%); /* 蓝色 - 信息、主要 */
}
用法:
<div className="bg-destructive text-destructive-foreground">关键</div>
<div className="bg-success text-success-foreground">成功</div>
<div className="bg-warning text-warning-foreground">警告</div>
<div className="bg-info text-info-foreground">信息</div>
| 症状 | 原因 | 修复方法 |
|---|---|---|
bg-primary 不起作用 | 缺少 @theme inline 映射 | 添加 @theme inline 块 |
| 颜色全黑/全白 | 双重 hsl() 包装 | 使用 var(--color) 而不是 hsl(var(--color)) |
| 深色模式不切换 | 缺少 ThemeProvider | 用 <ThemeProvider> 包装应用 |
| 构建失败 | tailwind.config.ts 存在 | 删除该文件 |
| 文本不可见 | 错误的对比度颜色 | 检查 :root/.dark 中的颜色定义 |
完整故障排除指南请查看 reference/common-gotchas.md。
所有模板都可在 templates/ 目录中找到:
cn() 工具函数将这些文件复制到你的项目并根据需要进行自定义。
@tailwindcss/vite 已安装(不是 postcss)vite.config.ts 使用 tailwindcss() 插件tsconfig.json 已配置路径别名components.json 存在且包含 "config": ""tailwind.config.ts 文件src/index.css 遵循 v4 模式:
:root 和 .dark 在根级别(不在 @layer 内部)hsl() 包装@theme inline 映射所有变量@layer base 使用未包装的变量加载 references/advanced-usage.md 以获取高级模式,包括:
references/migration-guide.md快速示例:
:root { --brand: hsl(280 65% 60%); }
@theme inline { --color-brand: var(--brand); }
用法:<div className="bg-brand">品牌化</div>
有关详细模式和组件组合示例,请加载 references/advanced-usage.md。
{
"dependencies": {
"tailwindcss": "^4.1.17",
"@tailwindcss/vite": "^4.1.17",
"clsx": "^2.1.1",
"tailwind-merge": "^3.3.1",
"@radix-ui/react-*": "latest",
"lucide-react": "^0.554.0",
"react": "^19.2.0",
"react-dom": "^19.2.0"
},
"devDependencies": {
"@types/node": "^24.10.1",
"@vitejs/plugin-react": "^5.1.1",
"vite": "^7.2.4",
"typescript": "~5.9.3"
}
}
# 这些包会导致构建错误:
bun add tailwindcss-animate # ❌ 已弃用
# 或: npm install tailwindcss-animate # ❌ 已弃用
bun add tw-animate-css # ❌ 不存在
如果你看到这些包的导入错误,请移除它们并使用原生 CSS 动画或 @tailwindcss/motion 替代。
Tailwind v4 支持使用 CSS 中的 @plugin 指令的官方插件。
快速示例:
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";
常见错误: ❌ 错误:@import "@tailwindcss/typography" (不起作用) ✅ 正确:@plugin "@tailwindcss/typography" (使用 @plugin 指令)
内置功能: 容器查询现在是核心功能(不需要 @tailwindcss/container-queries 插件)。
完整文档请加载 references/plugins-reference.md,包括 Typography 插件 (prose 类)、Forms 插件、安装步骤和常见插件错误。
如需更深入理解,请查看:
根据用户的具体需求加载参考文件:
references/common-gotchas.md:references/dark-mode.md:references/migration-guide.md:references/plugins-reference.md:references/advanced-usage.md:此技能基于 WordPress Auditor 项目:
此技能中的所有模式已在生产环境中验证。
有问题?遇到问题?
reference/common-gotchas.mdcomponents.json 包含 "config": ""tailwind.config.ts每周安装次数
447
仓库
GitHub 星标
90
首次出现
Jan 22, 2026
安全审计
安装于
opencode401
codex383
gemini-cli382
cursor372
claude-code358
github-copilot356
Production-tested : WordPress Auditor (https://wordpress-auditor.webfonts.workers.dev) Last Updated : 2025-12-04 Status : Production Ready ✅
CRITICAL FOR AI AGENTS : If you're Claude Code helping a user set up Tailwind v4:
reference/common-gotchas.mdUSER ACTION REQUIRED : Tell Claude to check this skill first!
Say: "I'm setting up Tailwind v4 + shadcn/ui - check the tailwind-v4-shadcn skill first"
Without skill activation:
With skill activation:
All of these are handled automatically when the skill is active.
bun add tailwindcss @tailwindcss/vite
# or: npm install tailwindcss @tailwindcss/vite
bun add -d @types/node
# Note: Using pnpm for shadcn init due to known Bun compatibility issues
# (bunx has "Script not found" and postinstall/msw problems)
pnpm dlx shadcn@latest init
// vite.config.ts
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": {
"config": "", // ← CRITICAL: Empty for v4
"css": "src/index.css",
"cssVariables": true
}
}
rm tailwind.config.ts # v4 doesn't use this file
This pattern is mandatory - skipping steps will break your theme.
/* src/index.css */
@import "tailwindcss";
:root {
--background: hsl(0 0% 100%); /* ← hsl() wrapper required */
--foreground: hsl(222.2 84% 4.9%);
--primary: hsl(221.2 83.2% 53.3%);
/* ... all light mode colors */
}
.dark {
--background: hsl(222.2 84% 4.9%);
--foreground: hsl(210 40% 98%);
--primary: hsl(217.2 91.2% 59.8%);
/* ... all dark mode colors */
}
Critical Rules:
@layer base)hsl() wrapper on all color values.dark for dark mode (NOT .dark { @theme { } })@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--color-primary: var(--primary);
/* ... map ALL CSS variables */
}
Why This Is Required:
bg-background, text-primary)bg-primary etc. won't exist@layer base {
body {
background-color: var(--background); /* NO hsl() here */
color: var(--foreground);
}
}
Critical Rules:
var(--background)hsl(var(--background))<div className="bg-background text-foreground">
{/* No dark: variants needed - theme switches automatically */}
</div>
See reference/dark-mode.md for full implementation or use template:
// Copy from: templates/theme-provider.tsx
// src/main.tsx
import { ThemeProvider } from '@/components/theme-provider'
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<App />
</ThemeProvider>
</React.StrictMode>,
)
pnpm dlx shadcn@latest add dropdown-menu
See reference/dark-mode.md for ModeToggle component code.
Wrap color values withhsl() in :root and .dark
--background: hsl(0 0% 100%); /* ✅ Correct */
Use@theme inline to map all CSS variables
@theme inline {
--color-background: var(--background);
}
Set"tailwind.config": "" in components.json
{ "tailwind": { "config": "" } }
Deletetailwind.config.ts if it exists
Put:root or .dark inside @layer base
/* WRONG */
@layer base {
:root { --background: hsl(...); }
}
Use.dark { @theme { } } pattern
/* WRONG - v4 doesn't support nested @theme */
.dark {
@theme {
--color-primary: hsl(...);
}
}
Double-wrap colors
/* WRONG */
body {
background-color: hsl(var(--background));
}
Usetailwind.config.ts for theme colors
Always use semantic names for colors:
:root {
--destructive: hsl(0 84.2% 60.2%); /* Red - errors, critical */
--success: hsl(142.1 76.2% 36.3%); /* Green - success states */
--warning: hsl(38 92% 50%); /* Yellow - warnings */
--info: hsl(221.2 83.2% 53.3%); /* Blue - info, primary */
}
Usage:
<div className="bg-destructive text-destructive-foreground">Critical</div>
<div className="bg-success text-success-foreground">Success</div>
<div className="bg-warning text-warning-foreground">Warning</div>
<div className="bg-info text-info-foreground">Info</div>
| Symptom | Cause | Fix |
|---|---|---|
bg-primary doesn't work | Missing @theme inline mapping | Add @theme inline block |
| Colors all black/white | Double hsl() wrapping | Use var(--color) not hsl(var(--color)) |
| Dark mode not switching | Missing ThemeProvider | Wrap app in <ThemeProvider> |
See reference/common-gotchas.md for complete troubleshooting guide.
All templates are available in the templates/ directory:
cn() utility for class mergingCopy these files to your project and customize as needed.
@tailwindcss/vite installed (NOT postcss)vite.config.ts uses tailwindcss() plugintsconfig.json has path aliases configuredcomponents.json exists with "config": ""tailwind.config.ts file existssrc/index.css follows v4 pattern:
:root and .dark at root level (not in @layer)Load references/advanced-usage.md for advanced patterns including:
references/migration-guide.md for complete guideQuick Example:
:root { --brand: hsl(280 65% 60%); }
@theme inline { --color-brand: var(--brand); }
Usage: <div className="bg-brand">Branded</div>
For detailed patterns and component composition examples, load references/advanced-usage.md.
{
"dependencies": {
"tailwindcss": "^4.1.17",
"@tailwindcss/vite": "^4.1.17",
"clsx": "^2.1.1",
"tailwind-merge": "^3.3.1",
"@radix-ui/react-*": "latest",
"lucide-react": "^0.554.0",
"react": "^19.2.0",
"react-dom": "^19.2.0"
},
"devDependencies": {
"@types/node": "^24.10.1",
"@vitejs/plugin-react": "^5.1.1",
"vite": "^7.2.4",
"typescript": "~5.9.3"
}
}
# These packages will cause build errors:
bun add tailwindcss-animate # ❌ Deprecated
# or: npm install tailwindcss-animate # ❌ Deprecated
bun add tw-animate-css # ❌ Doesn't exist
If you see import errors for these packages , remove them and use native CSS animations or @tailwindcss/motion instead.
Tailwind v4 supports official plugins using the @plugin directive in CSS.
Quick Example:
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@plugin "@tailwindcss/forms";
Common Error: ❌ WRONG: @import "@tailwindcss/typography" (doesn't work) ✅ CORRECT: @plugin "@tailwindcss/typography" (use @plugin directive)
Built-in Features: Container queries are now core (no @tailwindcss/container-queries plugin needed).
Load references/plugins-reference.md for complete documentation including Typography plugin (prose classes), Forms plugin, installation steps, and common plugin errors.
For deeper understanding, see:
Load reference files based on user's specific needs:
references/common-gotchas.md when:references/dark-mode.md when:references/migration-guide.md when:references/plugins-reference.md when:references/advanced-usage.md when:This skill is based on the WordPress Auditor project:
All patterns in this skill have been validated in production.
Questions? Issues?
reference/common-gotchas.md firstcomponents.json has "config": ""tailwind.config.ts if it existsWeekly Installs
447
Repository
GitHub Stars
90
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode401
codex383
gemini-cli382
cursor372
claude-code358
github-copilot356
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
103,800 周安装
Gemini CLI 更新日志自动化流程指南 | 技术文档版本管理最佳实践
430 周安装
tsdown - 基于Rolldown的极速TypeScript/JavaScript库打包工具,支持ESM/CJS/IIFE/UMD
430 周安装
PDF OCR技能:双引擎文字提取,支持影印PDF和图片识别
430 周安装
MUI v7 使用指南:组件样式、主题定制与响应式设计模式详解
431 周安装
HubSpot CRM 集成指南:使用 Membrane CLI 自动化销售、营销与客户服务
431 周安装
index-knowledge:自动生成层级化AGENTS.md文档工具,Turso数据库出品
431 周安装
Use@tailwindcss/vite plugin (NOT PostCSS)
Usecn() for conditional classes
import { cn } from "@/lib/utils"
<div className={cn("base", isActive && "active")} />
/* WRONG - v4 ignores this */
export default {
theme: {
extend: {
colors: { primary: 'hsl(var(--primary))' }
}
}
}
Use@apply directive (deprecated in v4)
Usedark: variants for semantic colors
/* WRONG */
<div className="bg-primary dark:bg-primary-dark" />
/* CORRECT */
<div className="bg-primary" />
| Build fails | tailwind.config.ts exists | Delete the file |
| Text invisible | Wrong contrast colors | Check color definitions in :root/.dark |
hsl()@theme inline maps all variables@layer base uses unwrapped variables