跳到正文
语言22
菜单

文档 / 框架

Next.js

用 shadcn CLI 把 UI Loft 项目添加到 Next.js 应用。只需设置一次,之后每个项目都只要一条命令。

版本
React
需要
Tailwind 4 + shadcn
设置时间
约 2 分钟

设置

  1. 创建应用

    如果已经有应用就跳过。shadcn 的 Next.js 模板已配好 TypeScript、Tailwind、App Router 和 Geist 字体。

    终端
    npx shadcn@latest init -t next -n my-app -d
    cd my-app
  2. 已经有应用?设置 shadcn

    它会生成 components.json,说明新文件放在哪里。如果你的 app/layout.tsx 来自 create-next-app,请把其中的 --font-geist-sans 改为 --font-sans,否则文字会显示为衬线字体。

    终端
    npx shadcn@latest init -d
  3. 添加一个项目

    从任意项目页面复制命令。文件会放进 components/。

    终端
    npx shadcn@latest add https://uiloft.dev/r/glow-button.json
  4. 使用它

    在任意页面里导入。放在服务器组件页面里也能直接用。

    app/page.tsx
    import { GlowButton } from "@/components/glow-button"
    
    export default function Page() {
      return <GlowButton>Get started</GlowButton>
    }

须知

  • 会动或会响应操作的项目,开头已经带有 "use client",你不用自己加。
  • 使用 --src-dir 时,文件会放进 src/components/,导入路径仍是 @/components/glow-button。
  • 要更新项目,再运行一次它的命令。覆盖文件之前,shadcn 会先询问。

其他框架