ShtukbaseStandard
package page→ /design/conventions/component-library.md

pages/design/conventions/component-library.md

127 lines, 9.5 KB. As a page.

  1. ---
  2. type: Convention
  3. title: "A component library"
  4. 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."
  5. icon: "🧩"
  6. ---
  7. # A component library
  8. How [conventions](/base/conventions.md) work: this page is the convention of the base package `design-components`. The packages that
  9. follow it say `follows: [design/conventions/component-library]`: `shtukbase-ui-html`, `shtukbase-ui-react` and `keer-ui`.
  10. ## 1. What it covers
  11. A **component library** is a package that gives a project components: the buttons, cards, fields and blocks of its screens, in
  12. HTML and CSS or in React. Many libraries may stand side by side in one project (`libraries:` on [Design](/design/design.md)), and
  13. one component id may have an HTML and a React implementation joined on one page. The libraries read the look from the chosen style
  14. and never carry a colour of their own: change the style and every library changes with it.
  15. ### Partial libraries
  16. A project may put a partial library first in `libraries:`, for example `[my-ui, shtukbase-ui-html, shtukbase-ui-react]`.
  17. For each component id and implementation (HTML or React), the first library that supplies it wins. The remaining
  18. components come from the next libraries. HTML and React use the same order, so an HTML-only override can keep the
  19. React implementation from a later library. An empty list retains automatic discovery. Unnamed local `sources:`
  20. are considered after the chosen libraries.
  21. An override keeps the same id, `data-slot` names, interaction and data states, and settings (including the control
  22. names and values) as the entry it replaces. Keep the classes the lens shell uses as well. The health check warns
  23. about missing slots and states or different controls. Contract warnings allow sync and build to finish.
  24. The sync action records the winning library and the next library it overrides for each implementation in
  25. `lib/design/components.data.js`. It also assembles `lib/design/ui.css` from the winning HTML components' adjacent
  26. stylesheets. The catalog and the lens kit use that stylesheet. Lenses supply their live markup through the existing
  27. slot contract. React resolution records the chosen source file for consumers without rewriting application imports.
  28. ## 2. Gives
  29. - **Component names** as shadcn names them. When shadcn has the component, the id is its name (`button`, `card`, `dialog`, `tabs`);
  30. an own component takes a kebab-case id of its own (`package-card`). The list the health compares with is
  31. `lib/shadcn-names.json` of the package (in a project: `.shtukbase/packages/design-components/lib/shadcn-names.json`), the 29
  32. components of the registry of `shadcn`.
  33. - **Paths.** An HTML library lays a component at `/design/<lib>/components/<level>/<id>/<id>.html` and `<id>.css` (`<level>` is
  34. `primitive`, `composite`, `block`, `shell` or `page`). The `shell` level holds the parts listed in [The lens shell](lens-shell.md).
  35. 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
  36. folder in `sources:` of [Components](/design/components.md) (its `place` action does it). Its `/design/<lib>.md`
  37. page declares that folder as `target:` so the build can match it to the library id. The same header sits in each code file.
  38. - **The header** in the first comment of every component file: `id`, `level`, `description`, `uses`, `parts`, `states`, `status`,
  39. `controls`, `cssProperties`; one parser reads HTML and code, see [The component header](/design/components-header.md).
  40. - **States.** The interaction states and the data states the header lists are shown side by side: an HTML library has a
  41. `<template data-state="…">` for each state and `<template data-status="…">` for each data state; a React library has a
  42. `<id>.states.tsx`. The same id has the same level, states and controls in both.
  43. - **CSS on tokens only.** No colour literal outside the fallback of a `var()` (DSC-R04); an HTML library also joins its component
  44. CSS into one `ui.css`.
  45. ## 3. Expects
  46. From the style, **only the names of the token contract**, never the id of a style, never a file of a style. The ones
  47. `shtukbase-ui-html` reads today, which is the practical minimum of a library of that size:
  48. - surfaces and text: `--background`, `--foreground`, `--card`, `--card-foreground`, `--popover`, `--popover-foreground`,
  49. `--muted`, `--muted-foreground`;
  50. - action and state: `--primary`, `--primary-foreground`, `--secondary`, `--secondary-foreground`, `--accent`,
  51. `--accent-foreground`, `--destructive`, `--border`, `--input`, `--ring`;
  52. - shape and type: `--radius`, `--font-sans`, `--font-mono`, `--font-display`, `--shadow-sm`, `--shadow-md`, `--shadow-lg`;
  53. - the sidebar of the lens shell: `--sidebar`, `--sidebar-foreground`, `--sidebar-primary`, `--sidebar-primary-foreground`,
  54. `--sidebar-accent`, `--sidebar-accent-foreground`, `--sidebar-border`, `--sidebar-ring`;
  55. - the optional extension, when the library wants the look (its defaults apply when a style leaves them out): `--sb-hue-1` …
  56. `--sb-hue-12`, `--sb-edge`, `--sb-shadow-lift`, `--sb-shadow-sheet`, `--sb-ease`, `--sb-dur`.
  57. From `design-components`: the header parser and `sync` (they turn the headers into pages, the data file, the manifest and the
  58. stories). A React library may also expect the HTML library of the same ids (`shtukbase-ui-react` draws the classes of
  59. `shtukbase-ui-html`); it says so in `requires`.
  60. ## 4. Free to choose
  61. - the **markup** and the **look**: the CSS, the classes, how a component is built inside;
  62. - which components and how many: a library need not have the whole shadcn list, and it may have components of its own;
  63. - the **controls** of a component (`controls:` of the header) and the presets;
  64. - the technology: plain HTML and CSS, React, Tailwind, anything that keeps the header and the states;
  65. - local custom properties: `--<id>-<control>` set from outside by the controls, and any `--` name the library declares itself.
  66. ## 5. Make an alternative
  67. The recipe for an agent who is told "make a library like `shtukbase-ui-html` for another look" or "in another technology":
  68. 1. Read the reference (below) and the header page. Pick the ids: take the shadcn name when shadcn has the component.
  69. 2. Make a package with `follows: [design/conventions/component-library]` and `requires` of `design-tokens` and `design-components`.
  70. Do not require a style: the library must work with any.
  71. 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
  72. composites that use them (`uses`); a component uses its own level or lower (DSC-R03).
  73. 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.
  74. 5. Run `shtuk run design-components sync`, then `shtuk run design-components health` and read the lines: fix what is `missing`,
  75. look at `fallbacks` (the shadcn components you do not have) and decide whether to add them.
  76. 6. Check it under two styles: install `shadcn`'s theme or `shtukbase-style`, choose one, then the other on
  77. [Design](/design/design.md), and look at the catalog in both. If a component breaks under the second style, it reads something
  78. that is not on the contract.
  79. 7. Run `shtuk check`.
  80. **To override a few components**, create a new package as in steps 1–4 and ship only the ids you want to replace.
  81. Start from each base component's header and states, and give its replacement CSS the same slot and control selectors.
  82. Install the package, put its id before the full library in [Design](/design/design.md), then run
  83. `shtuk run design-components sync`, `shtuk run design-components health`, and `shtuk run lenses build`.
  84. Check both themes in the catalog. Remove the override id from that list and run sync and build again to restore the
  85. original components.
  86. **Reference.** `shtukbase-ui-html`: 16 components, the headers, the states in templates, the CSS on contract names, `ui.css` built from
  87. 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
  88. its taste. `shtukbase-ui-react` shows the React side of the same ids, with shadcn primitives and our classes; `keer-ui` is a library
  89. of an earlier product written for its own style `keer-style` and does not carry headers yet, so its health is silent.
  90. ## 6. Health
  91. The norm [DSC-R05](/norms/rules/design-components/DSC-R05.md), enforcer `checks/health.ts`, mode `warn`. It judges the libraries chosen
  92. on [Design](/design/design.md) (every library that holds components when none is chosen; `--lib <id>` names one) and prints lines
  93. `health component-library/<lib>: <band>`:
  94. | Band | Means for a library |
  95. |---|---|
  96. | `full` | every header is valid, no raw colour, every shadcn component is there |
  97. | `missing` | a component file whose header does not parse, or a CSS file with a raw colour; the file is named |
  98. | `fallbacks` | shadcn components the library does not have; a project takes them from another library or from `shadcn` itself |
  99. | `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 |
  100. | `override` | contract differences between a selected override and a lower-priority implementation of the same id |
  101. It never fails. The gates that stand behind it are [DSC-R01](/norms/rules/design-components/DSC-R01.md) to
  102. [DSC-R04](/norms/rules/design-components/DSC-R04.md).