Docs
入门

入门

了解如何设置和运行你自己的组件注册表。

本指南将引导你完成设置自己的组件注册表的过程。

¥This guide will walk you through the process of setting up your own component registry.

它假设你已经有一个包含组件的项目,并希望将其转换为注册表。

¥It assumes you already have a project with components and would like to turn it into a registry.

如果你正在启动一个新的注册表项目,你可以使用 注册表模板 作为起点。我们已经为你配置好了。

¥If you're starting a new registry project, you can use the registry template as a starting point. We have already configured it for you.

registry.json

仅当你使用 shadcn CLI 构建注册表时,才需要 registry.json 文件。

¥The registry.json file is only required if you're using the shadcn CLI to build your registry.

如果你使用的是不同的构建系统,你可以跳过此步骤,只要你的构建系统生成符合 注册表项架构规范 的有效 JSON 文件即可。

¥If you're using a different build system, you can skip this step as long as your build system produces valid JSON files that conform to the registry-item schema specification.

添加一个 registry.json 文件

¥Add a registry.json file

在项目的根目录中创建一个 registry.json 文件。你的项目可以是 Next.js、Remix、Vite 或任何其他支持 React 的项目。

¥Create a registry.json file in the root of your project. Your project can be a Next.js, Remix, Vite, or any other project that supports React.

registry.json
{
  "$schema": "https://shadcn.nodejs.cn/schema/registry.json",
  "name": "acme",
  "homepage": "https://acme.com",
  "items": [
    // ...
  ]
}

registry.json 文件必须符合 注册表架构规范

¥This registry.json file must conform to the registry schema specification.

添加一个注册表项

¥Add a registry item

创建你的组件

¥Create your component

添加你的第一个组件。以下是简单 <HelloWorld /> 组件的示例:

¥Add your first component. Here's an example of a simple <HelloWorld /> component:

registry/hello-world/hello-world.tsx
import { Button } from "@/components/ui/button"
 
export function HelloWorld() {
  return <Button>Hello World</Button>
}
registry
└── hello-world
    └── hello-world.tsx

将你的组件添加到注册表

¥Add your component to the registry

要将你的组件添加到注册表,你需要将你的组件定义添加到 registry.json

¥To add your component to the registry, you need to add your component definition to registry.json.

registry.json
{
  "$schema": "https://shadcn.nodejs.cn/schema/registry.json",
  "name": "acme",
  "homepage": "https://acme.com",
  "items": [
    {
      "name": "hello-world",
      "type": "registry:block",
      "title": "Hello World",
      "description": "A simple hello world component.",
      "files": [
        {
          "path": "registry/hello-world/hello-world.tsx",
          "type": "registry:component"
        }
      ]
    }
  ]
}

你可以通过添加 nametypetitledescriptionfiles 来定义注册表项。

¥You define your registry item by adding a name, type, title, description and files.

对于你添加的每个文件,你必须指定文件的 pathtypepath 是从项目根目录到文件的相对路径。type 是文件的类型。

¥For every file you add, you must specify the path and type of the file. The path is the relative path to the file from the root of your project. The type is the type of the file.

你可以在 注册表项架构文档 中阅读有关注册表项架构和文件类型的更多信息。

¥You can read more about the registry item schema and file types in the registry item schema docs.

构建你的注册表

¥Build your registry

安装 shadcn CLI

¥Install the shadcn CLI

注意:build 命令目前仅在 CLI 的 shadcn@canary 版本中可用。

¥Note: the build command is currently only available in the shadcn@canary version of the CLI.

pnpm add shadcn@canary

添加一个构建脚本

¥Add a build script

registry:build 脚本添加到你的 package.json 文件。

¥Add a registry:build script to your package.json file.

package.json
{
  "scripts": {
    "registry:build": "shadcn build"
  }
}

运行构建脚本

¥Run the build script

运行构建脚本以生成注册表 JSON 文件。

¥Run the build script to generate the registry JSON files.

pnpm registry:build

为你的注册表提供服务

¥Serve your registry

如果你在 Next.js 上运行注册表,你现在可以通过运行 next 服务器来提供注册表。对于其他框架,命令可能有所不同。

¥If you're running your registry on Next.js, you can now serve your registry by running the next server. The command might differ for other frameworks.

pnpm dev

你的文件现在将在 http://localhost:3000/r/[NAME].json(例如 http://localhost:3000/r/hello-world.json)上提供。

¥Your files will now be served at http://localhost:3000/r/[NAME].json eg. http://localhost:3000/r/hello-world.json.

发布你的注册表

¥Publish your registry

要使其他开发者可以使用你的注册表,你可以通过将项目部署到公共 URL 来发布它。

¥To make your registry available to other developers, you can publish it by deploying your project to a public URL.

指南

¥Guidelines

以下是构建注册表组件时要遵循的一些准则。

¥Here are some guidelines to follow when building components for a registry.

  • 块定义需要以下属性:namedescriptiontypefiles

    ¥The following properties are required for the block definition: name, description, type and files.

  • 确保列出 registryDependencies 中的所有注册表依赖。注册表依赖是注册表中组件的名称,例如 inputbuttoncard 等,或注册表项的 URL,例如 http://localhost:3000/r/editor.json

    ¥Make sure to list all registry dependencies in registryDependencies. A registry dependency is the name of the component in the registry eg. input, button, card, etc or a URL to a registry item eg. http://localhost:3000/r/editor.json.

  • 确保列出 dependencies 中的所有依赖。依赖是注册表中包的名称,例如 zodsonner 等。要设置版本,你可以使用 name@version 格式,例如 zod@^3.20.0

    ¥Make sure to list all dependencies in dependencies. A dependency is the name of the package in the registry eg. zod, sonner, etc. To set a version, you can use the name@version format eg. zod@^3.20.0.

  • 导入应始终使用 @/registry 路径。例如 import { HelloWorld } from "@/registry/hello-world/hello-world"

    ¥Imports should always use the @/registry path. eg. import { HelloWorld } from "@/registry/hello-world/hello-world"

  • 理想情况下,将你的文件放在 componentshookslib 目录中的注册表项中。

    ¥Ideally, place your files within a registry item in components, hooks, lib directories.

使用 CLI 安装

¥Install using the CLI

要使用 shadcn CLI 安装注册表项,请使用 add 命令,后跟注册表项的 URL。

¥To install a registry item using the shadcn CLI, use the add command followed by the URL of the registry item.

pnpm dlx shadcn@latest add http://localhost:3000/r/hello-world.json