# @gnist/component-utils 中文文档

规范地址：https://npmcn.com/package/%40gnist/component-utils/

版本：3\.0\.19
npm 元数据更新时间：2026-11-16T14:53:25.297Z
中文译文更新时间：2026-11-16T17:15:12.736Z
软件包许可：未声明

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40gnist/component-utils
原文对照：https://npmcn.com/package/%40gnist/component-utils/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：55.3 kB，9 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：3 个
- peer 依赖：react ^18\.0\.0 \|\| ^19\.0\.0，@types/react ^18\.0\.0 \|\| ^19\.0\.0
- 首次发布：2024-03-26T08:27:41.649Z
- 最新版本发布：2026-11-16T14:53:25.085Z
- 近 30 天下载量：3690（2026-10-17 至 2026-11-15，npm 官方下载统计）
- 已发布版本数：30（近 12 个月 9 个）

## 中文简介

`@gnist/component-utils` 目前包含一个单一的公用功能,用于从 Cs 类轻松创建 React 组件(例如用 [`style(...)`](https://vanilla-extract.style/documentation/api/style/) 从 `@vanilla-extract/css`)或食谱 c

## 安装

```sh
npm install @gnist/component-utils
```

也可以使用：`pnpm add @gnist/component-utils`、`yarn add @gnist/component-utils`、`bun add @gnist/component-utils`

## README

# @gnist/设计系统组件公用事业

`@gnist/component-utils` 目前包含一个便于创建 React 组件的单一功能 从 css 类(例如: [`style(...)`](https://vanilla-extract.style/documentation/api/style/) 从 `@vanilla-extract/css`或用下列方法创造的食谱: [`@vanilla-extract/recipes`](https://vanilla-extract.style/documentation/packages/recipes/)。 。 。 .

## 概览

职能 `component` 有两个超载,用于一个 `className` 字符串或配方函数.

### 使用类Name 字符串

```ts
// Heading.css.ts
import { atoms } from "@gnist/themes/atoms.css.js";
import { style } from "@vanilla-extract/css";

export const heading = style([
    { float: "left" },
    atoms({ margin: "none", typography: "subtitle-small" }),
]);
```

```tsx
// Heading.tsx
import { component } from "@gnist/component-utils";
import { bannerHeading } from "./Heading.css.js";

export const Heading = component("Heading", heading, "h2");
//                                   │         │       │
// The display name of the component ┘         │       │
// The className to use for the element ───────┘       │
// The default HTML element or React component to use ─┘

// ...later...

<Heading $as="h4" href="#anchor">
    Now this is a h4
</Heading>;
```

### 使用食谱函数

```ts
// Box.css.ts
import { recipe } from "@vanilla-extract/recipes";
import { atoms } from "@gnist/themes/atoms.css.js";

export const box = recipe({
    base: atoms({ display: "flex" }),
    variants: {
        density: {
            default: atoms({ padding: "s" }),
            compact: atoms({ padding: "xxs" }),
        },
    },
    defaultVariants: { density: "default" },
});
```

```tsx
// Box.tsx
import { component } from "@gnist/component-utils";
import { box } from "./Box.css.js";

export const Box = component("Box", box, "div");
//                             │     │     │
// The display name ───────────┘     │     │
// The recipe function  ─────────────┘     │
// The default element or component ───────┘

// ...later...

<Box $as="span" density="compact">
    Now the box has xxs padding and is a span
</Box>;
```

### 与其他反应组件一起使用

请注意: `component` 用于任何具有反射组件 `className` 螺丝管, 不仅仅是 HTML 元素.

## 常见问题

### @gnist/component-utils 是什么？

@gnist/component-utils 目前包含一个单一的公用功能,用于从 Cs 类轻松创建 React 组件\(例如用 style\(\.\.\.\) 从 @vanilla-extract/css\)或食谱 c

### 如何安装 @gnist/component-utils？

在项目目录运行 npm install @gnist/component-utils，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：react（^18\.0\.0 \|\| ^19\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @gnist/component-utils 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @gnist/component-utils 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @gnist/component-utils 可以商用吗？

@gnist/component-utils 使用 未声明 许可。package\.json 没有声明许可证。没有许可证的代码默认保留全部权利，商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括，不构成法律意见，请以许可原文为准。

### @gnist/component-utils 的下载量有多少？

根据 npm 官方下载统计，@gnist/component-utils 在 2026/10/17 – 2026/11/15 这 30 天内被下载 3,690 次，日均约 123 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @gnist/component-utils 还在维护吗？

最新版本 3\.0\.19 发布于 2026/11/16（今天）；至今共发布 30 个版本，其中过去 12 个月发布了 9 个；首次发布于 2024/03/26。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

