入门
了解如何设置和运行你自己的组件注册表。
本指南将引导你完成设置自己的组件注册表的过程。
¥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.
{
"$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:
import { Button } from "@/components/ui/button"
export function HelloWorld() {
return <Button>Hello World</Button>
}
注意:此示例将组件放在 registry
目录中。只要在 registry.json
文件中设置正确的路径,你就可以将其放置在项目中的任何位置。
¥Note: This example places the component in the registry
directory. You
can place it anywhere in your project as long as you set the correct path in
the registry.json
file.
registry
└── hello-world
└── hello-world.tsx
重要:如果你将组件放在自定义目录中,请确保它已在你的 tailwind.config.ts
文件中配置。
¥Important: If you're placing your component in a custom directory, make
sure it is configured in your tailwind.config.ts
file.
// tailwind.config.ts
export default {
content: ["./registry/**/*.{js,ts,jsx,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
.
{
"$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"
}
]
}
]
}
你可以通过添加 name
、type
、title
、description
和 files
来定义注册表项。
¥You define your registry item by adding a name
, type
, title
, description
and files
.
对于你添加的每个文件,你必须指定文件的 path
和 type
。path
是从项目根目录到文件的相对路径。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.
{
"scripts": {
"registry:build": "shadcn build"
}
}
运行构建脚本
¥Run the build script
运行构建脚本以生成注册表 JSON 文件。
¥Run the build script to generate the registry JSON files.
pnpm registry:build
注意:默认情况下,构建脚本将在 public/r
中生成注册表 JSON 文件,例如 public/r/hello-world.json
。
¥Note: By default, the build script will generate the registry JSON files
in public/r
e.g public/r/hello-world.json
.
你可以通过传递 --output
选项来更改输出目录。有关更多信息,请参阅 shadcn 构建命令。
¥You can change the output directory by passing the --output
option. See the shadcn build command for more information.
为你的注册表提供服务
¥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.
-
块定义需要以下属性:
name
、description
、type
和files
。¥The following properties are required for the block definition:
name
,description
,type
andfiles
. -
确保列出
registryDependencies
中的所有注册表依赖。注册表依赖是注册表中组件的名称,例如input
、button
、card
等,或注册表项的 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
中的所有依赖。依赖是注册表中包的名称,例如zod
、sonner
等。要设置版本,你可以使用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 thename@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"
-
理想情况下,将你的文件放在
components
、hooks
、lib
目录中的注册表项中。¥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