返回首页
🎨 前端 / 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 (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·
WebGPU + WebAssembly 3.0 完整实战 2026:浏览器内的原生级计算
浏览器过去是「渲染 UI 的容器」,2026 年它变成了「能直接调用 GPU、跑原生级 GC 运行时的计算平台」。本文完整实战 WebGPU + WebAssembly 3.0:从 WebGL 为什么不够用、WebGPU pipeline 设计、WGSL 着色器、Wasm 3.0 三大件(GC / Type Imports / Stack Switching)、端侧 LLM 推理实战、浏览器内视频处理、性能调优、跨浏览器兼容性,到独立开发者的机会窗口。
WebGPUWebAssemblyWASM 3.0
🎨前端 / Web·
Vite 8 + Rolldown 1.0 完整实战 2026:构建工具链 Rust 革命
Vite 8 默认启用 Rust 编写的 Rolldown 打包器,冷启动 < 200ms,构建速度提升 5x。本文深度拆解 Rolldown 架构、迁移指南、性能基准、与 esbuild / SWC / Turbopack 横向对比。
Vite 8RolldownRust
🎨前端 / Web·
Motion + 动画设计实战:现代 Web 交互动效完整指南 2026
Motion(原 Framer Motion)是 2026 年最流行的 React 动画库。本文从 0 到生产级交互动效,含 4 个实战项目 + 性能优化 + AI 动画 + 设计令牌。
MotionFramer Motion动画