@gnist/设计系统组件公用事业
@gnist/component-utils 目前包含一个便于创建 React 组件的单一功能
从 css 类(例如: style(...)
从 @vanilla-extract/css或用下列方法创造的食谱:
@vanilla-extract/recipes。 。 。 .
概览
职能 component 有两个超载,用于一个 className 字符串或配方函数.
使用类Name 字符串
// 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" }),
]);
// 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>;
使用食谱函数
// 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" },
});
// 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 元素.