包页面→ /design/conventions/component-library.md
pages/design/conventions/component-library.md
127 行, 9.5 KB. 以页面形式查看.
这段内容尚未翻译,下面是英文原文。
- ---
- type: Convention
- title: "A component library"
- description: "How a component library package is made: components named as shadcn names them, lying at fixed paths, each with a header and states; it reads the style only through the contract names. What it gives, expects, may choose freely, how to make an alternative, and its health."
- icon: "🧩"
- ---
- # A component library
- How [conventions](/base/conventions.md) work: this page is the convention of the base package `design-components`. The packages that
- follow it say `follows: [design/conventions/component-library]`: `shtukbase-ui-html`, `shtukbase-ui-react` and `keer-ui`.
- ## 1. What it covers
- A **component library** is a package that gives a project components: the buttons, cards, fields and blocks of its screens, in
- HTML and CSS or in React. Many libraries may stand side by side in one project (`libraries:` on [Design](/design/design.md)), and
- one component id may have an HTML and a React implementation joined on one page. The libraries read the look from the chosen style
- and never carry a colour of their own: change the style and every library changes with it.
- ### Partial libraries
- A project may put a partial library first in `libraries:`, for example `[my-ui, shtukbase-ui-html, shtukbase-ui-react]`.
- For each component id and implementation (HTML or React), the first library that supplies it wins. The remaining
- components come from the next libraries. HTML and React use the same order, so an HTML-only override can keep the
- React implementation from a later library. An empty list retains automatic discovery. Unnamed local `sources:`
- are considered after the chosen libraries.
- An override keeps the same id, `data-slot` names, interaction and data states, and settings (including the control
- names and values) as the entry it replaces. Keep the classes the lens shell uses as well. The health check warns
- about missing slots and states or different controls. Contract warnings allow sync and build to finish.
- The sync action records the winning library and the next library it overrides for each implementation in
- `lib/design/components.data.js`. It also assembles `lib/design/ui.css` from the winning HTML components' adjacent
- stylesheets. The catalog and the lens kit use that stylesheet. Lenses supply their live markup through the existing
- slot contract. React resolution records the chosen source file for consumers without rewriting application imports.
- ## 2. Gives
- - **Component names** as shadcn names them. When shadcn has the component, the id is its name (`button`, `card`, `dialog`, `tabs`);
- an own component takes a kebab-case id of its own (`package-card`). The list the health compares with is
- `lib/shadcn-names.json` of the package (in a project: `.shtukbase/packages/design-components/lib/shadcn-names.json`), the 29
- components of the registry of `shadcn`.
- - **Paths.** An HTML library lays a component at `/design/<lib>/components/<level>/<id>/<id>.html` and `<id>.css` (`<level>` is
- `primitive`, `composite`, `block`, `shell` or `page`). The `shell` level holds the parts listed in [The lens shell](lens-shell.md).
- The [lens layout](layout.md) places those parts. A library of code (React) keeps its files in a folder of the project and lists that
- folder in `sources:` of [Components](/design/components.md) (its `place` action does it). Its `/design/<lib>.md`
- page declares that folder as `target:` so the build can match it to the library id. The same header sits in each code file.
- - **The header** in the first comment of every component file: `id`, `level`, `description`, `uses`, `parts`, `states`, `status`,
- `controls`, `cssProperties`; one parser reads HTML and code, see [The component header](/design/components-header.md).
- - **States.** The interaction states and the data states the header lists are shown side by side: an HTML library has a
- `<template data-state="…">` for each state and `<template data-status="…">` for each data state; a React library has a
- `<id>.states.tsx`. The same id has the same level, states and controls in both.
- - **CSS on tokens only.** No colour literal outside the fallback of a `var()` (DSC-R04); an HTML library also joins its component
- CSS into one `ui.css`.
- ## 3. Expects
- From the style, **only the names of the token contract**, never the id of a style, never a file of a style. The ones
- `shtukbase-ui-html` reads today, which is the practical minimum of a library of that size:
- - surfaces and text: `--background`, `--foreground`, `--card`, `--card-foreground`, `--popover`, `--popover-foreground`,
- `--muted`, `--muted-foreground`;
- - action and state: `--primary`, `--primary-foreground`, `--secondary`, `--secondary-foreground`, `--accent`,
- `--accent-foreground`, `--destructive`, `--border`, `--input`, `--ring`;
- - shape and type: `--radius`, `--font-sans`, `--font-mono`, `--font-display`, `--shadow-sm`, `--shadow-md`, `--shadow-lg`;
- - the sidebar of the lens shell: `--sidebar`, `--sidebar-foreground`, `--sidebar-primary`, `--sidebar-primary-foreground`,
- `--sidebar-accent`, `--sidebar-accent-foreground`, `--sidebar-border`, `--sidebar-ring`;
- - the optional extension, when the library wants the look (its defaults apply when a style leaves them out): `--sb-hue-1` …
- `--sb-hue-12`, `--sb-edge`, `--sb-shadow-lift`, `--sb-shadow-sheet`, `--sb-ease`, `--sb-dur`.
- From `design-components`: the header parser and `sync` (they turn the headers into pages, the data file, the manifest and the
- stories). A React library may also expect the HTML library of the same ids (`shtukbase-ui-react` draws the classes of
- `shtukbase-ui-html`); it says so in `requires`.
- ## 4. Free to choose
- - the **markup** and the **look**: the CSS, the classes, how a component is built inside;
- - which components and how many: a library need not have the whole shadcn list, and it may have components of its own;
- - the **controls** of a component (`controls:` of the header) and the presets;
- - the technology: plain HTML and CSS, React, Tailwind, anything that keeps the header and the states;
- - local custom properties: `--<id>-<control>` set from outside by the controls, and any `--` name the library declares itself.
- ## 5. Make an alternative
- The recipe for an agent who is told "make a library like `shtukbase-ui-html` for another look" or "in another technology":
- 1. Read the reference (below) and the header page. Pick the ids: take the shadcn name when shadcn has the component.
- 2. Make a package with `follows: [design/conventions/component-library]` and `requires` of `design-tokens` and `design-components`.
- Do not require a style: the library must work with any.
- 3. For each component, make the file at the path of section 2 with its header and its states. Start with the primitives, then the
- composites that use them (`uses`); a component uses its own level or lower (DSC-R03).
- 4. Write the CSS with `var(--…)` of the contract names of section 3, with a fallback in `var()` if you want one. No raw colour.
- 5. Run `shtuk run design-components sync`, then `shtuk run design-components health` and read the lines: fix what is `missing`,
- look at `fallbacks` (the shadcn components you do not have) and decide whether to add them.
- 6. Check it under two styles: install `shadcn`'s theme or `shtukbase-style`, choose one, then the other on
- [Design](/design/design.md), and look at the catalog in both. If a component breaks under the second style, it reads something
- that is not on the contract.
- 7. Run `shtuk check`.
- **To override a few components**, create a new package as in steps 1–4 and ship only the ids you want to replace.
- Start from each base component's header and states, and give its replacement CSS the same slot and control selectors.
- Install the package, put its id before the full library in [Design](/design/design.md), then run
- `shtuk run design-components sync`, `shtuk run design-components health`, and `shtuk run lenses build`.
- Check both themes in the catalog. Remove the override id from that list and run sync and build again to restore the
- original components.
- **Reference.** `shtukbase-ui-html`: 16 components, the headers, the states in templates, the CSS on contract names, `ui.css` built from
- them. **Do not copy** its look (the even edge, the glow, the package card and the other own components) or the demo page: that is
- its taste. `shtukbase-ui-react` shows the React side of the same ids, with shadcn primitives and our classes; `keer-ui` is a library
- of an earlier product written for its own style `keer-style` and does not carry headers yet, so its health is silent.
- ## 6. Health
- The norm [DSC-R05](/norms/rules/design-components/DSC-R05.md), enforcer `checks/health.ts`, mode `warn`. It judges the libraries chosen
- on [Design](/design/design.md) (every library that holds components when none is chosen; `--lib <id>` names one) and prints lines
- `health component-library/<lib>: <band>`:
- | Band | Means for a library |
- |---|---|
- | `full` | every header is valid, no raw colour, every shadcn component is there |
- | `missing` | a component file whose header does not parse, or a CSS file with a raw colour; the file is named |
- | `fallbacks` | shadcn components the library does not have; a project takes them from another library or from `shadcn` itself |
- | `extra` | components of its own (ids outside the shadcn list), and CSS names it reads that the contract does not know and the library does not declare itself |
- | `override` | contract differences between a selected override and a lower-priority implementation of the same id |
- It never fails. The gates that stand behind it are [DSC-R01](/norms/rules/design-components/DSC-R01.md) to
- [DSC-R04](/norms/rules/design-components/DSC-R04.md).