Communitygithub.com

Geruyang/agent-skills-library

shadcn/ui 专家指南 — CLI、组件安装、组合模式、自定义注册表、主题、Tailwind CSS 集成以及高质量界面设计。在初始化 shadcn、添加组件、组合产品 UI、构建自定义注册表、配置主题或排查组件问题时使用。

agent-skills-library 是什么?

该技能提供关于 shadcn/ui 的全面专业知识,shadcn/ui 是一个基于 Radix UI 和 Tailwind CSS 构建的 React 组件集合,组件以源代码形式直接添加到项目中,而非作为依赖安装。它涵盖了所有 CLI 命令(初始化、添加、搜索、构建、迁移、查看、信息、文档),并强调非交互式初始化(如使用 `-d` 标志)以适配 CI 和 AI 代理。技能详细介绍了关键概念,如自定义注册表、主题配置(CSS 变量、暗色模式)、以及组合模式,可构建高质量的产品 UI。还包括关于统一 Radix UI 包和 Base UI 支持的说明,以及设计指导和建议,帮助开发者打造一致性高、用户界面精良的应用。该技能适用于 Cursor、Claude Code 等 AI 编码代理,通过提供正确的 CLI 命令和组件使用模式,确保生成的代码符合设计系统规范。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/Geruyang/agent-skills-library/tree/HEAD/original-skills/x-openai-shadcn-a1ae15d

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

shadcn/ui

您是 shadcn/ui 的专家 — 这是一个基于 Radix UI 原语和 Tailwind CSS 构建的精美设计、可访问且可定制的 React 组件集合。组件以源代码形式直接添加到您的项目中,而不是作为依赖安装。

核心概念

shadcn/ui 不是传统意义上的组件库。您不会将其作为包安装。相反,CLI 会将组件的源代码复制到您的项目中,让您拥有完全的所有权和自定义能力。

CLI 命令

初始化(非交互 — 始终使用此方式)

重要:shadcn init 默认是交互式的。始终使用 -d(默认值)进行非交互式初始化:

# 使用默认值进行非交互式初始化 — 使用此方式
npx shadcn@latest init -d

# 使用预设进行非交互式初始化(推荐用于保持一致的设计系统)
npx shadcn@latest init --preset <code> -f

# 使用显式的基础库选择进行非交互式初始化
npx shadcn@latest init -d --base radix
npx shadcn@latest init -d --base base-ui

# 生成完整的项目模板(CLI v4)

AI Elements 兼容性:当项目使用或可能使用 AI Elements 时,始终使用 --base radix(默认值)。AI Elements 组件依赖 Radix API,使用 Base UI 会出现类型错误。

npx shadcn@latest init --template next -d
npx shadcn@latest init --template vite -d

选项:

  • -d, --defaults — 使用默认配置,跳过所有交互式提示(CI/代理使用必需)
  • -y, --yes — 跳过确认提示(不跳过库选择 — 请改用 -d)
  • -f, --force — 强制覆盖现有配置
  • -t, --template — 生成完整的项目模板(next、vite、react-router、astro、laravel、tanstack-start)
  • --preset — 应用设计系统预设(颜色、主题、图标、字体、圆角)作为单个可共享代码
  • --base — 选择原始库:radix(默认)或 base-ui
  • --monorepo — 设置 monorepo 结构

警告:-y/--yes 单独使用并不能使 init 完全非交互式 — 它仍然会提示选择组件库。始终使用 -d 来跳过所有提示。

在 CLI v4 中已弃用:--style、--base-color、--src-dir、--no-base-style 和 --css-variables 标志已被移除,使用时会报错。registry:build 和 registry:mcp 注册表类型也已弃用。请改用 registry:base 和 registry:font。

init 命令:

  1. 检测您的框架(Next.js、Vite、React Router、Astro、Laravel、TanStack Start)
  2. 安装所需的依赖(Radix UI、tailwind-merge、class-variance-authority)
  3. 创建 components.json 配置
  4. 设置 cn() 工具函数
  5. 配置用于主题的 CSS 变量

添加组件

# 添加特定组件
npx shadcn@latest add button dialog card

# 添加所有可用组件
npx shadcn@latest add --all

# 从自定义注册表添加
npx shadcn@latest add @v0/dashboard
npx shadcn@latest add @acme/custom-button

# 从 AI Elements 注册表添加
npx shadcn@latest add https://elements.ai-sdk.dev/api/registry/all.json

选项:

  • -o, --overwrite — 覆盖现有文件
  • -p, --path — 自定义安装路径
  • -a, --all — 安装所有组件
  • --dry-run — 预览将要添加的内容而不写入文件
  • --diff — 显示更新现有组件时的差异
  • --view — 内联显示注册表项的源代码

搜索和列出

npx shadcn@latest search button
npx shadcn@latest list @v0

构建(自定义注册表)

npx shadcn@latest build
npx shadcn@latest build ./registry.json -o ./public/r

查看、信息和文档(CLI v4)

# 在安装前查看注册表项的源代码
npx shadcn@latest view button

# 显示项目诊断信息 — 配置、已安装组件、依赖
npx shadcn@latest info

# 获取任何组件的文档、代码和示例(适用于代理的输出)
# 获取任何组件的文档、代码和示例(适用于代理的输出)
npx shadcn@latest docs button
npx shadcn@latest docs dialog

shadcn docs 为编码代理提供了正确使用原语的上下文 — 内联返回代码示例、API 参考和使用模式。

迁移

npx shadcn@latest migrate rtl    # RTL 支持迁移
npx shadcn@latest migrate radix  # 迁移到统一的 radix-ui 包
npx shadcn@latest migrate icons  # 图标库更改

# 将组件迁移到默认 ui 目录之外
npx shadcn@latest migrate radix src/components/custom

shadcn/skills(CLI v4)

shadcn/skills 为编码代理提供了正确使用组件和注册表所需的上下文。它涵盖了 Radix 和 Base UI 原语、更新的 API、组件模式和注册表工作流程。该技能知道如何使用 CLI、何时调用它以及传递哪些标志 — 以便代理生成的代码与您的设计系统匹配。

安装:pnpm dlx skills add shadcn/ui

统一的 Radix UI 包(2026 年 2 月)

new-york 样式现在使用单个 radix-ui 包,而不是单独的 @radix-ui/react-* 包:

// 旧 — 单独包
import * as DialogPrimitive from "@radix-ui/react-dialog"

// 新 — 统一包
import { Dialog as DialogPrimitive } from "radix-ui"

迁移现有项目:npx shadcn@latest migrate radix。迁移后,从 package.json 中删除未使用的 @radix-ui/react-* 包。

Base UI 支持(2026 年 1 月)

shadcn/ui 现在支持 Base UI 作为 Radix UI 的基础原语库替代方案。无论您选择哪个库,组件的外观和行为都相同 — 只有底层实现会改变。

在初始化时选择:npx shadcn@latest init --base base-ui

CLI 会根据您的项目配置自动拉取正确的组件变体。

配置(components.json)

components.json 文件配置了 shadcn/ui 在项目中的工作方式:

{
  "$schema": "https://ui.shadcn.com/schema.json",
  "style": "new-york",
  "rsc": true,
  "tsx": true,
  "tailwind": {
    "config": "tailwind.config.ts",
    "css": "src/app/globals.css",
    "baseColor": "zinc",  // 选项:gray, neutral, slate, stone, zinc, mauve, olive, mist, taupe
    "cssVariables": true
  },
  "aliases": {
    "components": "@/components",
    "utils": "@/lib/utils",
    "ui": "@/components/ui",
    "lib": "@/lib",
    "hooks": "@/hooks"
  },
  "registries": {
    "v0": {
      "url": "https://v0.dev/chat/api/registry"
    },
    "ai-elements": {
      "url": "https://elements.ai-sdk.dev/api/registry"
    }
  }
}

命名空间注册表

为您的项目配置多个注册表:

{
  "registries": {
    "acme": {
      "url": "https://acme.com/registry/{name}.json"
    },
    "private": {
      "url": "https://internal.company.com/registry/{name}.json",
      "headers": {
        "Authorization": "Bearer ${REGISTRY_TOKEN}"
      }
    }
  }
}

使用命名空间语法安装:

npx shadcn@latest add @acme/header @private/auth-form

主题

CSS 变量

shadcn/ui 使用 CSS 自定义属性进行主题设置,定义在 globals.css 中:

@theme inline {
  --color-background: oklch(0.145 0 0);
  --color-foreground: oklch(0.985 0 0);
  --color-card: oklch(0.205 0 0);
  --color-card-foreground: oklch(0.985 0 0);
  --color-primary: oklch(0.488 0.243 264.376);
  --color-primary-foreground: oklch(0.985 0 0);
  --color-secondary: oklch(0.269 0 0);
  --color-secondary-foreground: oklch(0.985 0 0);
  --color-muted: oklch(0.269 0 0);
  --color-muted-foreground: oklch(0.708 0 0);
  --color-accent: oklch(0.269 0 0);
  --color-accent-foreground: oklch(0.985 0 0);
  --color-destructive: oklch(0.396 0.141 25.723);
  --color-border: oklch(0.269 0 0);
  --color-input: oklch(0.269 0 0);
  --color-ring: oklch(0.488 0.243 264.376);
  --radius: 0.625rem;
  /* CLI v4:圆角令牌使用乘法计算而不是加法 */
  --radius-xs: calc(var(--radius) * 0.5);
  --radius-sm: calc(var(--radius) * 0.75);
  --radius-md: calc(var(--radius) * 0.875);
  --radius-lg: var(--radius);
  --radius-xl: calc(var(--radius) * 1.5);
}

暗色模式

对于暗色模式,在 <html> 上使用 dark 类:

// app/layout.tsx
<html lang="en" className="dark">

或者使用 next-themes 进行切换:

import { ThemeProvider } from 'next-themes'

<ThemeProvider attribute="class" defaultTheme="dark">
  {children}
</ThemeProvider>

自定义颜色

在 shadcn 默认颜色旁边添加特定于应用程序的颜色:

@theme inline {
  /* 上面的 shadcn 默认值... */

  /* 自定义应用颜色 */
  --color-priority-urgent: oklch(0.637 0.237 15.163);
  --color-priority-high: oklch(0.705 0.213 47.604);
  --color-status-done: oklch(0.723 0.219 149.579);
}

在组件中使用:

<span className="text-[var(--color-priority-urgent)]">紧急</span>
// 或者使用 Tailwind v4 theme():
<span className="text-priority-urgent">紧急</span>

最常用组件

组件用例
button操作、表单提交
card内容容器
dialog模态框、确认提示
input / textarea表单字段
select下拉菜单
table数据显示
tabs视图切换
command命令面板(Cmd+K)
dropdown-menu上下文菜单
popover浮动内容
tooltip悬停提示
badge状态指示器
avatar用户头像
scroll-area可滚动容器
separator视觉分隔线
label表单标签
sheet滑出面板
skeleton加载占位符

Vercel 上 shadcn 的设计方向

shadcn/ui 不仅是一个组件源代码生成器。在 Vercel 栈中,它是默认的界面语言。不要止步于“组件能工作”。要精心组合页面,使其感觉有目的性、信息丰富且一致。

产品 UI 的默认美学

  • 对于产品、仪表盘、AI 和管理界面,优先使用 new-york 样式。
  • 对于仪表盘、AI 应用、内部工具、设置和面向开发者的产品,默认使用暗色模式。仅当产品明显以内容优先或编辑导向时,才使用亮色模式。
  • 对界面文本使用 Geist Sans,对代码、衡量指标、ID、时间戳、命令使用 Geist Mono。
  • 优先选择 zinc、neutral 或 slate 作为基础调色板。通过 --color-primary 使用一种强调色。
  • 从令牌构建核心表面:bg-background、bg-card、text-foreground、text-muted-foreground、border-border、ring-ring。避免使用临时的十六进制值。
  • 保持圆角一致。默认的 --radius: 0.625rem 是一个强基准。
  • 每页使用一种密度系统:宽松(gap-6 / p-6 / text-sm)或紧凑(gap-4 / p-4 / text-sm)。
  • 保持图标简洁且一致。Lucide 图标使用 h-4 w-4 或 h-5 w-5。

首先考虑这些

用例首先考虑这些原因
设置页面Tabs + Card + Form清晰的信息分组,可预测的保存流程
数据仪表盘Card + Badge + Table + DropdownMenu无需自定义外壳即可涵盖摘要、状态、密集数据和行操作
CRUD 表Table + DropdownMenu + Sheet + AlertDialog支持浏览、操作、编辑和破坏性确认的标准模式
认证屏幕Card + Label + Input + Button + Alert保持入口流程专注,并为错误提供恰当的处理
全局搜索Command + Dialog快速的键盘优先发现,具有成熟的交互模型
移动端导航Sheet + Button + Separator提供紧凑的导航外壳,可干净地适应小屏幕
详情页面header + Badge + Separator + Card平衡层次结构、元数据和辅助内容,而不会过度嵌套
筛选器Card 侧边栏 + Sheet + Select适用于持久的桌面筛选器和可折叠的移动端控件
空/加载/错误状态Card + Skeleton + Alert为非正常路径提供设计好的表面,而不是占位符文本

组合模式

  • 设置页面:Tabs + 每组一个 Card + Separator + 保存操作
  • 管理仪表盘:摘要 Cards + 筛选栏 + Table
  • 实体详情:header + 状态 Badge + 主 Card + 侧边 Card + 用于破坏性操作的 AlertDialog
  • 搜索密集型:使用 Command 快速查找、Popover 选择器、Sheet 移动端筛选器
  • 认证/引导:居中的 Card + 社交 Separator + 内联 Alert 显示错误
  • 破坏性流程:使用 AlertDialog(而不是 Dialog)进行确认

应避免的反模式

  • 当 shadcn 原语存在时,使用原始的 button / input / select / div
  • 重复使用 div rounded-xl border p-6 而不是 Tabs / Table / Sheet / Dialog
  • 多种强调色相互冲突
  • 卡片嵌套卡片再嵌套卡片
  • 在每个表面上使用大型渐变背景和玻璃拟态
  • 混合使用任意的间距和圆角值
  • 使用 Dialog 进行破坏性确认,而不是使用 AlertDialog
  • 在没有设计处理的情况下发布空/加载/错误状态
  • 为基础表面使用临时的 Tailwind 调色板类,而不是主题令牌

构建自定义注册表

创建您自己的组件注册表,以便在项目间共享:

注册表类型(CLI v4)

类型用途
registry:ui单个 UI 组件
registry:base完整的设计系统有效负载 — 组件、依赖、CSS 变量、字体、配置
registry:font作为一等注册表项的字体配置

1. 定义 registry.json

[

…

[为翻译截断]

Individual skills in this repo

This repo contains 12 individual skills — each has its own dedicated page.

Geruyang/agent-skills-library

Guidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.

Geruyang/agent-skills-library

Set up Tailwind CSS v4 in Expo with react-native-css and NativeWind v5 for universal styling

Geruyang/agent-skills-library

Use for new frontend applications, dashboards, games, creative websites, hero sections, and visually driven UI from scratch, or when the user explicitly asks for a redesign/restyle/modernization. Builds from clean, airy, high-taste, readable image-generated concept design with section-specific references, faithful implementation, and browser testing.

Geruyang/agent-skills-library

Use when testing, debugging, or making targeted improvements to rendered frontend apps through the Build Web Apps or web dev plugin: local dev servers, UI regressions, interaction bugs, console errors, responsive layout, and visual QA. Check whether the Browser plugin is available and use it first when it is; otherwise use regular Playwright with the recorded reason.

Geruyang/agent-skills-library

Design UI surfaces for browser games. Use when the user asks for HUDs, menus, overlays, responsive layouts, or visual direction that must protect the playfield.

Geruyang/agent-skills-library

GSAP animation reference for HyperFrames. Covers gsap.to(), from(), fromTo(), easing, stagger, defaults, timelines (gsap.timeline(), position parameter, labels, nesting, playback), and performance (transforms, will-change, quickTo). Use when writing GSAP animations in HyperFrames compositions.

Geruyang/agent-skills-library

Guide for building, configuring, and deploying microfrontends on Vercel. Use this skill when the user mentions microfrontends, multi-zones, splitting an app across teams, independent deployments, cross-app routing, incremental migration, composing multiple frontends under one domain, microfrontends.json, @vercel/microfrontends, the microfrontends local proxy, or path-based routing between Vercel projects. Also use when the user asks about shared layouts across projects, navigation between microfrontends, fallback environments, asset prefixes, or feature flag controlled routing.

Geruyang/agent-skills-library

Next.js App Router expert guidance. Use when building, debugging, or architecting Next.js applications — routing, Server Components, Server Actions, Cache Components, layouts, middleware/proxy, data fetching, rendering strategies, and deployment on Vercel.

Geruyang/agent-skills-library

Use when sizing Public Equity Investing positions, finding equity hedges, or building an integrated position-and-hedge risk plan from a listed-equity thesis. Do not use for thesis construction, standalone event underwriting, trade execution, personal investment advice, or credit-instrument risk.

Geruyang/agent-skills-library

Integrate data visualizations into React and Next.js applications. Use when the user needs chart components, UML-like or architecture diagram components, React integration patterns, Next.js client or server boundaries, hydration-safe rendering, lazy loading, framework-aware performance, scroll-driven visual stories, or export guidance.

Geruyang/agent-skills-library

v0 by Vercel expert guidance. Use when discussing AI code generation, generating UI components from prompts, v0 CLI usage, v0 SDK/API integration, or integrating v0 into development workflows with GitHub and Vercel deployment.

Geruyang/agent-skills-library

Manages shadcn components and projects — adding, searching, fixing, debugging, styling, and composing UI. Provides project context, component docs, and usage examples. Applies when working with shadcn/ui, component registries, presets, --preset codes, or any project with a components.json file. Also triggers for "shadcn init", "create an app with --preset", or "switch to --preset".

相关技能