@luxalgo/vela-ui 0.0.0-stage → 0.1.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 (248) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +201 -0
  3. package/NOTICE +28 -0
  4. package/README.md +83 -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 +41 -0
  12. package/dist/button.js +10 -0
  13. package/dist/calendar.d.ts +22 -0
  14. package/dist/calendar.js +1 -0
  15. package/dist/callout-bubble.d.ts +98 -0
  16. package/dist/callout-bubble.js +173 -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-2Q4JRINP.js +403 -0
  23. package/dist/chunk-3Q32K4PE.js +85 -0
  24. package/dist/chunk-5QHXOAZA.js +351 -0
  25. package/dist/chunk-6NXY62JS.js +25 -0
  26. package/dist/chunk-6QCILIT2.js +52 -0
  27. package/dist/chunk-A2FJ7Y7D.js +55 -0
  28. package/dist/chunk-A72NWYNP.js +26 -0
  29. package/dist/chunk-AJ2IYSOI.js +553 -0
  30. package/dist/chunk-BXMWQIBO.js +66 -0
  31. package/dist/chunk-CH3DV6FD.js +280 -0
  32. package/dist/chunk-EZQXTO6K.js +89 -0
  33. package/dist/chunk-FCO3YZLT.js +81 -0
  34. package/dist/chunk-FN2BUFNM.js +10 -0
  35. package/dist/chunk-FVH6L7FM.js +18 -0
  36. package/dist/chunk-G7IQLY3A.js +183 -0
  37. package/dist/chunk-IDA2BT7D.js +29 -0
  38. package/dist/chunk-J2QJATCD.js +12 -0
  39. package/dist/chunk-LZWZV2ZS.js +18 -0
  40. package/dist/chunk-MMTMOV6R.js +31 -0
  41. package/dist/chunk-OBLIX3VN.js +227 -0
  42. package/dist/chunk-P4MTEMT7.js +23 -0
  43. package/dist/chunk-Q6WTDVH6.js +31 -0
  44. package/dist/chunk-S7USVSYP.js +50 -0
  45. package/dist/chunk-SAPURP46.js +133 -0
  46. package/dist/chunk-TAKSIV57.js +90 -0
  47. package/dist/chunk-V6TTTMTE.js +79 -0
  48. package/dist/chunk-VMCR3S2A.js +22 -0
  49. package/dist/chunk-X4YPSIW6.js +72 -0
  50. package/dist/chunk-X66RJAIS.js +14 -0
  51. package/dist/chunk-XJAPCCOJ.js +101 -0
  52. package/dist/chunk-ZB3BZHSN.js +219 -0
  53. package/dist/color-picker.d.ts +103 -0
  54. package/dist/color-picker.js +19 -0
  55. package/dist/color.d.ts +24 -0
  56. package/dist/color.js +1 -0
  57. package/dist/context-menu.d.ts +72 -0
  58. package/dist/context-menu.js +161 -0
  59. package/dist/css.d.ts +15 -0
  60. package/dist/css.js +4 -0
  61. package/dist/data-table-DtU3FX9j.d.ts +61 -0
  62. package/dist/data-table.d.ts +3 -0
  63. package/dist/data-table.js +88 -0
  64. package/dist/date-picker.d.ts +90 -0
  65. package/dist/date-picker.js +257 -0
  66. package/dist/define-D5u3F5Dj.d.ts +21 -0
  67. package/dist/dialog.d.ts +89 -0
  68. package/dist/dialog.js +258 -0
  69. package/dist/dispose.d.ts +9 -0
  70. package/dist/dispose.js +2 -0
  71. package/dist/donut.d.ts +27 -0
  72. package/dist/donut.js +67 -0
  73. package/dist/drawer.d.ts +119 -0
  74. package/dist/drawer.js +326 -0
  75. package/dist/dropdown-menu.d.ts +114 -0
  76. package/dist/dropdown-menu.js +18 -0
  77. package/dist/empty.d.ts +21 -0
  78. package/dist/empty.js +38 -0
  79. package/dist/field.d.ts +147 -0
  80. package/dist/field.js +328 -0
  81. package/dist/glyph-select.d.ts +57 -0
  82. package/dist/glyph-select.js +17 -0
  83. package/dist/headless.d.ts +6 -0
  84. package/dist/headless.js +9 -0
  85. package/dist/hover-card.d.ts +43 -0
  86. package/dist/hover-card.js +97 -0
  87. package/dist/i18n.d.ts +20 -0
  88. package/dist/i18n.js +1 -0
  89. package/dist/icons.d.ts +20 -0
  90. package/dist/icons.js +2 -0
  91. package/dist/index.d.ts +45 -0
  92. package/dist/index.js +13 -0
  93. package/dist/input-group.d.ts +57 -0
  94. package/dist/input-group.js +86 -0
  95. package/dist/input.d.ts +54 -0
  96. package/dist/input.js +9 -0
  97. package/dist/kbd.d.ts +19 -0
  98. package/dist/kbd.js +8 -0
  99. package/dist/kit-text.d.ts +8 -0
  100. package/dist/kit-text.js +2 -0
  101. package/dist/line-chart.d.ts +32 -0
  102. package/dist/line-chart.js +120 -0
  103. package/dist/manifest.json +3854 -0
  104. package/dist/number-input.d.ts +99 -0
  105. package/dist/number-input.js +14 -0
  106. package/dist/page.d.ts +7 -0
  107. package/dist/page.js +1 -0
  108. package/dist/palette.d.ts +55 -0
  109. package/dist/palette.js +1 -0
  110. package/dist/placement.d.ts +98 -0
  111. package/dist/placement.js +1 -0
  112. package/dist/popover.d.ts +107 -0
  113. package/dist/popover.js +15 -0
  114. package/dist/scatter-chart.d.ts +32 -0
  115. package/dist/scatter-chart.js +121 -0
  116. package/dist/select.d.ts +112 -0
  117. package/dist/select.js +19 -0
  118. package/dist/sparkline.d.ts +22 -0
  119. package/dist/sparkline.js +72 -0
  120. package/dist/stat.d.ts +29 -0
  121. package/dist/stat.js +41 -0
  122. package/dist/styles.d.ts +22 -0
  123. package/dist/styles.js +2 -0
  124. package/dist/surface-events.d.ts +17 -0
  125. package/dist/surface-events.js +1 -0
  126. package/dist/surface-exit.d.ts +25 -0
  127. package/dist/surface-exit.js +1 -0
  128. package/dist/switch.d.ts +54 -0
  129. package/dist/switch.js +92 -0
  130. package/dist/t.d.ts +89 -0
  131. package/dist/t.js +1 -0
  132. package/dist/tabs.d.ts +63 -0
  133. package/dist/tabs.js +150 -0
  134. package/dist/text-area.d.ts +54 -0
  135. package/dist/text-area.js +9 -0
  136. package/dist/theme.d.ts +188 -0
  137. package/dist/theme.js +8 -0
  138. package/dist/theme.schema.json +1745 -0
  139. package/dist/themes/dark.css +3 -0
  140. package/dist/themes/light.css +3 -0
  141. package/dist/toggle-group.d.ts +56 -0
  142. package/dist/toggle-group.js +130 -0
  143. package/dist/tokens.d.ts +16 -0
  144. package/dist/tokens.js +3 -0
  145. package/dist/tooltip.d.ts +47 -0
  146. package/dist/tooltip.js +74 -0
  147. package/dist/variants-B4stUBnY.d.ts +16 -0
  148. package/dist/zag.d.ts +19 -0
  149. package/dist/zag.js +2 -0
  150. package/docs/theming.md +211 -0
  151. package/docs/what-to-change.md +20 -0
  152. package/docs/writing-a-component.md +247 -0
  153. package/llms.txt +66 -0
  154. package/package.json +90 -6
  155. package/registry/badge.ts +119 -0
  156. package/registry/bar-chart.ts +215 -0
  157. package/registry/button.ts +303 -0
  158. package/registry/callout-bubble.ts +336 -0
  159. package/registry/checkbox.ts +234 -0
  160. package/registry/color-picker.ts +582 -0
  161. package/registry/context-menu.ts +208 -0
  162. package/registry/data-table.ts +215 -0
  163. package/registry/date-picker.ts +483 -0
  164. package/registry/dialog.ts +535 -0
  165. package/registry/donut.ts +145 -0
  166. package/registry/drawer.ts +636 -0
  167. package/registry/dropdown-menu.ts +762 -0
  168. package/registry/empty.ts +91 -0
  169. package/registry/field.ts +585 -0
  170. package/registry/glyph-select.ts +268 -0
  171. package/registry/hover-card.ts +222 -0
  172. package/registry/input-group.ts +229 -0
  173. package/registry/input.ts +181 -0
  174. package/registry/kbd.ts +64 -0
  175. package/registry/line-chart.ts +218 -0
  176. package/registry/number-input.ts +443 -0
  177. package/registry/popover.ts +368 -0
  178. package/registry/scatter-chart.ts +221 -0
  179. package/registry/select.ts +620 -0
  180. package/registry/sparkline.ts +114 -0
  181. package/registry/stat.ts +96 -0
  182. package/registry/switch.ts +205 -0
  183. package/registry/tabs.ts +392 -0
  184. package/registry/text-area.ts +186 -0
  185. package/registry/toggle-group.ts +295 -0
  186. package/registry/tooltip.ts +156 -0
  187. package/skills/theming-vela-ui/SKILL.md +137 -0
  188. package/skills/using-vela-ui-components/SKILL.md +134 -0
  189. package/skills/writing-vela-ui-components/SKILL.md +163 -0
  190. package/src/components/badge.ts +119 -0
  191. package/src/components/bar-chart.ts +215 -0
  192. package/src/components/button.ts +303 -0
  193. package/src/components/callout-bubble.ts +336 -0
  194. package/src/components/checkbox.ts +234 -0
  195. package/src/components/color-picker.ts +582 -0
  196. package/src/components/context-menu.ts +208 -0
  197. package/src/components/data-table.ts +215 -0
  198. package/src/components/date-picker.ts +483 -0
  199. package/src/components/dialog.ts +535 -0
  200. package/src/components/donut.ts +145 -0
  201. package/src/components/drawer.ts +636 -0
  202. package/src/components/dropdown-menu.ts +762 -0
  203. package/src/components/empty.ts +91 -0
  204. package/src/components/field.ts +585 -0
  205. package/src/components/glyph-select.ts +268 -0
  206. package/src/components/hover-card.ts +222 -0
  207. package/src/components/input-group.ts +229 -0
  208. package/src/components/input.ts +181 -0
  209. package/src/components/kbd.ts +64 -0
  210. package/src/components/line-chart.ts +218 -0
  211. package/src/components/number-input.ts +443 -0
  212. package/src/components/popover.ts +368 -0
  213. package/src/components/scatter-chart.ts +221 -0
  214. package/src/components/select.ts +620 -0
  215. package/src/components/sparkline.ts +114 -0
  216. package/src/components/stat.ts +96 -0
  217. package/src/components/switch.ts +205 -0
  218. package/src/components/tabs.ts +392 -0
  219. package/src/components/text-area.ts +186 -0
  220. package/src/components/toggle-group.ts +295 -0
  221. package/src/components/tooltip.ts +156 -0
  222. package/src/index.ts +24 -0
  223. package/src/lib/calendar.ts +112 -0
  224. package/src/lib/chart.ts +72 -0
  225. package/src/lib/color.ts +89 -0
  226. package/src/lib/component-index.ts +131 -0
  227. package/src/lib/css.ts +44 -0
  228. package/src/lib/define.ts +45 -0
  229. package/src/lib/dispose.ts +36 -0
  230. package/src/lib/h.ts +55 -0
  231. package/src/lib/headless.ts +16 -0
  232. package/src/lib/i18n.ts +30 -0
  233. package/src/lib/icons.ts +237 -0
  234. package/src/lib/kit-text.ts +26 -0
  235. package/src/lib/page.ts +24 -0
  236. package/src/lib/palette.ts +81 -0
  237. package/src/lib/placement.ts +188 -0
  238. package/src/lib/styles.ts +53 -0
  239. package/src/lib/surface-events.ts +31 -0
  240. package/src/lib/surface-exit.ts +87 -0
  241. package/src/lib/t.ts +68 -0
  242. package/src/lib/theme.ts +654 -0
  243. package/src/lib/tokens.ts +111 -0
  244. package/src/lib/variants.ts +32 -0
  245. package/src/lib/zag.ts +46 -0
  246. package/stylelint/index.mjs +60 -0
  247. package/stylelint/lint.mjs +81 -0
  248. package/stylelint/plugin.mjs +115 -0
@@ -0,0 +1,134 @@
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, form controls (checkbox, switch, input, select, number input, date and color pickers) and data components (data table, 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`) |
44
+ | Switch between views | `tabs`; 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 form | `input`, `input-group`, `text-area`, `number-input`, `select`, `checkbox`, `switch`, `date-picker`, `color-picker` |
51
+ | Data | `data-table`, `sparkline`, `line-chart`, `bar-chart`, `scatter-chart`, `donut`, `empty` |
52
+
53
+ ## Rules that prevent the common bugs
54
+
55
+ 1. **Pass `host` to a floating component built before its trigger is in the page** (a select
56
+ takes it as `list: { host }`). Without it, the component looks for a host from the trigger,
57
+ finds none, and mounts in `<body>`, unthemed. When unsure whether the trigger is in the page
58
+ yet (inside a panel's `mount`, say), pass it.
59
+ 2. **Start a value with `defaultValue` or `defaultChecked`, listen with `onValueChange` or
60
+ `onCheckedChange`, and change it from code with `setValue` or `setChecked`.** The setters
61
+ never call the listeners, so a host can refresh a control without loops. A select's options
62
+ are fixed: build a new one to change them.
63
+ 3. **Every floating part takes the same placement props:** `side`, `align`, `sideOffset`,
64
+ `alignOffset`, `avoidCollisions`, `collisionPadding`, `collisionBoundary`.
65
+ 4. **Release what you discard.** Controls that run a state machine keep page-wide listeners
66
+ until released: call `disposeWithin(root)` (`@luxalgo/vela-ui/dispose`) before dropping a
67
+ subtree, or `destroy()` on the handles. A destroyed dialog, drawer or popover releases its
68
+ content.
69
+ 5. **Menus take rows as data:** `items: [{ id, label, icon?, hint?, checked?, toggle?, disabled?,
70
+ separatorBefore?, submenu? }]` and `onSelect(id)`. `checkmarks: true` shows checked rows
71
+ with a leading check; `iconBadges: true` puts icons in tiles.
72
+ 6. **Icons are ids from the registry** (`icon: 'kebab'`). Add your own with
73
+ `registerIcon(id, svg)` from `@luxalgo/vela-ui/icons`, drawn with `currentColor`.
74
+ 7. **Restyle through the theme or the parts' `data-slot` attributes, never class names.**
75
+ Class names are layout hooks, not the styling contract. Your own elements beside the kit
76
+ read the same tokens as CSS variables: `color: var(--vela-fg-muted)`,
77
+ `background: var(--vela-surface-elev)` (names in `docs/theming.md`).
78
+ 8. **An inline or authored `display` defeats the `hidden` attribute.** Toggle visibility of
79
+ your own elements one way only: `hidden`, or `style.display`, not both.
80
+ 9. **Two components called Switch.** `@luxalgo/vela-ui/switch` is the sliding switch. In
81
+ `@luxalgo/vela/ui` 0.10, `Switch` is the deprecated name of `Checkbox`.
82
+
83
+ ## Example: an order form
84
+
85
+ ```ts
86
+ import { createButton } from '@luxalgo/vela-ui/button';
87
+ import { createDropdownMenu } from '@luxalgo/vela-ui/dropdown-menu';
88
+ import { createInput } from '@luxalgo/vela-ui/input';
89
+ import { createSelect } from '@luxalgo/vela-ui/select';
90
+ import { createToggleGroup } from '@luxalgo/vela-ui/toggle-group';
91
+ import { createTooltip } from '@luxalgo/vela-ui/tooltip';
92
+
93
+ const side = createToggleGroup({
94
+ items: [{ value: 'buy', label: 'Buy', tone: 'up' }, { value: 'sell', label: 'Sell', tone: 'down' }],
95
+ defaultValue: 'buy',
96
+ fill: true,
97
+ onValueChange: (value) => (order.side = value),
98
+ });
99
+ const type = createSelect({
100
+ options: [{ value: 'market', label: 'Market' }, { value: 'limit', label: 'Limit' }],
101
+ defaultValue: 'limit',
102
+ fill: true,
103
+ onValueChange: (value) => (order.type = value),
104
+ });
105
+ const size = createInput({ placeholder: 'Size', onChange: (text) => (order.size = Number(text)) });
106
+ const submit = createButton({ text: 'Place order', variant: 'default', onClick: () => place(order) });
107
+ const more = createButton({ icon: 'kebab', size: 'icon-sm', variant: 'ghost', label: 'More' });
108
+ createDropdownMenu({
109
+ trigger: more.el,
110
+ host: app,
111
+ items: [{ id: 'reset', label: 'Reset form' }, { id: 'help', label: 'Order types…', separatorBefore: true }],
112
+ onSelect: (id) => (id === 'reset' ? reset() : openHelp()),
113
+ });
114
+ createTooltip({ trigger: submit.el, content: 'Sends the order to the paper account' });
115
+
116
+ app.append(side.el, type.el, size.el, submit.el, more.el);
117
+ ```
118
+
119
+ ## Check your work
120
+
121
+ 1. Typecheck: props are typed, so a wrong prop or variant fails there.
122
+ 2. In the browser, open each floating part and confirm it sits inside the themed host:
123
+ `el.closest('[data-vela-theme]')` is not null and its background is not transparent.
124
+ 3. `npx @luxalgo/vela-ui check` finds a second installed copy of the kit, which would load
125
+ every component twice.
126
+
127
+ ## Further reading
128
+
129
+ - `node_modules/@luxalgo/vela-ui/dist/manifest.json`: every component's import, props, parts,
130
+ settings, variants and an example.
131
+ - `node_modules/@luxalgo/vela-ui/docs/what-to-change.md`: which file or setting a change
132
+ belongs in.
133
+ - The `theming-vela-ui` skill restyles components; `writing-vela-ui-components` builds new
134
+ 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
+ }
@@ -0,0 +1,215 @@
1
+ // BarChart: vertical bars around a zero line with a hover readout (daily P&L, a distribution),
2
+ // in the profit tone above zero and the loss tone below unless a bar names its own; optionally
3
+ // a value axis with gridlines and a title, slanted labels, and dashed markers.
4
+ import { css, defineStyles } from '../lib/css';
5
+ import { chartFrame, compactNumber, escapeXml, showTip, valueAxis } from '../lib/chart';
6
+ import { defineComponent } from '../lib/define';
7
+ import { t } from '../lib/t';
8
+
9
+ export const barChartDef = defineComponent({
10
+ name: 'bar-chart',
11
+ parts: ['root', 'grid', 'label', 'bar', 'marker', 'tip'],
12
+ settings: { 'tip-radius': '3px', 'tip-shadow': t.shadow, 'tip-z': '2', 'tip-bg': t.surface.elev },
13
+ });
14
+
15
+ export interface BarDatum {
16
+ label: string;
17
+ value: number;
18
+ /** The bar's tone (default: by its sign). */
19
+ tone?: 'profit' | 'loss' | 'muted';
20
+ }
21
+
22
+ export interface BarChartProps {
23
+ /** The hover readout of a bar. */
24
+ format?: (d: BarDatum) => string | Node;
25
+ /** Label every Nth bar under the axis (default: as many as fit). */
26
+ labelEvery?: number;
27
+ /** A value axis on the start side, with gridlines. */
28
+ axes?: boolean;
29
+ /** The value axis's title, written up its side. */
30
+ axisTitle?: string;
31
+ /** Slant the bar labels (long labels on many bars). */
32
+ slantLabels?: boolean;
33
+ yFormat?: (v: number) => string;
34
+ /** What a bar's label reads under the axis (default: the label itself). */
35
+ labelFormat?: (label: string) => string;
36
+ /** Extra class names (layout hooks). */
37
+ className?: string;
38
+ }
39
+
40
+ export interface BarChartHandle {
41
+ readonly el: HTMLElement;
42
+ /** `markers` are dashed vertical lines at fractional bar positions (0: the first bar's
43
+ * start edge), each in its own ink. */
44
+ setData(data: readonly BarDatum[], markers?: ReadonlyArray<{ at: number; color: string }>): void;
45
+ destroy(): void;
46
+ }
47
+
48
+ const injectStyles = defineStyles(
49
+ 'vela-ui-bar-chart',
50
+ css`
51
+ @layer vela.ui {
52
+ [data-slot='bar-chart'] {
53
+ position: relative;
54
+ width: 100%;
55
+ height: 100%;
56
+
57
+ & > svg {
58
+ display: block;
59
+ width: 100%;
60
+ height: 100%;
61
+ overflow: visible;
62
+ }
63
+ }
64
+ [data-slot='bar-chart-grid'] {
65
+ stroke: color-mix(in srgb, ${t.fg.bright} 5%, transparent);
66
+
67
+ &[data-zero] {
68
+ stroke: color-mix(in srgb, ${t.fg.bright} 20%, transparent);
69
+ }
70
+ &[data-base] {
71
+ stroke: ${t.border};
72
+ }
73
+ }
74
+ [data-slot='bar-chart-label'] {
75
+ fill: ${t.fg.muted};
76
+ font-size: 10px;
77
+
78
+ &[data-title] {
79
+ font-size: 11px;
80
+ }
81
+ }
82
+ [data-slot='bar-chart-bar'] {
83
+ fill: ${t.profit};
84
+
85
+ &[data-tone='loss'] {
86
+ fill: ${t.loss};
87
+ }
88
+ &[data-tone='muted'] {
89
+ fill: ${t.fg.faint};
90
+ }
91
+ &[data-thin] {
92
+ opacity: 0.85;
93
+ }
94
+ }
95
+ [data-slot='bar-chart-marker'] {
96
+ stroke-width: 2;
97
+ stroke-dasharray: 4 3;
98
+ }
99
+ [data-slot='bar-chart-tip'] {
100
+ position: absolute;
101
+ z-index: ${barChartDef.vars['tip-z']};
102
+ top: 0;
103
+ pointer-events: none;
104
+ padding: 8px;
105
+ border-radius: ${barChartDef.vars['tip-radius']};
106
+ border: 1px solid ${t.border};
107
+ background: ${barChartDef.vars['tip-bg']};
108
+ color: ${t.fg.bright};
109
+ box-shadow: ${barChartDef.vars['tip-shadow']};
110
+ font-size: 12px;
111
+ line-height: 1.35;
112
+ white-space: nowrap;
113
+ font-variant-numeric: tabular-nums;
114
+ transition:
115
+ left ${t.dur.fast} ease-out,
116
+ top ${t.dur.fast} ease-out;
117
+
118
+ &[data-instant] {
119
+ transition: none;
120
+ }
121
+ }
122
+ }
123
+ `,
124
+ );
125
+
126
+ export function createBarChart(props: BarChartProps = {}): BarChartHandle {
127
+ injectStyles(document);
128
+ let data: readonly BarDatum[] = [];
129
+ let markers: ReadonlyArray<{ at: number; color: string }> = [];
130
+ const slot = (part: (typeof barChartDef.parts)[number]) => `data-slot="${barChartDef.slot(part)}"`;
131
+
132
+ const geometry = () => {
133
+ const w = frame.el.clientWidth;
134
+ const ht = frame.el.clientHeight;
135
+ if (w < 4 || ht < 24 || data.length === 0) return null;
136
+ const left = props.axes ? (props.axisTitle ? 54 : 40) : 0;
137
+ const top = props.axes ? 6 : 0;
138
+ const plotH = ht - top - (props.slantLabels ? 46 : 16);
139
+ const lo = Math.min(0, ...data.map((d) => d.value));
140
+ const hi = Math.max(0, ...data.map((d) => d.value));
141
+ const axis = props.axes ? valueAxis(lo, hi, 4) : { min: lo, max: hi, step: 0, ticks: [] as number[] };
142
+ const range = axis.max - axis.min || 1;
143
+ return { w, h: ht, left, top, plotH, bw: (w - left) / data.length, zero: top + (axis.max / range) * plotH, scale: plotH / range, ticks: axis.ticks };
144
+ };
145
+
146
+ const draw = (): void => {
147
+ const g = geometry();
148
+ if (!g) {
149
+ frame.svg.replaceChildren();
150
+ return;
151
+ }
152
+ const every = props.labelEvery ?? Math.max(1, Math.ceil(data.length / Math.max(1, Math.floor((g.w - g.left) / (props.slantLabels ? 32 : 56)))));
153
+ const label = (x: number, y: number, text: string, anchor: string, extra = ''): string =>
154
+ `<text ${slot('label')} x="${x.toFixed(1)}" y="${y.toFixed(1)}" text-anchor="${anchor}"${extra}>${escapeXml(text)}</text>`;
155
+ let body = '';
156
+ if (props.axes) {
157
+ const fmt = props.yFormat ?? compactNumber;
158
+ for (const v of g.ticks) {
159
+ const y = g.zero - v * g.scale;
160
+ body += `<line ${slot('grid')}${v === 0 ? ' data-zero=""' : ''} x1="${g.left}" x2="${g.w}" y1="${y.toFixed(1)}" y2="${y.toFixed(1)}"/>`;
161
+ body += label(g.left - 6, y + 3, fmt(v), 'end');
162
+ }
163
+ if (props.axisTitle) {
164
+ const cy = g.top + g.plotH / 2;
165
+ body += label(10, cy, props.axisTitle, 'middle', ` data-title="" transform="rotate(-90 10 ${cy.toFixed(1)})"`);
166
+ }
167
+ } else body += `<line ${slot('grid')} data-base="" x1="0" x2="${g.w}" y1="${g.zero}" y2="${g.zero}"/>`;
168
+ const barW = props.axes ? Math.max(1, Math.min(g.bw * 0.62, 48)) : Math.max(1, g.bw - Math.min(4, g.bw * 0.2));
169
+ data.forEach((d, i) => {
170
+ const cx = g.left + i * g.bw + g.bw / 2;
171
+ const ht = Math.max(1, Math.abs(d.value) * g.scale);
172
+ const y = d.value >= 0 ? g.zero - ht : g.zero;
173
+ const tone = d.tone ?? (d.value >= 0 ? 'profit' : 'loss');
174
+ body += `<rect ${slot('bar')} data-tone="${tone}"${props.axes ? '' : ' data-thin=""'} x="${(cx - barW / 2).toFixed(1)}" y="${y.toFixed(1)}" width="${barW.toFixed(1)}" height="${ht.toFixed(1)}" rx="${props.axes ? 2 : 1.5}"/>`;
175
+ if (i % every !== 0) return;
176
+ const ly = g.top + g.plotH + 12;
177
+ const text = props.labelFormat ? props.labelFormat(d.label) : d.label;
178
+ body += props.slantLabels ? label(cx, ly, text, 'end', ` transform="rotate(-45 ${cx.toFixed(1)} ${ly.toFixed(1)})"`) : label(cx, g.h - 3, text, 'middle');
179
+ });
180
+ for (const m of markers) {
181
+ const x = g.left + m.at * g.bw;
182
+ body += `<line ${slot('marker')} style="stroke: ${escapeXml(m.color)}" x1="${x.toFixed(1)}" x2="${x.toFixed(1)}" y1="${g.top}" y2="${(g.top + g.plotH).toFixed(1)}"/>`;
183
+ }
184
+ frame.svg.setAttribute('viewBox', `0 0 ${g.w} ${g.h}`);
185
+ frame.svg.innerHTML = body;
186
+ };
187
+
188
+ const frame = chartFrame(barChartDef.slot('root'), barChartDef.slot('tip'), props.className, draw);
189
+ frame.el.addEventListener('pointermove', (e) => {
190
+ const g = geometry();
191
+ if (!g) return;
192
+ const i = Math.floor((e.clientX - frame.el.getBoundingClientRect().left - g.left) / g.bw);
193
+ const d = data[i];
194
+ if (!d) {
195
+ frame.tip.hidden = true;
196
+ return;
197
+ }
198
+ const top = d.value >= 0 ? g.zero - Math.abs(d.value) * g.scale : g.zero + Math.abs(d.value) * g.scale;
199
+ showTip(frame.tip, props.format ? props.format(d) : `${d.label}: ${d.value}`, g, g.left + i * g.bw + g.bw / 2, top);
200
+ });
201
+ frame.el.addEventListener('pointerleave', () => (frame.tip.hidden = true));
202
+
203
+ return {
204
+ el: frame.el,
205
+ setData(next, nextMarkers = []) {
206
+ data = next;
207
+ markers = nextMarkers;
208
+ draw();
209
+ },
210
+ destroy() {
211
+ frame.disconnect();
212
+ frame.el.remove();
213
+ },
214
+ };
215
+ }