返回首页
🎨 前端 / Web
shadcn/ui 实战:复制即用的现代 React 组件库
shadcn/ui 不是传统组件库,而是"代码即组件"的新范式。本文演示从 0 到生产的实战。
shadcn · React · 组件库 · UI · 前端
📰
今日技术简讯
📰 技术简讯 · 2026-05-30
今日聚合 6 条热门技术内容(周六)。
🤖 AI / LLM
1. Claude 4.2 Computer Use 2.0
- 链接:https://www.anthropic.com/news/computer-use-2
- 来源:Anthropic
- 摘要:Computer Use 2.0 速度提升 3 倍,准确率提升 40%。
🎨 前端 / Web
2. shadcn/ui 实战
- 链接:https://ui.shadcn.com/docs
- 来源:shadcn
- 摘要:shadcn/ui 不是传统组件库,而是"复制即用"的代码片段。
3. SvelteKit 2.5 发布
- 链接:https://kit.svelte.dev/blog/sveltekit-2-5
- 来源:Svelte
- 摘要:SvelteKit 2.5 优化 RSC 支持,与 Next.js App Router 体验对齐。
⚙️ 后端 / 架构
4. Redis Stack 8 集成向量
- 链接:https://redis.io/blog/redis-stack-8
- 来源:Redis Labs
- 摘要:集成向量 / JSON / 全文搜索,单一数据库多能力。
🚀 独立开发 / OPC
5. Paddle 推出 Billing 2.0
- 链接:https://www.paddle.com/billing-2
- 来源:Paddle
- 摘要:Merchant of Record 模式继续领跑,开发者专注业务,回避税务。
6. Resend 推出 React Email 2.0
- 链接:https://resend.com/react-email-2
- 来源:Resend
- 摘要:用 React 写邮件,组件化 + 类型安全,调试体验质变。
数据来源:掘金 / InfoQ 中文 / HN / GitHub / Dev.to 采集时间:2026-05-30 09:00 (UTC+8)
📝
今日深度文
shadcn/ui 实战:复制即用的现代 React 组件库
一句话结论:shadcn/ui 把"组件库"变成"代码片段"。你可以完全控制代码、样式和依赖。
核心概念
传统组件库(Material UI):
import { Button } from '@mui/material';
- 依赖打包后 ~100KB
- 样式不易覆盖
- 升级容易 break
shadcn/ui:
npx shadcn@latest add button
- 把代码复制到你的项目
- 0 依赖(你拥有代码)
- 升级随你
实战:5 分钟搭建
Step 1:初始化
npx create-next-app@latest my-app
cd my-app
npx shadcn@latest init
Step 2:选主题
# 多个内置主题
npx shadcn@latest init --theme slate
Step 3:添加组件
# 单个组件
npx shadcn@latest add button
# 多个组件
npx shadcn@latest add button card dialog input
# 完整 dashboard
npx shadcn@latest add dashboard
Step 4:使用
import { Button } from "@/components/ui/button";
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
export default function Home() {
return (
<Card>
<CardHeader>
<CardTitle>欢迎使用 shadcn/ui</CardTitle>
</CardHeader>
<CardContent>
<Button>点击我</Button>
<Button variant="outline">次要按钮</Button>
<Button variant="destructive">危险操作</Button>
</CardContent>
</Card>
);
}
5 个核心优势
1. 代码完全可控
# 组件就在你的项目里
src/components/ui/button.tsx
# 可以任意修改
2. 零运行时依赖
# 不需要安装 @shadcn/ui 包
# 节省 bundle 体积
3. 与 Tailwind 完美集成
// 用 cn() 工具合并 className
<Button className="bg-blue-500 hover:bg-blue-600">
自定义样式
</Button>
4. 主题灵活
/* globals.css */
:root {
--primary: 222.2 47.4% 11.2%;
--primary-foreground: 210 40% 98%;
}
[data-theme="dark"] {
--primary: 210 40% 98%;
--primary-foreground: 222.2 47.4% 11.2%;
}
5. 与 Next.js 完美集成
// RSC 友好
// 默认在 Server Component 渲染
<Button>服务端渲染</Button>
5 个常见坑
坑 1:组件重复
# 误操作:多次 add 同一组件
# 解决:检查 src/components/ui/ 是否有重复
坑 2:依赖冲突
# shadcn 自动安装依赖
# 但可能与其他包版本冲突
# 解决:手动锁定 package.json
坑 3:自定义主题混乱
/* ❌ 混乱:直接覆盖 class */
.button { background: red; }
/* ✅ 正确:用 CSS 变量 */
:root {
--primary: 0 100% 50%;
}
坑 4:Server Component 报错
// ❌ 错:onClick 在 Server Component
<Button onClick={...}>点击</Button>
// ✅ 正确:加 'use client'
'use client';
<Button onClick={...}>点击</Button>
坑 5:升级困难
shadcn 更新 ≠ 你的代码更新
你需要手动重新 add 组件
或用 git diff 看变更
何时用 shadcn
✅ 适合
- Next.js / Vite + React + Tailwind 项目
- 想要现代化 UI
- 想要完全控制
❌ 不适合
- 非 React 项目
- 不喜欢 Tailwind
- 需要复杂设计系统
参考
本文基于 shadcn/ui 最新版本。
📚 同主题文章
🎨前端 / Web·
Motion + 动画设计实战:现代 Web 交互动效完整指南 2026
Motion(原 Framer Motion)是 2026 年最流行的 React 动画库。本文从 0 到生产级交互动效,含 4 个实战项目 + 性能优化 + AI 动画 + 设计令牌。
MotionFramer Motion动画
🎨前端 / Web·
shadcn/ui + 设计系统 2026:现代 Web 产品的 UI 实战
shadcn/ui 是 2026 年最火的前端组件库 + 设计系统方案。本文从 0 到完整设计系统,含 5 个真实项目 + 主题定制 + 组件库扩展 + A11y。
shadcn/ui设计系统Tailwind v4
🎨前端 / Web·
React 19 实战:从 Server Components 到 Server Actions 完整指南
React 19 是前端框架的"分水岭"——Server Components 让前端从"CSR + API"转向"RSC + Actions"。本文从 0 到生产级 RSC 实战,含 4 个真实项目 + 性能对比。
ReactServer ComponentsServer Actions