98.0k

registry-item.json

Previous

注册表项的规范。

registry-item.json 模式用于定义您的自定义注册表项。

registry-item.json
{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "hello-world",
  "type": "registry:block",
  "title": "Hello World",
  "description": "一个简单的 Hello World 组件。",
  "registryDependencies": [
    "button",
    "@acme/input-form",
    "https://example.com/r/foo"
  ],
  "dependencies": ["is-even@3.0.0", "motion"],
  "files": [
    {
      "path": "registry/new-york/hello-world/hello-world.tsx",
      "type": "registry:component"
    },
    {
      "path": "registry/new-york/hello-world/use-hello-world.ts",
      "type": "registry:hook"
    }
  ],
  "cssVars": {
    "theme": {
      "font-heading": "Poppins, sans-serif"
    },
    "light": {
      "brand": "20 14.3% 4.1%"
    },
    "dark": {
      "brand": "20 14.3% 4.1%"
    }
  }
}

定义

您可以在这里查看 registry-item.json 的 JSON Schema。

$schema

$schema 属性用于指定 registry-item.json 文件的模式。

registry-item.json
{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json"
}

name

项目的名称。用于在注册表中识别该项目。应在您的注册表中唯一。

registry-item.json
{
  "name": "hello-world"
}

title

注册表项目的可读标题。保持简短且具描述性。

registry-item.json
{
  "title": "Hello World"
}

description

注册表项目的描述。可以比 title 更长且更详细。

registry-item.json
{
  "description": "一个简单的 Hello World 组件。"
}

type

type 属性用于指定注册表项目的类型。用于确定项目在解析到项目时的类型与目标路径。

registry-item.json
{
  "type": "registry:block"
}

支持的类型如下:

类型描述
registry:block用于包含多个文件的复杂组件。
registry:component用于简单组件。
registry:lib用于库和工具函数。
registry:hook用于钩子函数。
registry:ui用于 UI 组件及单文件基础组件。
registry:page用于页面或基于文件的路由。
registry:file用于杂项文件。
registry:style用于注册表样式,如 new-york
registry:theme用于主题。
registry:item用于通用注册表项。

author

author 属性用于指定注册表项目的作者。

可以是项目独有,也可以与注册表的作者相同。

registry-item.json
{
  "author": "John Doe <john@doe.com>"
}

dependencies

dependencies 属性用于指定注册表项的依赖,针对 npm 包。

使用 @version 指定版本号。

registry-item.json
{
  "dependencies": [
    "@radix-ui/react-accordion",
    "zod",
    "lucide-react",
    "name@1.0.2"
  ]
}

registryDependencies

用于注册表依赖。可以用名称、命名空间或 URL。

  • 对于 shadcn/ui 注册表的项目如 buttoninputselect 等,使用名称,如 ['button', 'input', 'select']
  • 对于命名空间下的注册表项目如 @acme,使用名称,如 ['@acme/input-form']
  • 对于自定义注册表项目,使用项目的 URL,如 ['https://example.com/r/hello-world.json']
registry-item.json
{
  "registryDependencies": [
    "button",
    "@acme/input-form",
    "https://example.com/r/editor.json"
  ]
}

注意:CLI 会自动解析远程注册表依赖。

files

files 属性用于指定注册表项目的文件。每个文件拥有 pathtype 和可选的 target 属性。

target 属性是 registry:pageregistry:file 类型必须的。

registry-item.json
{
  "files": [
    {
      "path": "registry/new-york/hello-world/page.tsx",
      "type": "registry:page",
      "target": "app/hello/page.tsx"
    },
    {
      "path": "registry/new-york/hello-world/hello-world.tsx",
      "type": "registry:component"
    },
    {
      "path": "registry/new-york/hello-world/use-hello-world.ts",
      "type": "registry:hook"
    },
    {
      "path": "registry/new-york/hello-world/.env",
      "type": "registry:file",
      "target": "~/.env"
    }
  ]
}

path

path 属性用于指定文件在注册表中的路径。此路径被构建脚本用来解析、转换并生成注册表JSON负载。

type

type 属性用于指定文件的类型。具体请参见type节。

target

target 属性表示文件应放置在项目中的位置。此属性为可选,仅对 registry:pageregistry:file 类型必需。

默认情况下,shadcn CLI 会读取项目的 components.json 文件来确定目标路径。对于某些文件,如路由或配置文件,可以手动指定目标路径。

使用 ~ 表示项目根目录,例如 ~/foo.config.js

tailwind

弃用: Tailwind V4 项目建议使用 cssVars.theme 来代替。

tailwind 属性用于 Tailwind 配置,例如 themepluginscontent

您可以通过 tailwind.config 为注册表项添加颜色、动画和插件。

registry-item.json
{
  "tailwind": {
    "config": {
      "theme": {
        "extend": {
          "colors": {
            "brand": "hsl(var(--brand))"
          },
          "keyframes": {
            "wiggle": {
              "0%, 100%": { "transform": "rotate(-3deg)" },
              "50%": { "transform": "rotate(3deg)" }
            }
          },
          "animation": {
            "wiggle": "wiggle 1s ease-in-out infinite"
          }
        }
      }
    }
  }
}

cssVars

用于定义注册表项目的 CSS 变量。

registry-item.json
{
  "cssVars": {
    "theme": {
      "font-heading": "Poppins, sans-serif"
    },
    "light": {
      "brand": "20 14.3% 4.1%",
      "radius": "0.5rem"
    },
    "dark": {
      "brand": "20 14.3% 4.1%"
    }
  }
}

css

使用 css 向项目的 CSS 文件添加新规则,如 @layer base@layer components@utility@keyframes@plugin 等。

registry-item.json
{
  "css": {
    "@plugin @tailwindcss/typography": {},
    "@plugin foo": {},
    "@layer base": {
      "body": {
        "font-size": "var(--text-base)",
        "line-height": "1.5"
      }
    },
    "@layer components": {
      "button": {
        "background-color": "var(--color-primary)",
        "color": "var(--color-white)"
      }
    },
    "@utility text-magic": {
      "font-size": "var(--text-base)",
      "line-height": "1.5"
    },
    "@keyframes wiggle": {
      "0%, 100%": {
        "transform": "rotate(-3deg)"
      },
      "50%": {
        "transform": "rotate(3deg)"
      }
    }
  }
}

envVars

使用 envVars 向注册表项目添加环境变量。

registry-item.json
{
  "envVars": {
    "NEXT_PUBLIC_APP_URL": "http://localhost:4000",
    "DATABASE_URL": "postgresql://postgres:postgres@localhost:5432/postgres",
    "OPENAI_API_KEY": ""
  }
}

环境变量将被添加到 .env.local.env 文件。已存在的变量不会被覆盖。

docs

使用 docs 在通过 CLI 安装注册表项时显示自定义文档或信息。

registry-item.json
{
  "docs": "如需获取 OPENAI_API_KEY,请前往 https://platform.openai.com 注册账户。"
}

categories

使用 categories 来组织您的注册表项目。

registry-item.json
{
  "categories": ["sidebar", "dashboard"]
}

meta

使用 meta 为您的注册表项添加额外的元数据。您可以添加任何希望在注册表项中可用的键值对。

registry-item.json
{
  "meta": { "foo": "bar" }
}