@luxalgo/vela-ui 0.3.0 → 0.4.1

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 (177) hide show
  1. package/CHANGELOG.md +165 -132
  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/button.js +2 -2
  8. package/dist/callout-bubble.js +3 -3
  9. package/dist/checkbox.js +3 -3
  10. package/dist/{chunk-OJ2SEMN6.js → chunk-5A54GODF.js} +2 -2
  11. package/dist/{chunk-UEO6W37Z.js → chunk-7LYWI32I.js} +2 -2
  12. package/dist/{chunk-IOOYDSSK.js → chunk-BIU66EHS.js} +2 -2
  13. package/dist/{chunk-KKS6NEGI.js → chunk-BWSOMG3G.js} +1 -1
  14. package/dist/{chunk-DXDCKZ2M.js → chunk-CZRHHVSB.js} +1 -1
  15. package/dist/{chunk-C3PXY2SW.js → chunk-EDYD5XKJ.js} +9 -0
  16. package/dist/chunk-GUIYFQ27.js +30 -0
  17. package/dist/{chunk-WWHNUKSZ.js → chunk-JEGB72M4.js} +8 -8
  18. package/dist/{chunk-B2EZB5ZG.js → chunk-ODPGOUQT.js} +4 -3
  19. package/dist/{chunk-TBXUF5YX.js → chunk-RH53NQGE.js} +18 -4
  20. package/dist/{chunk-OQXFQMZM.js → chunk-TGJNHUYV.js} +2 -2
  21. package/dist/collapsible.js +2 -2
  22. package/dist/color-picker.js +4 -4
  23. package/dist/context-menu.js +4 -4
  24. package/dist/date-picker.js +1 -1
  25. package/dist/dialog.js +4 -4
  26. package/dist/donut.d.ts +1 -1
  27. package/dist/drawer.js +1 -1
  28. package/dist/dropdown-menu.d.ts +2 -0
  29. package/dist/dropdown-menu.js +4 -4
  30. package/dist/empty.js +1 -1
  31. package/dist/field.js +9 -9
  32. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  33. package/dist/figure.js +41 -0
  34. package/dist/glyph-select.js +4 -4
  35. package/dist/hover-card.js +1 -1
  36. package/dist/icons.js +1 -1
  37. package/dist/index.d.ts +2 -2
  38. package/dist/index.js +1 -1
  39. package/dist/input-group.js +2 -2
  40. package/dist/item.js +1 -1
  41. package/dist/manifest.json +75 -74
  42. package/dist/number-input.js +3 -3
  43. package/dist/popover.js +2 -2
  44. package/dist/select.d.ts +5 -1
  45. package/dist/select.js +5 -5
  46. package/dist/slider.js +1 -1
  47. package/dist/sort-button.js +2 -2
  48. package/dist/switch.js +1 -1
  49. package/dist/tabs.js +2 -2
  50. package/dist/{theme-Ct-q58xz.d.ts → theme-TefB8vtl.d.ts} +2 -2
  51. package/dist/theme.d.ts +2 -2
  52. package/dist/theme.js +1 -1
  53. package/dist/theme.schema.json +30 -30
  54. package/dist/toast.d.ts +2 -2
  55. package/dist/toast.js +2 -2
  56. package/dist/toggle-group.js +2 -2
  57. package/dist/tooltip.js +1 -1
  58. package/dist/zag.d.ts +11 -2
  59. package/dist/zag.js +1 -1
  60. package/docs/theming.md +217 -217
  61. package/docs/what-to-change.md +20 -20
  62. package/docs/writing-a-component.md +250 -250
  63. package/llms.txt +3 -3
  64. package/package.json +90 -90
  65. package/registry/badge.ts +119 -119
  66. package/registry/bar-chart.ts +215 -215
  67. package/registry/button.ts +387 -387
  68. package/registry/callout-bubble.ts +347 -347
  69. package/registry/card.ts +379 -379
  70. package/registry/checkbox.ts +234 -234
  71. package/registry/collapsible.ts +271 -271
  72. package/registry/color-picker.ts +582 -582
  73. package/registry/context-menu.ts +208 -208
  74. package/registry/data-table.ts +215 -215
  75. package/registry/date-picker.ts +483 -483
  76. package/registry/dialog.ts +551 -551
  77. package/registry/donut.ts +145 -145
  78. package/registry/drawer.ts +636 -636
  79. package/registry/dropdown-menu.ts +765 -762
  80. package/registry/empty.ts +91 -91
  81. package/registry/field.ts +584 -584
  82. package/registry/{stat.ts → figure.ts} +96 -96
  83. package/registry/glyph-select.ts +268 -268
  84. package/registry/hover-card.ts +222 -222
  85. package/registry/input-group.ts +229 -229
  86. package/registry/input.ts +181 -181
  87. package/registry/item.ts +376 -376
  88. package/registry/kbd.ts +64 -64
  89. package/registry/line-chart.ts +218 -218
  90. package/registry/month-grid.ts +328 -328
  91. package/registry/number-input.ts +443 -443
  92. package/registry/popover.ts +368 -368
  93. package/registry/scatter-chart.ts +221 -221
  94. package/registry/select.ts +629 -620
  95. package/registry/slider.ts +259 -259
  96. package/registry/sort-button.ts +189 -189
  97. package/registry/sparkline.ts +114 -114
  98. package/registry/switch.ts +205 -205
  99. package/registry/tabs.ts +426 -426
  100. package/registry/text-area.ts +186 -186
  101. package/registry/toast.ts +965 -965
  102. package/registry/toggle-group.ts +295 -295
  103. package/registry/tooltip.ts +156 -156
  104. package/registry/treemap.ts +847 -847
  105. package/skills/theming-vela-ui/SKILL.md +137 -137
  106. package/skills/using-vela-ui-components/SKILL.md +141 -139
  107. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  108. package/src/components/badge.ts +119 -119
  109. package/src/components/bar-chart.ts +215 -215
  110. package/src/components/button.ts +387 -387
  111. package/src/components/callout-bubble.ts +347 -347
  112. package/src/components/card.ts +379 -379
  113. package/src/components/checkbox.ts +234 -234
  114. package/src/components/collapsible.ts +271 -271
  115. package/src/components/color-picker.ts +582 -582
  116. package/src/components/context-menu.ts +208 -208
  117. package/src/components/data-table.ts +215 -215
  118. package/src/components/date-picker.ts +483 -483
  119. package/src/components/dialog.ts +551 -551
  120. package/src/components/donut.ts +145 -145
  121. package/src/components/drawer.ts +636 -636
  122. package/src/components/dropdown-menu.ts +765 -762
  123. package/src/components/empty.ts +91 -91
  124. package/src/components/field.ts +584 -584
  125. package/src/components/{stat.ts → figure.ts} +96 -96
  126. package/src/components/glyph-select.ts +268 -268
  127. package/src/components/hover-card.ts +222 -222
  128. package/src/components/input-group.ts +229 -229
  129. package/src/components/input.ts +181 -181
  130. package/src/components/item.ts +376 -376
  131. package/src/components/kbd.ts +64 -64
  132. package/src/components/line-chart.ts +218 -218
  133. package/src/components/month-grid.ts +328 -328
  134. package/src/components/number-input.ts +443 -443
  135. package/src/components/popover.ts +368 -368
  136. package/src/components/scatter-chart.ts +221 -221
  137. package/src/components/select.ts +629 -620
  138. package/src/components/slider.ts +259 -259
  139. package/src/components/sort-button.ts +189 -189
  140. package/src/components/sparkline.ts +114 -114
  141. package/src/components/switch.ts +205 -205
  142. package/src/components/tabs.ts +426 -426
  143. package/src/components/text-area.ts +186 -186
  144. package/src/components/toast.ts +965 -965
  145. package/src/components/toggle-group.ts +295 -295
  146. package/src/components/tooltip.ts +156 -156
  147. package/src/components/treemap.ts +847 -847
  148. package/src/index.ts +24 -24
  149. package/src/lib/calendar.ts +117 -117
  150. package/src/lib/chart.ts +72 -72
  151. package/src/lib/color.ts +89 -89
  152. package/src/lib/component-index.ts +163 -163
  153. package/src/lib/css.ts +48 -48
  154. package/src/lib/define.ts +45 -45
  155. package/src/lib/dispose.ts +36 -36
  156. package/src/lib/h.ts +55 -55
  157. package/src/lib/headless.ts +16 -16
  158. package/src/lib/i18n.ts +30 -30
  159. package/src/lib/icons.ts +253 -244
  160. package/src/lib/kit-text.ts +26 -26
  161. package/src/lib/page.ts +24 -24
  162. package/src/lib/palette.ts +81 -81
  163. package/src/lib/placement.ts +188 -188
  164. package/src/lib/scope.ts +135 -135
  165. package/src/lib/styles.ts +53 -53
  166. package/src/lib/surface-events.ts +31 -31
  167. package/src/lib/surface-exit.ts +87 -87
  168. package/src/lib/t.ts +68 -68
  169. package/src/lib/theme.ts +684 -670
  170. package/src/lib/tokens.ts +110 -110
  171. package/src/lib/variants.ts +32 -32
  172. package/src/lib/zag.ts +61 -46
  173. package/stylelint/index.mjs +60 -60
  174. package/stylelint/lint.mjs +81 -81
  175. package/stylelint/plugin.mjs +115 -115
  176. package/dist/chunk-P4MTEMT7.js +0 -23
  177. package/dist/stat.js +0 -41
@@ -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.3.0
1
+ # @luxalgo/vela-ui 0.4.1
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, 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, stat, sparkline, bar, line and scatter charts, treemap).
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
@@ -49,6 +49,7 @@ installed, or install them from the repository with `npx skills add LuxAlgo/Vela
49
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).
50
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).
51
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.
52
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.
53
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.
54
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).
@@ -64,7 +65,6 @@ installed, or install them from the repository with `npx skills add LuxAlgo/Vela
64
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.
65
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 %).
66
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).
67
- - createStat (`@luxalgo/vela-ui/stat`): Stat: a figure under its label (Net P&L, Win rate), with an optional line under it.
68
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.
69
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).
70
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.