@luxalgo/vela-ui 0.0.0-stage → 0.2.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 (269) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +28 -0
  4. package/README.md +86 -3
  5. package/cli/engine.mjs +282 -0
  6. package/cli/index.mjs +154 -0
  7. package/dist/badge.d.ts +26 -0
  8. package/dist/badge.js +44 -0
  9. package/dist/bar-chart.d.ts +40 -0
  10. package/dist/bar-chart.js +105 -0
  11. package/dist/button.d.ts +55 -0
  12. package/dist/button.js +11 -0
  13. package/dist/calendar.d.ts +24 -0
  14. package/dist/calendar.js +1 -0
  15. package/dist/callout-bubble.d.ts +103 -0
  16. package/dist/callout-bubble.js +177 -0
  17. package/dist/chart.d.ts +30 -0
  18. package/dist/chart.js +1 -0
  19. package/dist/checkbox.d.ts +60 -0
  20. package/dist/checkbox.js +12 -0
  21. package/dist/chunk-2GQIZM5Z.js +28 -0
  22. package/dist/chunk-3Q32K4PE.js +85 -0
  23. package/dist/chunk-6QCILIT2.js +52 -0
  24. package/dist/chunk-7VMN7NKJ.js +133 -0
  25. package/dist/chunk-A2FJ7Y7D.js +55 -0
  26. package/dist/chunk-A72NWYNP.js +26 -0
  27. package/dist/chunk-B5ZSRUDQ.js +227 -0
  28. package/dist/chunk-BXMWQIBO.js +66 -0
  29. package/dist/chunk-C3ICYMI3.js +25 -0
  30. package/dist/chunk-CC3DDNFC.js +117 -0
  31. package/dist/chunk-E2FE6V2N.js +81 -0
  32. package/dist/chunk-FN2BUFNM.js +10 -0
  33. package/dist/chunk-FVH6L7FM.js +18 -0
  34. package/dist/chunk-G7IQLY3A.js +183 -0
  35. package/dist/chunk-GVRX3UAV.js +82 -0
  36. package/dist/chunk-J2QJATCD.js +12 -0
  37. package/dist/chunk-LZWZV2ZS.js +18 -0
  38. package/dist/chunk-MJ4RUGX7.js +403 -0
  39. package/dist/chunk-MMTMOV6R.js +31 -0
  40. package/dist/chunk-P4MTEMT7.js +23 -0
  41. package/dist/chunk-Q6WTDVH6.js +31 -0
  42. package/dist/chunk-S7USVSYP.js +50 -0
  43. package/dist/chunk-TAKSIV57.js +90 -0
  44. package/dist/chunk-U3EH42YY.js +72 -0
  45. package/dist/chunk-V2Q4MRMS.js +573 -0
  46. package/dist/chunk-VMCR3S2A.js +22 -0
  47. package/dist/chunk-WBGSYCV2.js +101 -0
  48. package/dist/chunk-WGULXRWD.js +351 -0
  49. package/dist/chunk-WXH3EFV5.js +219 -0
  50. package/dist/chunk-X66RJAIS.js +14 -0
  51. package/dist/chunk-XBEL4K33.js +142 -0
  52. package/dist/chunk-Z5PINFQO.js +280 -0
  53. package/dist/collapsible.d.ts +49 -0
  54. package/dist/collapsible.js +106 -0
  55. package/dist/color-picker.d.ts +103 -0
  56. package/dist/color-picker.js +19 -0
  57. package/dist/color.d.ts +24 -0
  58. package/dist/color.js +1 -0
  59. package/dist/context-menu.d.ts +72 -0
  60. package/dist/context-menu.js +161 -0
  61. package/dist/css.d.ts +17 -0
  62. package/dist/css.js +4 -0
  63. package/dist/data-table.d.ts +40 -0
  64. package/dist/data-table.js +88 -0
  65. package/dist/date-picker.d.ts +90 -0
  66. package/dist/date-picker.js +257 -0
  67. package/dist/define-D5u3F5Dj.d.ts +21 -0
  68. package/dist/dialog.d.ts +89 -0
  69. package/dist/dialog.js +258 -0
  70. package/dist/dispose.d.ts +9 -0
  71. package/dist/dispose.js +2 -0
  72. package/dist/donut.d.ts +27 -0
  73. package/dist/donut.js +67 -0
  74. package/dist/drawer.d.ts +119 -0
  75. package/dist/drawer.js +326 -0
  76. package/dist/dropdown-menu.d.ts +114 -0
  77. package/dist/dropdown-menu.js +18 -0
  78. package/dist/empty.d.ts +21 -0
  79. package/dist/empty.js +38 -0
  80. package/dist/field.d.ts +147 -0
  81. package/dist/field.js +328 -0
  82. package/dist/glyph-select.d.ts +57 -0
  83. package/dist/glyph-select.js +17 -0
  84. package/dist/h-Bl37qUis.d.ts +24 -0
  85. package/dist/headless.d.ts +6 -0
  86. package/dist/headless.js +9 -0
  87. package/dist/hover-card.d.ts +43 -0
  88. package/dist/hover-card.js +97 -0
  89. package/dist/i18n.d.ts +20 -0
  90. package/dist/i18n.js +1 -0
  91. package/dist/icons.d.ts +20 -0
  92. package/dist/icons.js +2 -0
  93. package/dist/index.d.ts +51 -0
  94. package/dist/index.js +13 -0
  95. package/dist/input-group.d.ts +57 -0
  96. package/dist/input-group.js +87 -0
  97. package/dist/input.d.ts +54 -0
  98. package/dist/input.js +9 -0
  99. package/dist/item.d.ts +72 -0
  100. package/dist/item.js +131 -0
  101. package/dist/kbd.d.ts +19 -0
  102. package/dist/kbd.js +8 -0
  103. package/dist/kit-text.d.ts +8 -0
  104. package/dist/kit-text.js +2 -0
  105. package/dist/line-chart.d.ts +32 -0
  106. package/dist/line-chart.js +120 -0
  107. package/dist/manifest.json +4508 -0
  108. package/dist/month-grid.d.ts +45 -0
  109. package/dist/month-grid.js +157 -0
  110. package/dist/number-input.d.ts +99 -0
  111. package/dist/number-input.js +14 -0
  112. package/dist/page.d.ts +7 -0
  113. package/dist/page.js +1 -0
  114. package/dist/palette.d.ts +55 -0
  115. package/dist/palette.js +1 -0
  116. package/dist/placement.d.ts +98 -0
  117. package/dist/placement.js +1 -0
  118. package/dist/popover.d.ts +107 -0
  119. package/dist/popover.js +15 -0
  120. package/dist/scatter-chart.d.ts +32 -0
  121. package/dist/scatter-chart.js +121 -0
  122. package/dist/select.d.ts +112 -0
  123. package/dist/select.js +19 -0
  124. package/dist/slider.d.ts +46 -0
  125. package/dist/slider.js +111 -0
  126. package/dist/sort-button.d.ts +41 -0
  127. package/dist/sort-button.js +88 -0
  128. package/dist/sparkline.d.ts +22 -0
  129. package/dist/sparkline.js +72 -0
  130. package/dist/stat.d.ts +29 -0
  131. package/dist/stat.js +41 -0
  132. package/dist/styles.d.ts +22 -0
  133. package/dist/styles.js +2 -0
  134. package/dist/surface-events.d.ts +17 -0
  135. package/dist/surface-events.js +1 -0
  136. package/dist/surface-exit.d.ts +25 -0
  137. package/dist/surface-exit.js +1 -0
  138. package/dist/switch.d.ts +54 -0
  139. package/dist/switch.js +92 -0
  140. package/dist/t.d.ts +89 -0
  141. package/dist/t.js +1 -0
  142. package/dist/tabs.d.ts +65 -0
  143. package/dist/tabs.js +155 -0
  144. package/dist/text-area.d.ts +54 -0
  145. package/dist/text-area.js +9 -0
  146. package/dist/theme.d.ts +199 -0
  147. package/dist/theme.js +8 -0
  148. package/dist/theme.schema.json +1994 -0
  149. package/dist/themes/dark.css +3 -0
  150. package/dist/themes/light.css +3 -0
  151. package/dist/toggle-group.d.ts +56 -0
  152. package/dist/toggle-group.js +130 -0
  153. package/dist/tokens.d.ts +16 -0
  154. package/dist/tokens.js +3 -0
  155. package/dist/tooltip.d.ts +47 -0
  156. package/dist/tooltip.js +74 -0
  157. package/dist/variants-B4stUBnY.d.ts +16 -0
  158. package/dist/zag.d.ts +19 -0
  159. package/dist/zag.js +2 -0
  160. package/docs/theming.md +217 -0
  161. package/docs/what-to-change.md +20 -0
  162. package/docs/writing-a-component.md +250 -0
  163. package/llms.txt +71 -0
  164. package/package.json +90 -6
  165. package/registry/badge.ts +119 -0
  166. package/registry/bar-chart.ts +215 -0
  167. package/registry/button.ts +379 -0
  168. package/registry/callout-bubble.ts +347 -0
  169. package/registry/checkbox.ts +234 -0
  170. package/registry/collapsible.ts +271 -0
  171. package/registry/color-picker.ts +582 -0
  172. package/registry/context-menu.ts +208 -0
  173. package/registry/data-table.ts +215 -0
  174. package/registry/date-picker.ts +483 -0
  175. package/registry/dialog.ts +535 -0
  176. package/registry/donut.ts +145 -0
  177. package/registry/drawer.ts +636 -0
  178. package/registry/dropdown-menu.ts +762 -0
  179. package/registry/empty.ts +91 -0
  180. package/registry/field.ts +585 -0
  181. package/registry/glyph-select.ts +268 -0
  182. package/registry/hover-card.ts +222 -0
  183. package/registry/input-group.ts +229 -0
  184. package/registry/input.ts +181 -0
  185. package/registry/item.ts +376 -0
  186. package/registry/kbd.ts +64 -0
  187. package/registry/line-chart.ts +218 -0
  188. package/registry/month-grid.ts +328 -0
  189. package/registry/number-input.ts +443 -0
  190. package/registry/popover.ts +368 -0
  191. package/registry/scatter-chart.ts +221 -0
  192. package/registry/select.ts +620 -0
  193. package/registry/slider.ts +259 -0
  194. package/registry/sort-button.ts +189 -0
  195. package/registry/sparkline.ts +114 -0
  196. package/registry/stat.ts +96 -0
  197. package/registry/switch.ts +205 -0
  198. package/registry/tabs.ts +426 -0
  199. package/registry/text-area.ts +186 -0
  200. package/registry/toggle-group.ts +295 -0
  201. package/registry/tooltip.ts +156 -0
  202. package/skills/theming-vela-ui/SKILL.md +137 -0
  203. package/skills/using-vela-ui-components/SKILL.md +137 -0
  204. package/skills/writing-vela-ui-components/SKILL.md +163 -0
  205. package/src/components/badge.ts +119 -0
  206. package/src/components/bar-chart.ts +215 -0
  207. package/src/components/button.ts +379 -0
  208. package/src/components/callout-bubble.ts +347 -0
  209. package/src/components/checkbox.ts +234 -0
  210. package/src/components/collapsible.ts +271 -0
  211. package/src/components/color-picker.ts +582 -0
  212. package/src/components/context-menu.ts +208 -0
  213. package/src/components/data-table.ts +215 -0
  214. package/src/components/date-picker.ts +483 -0
  215. package/src/components/dialog.ts +535 -0
  216. package/src/components/donut.ts +145 -0
  217. package/src/components/drawer.ts +636 -0
  218. package/src/components/dropdown-menu.ts +762 -0
  219. package/src/components/empty.ts +91 -0
  220. package/src/components/field.ts +585 -0
  221. package/src/components/glyph-select.ts +268 -0
  222. package/src/components/hover-card.ts +222 -0
  223. package/src/components/input-group.ts +229 -0
  224. package/src/components/input.ts +181 -0
  225. package/src/components/item.ts +376 -0
  226. package/src/components/kbd.ts +64 -0
  227. package/src/components/line-chart.ts +218 -0
  228. package/src/components/month-grid.ts +328 -0
  229. package/src/components/number-input.ts +443 -0
  230. package/src/components/popover.ts +368 -0
  231. package/src/components/scatter-chart.ts +221 -0
  232. package/src/components/select.ts +620 -0
  233. package/src/components/slider.ts +259 -0
  234. package/src/components/sort-button.ts +189 -0
  235. package/src/components/sparkline.ts +114 -0
  236. package/src/components/stat.ts +96 -0
  237. package/src/components/switch.ts +205 -0
  238. package/src/components/tabs.ts +426 -0
  239. package/src/components/text-area.ts +186 -0
  240. package/src/components/toggle-group.ts +295 -0
  241. package/src/components/tooltip.ts +156 -0
  242. package/src/index.ts +24 -0
  243. package/src/lib/calendar.ts +117 -0
  244. package/src/lib/chart.ts +72 -0
  245. package/src/lib/color.ts +89 -0
  246. package/src/lib/component-index.ts +151 -0
  247. package/src/lib/css.ts +48 -0
  248. package/src/lib/define.ts +45 -0
  249. package/src/lib/dispose.ts +36 -0
  250. package/src/lib/h.ts +55 -0
  251. package/src/lib/headless.ts +16 -0
  252. package/src/lib/i18n.ts +30 -0
  253. package/src/lib/icons.ts +237 -0
  254. package/src/lib/kit-text.ts +26 -0
  255. package/src/lib/page.ts +24 -0
  256. package/src/lib/palette.ts +81 -0
  257. package/src/lib/placement.ts +188 -0
  258. package/src/lib/scope.ts +135 -0
  259. package/src/lib/styles.ts +53 -0
  260. package/src/lib/surface-events.ts +31 -0
  261. package/src/lib/surface-exit.ts +87 -0
  262. package/src/lib/t.ts +68 -0
  263. package/src/lib/theme.ts +664 -0
  264. package/src/lib/tokens.ts +111 -0
  265. package/src/lib/variants.ts +32 -0
  266. package/src/lib/zag.ts +46 -0
  267. package/stylelint/index.mjs +60 -0
  268. package/stylelint/lint.mjs +81 -0
  269. package/stylelint/plugin.mjs +115 -0
@@ -0,0 +1,137 @@
1
+ ---
2
+ name: theming-vela-ui
3
+ description: 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. Use when the user wants a chart or its menus, dialogs and controls to match a brand or design system, asks for a new theme, a color or radius change, a component restyle or a CSS override that does not stick, even if they only say "make the chart look like our app".
4
+ license: Apache-2.0
5
+ ---
6
+
7
+ # Theming Vela and vela-ui
8
+
9
+ A theme is data built by `createTheme`. One theme reaches the chart's canvas (candles, grid,
10
+ axis text), Vela's chrome (toolbars, menus, dialogs) and every kit component.
11
+
12
+ ## Where a change belongs
13
+
14
+ Pick the first row that can express the change; each later row is more specific and more
15
+ work to maintain.
16
+
17
+ | To change | Use |
18
+ | --- | --- |
19
+ | Background, text, candles, grid | `createTheme({ colors: { background, text, grid, border, up, down } })`; light or dark is read from the background |
20
+ | Brand color, corner roundness, font | `accent` (the same choice as `colors.accent`; the top-level one wins), `radius` (`none`, `sm`, `md`, `lg`), `font` |
21
+ | One design token | `tokens: { 'surface-elev': '#1b1c22' }` (name without `--vela-`) |
22
+ | One component's sizes, radius or colors | `components: { button: { radius: '2px' } }` (its settings) |
23
+ | A component's whole look | ``components: { button: { css: { root: css`…`, icon: css`…` } } }`` (its parts) |
24
+ | Scrollbars | tokens `scroll` (thumb color) and `scroll-size` (width) |
25
+
26
+ Token names, layers and the full rules are in `node_modules/@luxalgo/vela-ui/docs/theming.md`.
27
+ A component's settings, parts and the state attributes its CSS keys on are in
28
+ `node_modules/@luxalgo/vela-ui/dist/manifest.json` (`settings`, `parts`, `attributes`).
29
+
30
+ ## Workflow
31
+
32
+ Copy this checklist and tick it off:
33
+
34
+ ```
35
+ - [ ] 1. Start from colors: createTheme({ colors, accent, radius, font })
36
+ - [ ] 2. Adjust single tokens only where the derived ones miss
37
+ - [ ] 3. Use component settings before part CSS
38
+ - [ ] 4. Write part CSS with the css tag, against parts and data attributes
39
+ - [ ] 5. Run checkTheme(input) and fix every problem it names
40
+ - [ ] 6. Apply it (Vela: theme option or setTheme; elsewhere: ensureUIHost)
41
+ - [ ] 7. Check computed styles in the browser, menus and dialogs opened
42
+ ```
43
+
44
+ ## Applying a theme
45
+
46
+ In a Vela project, the theme functions come from `@luxalgo/vela`, and the `css` tag and the
47
+ tokens `t` from `@luxalgo/vela/ui`. In a project of components only, all of them come from
48
+ `@luxalgo/vela-ui/theme` (`css` and `t` from `@luxalgo/vela-ui`).
49
+
50
+ ```ts
51
+ import { checkTheme, createTheme, registerTheme, type ThemeInput } from '@luxalgo/vela';
52
+ import { VelaWorkspace } from '@luxalgo/vela/workspace';
53
+
54
+ const input: ThemeInput = { colors: { background: '#0f1117', text: '#c9d1d9' }, accent: '#7c3aed', radius: 'md' };
55
+ const problems = checkTheme(input); // [] when every name is right; runs without a page, so in tests too
56
+ if (problems.length) console.warn(problems);
57
+ const brand = createTheme(input);
58
+
59
+ registerTheme({ id: 'brand', label: 'Brand', theme: brand }); // optional: lists it in chart settings
60
+ new VelaWorkspace('#chart', { symbol: 'BTCUSDT', theme: brand }); // or ws.setTheme(brand) later
61
+ ```
62
+
63
+ On a page of components without a chart: `ensureUIHost(el, theme)` from
64
+ `@luxalgo/vela-ui/theme`. `applyTheme(el, theme)` only switches a host that already is one.
65
+
66
+ ## Restyling a component by its parts
67
+
68
+ ```ts
69
+ import { createTheme, css, t } from '@luxalgo/vela/ui'; // or '@luxalgo/vela-ui'
70
+
71
+ const theme = createTheme({
72
+ components: {
73
+ button: {
74
+ radius: '999px',
75
+ css: {
76
+ root: css`
77
+ text-transform: uppercase;
78
+ &[data-variant='outline'] { border-width: 2px; }
79
+ &:not(:disabled):hover { box-shadow: ${t.shadow}; }
80
+ `,
81
+ },
82
+ },
83
+ },
84
+ });
85
+ ```
86
+
87
+ - **Write it with the `css` tag.** CSS not written with the tag (a theme loaded from JSON) is
88
+ dropped unless you pass `createTheme(input, { allowCss: true })`, and only do that for CSS
89
+ you trust. A plain string still typechecks: `checkTheme` is what catches a missing tag.
90
+ - **A part's CSS overrides the component's own declarations it repeats.** A `background`
91
+ shorthand on a fill erases the color the component sets for its state: add to it
92
+ (`background-image`) or key the rule on the state attribute.
93
+ - **`&` is the part.** Key states on its data attributes (`data-state`, `data-selected`,
94
+ `data-highlighted`, `data-variant`, `data-size`), never on class names, never `!important`.
95
+ - **A state can live on another part.** The switch's `data-state` sits on its root, so style
96
+ the track from the root: ``root: css`&[data-state='checked'] [data-slot~='switch-control'] { … }` ``.
97
+ - **One level of nesting**, plus `@media`, `@supports` and `@container`. Declare `@keyframes`
98
+ and `@font-face` in the page's stylesheet and use them by name.
99
+
100
+ ## Gotchas
101
+
102
+ - **A misspelled component, setting or part does nothing.** `checkTheme(input)` names it,
103
+ and `createTheme` warns about it once in the console. Run it on every theme you write.
104
+ - **`components.button` restyles the kit's buttons** (in dialogs, panels, your own UI). Vela's
105
+ toolbars are its own chrome: tokens reach them, component settings do not.
106
+ - **Tokens derived from a `var()` cannot be computed.** When a color comes from the host's
107
+ CSS variables (`accent: 'var(--primary)'`), also set the tokens derived from it, such as
108
+ `'focus-soft'` and `'fg-on-fill'`.
109
+ - **Page CSS outside any layer beats the whole kit.** A global reset such as
110
+ `button { background: none; }` now restyles the kit's buttons too: scope it with
111
+ `button:where(:not(.vela-ui *))` or move it into a layer declared first (`@layer reset;`).
112
+ - **Native scrollbars inside the chart** mean the page sets `scrollbar-width` or
113
+ `scrollbar-color` on every element; reset them for `.vela-ui, .vela-ui *`.
114
+ - **Your own components** put their base styles in `@layer vela.ui` so the theme's part CSS
115
+ (in `vela.custom`) can restyle them; see the `writing-vela-ui-components` skill.
116
+
117
+ ## Themes as JSON files
118
+
119
+ A theme of colors, tokens and settings is plain JSON. Point it at the schema so editors and
120
+ CI validate it, then load it with `createTheme(json)`:
121
+
122
+ ```json
123
+ {
124
+ "$schema": "./node_modules/@luxalgo/vela-ui/dist/theme.schema.json",
125
+ "colors": { "background": "#fdf6e3", "text": "#586e75", "accent": "#268bd2" },
126
+ "radius": "none"
127
+ }
128
+ ```
129
+
130
+ `npx @luxalgo/vela-ui check` validates the file named by `theme` in `vela-ui.json` at the
131
+ project root.
132
+
133
+ ## Check your work
134
+
135
+ 1. `checkTheme(input)` returns an empty list.
136
+ 2. In the browser, read the result, not the source: `getComputedStyle(el)` on a restyled
137
+ part, with menus and dialogs opened, since they only exist while open.
@@ -0,0 +1,137 @@
1
+ ---
2
+ name: using-vela-ui-components
3
+ description: 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). Use when adding or wiring these components on a page or next to a Vela chart, when a menu, dialog or tooltip from the kit renders unstyled or in the wrong place, or when the user wants UI that matches their Vela chart, even if they do not name the kit.
4
+ license: Apache-2.0
5
+ ---
6
+
7
+ # Using vela-ui components
8
+
9
+ ## Make a kit host first
10
+
11
+ Components take their theme and base styles from the nearest kit host, and floating parts
12
+ (menus, popovers, select lists, tooltips, dialogs) mount into it. On a page of your own:
13
+
14
+ ```ts
15
+ import { createTheme, ensureUIHost } from '@luxalgo/vela-ui/theme';
16
+
17
+ const app = document.getElementById('app')!;
18
+ ensureUIHost(app, createTheme()); // dark by default; the theming-vela-ui skill covers the rest
19
+ ```
20
+
21
+ Next to a Vela chart, the chart's root already is a kit host: mount inside it, or pass a
22
+ plugin's `ctx.host` as `host`. Use `ensureUIHost`, not `applyTheme`, to set a host up:
23
+ `applyTheme` only themes an element, so a menu opened from it mounts unthemed in `<body>`.
24
+
25
+ ## One shape for every component
26
+
27
+ `create<Name>(props)` from `@luxalgo/vela-ui/<name>` returns a handle with `el` and
28
+ `destroy()`. Append `el` where it belongs.
29
+
30
+ ```ts
31
+ import { createButton } from '@luxalgo/vela-ui/button';
32
+
33
+ const save = createButton({ text: 'Save', variant: 'default', size: 'sm', onClick: () => submit() });
34
+ app.append(save.el);
35
+ ```
36
+
37
+ To find a component, its props, parts and an example, search
38
+ `node_modules/@luxalgo/vela-ui/dist/manifest.json` for `"name": "<name>"`, or run
39
+ `npx @luxalgo/vela-ui list`. Pick by need:
40
+
41
+ | Need | Component |
42
+ | --- | --- |
43
+ | An action | `button` (variants `default`, `outline`, `secondary`, `ghost`, `destructive`, `link`, `up`, `down`, `favorite`; `keys` for key caps) |
44
+ | Switch between views | `tabs` (a `description` puts a second line under a label); a few exclusive options on one track: `toggle-group` |
45
+ | A status or a count | `badge`, `stat`, `kbd` |
46
+ | A list of actions from a button or a right-click | `dropdown-menu`, `context-menu` |
47
+ | A label or a card on hover | `tooltip`, `hover-card` |
48
+ | A modal or a sheet | `dialog`, `drawer` |
49
+ | Floating content of your own | `popover` |
50
+ | A section that folds | `collapsible` |
51
+ | Rows of a list (a watchlist, alerts, results) | `item`, stacked by `createItemGroup` |
52
+ | A form | `input`, `input-group`, `text-area`, `number-input`, `select`, `checkbox`, `switch`, `slider`, `date-picker`, `color-picker` |
53
+ | Data | `data-table`, `sort-button` (sorts a list that is not a table), `month-grid` (a figure per day), `sparkline`, `line-chart`, `bar-chart`, `scatter-chart`, `donut`, `empty` |
54
+
55
+ ## Rules that prevent the common bugs
56
+
57
+ 1. **Pass `host` to a floating component built before its trigger is in the page** (a select
58
+ takes it as `list: { host }`). Without it, the component looks for a host from the trigger,
59
+ finds none, and mounts in `<body>`, unthemed. When unsure whether the trigger is in the page
60
+ yet (inside a panel's `mount`, say), pass it.
61
+ 2. **Start a value with `defaultValue`, `defaultChecked`, `defaultOpen` or `defaultPressed`,
62
+ listen with the matching `on…Change`, and change it from code with `setValue`,
63
+ `setChecked`, `setOpen` or `setPressed`.** The setters never call the listeners, so a host
64
+ can refresh a control without loops. A select's options are fixed: build a new one to
65
+ change them. A slider also reports `onValueCommit` once a drag ends: run costly work there.
66
+ 3. **Every floating part takes the same placement props:** `side`, `align`, `sideOffset`,
67
+ `alignOffset`, `avoidCollisions`, `collisionPadding`, `collisionBoundary`.
68
+ 4. **Release what you discard.** Controls that run a state machine keep page-wide listeners
69
+ until released: call `disposeWithin(root)` (`@luxalgo/vela-ui/dispose`) before dropping a
70
+ subtree, or `destroy()` on the handles. A destroyed dialog, drawer or popover releases its
71
+ content.
72
+ 5. **Menus take rows as data:** `items: [{ id, label, icon?, hint?, checked?, toggle?, disabled?,
73
+ separatorBefore?, submenu? }]` and `onSelect(id)`. `checkmarks: true` shows checked rows
74
+ with a leading check; `iconBadges: true` puts icons in tiles.
75
+ 6. **Icons are ids from the registry** (`icon: 'kebab'`). Add your own with
76
+ `registerIcon(id, svg)` from `@luxalgo/vela-ui/icons`, drawn with `currentColor`.
77
+ 7. **Restyle through the theme or the parts' `data-slot` attributes, never class names.**
78
+ Class names are layout hooks, not the styling contract. Your own elements beside the kit
79
+ read the same tokens as CSS variables: `color: var(--vela-fg-muted)`,
80
+ `background: var(--vela-surface-elev)` (names in `docs/theming.md`).
81
+ 8. **An inline or authored `display` defeats the `hidden` attribute.** Toggle visibility of
82
+ your own elements one way only: `hidden`, or `style.display`, not both.
83
+ 9. **Two components called Switch.** `@luxalgo/vela-ui/switch` is the sliding switch. In
84
+ `@luxalgo/vela/ui` 0.10, `Switch` is the deprecated name of `Checkbox`.
85
+
86
+ ## Example: an order form
87
+
88
+ ```ts
89
+ import { createButton } from '@luxalgo/vela-ui/button';
90
+ import { createDropdownMenu } from '@luxalgo/vela-ui/dropdown-menu';
91
+ import { createInput } from '@luxalgo/vela-ui/input';
92
+ import { createSelect } from '@luxalgo/vela-ui/select';
93
+ import { createToggleGroup } from '@luxalgo/vela-ui/toggle-group';
94
+ import { createTooltip } from '@luxalgo/vela-ui/tooltip';
95
+
96
+ const side = createToggleGroup({
97
+ items: [{ value: 'buy', label: 'Buy', tone: 'up' }, { value: 'sell', label: 'Sell', tone: 'down' }],
98
+ defaultValue: 'buy',
99
+ fill: true,
100
+ onValueChange: (value) => (order.side = value),
101
+ });
102
+ const type = createSelect({
103
+ options: [{ value: 'market', label: 'Market' }, { value: 'limit', label: 'Limit' }],
104
+ defaultValue: 'limit',
105
+ fill: true,
106
+ onValueChange: (value) => (order.type = value),
107
+ });
108
+ const size = createInput({ placeholder: 'Size', onChange: (text) => (order.size = Number(text)) });
109
+ const submit = createButton({ text: 'Place order', variant: 'default', onClick: () => place(order) });
110
+ const more = createButton({ icon: 'kebab', size: 'icon-sm', variant: 'ghost', label: 'More' });
111
+ createDropdownMenu({
112
+ trigger: more.el,
113
+ host: app,
114
+ items: [{ id: 'reset', label: 'Reset form' }, { id: 'help', label: 'Order types…', separatorBefore: true }],
115
+ onSelect: (id) => (id === 'reset' ? reset() : openHelp()),
116
+ });
117
+ createTooltip({ trigger: submit.el, content: 'Sends the order to the paper account' });
118
+
119
+ app.append(side.el, type.el, size.el, submit.el, more.el);
120
+ ```
121
+
122
+ ## Check your work
123
+
124
+ 1. Typecheck: props are typed, so a wrong prop or variant fails there.
125
+ 2. In the browser, open each floating part and confirm it sits inside the themed host:
126
+ `el.closest('[data-vela-theme]')` is not null and its background is not transparent.
127
+ 3. `npx @luxalgo/vela-ui check` finds a second installed copy of the kit, which would load
128
+ every component twice.
129
+
130
+ ## Further reading
131
+
132
+ - `node_modules/@luxalgo/vela-ui/dist/manifest.json`: every component's import, props, parts,
133
+ settings, variants and an example.
134
+ - `node_modules/@luxalgo/vela-ui/docs/what-to-change.md`: which file or setting a change
135
+ belongs in.
136
+ - The `theming-vela-ui` skill restyles components; `writing-vela-ui-components` builds new
137
+ ones.
@@ -0,0 +1,163 @@
1
+ ---
2
+ name: writing-vela-ui-components
3
+ description: 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. Use when no kit component fits, when a component must look or behave differently than its props and theme allow, when building a widget or panel for a Vela chart, or when a theme fails to restyle a hand-written component.
4
+ license: Apache-2.0
5
+ ---
6
+
7
+ # Writing vela-ui components
8
+
9
+ ## Choose the route
10
+
11
+ | Situation | Route |
12
+ | --- | --- |
13
+ | A kit component almost fits | Its props and the theme first (the `theming-vela-ui` skill) |
14
+ | A kit component needs other markup or behavior | `npx @luxalgo/vela-ui add <name> --source`: copies it into your project as `app-<name>`, yours to change; `npx @luxalgo/vela-ui diff app-<name>` shows upstream changes since |
15
+ | Nothing in the kit fits | A component of your own, in the shape below |
16
+ | You are working in the Vela repository | `npm run new-component -- <name>` and `packages/vela-ui/AGENTS.md` |
17
+
18
+ ## The shape of a component
19
+
20
+ One file per component: its declaration, variants, CSS and `create<Name>` function.
21
+
22
+ ```ts
23
+ // Gauge: a value between 0 and 100 on a track, in the loss tone above 75.
24
+ import { css, defineComponent, defineStyles, h, t, variants, type VariantProps } from '@luxalgo/vela-ui';
25
+
26
+ export const gaugeDef = defineComponent({
27
+ name: 'gauge',
28
+ parts: ['root', 'track', 'fill', 'label'],
29
+ settings: { radius: t.radius.md, height: '8px' },
30
+ });
31
+
32
+ // A variant is fixed at creation; its first value is the default.
33
+ export const gaugeVariants = variants({ size: ['md', 'sm'] });
34
+
35
+ const injectStyles = defineStyles(
36
+ 'app-gauge',
37
+ css`
38
+ @layer vela.ui {
39
+ [data-slot='gauge'] {
40
+ gap: ${t.space[1]};
41
+ color: ${t.fg};
42
+ font-family: ${t.font};
43
+ font-size: ${t.fontSize.md};
44
+
45
+ &:where(:not([hidden])) { display: grid; }
46
+ &[data-size='sm'] { font-size: ${t.fontSize.sm}; }
47
+ }
48
+ [data-slot='gauge-track'] {
49
+ block-size: ${gaugeDef.vars.height};
50
+ border-radius: ${gaugeDef.vars.radius};
51
+ background: ${t.surface.sunken};
52
+ overflow: hidden;
53
+ }
54
+ [data-slot='gauge-fill'] {
55
+ block-size: 100%;
56
+ background-color: ${t.profit};
57
+ transition: inline-size ${t.dur.med} ${t.ease};
58
+
59
+ &:is([data-level='high'] *) { background-color: ${t.loss}; }
60
+ }
61
+ [data-slot='gauge-label'] {
62
+ color: ${t.fg.muted};
63
+ }
64
+ }
65
+ `,
66
+ );
67
+
68
+ export interface GaugeProps extends VariantProps<typeof gaugeVariants> {
69
+ value: number;
70
+ label?: string;
71
+ }
72
+
73
+ export interface GaugeHandle {
74
+ readonly el: HTMLElement;
75
+ setValue(value: number): void;
76
+ destroy(): void;
77
+ }
78
+
79
+ export function createGauge(props: GaugeProps): GaugeHandle {
80
+ injectStyles(document);
81
+ const fill = h('div', { slot: gaugeDef.slot('fill') });
82
+ const el = h(
83
+ 'div',
84
+ { slot: gaugeDef.slot('root'), attrs: gaugeVariants(props) },
85
+ props.label ? h('span', { slot: gaugeDef.slot('label'), text: props.label }) : null,
86
+ h('div', { slot: gaugeDef.slot('track') }, fill),
87
+ );
88
+ // A state that follows the value is a data attribute on the root, set on every update.
89
+ const setValue = (value: number) => {
90
+ const v = Math.max(0, Math.min(100, value));
91
+ fill.style.inlineSize = `${v}%`;
92
+ el.setAttribute('data-level', v > 75 ? 'high' : 'normal');
93
+ };
94
+ setValue(props.value);
95
+ return { el, setValue, destroy: () => el.remove() };
96
+ }
97
+ ```
98
+
99
+ ## Rules
100
+
101
+ - **Base styles go in `@layer vela.ui`**, like the kit's. A theme's part CSS lives in
102
+ `vela.custom`; in the same layer, your component's own rules would tie with it and win.
103
+ - **Selectors start with one of the component's parts** (`[data-slot='gauge-fill']`), nest
104
+ with `&`, and key states on attributes (`data-state`, `data-tone`), never on classes.
105
+ - **Values come from the tokens (`t`) or the component's settings (`gaugeDef.vars`).** No color
106
+ literals: a theme can then restyle everything.
107
+ - **A root that can be `hidden`** takes its `display` under `:where(:not([hidden]))`, or the
108
+ browser's `[hidden]` rule loses to it.
109
+ - **Logical directions** (`padding-inline`, `inset-inline-end`, `text-align: start`), so the
110
+ component mirrors in right-to-left layouts.
111
+ - **Floating content mounts in the nearest kit host.** Build menus and popovers your component
112
+ opens with an explicit `host` when it may be built before it is in the page.
113
+ - **Release what runs.** A component that starts a state machine or page-wide listeners ties
114
+ their teardown to its element with `ownTeardown(el, stop)`, so `disposeWithin(root)` reaches
115
+ it even when nobody kept the handle.
116
+
117
+ ## Make themes see it
118
+
119
+ 1. Import the component before creating a theme that names it: `defineComponent` records it,
120
+ and `checkTheme` then checks the theme's settings and parts for it.
121
+ 2. Type it in themes by merging its declaration:
122
+
123
+ ```ts
124
+ declare module '@luxalgo/vela-ui/theme' {
125
+ interface ComponentDefs {
126
+ gauge: typeof gaugeDef; // the key is the component's name: quote it if hyphenated ('risk-meter')
127
+ }
128
+ }
129
+ ```
130
+
131
+ A theme can then set ``components: { gauge: { height: '4px', css: { fill: css`…` } } }``, and a
132
+ wrong setting or part fails the typecheck. `createTheme` from `@luxalgo/vela` and
133
+ `@luxalgo/vela/ui` read the same declaration. To prove the typing in a test, pair a
134
+ `// @ts-expect-error` theme naming a wrong part with a valid theme that compiles, so the
135
+ expected error cannot come from the declaration failing to merge.
136
+
137
+ ## Lint it
138
+
139
+ Extend the kit's stylelint config for CSS written with the `css` tag:
140
+
141
+ ```js
142
+ // stylelint.config.mjs
143
+ export default { extends: ['@luxalgo/vela-ui/stylelint'], rules: { 'vela/layer': 'vela.ui' } };
144
+ ```
145
+
146
+ It needs `stylelint`, `postcss-lit` and `stylelint-config-recommended` installed.
147
+
148
+ ## Check your work
149
+
150
+ 1. Typecheck.
151
+ 2. Render it in a kit host (`ensureUIHost(el, createTheme())`, both from
152
+ `@luxalgo/vela-ui/theme`) and inspect: every part carries its `data-slot`, variants appear
153
+ as `data-*`.
154
+ 3. `checkTheme({ components: { gauge: { radius: '2px' } } })` returns an empty list (it runs
155
+ without a page).
156
+ 4. A theme's part CSS restyles it in the browser (read `getComputedStyle`, not the source).
157
+
158
+ ## Further reading
159
+
160
+ - `node_modules/@luxalgo/vela-ui/docs/writing-a-component.md`: the helpers, every CSS rule and
161
+ the rules for components that run Zag state machines.
162
+ - `node_modules/@luxalgo/vela-ui/registry/<name>.ts`: each kit component's source as a copy
163
+ imports it, a worked example for any pattern.
@@ -0,0 +1,119 @@
1
+ // Badge: a short status label, or a round count. `default` is filled with the selection color,
2
+ // `secondary` a neutral tint, `outline` bordered, `destructive` tinted with the danger color,
3
+ // `up` and `down` with the market colors.
4
+ import { css, defineStyles } from '../lib/css';
5
+ import { defineComponent } from '../lib/define';
6
+ import { h } from '../lib/h';
7
+ import { t } from '../lib/t';
8
+ import { variants, type VariantProps } from '../lib/variants';
9
+
10
+ export const badgeDef = defineComponent({
11
+ name: 'badge',
12
+ parts: ['root'],
13
+ settings: { radius: '5px' },
14
+ });
15
+
16
+ export const badgeVariants = variants({ variant: ['default', 'secondary', 'outline', 'destructive', 'up', 'down'] });
17
+
18
+ export type BadgeVariant = (typeof badgeVariants.spec.variant)[number];
19
+
20
+ export interface BadgeProps extends VariantProps<typeof badgeVariants> {
21
+ text: string;
22
+ /** A round count pill (`3`, `9+`) rather than a label. */
23
+ count?: boolean;
24
+ className?: string;
25
+ }
26
+
27
+ export interface BadgeHandle {
28
+ readonly el: HTMLSpanElement;
29
+ setText(text: string): void;
30
+ setVariant(variant: BadgeVariant): void;
31
+ destroy(): void;
32
+ }
33
+
34
+ export const BADGE_STYLE_ID = 'vela-ui-badge';
35
+
36
+ export const BADGE_CSS = css`
37
+ @layer vela.ui {
38
+ [data-slot='badge'] {
39
+ align-items: center;
40
+ gap: 4px;
41
+ box-sizing: border-box;
42
+ height: 20px;
43
+ padding: 0 6px;
44
+ border-radius: ${badgeDef.vars.radius};
45
+ border: 1px solid transparent;
46
+ font-size: 12px;
47
+ font-weight: 500;
48
+ line-height: 1;
49
+ white-space: nowrap;
50
+ font-variant-numeric: tabular-nums;
51
+ background: ${t.selected.bg};
52
+ color: ${t.selected.fg};
53
+
54
+ &:where(:not([hidden])) {
55
+ display: inline-flex;
56
+ }
57
+ &[data-count] {
58
+ min-width: 18px;
59
+ height: 18px;
60
+ padding: 0 5px;
61
+ justify-content: center;
62
+ border-radius: 999px;
63
+ font-size: 11px;
64
+ }
65
+ &[data-variant='secondary'] {
66
+ background: ${t.hover};
67
+ color: ${t.fg.muted};
68
+ }
69
+ &[data-variant='outline'] {
70
+ background: transparent;
71
+ border-color: ${t.border.strong};
72
+ color: ${t.fg};
73
+ }
74
+ &[data-variant='destructive'] {
75
+ background: color-mix(in srgb, ${t.danger} 18%, transparent);
76
+ color: ${t.danger};
77
+ }
78
+ &[data-variant='up'] {
79
+ background: color-mix(in srgb, ${t.up} 18%, transparent);
80
+ color: ${t.up};
81
+ }
82
+ &[data-variant='down'] {
83
+ background: color-mix(in srgb, ${t.down} 18%, transparent);
84
+ color: ${t.down};
85
+ }
86
+ }
87
+ }
88
+ `;
89
+
90
+ const injectStyles = defineStyles(BADGE_STYLE_ID, BADGE_CSS);
91
+
92
+ class BadgeImpl implements BadgeHandle {
93
+ readonly el: HTMLSpanElement;
94
+
95
+ constructor(props: BadgeProps) {
96
+ injectStyles(document);
97
+ this.el = h('span', {
98
+ className: props.className ? `vela-badge ${props.className}` : 'vela-badge',
99
+ slot: badgeDef.slot('root'),
100
+ attrs: badgeVariants(props),
101
+ data: { count: props.count === true },
102
+ text: props.text,
103
+ });
104
+ }
105
+
106
+ setText(text: string): void {
107
+ this.el.textContent = text;
108
+ }
109
+
110
+ setVariant(variant: BadgeVariant): void {
111
+ this.el.setAttribute('data-variant', variant);
112
+ }
113
+
114
+ destroy(): void {}
115
+ }
116
+
117
+ export function createBadge(props: BadgeProps): BadgeHandle {
118
+ return new BadgeImpl(props);
119
+ }