@luxalgo/vela-ui 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/CHANGELOG.md +153 -91
  2. package/LICENSE +201 -201
  3. package/NOTICE +28 -28
  4. package/README.md +86 -86
  5. package/cli/engine.mjs +282 -282
  6. package/cli/index.mjs +154 -154
  7. package/dist/badge.js +1 -1
  8. package/dist/bar-chart.d.ts +1 -1
  9. package/dist/bar-chart.js +1 -1
  10. package/dist/button.d.ts +1 -1
  11. package/dist/button.js +4 -4
  12. package/dist/callout-bubble.d.ts +1 -1
  13. package/dist/callout-bubble.js +4 -4
  14. package/dist/card.d.ts +52 -0
  15. package/dist/card.js +179 -0
  16. package/dist/checkbox.js +5 -5
  17. package/dist/{chunk-B5ZSRUDQ.js → chunk-BWSOMG3G.js} +2 -2
  18. package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
  19. package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
  20. package/dist/chunk-GUIYFQ27.js +30 -0
  21. package/dist/{chunk-WGULXRWD.js → chunk-KUWTI2PE.js} +4 -4
  22. package/dist/{chunk-Z5PINFQO.js → chunk-QGWAN3EP.js} +5 -5
  23. package/dist/{chunk-V2Q4MRMS.js → chunk-RH53NQGE.js} +30 -4
  24. package/dist/{chunk-WBGSYCV2.js → chunk-SKFAYTDK.js} +4 -4
  25. package/dist/{chunk-MJ4RUGX7.js → chunk-TGJNHUYV.js} +3 -3
  26. package/dist/{chunk-WXH3EFV5.js → chunk-VC7SVSDK.js} +4 -4
  27. package/dist/{chunk-7VMN7NKJ.js → chunk-YT2BSP7O.js} +3 -3
  28. package/dist/collapsible.d.ts +1 -1
  29. package/dist/collapsible.js +3 -3
  30. package/dist/color-picker.js +5 -5
  31. package/dist/context-menu.js +5 -5
  32. package/dist/data-table.d.ts +1 -1
  33. package/dist/data-table.js +1 -1
  34. package/dist/date-picker.d.ts +1 -1
  35. package/dist/date-picker.js +2 -2
  36. package/dist/dialog.d.ts +9 -1
  37. package/dist/dialog.js +15 -5
  38. package/dist/donut.d.ts +1 -1
  39. package/dist/donut.js +1 -1
  40. package/dist/drawer.d.ts +1 -1
  41. package/dist/drawer.js +2 -2
  42. package/dist/dropdown-menu.d.ts +1 -1
  43. package/dist/dropdown-menu.js +5 -5
  44. package/dist/empty.d.ts +1 -1
  45. package/dist/empty.js +2 -2
  46. package/dist/field.js +13 -13
  47. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  48. package/dist/figure.js +41 -0
  49. package/dist/glyph-select.js +5 -5
  50. package/dist/hover-card.d.ts +1 -1
  51. package/dist/hover-card.js +3 -3
  52. package/dist/icons.js +1 -1
  53. package/dist/index.d.ts +4 -2
  54. package/dist/index.js +3 -3
  55. package/dist/input-group.d.ts +1 -1
  56. package/dist/input-group.js +4 -4
  57. package/dist/input.js +2 -2
  58. package/dist/item.d.ts +1 -1
  59. package/dist/item.js +2 -2
  60. package/dist/kbd.d.ts +1 -1
  61. package/dist/kbd.js +2 -2
  62. package/dist/line-chart.d.ts +1 -1
  63. package/dist/line-chart.js +1 -1
  64. package/dist/manifest.json +691 -74
  65. package/dist/month-grid.js +1 -1
  66. package/dist/number-input.d.ts +1 -1
  67. package/dist/number-input.js +5 -5
  68. package/dist/popover.js +3 -3
  69. package/dist/scatter-chart.d.ts +1 -1
  70. package/dist/scatter-chart.js +1 -1
  71. package/dist/select.d.ts +1 -1
  72. package/dist/select.js +7 -7
  73. package/dist/slider.d.ts +1 -1
  74. package/dist/slider.js +2 -2
  75. package/dist/sort-button.js +4 -4
  76. package/dist/sparkline.d.ts +1 -1
  77. package/dist/sparkline.js +1 -1
  78. package/dist/switch.d.ts +1 -1
  79. package/dist/switch.js +3 -3
  80. package/dist/tabs.d.ts +1 -1
  81. package/dist/tabs.js +4 -4
  82. package/dist/text-area.d.ts +1 -1
  83. package/dist/text-area.js +2 -2
  84. package/dist/theme-TefB8vtl.d.ts +341 -0
  85. package/dist/theme.d.ts +42 -192
  86. package/dist/theme.js +1 -1
  87. package/dist/theme.schema.json +321 -30
  88. package/dist/toast.d.ts +49 -0
  89. package/dist/toast.js +463 -0
  90. package/dist/toggle-group.d.ts +1 -1
  91. package/dist/toggle-group.js +4 -4
  92. package/dist/tooltip.js +2 -2
  93. package/dist/treemap.d.ts +80 -0
  94. package/dist/treemap.js +470 -0
  95. package/dist/zag.d.ts +11 -2
  96. package/dist/zag.js +1 -1
  97. package/docs/theming.md +217 -217
  98. package/docs/what-to-change.md +20 -20
  99. package/docs/writing-a-component.md +250 -250
  100. package/llms.txt +6 -3
  101. package/package.json +90 -90
  102. package/registry/badge.ts +119 -119
  103. package/registry/bar-chart.ts +215 -215
  104. package/registry/button.ts +387 -379
  105. package/registry/callout-bubble.ts +347 -347
  106. package/registry/card.ts +379 -0
  107. package/registry/checkbox.ts +234 -234
  108. package/registry/collapsible.ts +271 -271
  109. package/registry/color-picker.ts +582 -582
  110. package/registry/context-menu.ts +208 -208
  111. package/registry/data-table.ts +215 -215
  112. package/registry/date-picker.ts +483 -483
  113. package/registry/dialog.ts +551 -535
  114. package/registry/donut.ts +145 -145
  115. package/registry/drawer.ts +636 -636
  116. package/registry/dropdown-menu.ts +762 -762
  117. package/registry/empty.ts +91 -91
  118. package/registry/field.ts +584 -584
  119. package/registry/{stat.ts → figure.ts} +96 -96
  120. package/registry/glyph-select.ts +268 -268
  121. package/registry/hover-card.ts +222 -222
  122. package/registry/input-group.ts +229 -229
  123. package/registry/input.ts +181 -181
  124. package/registry/item.ts +376 -376
  125. package/registry/kbd.ts +64 -64
  126. package/registry/line-chart.ts +218 -218
  127. package/registry/month-grid.ts +328 -328
  128. package/registry/number-input.ts +443 -443
  129. package/registry/popover.ts +368 -368
  130. package/registry/scatter-chart.ts +221 -221
  131. package/registry/select.ts +620 -620
  132. package/registry/slider.ts +259 -259
  133. package/registry/sort-button.ts +189 -189
  134. package/registry/sparkline.ts +114 -114
  135. package/registry/switch.ts +205 -205
  136. package/registry/tabs.ts +426 -426
  137. package/registry/text-area.ts +186 -186
  138. package/registry/toast.ts +965 -0
  139. package/registry/toggle-group.ts +295 -295
  140. package/registry/tooltip.ts +156 -156
  141. package/registry/treemap.ts +847 -0
  142. package/skills/theming-vela-ui/SKILL.md +137 -137
  143. package/skills/using-vela-ui-components/SKILL.md +139 -137
  144. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  145. package/src/components/badge.ts +119 -119
  146. package/src/components/bar-chart.ts +215 -215
  147. package/src/components/button.ts +387 -379
  148. package/src/components/callout-bubble.ts +347 -347
  149. package/src/components/card.ts +379 -0
  150. package/src/components/checkbox.ts +234 -234
  151. package/src/components/collapsible.ts +271 -271
  152. package/src/components/color-picker.ts +582 -582
  153. package/src/components/context-menu.ts +208 -208
  154. package/src/components/data-table.ts +215 -215
  155. package/src/components/date-picker.ts +483 -483
  156. package/src/components/dialog.ts +551 -535
  157. package/src/components/donut.ts +145 -145
  158. package/src/components/drawer.ts +636 -636
  159. package/src/components/dropdown-menu.ts +762 -762
  160. package/src/components/empty.ts +91 -91
  161. package/src/components/field.ts +584 -584
  162. package/src/components/{stat.ts → figure.ts} +96 -96
  163. package/src/components/glyph-select.ts +268 -268
  164. package/src/components/hover-card.ts +222 -222
  165. package/src/components/input-group.ts +229 -229
  166. package/src/components/input.ts +181 -181
  167. package/src/components/item.ts +376 -376
  168. package/src/components/kbd.ts +64 -64
  169. package/src/components/line-chart.ts +218 -218
  170. package/src/components/month-grid.ts +328 -328
  171. package/src/components/number-input.ts +443 -443
  172. package/src/components/popover.ts +368 -368
  173. package/src/components/scatter-chart.ts +221 -221
  174. package/src/components/select.ts +620 -620
  175. package/src/components/slider.ts +259 -259
  176. package/src/components/sort-button.ts +189 -189
  177. package/src/components/sparkline.ts +114 -114
  178. package/src/components/switch.ts +205 -205
  179. package/src/components/tabs.ts +426 -426
  180. package/src/components/text-area.ts +186 -186
  181. package/src/components/toast.ts +965 -0
  182. package/src/components/toggle-group.ts +295 -295
  183. package/src/components/tooltip.ts +156 -156
  184. package/src/components/treemap.ts +847 -0
  185. package/src/index.ts +24 -24
  186. package/src/lib/calendar.ts +117 -117
  187. package/src/lib/chart.ts +72 -72
  188. package/src/lib/color.ts +89 -89
  189. package/src/lib/component-index.ts +163 -151
  190. package/src/lib/css.ts +48 -48
  191. package/src/lib/define.ts +45 -45
  192. package/src/lib/dispose.ts +36 -36
  193. package/src/lib/h.ts +55 -55
  194. package/src/lib/headless.ts +16 -16
  195. package/src/lib/i18n.ts +30 -30
  196. package/src/lib/icons.ts +244 -237
  197. package/src/lib/kit-text.ts +26 -26
  198. package/src/lib/page.ts +24 -24
  199. package/src/lib/palette.ts +81 -81
  200. package/src/lib/placement.ts +188 -188
  201. package/src/lib/scope.ts +135 -135
  202. package/src/lib/styles.ts +53 -53
  203. package/src/lib/surface-events.ts +31 -31
  204. package/src/lib/surface-exit.ts +87 -87
  205. package/src/lib/t.ts +68 -68
  206. package/src/lib/theme.ts +684 -664
  207. package/src/lib/tokens.ts +110 -110
  208. package/src/lib/variants.ts +32 -32
  209. package/src/lib/zag.ts +61 -46
  210. package/stylelint/index.mjs +60 -60
  211. package/stylelint/lint.mjs +81 -81
  212. package/stylelint/plugin.mjs +115 -115
  213. package/dist/chunk-P4MTEMT7.js +0 -23
  214. package/dist/stat.js +0 -41
  215. package/dist/{chunk-U3EH42YY.js → chunk-5BJV5FY5.js} +1 -1
  216. package/dist/{chunk-E2FE6V2N.js → chunk-ALAHGSLC.js} +1 -1
  217. package/dist/{chunk-C3ICYMI3.js → chunk-FYVQO5TN.js} +1 -1
@@ -1,250 +1,250 @@
1
- # Writing a component
2
-
3
- Each component is **one file**, `src/components/<name>.ts`, published at
4
- `@luxalgo/vela-ui/<name>`, holding its declaration, its variants, its CSS and its
5
- `create<Name>` function. Overlays (menu, dialog, drawer, tooltip) and composite controls (tabs,
6
- toggle group) run a [Zag.js](https://zagjs.com) state machine, which owns their keyboard
7
- behavior and accessibility; form controls keep small vanilla state objects.
8
-
9
- ## One file per component
10
-
11
- Start one with `npm run new-component -- gauge` at the repo root: it writes the file in the
12
- shape below, adds its declaration to the theme, and writes its test. A component file reads
13
- top to bottom:
14
-
15
- ```ts
16
- // Gauge: … what it is and how it behaves, for the reader of the file.
17
- import { css, defineStyles } from '../lib/css';
18
- import { defineComponent } from '../lib/define';
19
- import { h } from '../lib/h';
20
- import { t } from '../lib/t';
21
- import { variants, type VariantProps } from '../lib/variants';
22
-
23
- // Its name, its parts (each part's data-slot) and its settings (custom properties a theme
24
- // or a host's CSS can set, with their defaults).
25
- export const gaugeDef = defineComponent({ name: 'gauge', parts: ['root', 'needle'], settings: { radius: t.radius.lg } });
26
-
27
- // Its variants: the names type the props, the first value is the default.
28
- export const gaugeVariants = variants({ size: ['md', 'sm'] });
29
-
30
- export interface GaugeProps extends VariantProps<typeof gaugeVariants> {
31
- value: number;
32
- }
33
-
34
- export interface GaugeHandle {
35
- readonly el: HTMLElement;
36
- setValue(value: number): void;
37
- destroy(): void;
38
- }
39
-
40
- const injectStyles = defineStyles(
41
- 'vela-ui-gauge',
42
- css`
43
- @layer vela.ui {
44
- [data-slot='gauge'] {
45
- border-radius: ${gaugeDef.vars.radius};
46
- background: ${t.surface.sunken};
47
-
48
- &[data-size='sm'] { … }
49
- & > [data-slot='gauge-needle'] { color: ${t.accent}; }
50
- }
51
- }
52
- `,
53
- );
54
-
55
- class GaugeImpl implements GaugeHandle { … } // methods shared across instances
56
-
57
- export function createGauge(props: GaugeProps): GaugeHandle {
58
- return new GaugeImpl(props);
59
- }
60
- ```
61
-
62
- Then add the declaration to `ComponentDefs` in `src/lib/theme.ts`
63
- (`gauge: typeof gaugeDef;`), which types the component's settings and parts in a theme. The
64
- build fails while one is missing. The manifest (`dist/manifest.json`), the theme's JSON
65
- Schema and `llms.txt` are generated from the file at build time, so there is nothing else to
66
- list.
67
-
68
- Released components keep their class (`new Checkbox(…)`, `new Tooltip(trigger, …)`) and their
69
- controller (`checkboxController`, `tooltipController`) next to the `create…` function; the
70
- class is the instance the function returns. New components start from the shape above.
71
-
72
- ## A component of your own
73
-
74
- The same shape works outside the kit, for a component a project writes: import the helpers
75
- from the package root (`import { css, defineComponent, defineStyles, h, t, variants } from
76
- '@luxalgo/vela-ui'`) and keep the rules below. Three things make it a full citizen:
77
-
78
- - **Its CSS goes in `@layer vela.ui`**, like the kit's, not in `vela.custom`: a theme's part
79
- CSS sits in `vela.custom`, and only from a later layer does it restyle the component.
80
- - **Declare it before creating a theme that names it.** `defineComponent` records the
81
- component for the page, and `checkTheme` (and `createTheme`'s warnings) then check the
82
- theme's settings and parts for it, as they do the kit's.
83
- - **Type it in themes** by merging its declaration into `ComponentDefs`, so a wrong setting
84
- or part fails the typecheck:
85
-
86
- ```ts
87
- export const gaugeDef = defineComponent({ name: 'gauge', parts: ['root', 'needle'], settings: { radius: t.radius.lg } });
88
-
89
- declare module '@luxalgo/vela-ui/theme' {
90
- interface ComponentDefs {
91
- gauge: typeof gaugeDef;
92
- }
93
- }
94
-
95
- createTheme({ components: { gauge: { radius: '2px', css: { needle: css`color: ${t.accent};` } } } });
96
- ```
97
-
98
- ## Changing a component
99
-
100
- A component's public contract is what hosts and themes build on: its props and its handle's
101
- methods, its part names (the `data-slot` values), its variant and size values, the state
102
- attributes its CSS keys on, and its settings. Inside the file, everything else is free to
103
- change.
104
-
105
- - **Keep the contract.** A change to how a component looks or behaves keeps those names. Add
106
- a prop, a variant or a setting freely; give a new setting a default that keeps today's look,
107
- so a theme that sets nothing renders as before.
108
- - **Rename in two steps.** A renamed prop, part, variant or component keeps its old name
109
- working for one release: the component accepts both (and says which is deprecated in the
110
- prop's comment), and Vela's `src/ui/renamed.ts` keeps the old export names on
111
- `@luxalgo/vela/ui`. The changelog names the old and the new, and the release after removes
112
- the old one.
113
- - **Prove it.** The component's test covers the change; `npm run lint` checks its CSS; the
114
- gallery (`npm run playground`, then `/primitives.html`) shows every component in every
115
- variant, beside the theme editor, so compare it before and after. The build regenerates the
116
- manifest, the theme's JSON Schema and `llms.txt` from the file, so they follow on their
117
- own.
118
- - **Write it down.** A change users notice gets an entry in Vela's `CHANGELOG.md`, and a
119
- breaking one says what to do instead.
120
-
121
- ## The helpers (`src/lib/`)
122
-
123
- - **`h(tag, props, ...children)`** builds an element, typed by its tag. `slot` writes the
124
- `data-slot`, `data` the `data-*` attributes (`true` writes an empty value, `false` and
125
- `null` nothing), `attrs`, `on` and `text` the rest. Class names are layout hooks, never the
126
- styling contract.
127
- - **`css`** marks a template as CSS (editors highlight it, stylelint checks it) and
128
- **`defineStyles(id, text)`** returns the injector a component calls at creation, never at
129
- import: it declares the layer order once per document or shadow root, then injects the
130
- sheet, id-guarded. The sheet applies inside kit hosts, their floating layers and themed
131
- hosts only: a `:where()` joins the first compound of each top-level selector, adding no
132
- weight, so a page's own elements that carry the same `data-slot` names are never restyled.
133
- Inject a component's CSS through it, never by hand.
134
- - **`t`** reads the design tokens: `${t.radius.md}` prints `var(--vela-radius-md)`, and a
135
- name that does not exist fails the typecheck. A test checks that `t` and the tokens the
136
- theme writes match in both directions.
137
- - **`variants(spec)`** turns `{ variant: [...], size: [...] }` into the `data-*` attributes
138
- the CSS keys on, and types the props through `VariantProps`.
139
- - **`defineComponent({ name, parts, settings })`** links a name to its CSS: `slot(part)` is
140
- the part's `data-slot` (`name` for `root`, `name-part` otherwise), and `vars.<setting>`
141
- prints `var(--vela-<name>-<setting>, <default>)`.
142
- - **`addSlot(el, part)`** adds a part to an element's slot. A shell one component owns and
143
- another builds on carries both, `data-slot="popover-content select-content"`, and CSS
144
- matches either with `[data-slot~='…']`.
145
-
146
- ## CSS rules
147
-
148
- - Everything sits in **`@layer vela.ui`**, below unlayered CSS, so a page's own rule wins
149
- without `!important` or a more specific selector. Selectors start with one of the
150
- component's own parts (`[data-slot='gauge-needle']`), nest with `&`, and key on state
151
- attributes (`data-state`, `data-selected`, `aria-expanded`), never on classes.
152
- - A root that can be `hidden` takes its `display` under `:where(:not([hidden]))`: any
153
- authored `display` beats the browser's `[hidden]` rule. A rule that depends on an ancestor
154
- (the mobile layout) reads `&:is([data-layout='mobile'] *)`, which keeps the selector's
155
- weight.
156
- - Values come from `t` or the component's settings; durations from `t.dur`; directions are
157
- logical (`padding-inline`, `inset-inline-end`, `text-align: start`).
158
- - `npm run lint` (at the repo root) checks every template with stylelint and the postcss-lit
159
- syntax, through the package's shared config (`@luxalgo/vela-ui/stylelint`, which Vela's
160
- chrome, Pro and a host's own components extend with their own layer): the `vela/layer`,
161
- `vela/slot-scope`, `vela/token-values` and `vela/no-raw-token-var` rules, no hex or named
162
- colors, no class or id selectors, no `!important`, no
163
- `box-sizing: content-box` (the kit's border-box reset is unlayered: size the full box
164
- instead). A template the parser cannot read fails the run instead of going unchecked.
165
- - The kit supports the browsers that brought `color-mix()`, which its tints use: Chrome and
166
- Edge 111, Firefox 113, Safari 16.2. The build flattens the nesting for them (Lightning CSS),
167
- as nesting came later; the tests and the playground run the nested source.
168
- - Three rules stay outside the layer on purpose, each next to a comment saying why: the
169
- kit's border-box reset, the field rows' dimmed state, and the reset of a dragged dialog's
170
- offset in the mobile layout.
171
-
172
- ## Zag machines
173
-
174
- Wrap the machine in a controller function that maps the component's options to machine
175
- props, with an explicit return interface (TypeScript cannot name Zag's inferred types
176
- portably), then start it with `runMachine` and re-project the api's props on every
177
- notification with `spreadProps`:
178
-
179
- ```ts
180
- export function tooltipController(opts: TooltipControllerOptions = {}): TooltipController {
181
- return {
182
- machine: tooltip.machine,
183
- props: { id: nextUid('vela-tooltip'), /* map opts */ },
184
- connect: (service) => tooltip.connect(service, normalizeProps),
185
- };
186
- }
187
-
188
- // in the component class:
189
- this.handle = runMachine(ctrl.machine, ctrl.props, (service) => {
190
- const api = ctrl.connect(service);
191
- spreadProps(trigger, api.getTriggerProps(), mid);
192
- // …spread the other parts onto their elements
193
- });
194
- this.release = ownTeardown(trigger, () => this.handle.stop());
195
-
196
- destroy(): void {
197
- this.release();
198
- }
199
- ```
200
-
201
- Tie the machine's teardown to the element with `ownTeardown`, and have `destroy()` call the
202
- release: a host that discards a subtree with `disposeWithin` reaches controls whose handles it
203
- never kept, and the release runs once whichever comes first. A component whose content holds
204
- other controls (a dialog's panel, a popover's content) calls `disposeWithin` on it when it is
205
- destroyed.
206
-
207
- Form controls keep the value they show in a small state of their own (getters, `set*` /
208
- `sync` that do not emit, and the one method that does), so a host refresh rewrites the
209
- displayed value without firing `onChange`. The text fields are plain inputs; the checkbox, the
210
- switch, the number field and the select's list run Zag machines underneath, which that state
211
- drives (`setChecked`, `setValue`) without reporting.
212
-
213
- ## Rules (learned the hard way)
214
-
215
- - **Always pass a `machineId` to `spreadProps`** (the `mid` above). Two machines sharing one
216
- element under the default scope wipe each other's listeners.
217
- - **Zag trigger props overwrite the element's DOM `id`.** To compose several machines on one
218
- trigger, share the id through the machines' `ids` prop (see the `triggerId` option on
219
- Tooltip and DropdownMenu).
220
- - **Dialog-like machines expect conditional rendering**: their props carry no `hidden`;
221
- toggle visibility from `api.open` in your render (tooltip and menu props do carry it).
222
- - **Never hard-code colors.** The tokens derive from the theme (`src/lib/tokens.ts`), and the
223
- fixed brand and meaning colors live in `src/lib/palette.ts`. A test fails on a stray hex
224
- literal.
225
- - **Take icons from the registry** (`src/lib/icons.ts`) rather than inlining SVG: 16×16 at
226
- stroke 1.2 for UI chrome, 24×24 at stroke 1.8 for drawing tools, always `currentColor`.
227
- - **No imports from Vela.** The kit is used without the chart; what it needs lives in
228
- `src/lib/`.
229
- - **A machine must be stopped, not only removed.** Some machines register page-wide
230
- listeners (the switch's focus-visible tracking is one) that keep running, and keep their
231
- memory, after the element is gone. See `ownTeardown` above.
232
- - **State other copies of the kit must see goes through `pageState`** (`src/lib/page.ts`),
233
- not a module variable: a page can load the kit twice, and a registry, an id counter or an
234
- "open popover" kept per copy splits in two. Caches each copy can rebuild stay local.
235
- - Floating layers mount into the nearest `.vela-ui` host, which carries the theme; use the
236
- `vela-ui-layer` class on portaled positioners. Popovers call `ensureUIHost` on show, so a
237
- list portaled to the body still has the theme. A component built before its trigger is in
238
- the DOM needs an explicit `host`: the `closest()` fallback portals to the body, outside the
239
- theme.
240
- - Keyboard-facing components report open and close through an `onOpenChange` option, so a
241
- host can scope its keyboard shortcuts while they are open.
242
- - A component that opens and closes a surface **announces it** with `announceSurface`
243
- (`@luxalgo/vela-ui/surface-events`): open once the surface shows, close while it still
244
- shows (before a fade-out, `hidden`, or removal), so the event bubbles to the host.
245
- - After announcing the close, run the hide or removal through `holdForExit`
246
- (`@luxalgo/vela-ui/surface-exit`), so hosts can animate it out by styling `data-closing`. It
247
- completes synchronously when no exit animation starts; keep the returned handle to
248
- `cancel()` it on a reopen and `finish()` it on teardown.
249
- - The primitive's **root is one element**. Settings rows use `display: contents`, so a
250
- fragment of sibling nodes would fall onto the pane grid as extra tracks.
1
+ # Writing a component
2
+
3
+ Each component is **one file**, `src/components/<name>.ts`, published at
4
+ `@luxalgo/vela-ui/<name>`, holding its declaration, its variants, its CSS and its
5
+ `create<Name>` function. Overlays (menu, dialog, drawer, tooltip) and composite controls (tabs,
6
+ toggle group) run a [Zag.js](https://zagjs.com) state machine, which owns their keyboard
7
+ behavior and accessibility; form controls keep small vanilla state objects.
8
+
9
+ ## One file per component
10
+
11
+ Start one with `npm run new-component -- gauge` at the repo root: it writes the file in the
12
+ shape below, adds its declaration to the theme, and writes its test. A component file reads
13
+ top to bottom:
14
+
15
+ ```ts
16
+ // Gauge: … what it is and how it behaves, for the reader of the file.
17
+ import { css, defineStyles } from '../lib/css';
18
+ import { defineComponent } from '../lib/define';
19
+ import { h } from '../lib/h';
20
+ import { t } from '../lib/t';
21
+ import { variants, type VariantProps } from '../lib/variants';
22
+
23
+ // Its name, its parts (each part's data-slot) and its settings (custom properties a theme
24
+ // or a host's CSS can set, with their defaults).
25
+ export const gaugeDef = defineComponent({ name: 'gauge', parts: ['root', 'needle'], settings: { radius: t.radius.lg } });
26
+
27
+ // Its variants: the names type the props, the first value is the default.
28
+ export const gaugeVariants = variants({ size: ['md', 'sm'] });
29
+
30
+ export interface GaugeProps extends VariantProps<typeof gaugeVariants> {
31
+ value: number;
32
+ }
33
+
34
+ export interface GaugeHandle {
35
+ readonly el: HTMLElement;
36
+ setValue(value: number): void;
37
+ destroy(): void;
38
+ }
39
+
40
+ const injectStyles = defineStyles(
41
+ 'vela-ui-gauge',
42
+ css`
43
+ @layer vela.ui {
44
+ [data-slot='gauge'] {
45
+ border-radius: ${gaugeDef.vars.radius};
46
+ background: ${t.surface.sunken};
47
+
48
+ &[data-size='sm'] { … }
49
+ & > [data-slot='gauge-needle'] { color: ${t.accent}; }
50
+ }
51
+ }
52
+ `,
53
+ );
54
+
55
+ class GaugeImpl implements GaugeHandle { … } // methods shared across instances
56
+
57
+ export function createGauge(props: GaugeProps): GaugeHandle {
58
+ return new GaugeImpl(props);
59
+ }
60
+ ```
61
+
62
+ Then add the declaration to `ComponentDefs` in `src/lib/theme.ts`
63
+ (`gauge: typeof gaugeDef;`), which types the component's settings and parts in a theme. The
64
+ build fails while one is missing. The manifest (`dist/manifest.json`), the theme's JSON
65
+ Schema and `llms.txt` are generated from the file at build time, so there is nothing else to
66
+ list.
67
+
68
+ Released components keep their class (`new Checkbox(…)`, `new Tooltip(trigger, …)`) and their
69
+ controller (`checkboxController`, `tooltipController`) next to the `create…` function; the
70
+ class is the instance the function returns. New components start from the shape above.
71
+
72
+ ## A component of your own
73
+
74
+ The same shape works outside the kit, for a component a project writes: import the helpers
75
+ from the package root (`import { css, defineComponent, defineStyles, h, t, variants } from
76
+ '@luxalgo/vela-ui'`) and keep the rules below. Three things make it a full citizen:
77
+
78
+ - **Its CSS goes in `@layer vela.ui`**, like the kit's, not in `vela.custom`: a theme's part
79
+ CSS sits in `vela.custom`, and only from a later layer does it restyle the component.
80
+ - **Declare it before creating a theme that names it.** `defineComponent` records the
81
+ component for the page, and `checkTheme` (and `createTheme`'s warnings) then check the
82
+ theme's settings and parts for it, as they do the kit's.
83
+ - **Type it in themes** by merging its declaration into `ComponentDefs`, so a wrong setting
84
+ or part fails the typecheck:
85
+
86
+ ```ts
87
+ export const gaugeDef = defineComponent({ name: 'gauge', parts: ['root', 'needle'], settings: { radius: t.radius.lg } });
88
+
89
+ declare module '@luxalgo/vela-ui/theme' {
90
+ interface ComponentDefs {
91
+ gauge: typeof gaugeDef;
92
+ }
93
+ }
94
+
95
+ createTheme({ components: { gauge: { radius: '2px', css: { needle: css`color: ${t.accent};` } } } });
96
+ ```
97
+
98
+ ## Changing a component
99
+
100
+ A component's public contract is what hosts and themes build on: its props and its handle's
101
+ methods, its part names (the `data-slot` values), its variant and size values, the state
102
+ attributes its CSS keys on, and its settings. Inside the file, everything else is free to
103
+ change.
104
+
105
+ - **Keep the contract.** A change to how a component looks or behaves keeps those names. Add
106
+ a prop, a variant or a setting freely; give a new setting a default that keeps today's look,
107
+ so a theme that sets nothing renders as before.
108
+ - **Rename in two steps.** A renamed prop, part, variant or component keeps its old name
109
+ working for one release: the component accepts both (and says which is deprecated in the
110
+ prop's comment), and Vela's `src/ui/renamed.ts` keeps the old export names on
111
+ `@luxalgo/vela/ui`. The changelog names the old and the new, and the release after removes
112
+ the old one.
113
+ - **Prove it.** The component's test covers the change; `npm run lint` checks its CSS; the
114
+ gallery (`npm run playground`, then `/primitives.html`) shows every component in every
115
+ variant, beside the theme editor, so compare it before and after. The build regenerates the
116
+ manifest, the theme's JSON Schema and `llms.txt` from the file, so they follow on their
117
+ own.
118
+ - **Write it down.** A change users notice gets an entry in Vela's `CHANGELOG.md`, and a
119
+ breaking one says what to do instead.
120
+
121
+ ## The helpers (`src/lib/`)
122
+
123
+ - **`h(tag, props, ...children)`** builds an element, typed by its tag. `slot` writes the
124
+ `data-slot`, `data` the `data-*` attributes (`true` writes an empty value, `false` and
125
+ `null` nothing), `attrs`, `on` and `text` the rest. Class names are layout hooks, never the
126
+ styling contract.
127
+ - **`css`** marks a template as CSS (editors highlight it, stylelint checks it) and
128
+ **`defineStyles(id, text)`** returns the injector a component calls at creation, never at
129
+ import: it declares the layer order once per document or shadow root, then injects the
130
+ sheet, id-guarded. The sheet applies inside kit hosts, their floating layers and themed
131
+ hosts only: a `:where()` joins the first compound of each top-level selector, adding no
132
+ weight, so a page's own elements that carry the same `data-slot` names are never restyled.
133
+ Inject a component's CSS through it, never by hand.
134
+ - **`t`** reads the design tokens: `${t.radius.md}` prints `var(--vela-radius-md)`, and a
135
+ name that does not exist fails the typecheck. A test checks that `t` and the tokens the
136
+ theme writes match in both directions.
137
+ - **`variants(spec)`** turns `{ variant: [...], size: [...] }` into the `data-*` attributes
138
+ the CSS keys on, and types the props through `VariantProps`.
139
+ - **`defineComponent({ name, parts, settings })`** links a name to its CSS: `slot(part)` is
140
+ the part's `data-slot` (`name` for `root`, `name-part` otherwise), and `vars.<setting>`
141
+ prints `var(--vela-<name>-<setting>, <default>)`.
142
+ - **`addSlot(el, part)`** adds a part to an element's slot. A shell one component owns and
143
+ another builds on carries both, `data-slot="popover-content select-content"`, and CSS
144
+ matches either with `[data-slot~='…']`.
145
+
146
+ ## CSS rules
147
+
148
+ - Everything sits in **`@layer vela.ui`**, below unlayered CSS, so a page's own rule wins
149
+ without `!important` or a more specific selector. Selectors start with one of the
150
+ component's own parts (`[data-slot='gauge-needle']`), nest with `&`, and key on state
151
+ attributes (`data-state`, `data-selected`, `aria-expanded`), never on classes.
152
+ - A root that can be `hidden` takes its `display` under `:where(:not([hidden]))`: any
153
+ authored `display` beats the browser's `[hidden]` rule. A rule that depends on an ancestor
154
+ (the mobile layout) reads `&:is([data-layout='mobile'] *)`, which keeps the selector's
155
+ weight.
156
+ - Values come from `t` or the component's settings; durations from `t.dur`; directions are
157
+ logical (`padding-inline`, `inset-inline-end`, `text-align: start`).
158
+ - `npm run lint` (at the repo root) checks every template with stylelint and the postcss-lit
159
+ syntax, through the package's shared config (`@luxalgo/vela-ui/stylelint`, which Vela's
160
+ chrome, Pro and a host's own components extend with their own layer): the `vela/layer`,
161
+ `vela/slot-scope`, `vela/token-values` and `vela/no-raw-token-var` rules, no hex or named
162
+ colors, no class or id selectors, no `!important`, no
163
+ `box-sizing: content-box` (the kit's border-box reset is unlayered: size the full box
164
+ instead). A template the parser cannot read fails the run instead of going unchecked.
165
+ - The kit supports the browsers that brought `color-mix()`, which its tints use: Chrome and
166
+ Edge 111, Firefox 113, Safari 16.2. The build flattens the nesting for them (Lightning CSS),
167
+ as nesting came later; the tests and the playground run the nested source.
168
+ - Three rules stay outside the layer on purpose, each next to a comment saying why: the
169
+ kit's border-box reset, the field rows' dimmed state, and the reset of a dragged dialog's
170
+ offset in the mobile layout.
171
+
172
+ ## Zag machines
173
+
174
+ Wrap the machine in a controller function that maps the component's options to machine
175
+ props, with an explicit return interface (TypeScript cannot name Zag's inferred types
176
+ portably), then start it with `runMachine` and re-project the api's props on every
177
+ notification with `spreadProps`:
178
+
179
+ ```ts
180
+ export function tooltipController(opts: TooltipControllerOptions = {}): TooltipController {
181
+ return {
182
+ machine: tooltip.machine,
183
+ props: { id: nextUid('vela-tooltip'), /* map opts */ },
184
+ connect: (service) => tooltip.connect(service, normalizeProps),
185
+ };
186
+ }
187
+
188
+ // in the component class:
189
+ this.handle = runMachine(ctrl.machine, ctrl.props, (service) => {
190
+ const api = ctrl.connect(service);
191
+ spreadProps(trigger, api.getTriggerProps(), mid);
192
+ // …spread the other parts onto their elements
193
+ });
194
+ this.release = ownTeardown(trigger, () => this.handle.stop());
195
+
196
+ destroy(): void {
197
+ this.release();
198
+ }
199
+ ```
200
+
201
+ Tie the machine's teardown to the element with `ownTeardown`, and have `destroy()` call the
202
+ release: a host that discards a subtree with `disposeWithin` reaches controls whose handles it
203
+ never kept, and the release runs once whichever comes first. A component whose content holds
204
+ other controls (a dialog's panel, a popover's content) calls `disposeWithin` on it when it is
205
+ destroyed.
206
+
207
+ Form controls keep the value they show in a small state of their own (getters, `set*` /
208
+ `sync` that do not emit, and the one method that does), so a host refresh rewrites the
209
+ displayed value without firing `onChange`. The text fields are plain inputs; the checkbox, the
210
+ switch, the number field and the select's list run Zag machines underneath, which that state
211
+ drives (`setChecked`, `setValue`) without reporting.
212
+
213
+ ## Rules (learned the hard way)
214
+
215
+ - **Always pass a `machineId` to `spreadProps`** (the `mid` above). Two machines sharing one
216
+ element under the default scope wipe each other's listeners.
217
+ - **Zag trigger props overwrite the element's DOM `id`.** To compose several machines on one
218
+ trigger, share the id through the machines' `ids` prop (see the `triggerId` option on
219
+ Tooltip and DropdownMenu).
220
+ - **Dialog-like machines expect conditional rendering**: their props carry no `hidden`;
221
+ toggle visibility from `api.open` in your render (tooltip and menu props do carry it).
222
+ - **Never hard-code colors.** The tokens derive from the theme (`src/lib/tokens.ts`), and the
223
+ fixed brand and meaning colors live in `src/lib/palette.ts`. A test fails on a stray hex
224
+ literal.
225
+ - **Take icons from the registry** (`src/lib/icons.ts`) rather than inlining SVG: 16×16 at
226
+ stroke 1.2 for UI chrome, 24×24 at stroke 1.8 for drawing tools, always `currentColor`.
227
+ - **No imports from Vela.** The kit is used without the chart; what it needs lives in
228
+ `src/lib/`.
229
+ - **A machine must be stopped, not only removed.** Some machines register page-wide
230
+ listeners (the switch's focus-visible tracking is one) that keep running, and keep their
231
+ memory, after the element is gone. See `ownTeardown` above.
232
+ - **State other copies of the kit must see goes through `pageState`** (`src/lib/page.ts`),
233
+ not a module variable: a page can load the kit twice, and a registry, an id counter or an
234
+ "open popover" kept per copy splits in two. Caches each copy can rebuild stay local.
235
+ - Floating layers mount into the nearest `.vela-ui` host, which carries the theme; use the
236
+ `vela-ui-layer` class on portaled positioners. Popovers call `ensureUIHost` on show, so a
237
+ list portaled to the body still has the theme. A component built before its trigger is in
238
+ the DOM needs an explicit `host`: the `closest()` fallback portals to the body, outside the
239
+ theme.
240
+ - Keyboard-facing components report open and close through an `onOpenChange` option, so a
241
+ host can scope its keyboard shortcuts while they are open.
242
+ - A component that opens and closes a surface **announces it** with `announceSurface`
243
+ (`@luxalgo/vela-ui/surface-events`): open once the surface shows, close while it still
244
+ shows (before a fade-out, `hidden`, or removal), so the event bubbles to the host.
245
+ - After announcing the close, run the hide or removal through `holdForExit`
246
+ (`@luxalgo/vela-ui/surface-exit`), so hosts can animate it out by styling `data-closing`. It
247
+ completes synchronously when no exit animation starts; keep the returned handle to
248
+ `cancel()` it on a reopen and `finish()` it on teardown.
249
+ - The primitive's **root is one element**. Settings rows use `display: contents`, so a
250
+ fragment of sibling nodes would fall onto the pane grid as extra tracks.
package/llms.txt CHANGED
@@ -1,4 +1,4 @@
1
- # @luxalgo/vela-ui 0.2.0
1
+ # @luxalgo/vela-ui 0.4.0
2
2
 
3
3
  > The component kit of Vela: one file per component, Zag state machines for behavior, CSS in
4
4
  > cascade layers over design tokens, and one typed theme config.
@@ -11,7 +11,7 @@ kit host: `ensureUIHost(el, createTheme())` (`@luxalgo/vela-ui/theme`), or insid
11
11
  ## Skills (agent instructions in this package)
12
12
 
13
13
  - [theming-vela-ui](skills/theming-vela-ui/SKILL.md): Themes and restyles Vela charts and @luxalgo/vela-ui components with createTheme - brand colors, light, dark and custom themes, design tokens, component settings, part-by-part CSS restyles, scrollbars, cascade layers and theme JSON files checked with checkTheme.
14
- - [using-vela-ui-components](skills/using-vela-ui-components/SKILL.md): Builds interface pieces with @luxalgo/vela-ui, the component kit behind Vela charts - buttons, tabs, toggle groups, badges, dialogs, drawers, dropdown and context menus, tooltips, hover cards, popovers, collapsible sections, list rows, form controls (checkbox, switch, slider, input, select, number input, date and color pickers) and data components (data table, sort buttons, month grid, stat, sparkline, bar, line and scatter charts).
14
+ - [using-vela-ui-components](skills/using-vela-ui-components/SKILL.md): Builds interface pieces with @luxalgo/vela-ui, the component kit behind Vela charts - buttons, tabs, toggle groups, badges, dialogs, drawers, dropdown and context menus, tooltips, hover cards, popovers, toasts, collapsible sections, cards, list rows, form controls (checkbox, switch, slider, input, select, number input, date and color pickers) and data components (data table, sort buttons, month grid, figure, sparkline, bar, line and scatter charts, treemap).
15
15
  - [writing-vela-ui-components](skills/writing-vela-ui-components/SKILL.md): Writes new UI components with the @luxalgo/vela-ui authoring helpers (defineComponent, h, css, defineStyles, variants, t), customizes a kit component's markup or behavior by copying its source with "vela-ui add --source", and adds components to the kit itself.
16
16
 
17
17
  Link them into a project's coding agent with `npx skills experimental_sync` once the package is
@@ -36,6 +36,7 @@ installed, or install them from the repository with `npx skills add LuxAlgo/Vela
36
36
  - createBarChart (`@luxalgo/vela-ui/bar-chart`): BarChart: vertical bars around a zero line with a hover readout (daily P&L, a distribution), in the profit tone above zero and the loss tone below unless a bar names its own; optionally a value axis with gridlines and a title, slanted labels, and dashed markers.
37
37
  - createButton (`@luxalgo/vela-ui/button`): Button: a <button> with optional icons and key caps before and after its text (`↵ Open`, `Esc Close` in a picker's footer).
38
38
  - createCalloutBubble (`@luxalgo/vela-ui/callout-bubble`): CalloutBubble: a small tinted circle with a centered icon.
39
+ - createCard (`@luxalgo/vela-ui/card`): Card: a surface for one subject (a panel of figures, a chart, a summary): a header with an eyebrow over the title and a description under it, an action at its end (buttons, a toggle group), then the content and a footer past a line.
39
40
  - createCheckbox (`@luxalgo/vela-ui/checkbox`): Checkbox: a square check toggle, filled with a check mark when on.
40
41
  - createCollapsible (`@luxalgo/vela-ui/collapsible`): Collapsible: a section that shows or hides its content.
41
42
  - createColorField (`@luxalgo/vela-ui/color-picker`): ColorPicker: a swatch grid (grays, then hue × shade), a recents row with a custom "+" chooser, and an opacity slider over a transparency checker; and ColorField, the swatch button that opens it on a popover.
@@ -48,6 +49,7 @@ installed, or install them from the repository with `npx skills add LuxAlgo/Vela
48
49
  - createDropdownMenu (`@luxalgo/vela-ui/dropdown-menu`): DropdownMenu: MenuItemDescriptor[] projected into a floating list over Zag's menu machine (keyboard navigation, typeahead and dismissal belong to the machine).
49
50
  - createEmpty (`@luxalgo/vela-ui/empty`): Empty: what a panel shows when it has nothing to list: an icon in a tile, a title, a line saying why or what to do, and an optional action (a button that fills it).
50
51
  - FieldGridVariant (`@luxalgo/vela-ui/field`): Field: the label-and-control grid the settings dialogs lay their inputs out on: rows (a label and its control, an optional master toggle that dims the control, an optional info mark), full-width toggles, stacked and inline rows, section headings and separators, and a factory that builds a row's control from a descriptor.
52
+ - createFigure (`@luxalgo/vela-ui/figure`): Figure: a value under its label (Net P&L, Win rate), with an optional line under it.
51
53
  - createGlyphSelect (`@luxalgo/vela-ui/glyph-select`): GlyphSelect: a trigger showing the current option's glyph, and a list of glyph options on a popover.
52
54
  - createHoverCard (`@luxalgo/vela-ui/hover-card`): HoverCard: a card beside a control while the pointer rests on it, richer than a tooltip: a title with an icon tile and a shortcut drawn as keycaps, a description, rich content (figures, a sparkline) and a hint line.
53
55
  - createInputGroup (`@luxalgo/vela-ui/input-group`): InputGroup: an input with addons inside its border: an icon, a short text or a button, at its start or end, or in a row above or below it (a search field with its magnifier, an amount with its unit).
@@ -63,9 +65,10 @@ installed, or install them from the repository with `npx skills add LuxAlgo/Vela
63
65
  - createSlider (`@luxalgo/vela-ui/slider`): Slider: a value on a track, dragged by its thumb or moved with the arrow keys, Page Up/Down and Home/End, over Zag's slider machine.
64
66
  - createSortButton (`@luxalgo/vela-ui/sort-button`): SortButton: the header of a column that sorts a list which is not a table (a watchlist's Last and Day %).
65
67
  - createSparkline (`@luxalgo/vela-ui/sparkline`): Sparkline: a tiny line of values (a watchlist row's day, an equity curve in a card), in the profit tone when it ends at or above where it started (or its baseline) and the loss tone below, with an optional soft area under it and a dotted baseline (the previous close).
66
- - createStat (`@luxalgo/vela-ui/stat`): Stat: a figure under its label (Net P&L, Win rate), with an optional line under it.
67
68
  - createSwitch (`@luxalgo/vela-ui/switch`): Switch: a sliding on/off toggle, a track with a round thumb, for a setting that applies at once.
68
69
  - createTabs (`@luxalgo/vela-ui/tabs`): Tabs: a tab list over Zag's tabs machine (WAI-ARIA tabs: roving focus, the arrow keys along the orientation, Home/End, automatic or manual activation).
69
70
  - createTextArea (`@luxalgo/vela-ui/text-area`): TextArea: a multi-line text input that commits on blur, and can grow with its content up to a number of lines before it scrolls.
71
+ - createToaster (`@luxalgo/vela-ui/toast`): Toast: short notices that stack in a corner and leave on their own, shaped after shadcn's toast (Sonner).
70
72
  - createToggleGroup (`@luxalgo/vela-ui/toggle-group`): ToggleGroup: a few options side by side on one track, over Zag's toggle-group machine.
71
73
  - createTooltip (`@luxalgo/vela-ui/tooltip`): Tooltip: a floating label over Zag's tooltip machine (floating-ui placement).
74
+ - createTreemap (`@luxalgo/vela-ui/treemap`): Treemap: weighted items laid into a rectangle as tiles close to square, each tinted by its value on a diverging scale (up above zero, down below, stronger as the value grows) and labeled when it has room.