@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,295 @@
1
+ // ToggleGroup: a few options side by side on one track, over Zag's toggle-group machine.
2
+ // `single` (default): one option is always chosen (a radio group, a segmented control);
3
+ // `multiple`: each option toggles on its own. The arrow keys move focus; Space and Enter
4
+ // press. The track and the chosen option are tints of the foreground, so both read on a dark
5
+ // and a light theme.
6
+ import * as toggleGroup from '@zag-js/toggle-group';
7
+ import { iconEl } from '@luxalgo/vela-ui/icons';
8
+ import { css, defineStyles } from '@luxalgo/vela-ui';
9
+ import { defineComponent } from '@luxalgo/vela-ui';
10
+ import { ownTeardown } from '@luxalgo/vela-ui/dispose';
11
+ import { h } from '@luxalgo/vela-ui';
12
+ import { t } from '@luxalgo/vela-ui';
13
+ import { variants, type VariantProps } from '@luxalgo/vela-ui';
14
+ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
15
+
16
+ export const toggleGroupDef = defineComponent({
17
+ name: 'toggle-group',
18
+ parts: ['root', 'item', 'icon'],
19
+ settings: { radius: '8px', 'radius-sm': '7px', 'item-radius': t.radius.md, 'item-radius-sm': '5px' },
20
+ });
21
+
22
+ /** `default` = 32px; `sm` = 28px. */
23
+ export const toggleGroupVariants = variants({ size: ['default', 'sm'] });
24
+
25
+ export interface ToggleGroupItem {
26
+ value: string;
27
+ /** Visible text, or the accessible name of an icon-only option. */
28
+ label: string;
29
+ icon?: string;
30
+ disabled?: boolean;
31
+ /** Fill the option with a market color while it is on (buy and sell). */
32
+ tone?: 'up' | 'down';
33
+ }
34
+
35
+ interface ToggleGroupCommonProps extends VariantProps<typeof toggleGroupVariants> {
36
+ items: readonly ToggleGroupItem[];
37
+ /** Accessible name of the group. */
38
+ label?: string;
39
+ /** Options show their icon only; each label becomes its option's accessible name. */
40
+ iconOnly?: boolean;
41
+ dir?: 'ltr' | 'rtl';
42
+ /** Stretch across the parent, options sharing the width. */
43
+ fill?: boolean;
44
+ className?: string;
45
+ }
46
+
47
+ export interface ToggleGroupSingleProps extends ToggleGroupCommonProps {
48
+ type?: 'single';
49
+ /** The chosen option (default: the first enabled one). */
50
+ defaultValue?: string;
51
+ onValueChange?: (value: string) => void;
52
+ }
53
+
54
+ export interface ToggleGroupMultipleProps extends ToggleGroupCommonProps {
55
+ type: 'multiple';
56
+ /** The options turned on (default: none). */
57
+ defaultValue?: readonly string[];
58
+ onValueChange?: (value: string[]) => void;
59
+ }
60
+
61
+ export type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps;
62
+
63
+ export interface ToggleGroupHandle {
64
+ readonly el: HTMLElement;
65
+ /** The options turned on. */
66
+ readonly values: readonly string[];
67
+ /** Show options as on without calling `onValueChange`. */
68
+ setValue(value: string | readonly string[] | null): void;
69
+ setItems(items: readonly ToggleGroupItem[]): void;
70
+ destroy(): void;
71
+ }
72
+
73
+ export const TOGGLE_GROUP_STYLE_ID = 'vela-ui-toggle-group';
74
+
75
+ export const TOGGLE_GROUP_CSS = css`
76
+ @layer vela.ui {
77
+ [data-slot='toggle-group-item'] {
78
+ all: unset;
79
+ box-sizing: border-box;
80
+ display: inline-flex;
81
+ align-items: center;
82
+ justify-content: center;
83
+ gap: 6px;
84
+ min-width: 0;
85
+ padding: 0 10px;
86
+ border-radius: ${toggleGroupDef.vars['item-radius']};
87
+ color: ${t.fg.muted};
88
+ font-size: 13px;
89
+ font-weight: 500;
90
+ white-space: nowrap;
91
+ cursor: pointer;
92
+ -webkit-tap-highlight-color: transparent;
93
+ transition:
94
+ background ${t.dur.fast} ease,
95
+ color ${t.dur.fast} ease;
96
+
97
+ & > [data-slot='toggle-group-icon'] {
98
+ width: 16px;
99
+ height: 16px;
100
+ flex: none;
101
+ }
102
+ &[data-icon-only] {
103
+ padding: 0 8px;
104
+ }
105
+ &[data-state='off']:not([data-disabled]):hover {
106
+ color: ${t.fg.bright};
107
+ }
108
+ &[data-state='on'] {
109
+ background: color-mix(in srgb, ${t.fg.bright} 12%, transparent);
110
+ color: ${t.fg.bright};
111
+ }
112
+ &[data-state='on'][data-tone='up'] {
113
+ background: ${t.up};
114
+ color: ${t.fg.onFill};
115
+ }
116
+ &[data-state='on'][data-tone='down'] {
117
+ background: ${t.down};
118
+ color: ${t.fg.onFill};
119
+ }
120
+ &[data-disabled] {
121
+ cursor: default;
122
+ opacity: 0.45;
123
+ }
124
+ &:focus-visible {
125
+ outline: 2px solid ${t.focus};
126
+ outline-offset: -2px;
127
+ }
128
+ }
129
+ [data-slot='toggle-group'] {
130
+ align-items: stretch;
131
+ gap: 2px;
132
+ padding: 3px;
133
+ box-sizing: border-box;
134
+ height: 32px;
135
+ border-radius: ${toggleGroupDef.vars.radius};
136
+ background: color-mix(in srgb, ${t.fg.bright} 6%, transparent);
137
+
138
+ &:where(:not([hidden])) {
139
+ display: inline-flex;
140
+ }
141
+ &[data-size='sm'] {
142
+ height: 28px;
143
+ padding: 2px;
144
+ border-radius: ${toggleGroupDef.vars['radius-sm']};
145
+ }
146
+ &[data-fill] {
147
+ width: 100%;
148
+ }
149
+ &[data-fill]:where(:not([hidden])) {
150
+ display: flex;
151
+ }
152
+ &[data-fill] > [data-slot='toggle-group-item'] {
153
+ flex: 1 1 0;
154
+ }
155
+ &[data-size='sm'] > [data-slot='toggle-group-item'] {
156
+ padding: 0 8px;
157
+ border-radius: ${toggleGroupDef.vars['item-radius-sm']};
158
+ font-size: 12px;
159
+ }
160
+ }
161
+ }
162
+ `;
163
+
164
+ const injectStyles = defineStyles(TOGGLE_GROUP_STYLE_ID, TOGGLE_GROUP_CSS);
165
+
166
+ const keyOf = (values: readonly string[]): string => values.join('\u0000');
167
+
168
+ class ToggleGroupImpl implements ToggleGroupHandle {
169
+ readonly el: HTMLElement;
170
+ private readonly machine: HandleOf<typeof toggleGroup.machine>;
171
+ private readonly mid: string;
172
+ private readonly release: () => void;
173
+ private readonly iconOnly: boolean;
174
+ private readonly label: string | undefined;
175
+ private items: ToggleGroupItem[];
176
+ private readonly buttons = new Map<string, HTMLButtonElement>();
177
+ /** The options on as of the last write or report: the machine applies events on a
178
+ * microtask, so its own value lags a write. */
179
+ private current: string[];
180
+ /** A host write the machine has yet to report: that report is not the user's change. */
181
+ private silentNext: string | undefined;
182
+
183
+ constructor(props: ToggleGroupProps) {
184
+ injectStyles(document);
185
+ this.iconOnly = props.iconOnly ?? false;
186
+ this.label = props.label;
187
+ this.items = [...props.items];
188
+ const multiple = props.type === 'multiple';
189
+ this.current =
190
+ props.type === 'multiple' ? [...(props.defaultValue ?? [])] : [props.defaultValue ?? props.items.find((i) => !i.disabled)?.value].filter((v): v is string => v !== undefined);
191
+ this.mid = nextUid('vela-toggle-group');
192
+ this.el = h('div', {
193
+ className: props.className ? `vela-toggle-group ${props.className}` : 'vela-toggle-group',
194
+ slot: toggleGroupDef.slot('root'),
195
+ attrs: toggleGroupVariants(props),
196
+ data: { fill: props.fill === true },
197
+ });
198
+ this.build();
199
+ const onValueChange = props.onValueChange as ((v: string | string[]) => void) | undefined;
200
+ this.machine = runMachine(
201
+ toggleGroup.machine,
202
+ {
203
+ id: this.mid,
204
+ multiple,
205
+ // A single group never empties: pressing the chosen option keeps it chosen.
206
+ deselectable: false,
207
+ defaultValue: [...this.current],
208
+ dir: props.dir ?? 'ltr',
209
+ loopFocus: true,
210
+ rovingFocus: true,
211
+ onValueChange: ({ value }) => {
212
+ if (!multiple && value[0] === undefined) return;
213
+ const next = multiple ? [...value] : [value[0]!];
214
+ const key = keyOf(next);
215
+ // The machine compares arrays by identity: a press that changes nothing reports too.
216
+ if (key === keyOf(this.current)) {
217
+ if (this.silentNext === key) this.silentNext = undefined;
218
+ return;
219
+ }
220
+ this.current = next;
221
+ if (this.silentNext === key) {
222
+ this.silentNext = undefined;
223
+ return;
224
+ }
225
+ onValueChange?.(multiple ? [...next] : next[0]!);
226
+ },
227
+ },
228
+ (service) => this.render(service),
229
+ );
230
+ this.release = ownTeardown(this.el, () => this.machine.stop());
231
+ }
232
+
233
+ get values(): readonly string[] {
234
+ return this.current;
235
+ }
236
+
237
+ setValue(value: string | readonly string[] | null): void {
238
+ const next = value === null ? [] : typeof value === 'string' ? [value] : [...value];
239
+ const key = keyOf(next);
240
+ if (key === keyOf(this.current)) return;
241
+ this.current = next;
242
+ this.silentNext = key;
243
+ this.api().setValue(next);
244
+ }
245
+
246
+ setItems(items: readonly ToggleGroupItem[]): void {
247
+ this.items = [...items];
248
+ this.build();
249
+ const kept = this.current.filter((v) => this.items.some((i) => i.value === v));
250
+ if (kept.length !== this.current.length) this.setValue(kept);
251
+ this.machine.flush();
252
+ }
253
+
254
+ destroy(): void {
255
+ this.release();
256
+ }
257
+
258
+ private api(): toggleGroup.Api {
259
+ return toggleGroup.connect(this.machine.service, normalizeProps);
260
+ }
261
+
262
+ private build(): void {
263
+ this.buttons.clear();
264
+ const doc = this.el.ownerDocument;
265
+ this.el.replaceChildren(
266
+ ...this.items.map((item) => {
267
+ const icon = item.icon ? iconEl(item.icon, doc) : null;
268
+ if (icon) icon.dataset.slot = toggleGroupDef.slot('icon');
269
+ const iconOnly = this.iconOnly && icon !== null;
270
+ const b = h(
271
+ 'button',
272
+ { className: 'vela-toggle-group-item', slot: toggleGroupDef.slot('item'), data: { 'icon-only': iconOnly, tone: item.tone ?? null }, attrs: { 'aria-label': iconOnly ? item.label : null } },
273
+ icon,
274
+ iconOnly ? null : item.label,
275
+ );
276
+ this.buttons.set(item.value, b);
277
+ return b;
278
+ }),
279
+ );
280
+ }
281
+
282
+ private render(service: toggleGroup.Service): void {
283
+ const api = toggleGroup.connect(service, normalizeProps);
284
+ spreadProps(this.el, api.getRootProps(), this.mid);
285
+ if (this.label) this.el.setAttribute('aria-label', this.label);
286
+ for (const item of this.items) {
287
+ const b = this.buttons.get(item.value);
288
+ if (b) spreadProps(b, api.getItemProps({ value: item.value, disabled: item.disabled }), this.mid);
289
+ }
290
+ }
291
+ }
292
+
293
+ export function createToggleGroup(props: ToggleGroupProps): ToggleGroupHandle {
294
+ return new ToggleGroupImpl(props);
295
+ }
@@ -0,0 +1,156 @@
1
+ // Tooltip: a floating label over Zag's tooltip machine (floating-ui placement). The floating
2
+ // layer mounts into `host` (default: the trigger's nearest `.vela-ui` ancestor, else <body>)
3
+ // so the design tokens inherit. The trigger is the host's own element and keeps its slot.
4
+ import * as tooltip from '@zag-js/tooltip';
5
+ import { css, defineStyles } from '@luxalgo/vela-ui';
6
+ import { defineComponent } from '@luxalgo/vela-ui';
7
+ import { ownTeardown } from '@luxalgo/vela-ui/dispose';
8
+ import { h } from '@luxalgo/vela-ui';
9
+ import { t } from '@luxalgo/vela-ui';
10
+ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
11
+ import { splitPlacement, zagPositioning, type PlacementProps } from '@luxalgo/vela-ui/placement';
12
+
13
+ export interface TooltipControllerOptions extends PlacementProps {
14
+ /** @deprecated Use `side` and `align`. */
15
+ placement?: tooltip.Placement;
16
+ /** ms before opening on hover (default 0 — the reference tooltips are instant). */
17
+ openDelay?: number;
18
+ closeDelay?: number;
19
+ /** Keep open while the pointer is over the content (rich tooltips). */
20
+ interactive?: boolean;
21
+ /** Share the trigger's DOM id with other machines composed on the SAME element
22
+ * (each Zag machine otherwise stamps its own id — last one wins and breaks the rest). */
23
+ triggerId?: string;
24
+ }
25
+
26
+ export type TooltipService = tooltip.Service;
27
+ export type TooltipApi = tooltip.Api;
28
+
29
+ export interface TooltipController {
30
+ machine: typeof tooltip.machine;
31
+ props: Partial<tooltip.Props>;
32
+ connect(service: TooltipService): TooltipApi;
33
+ }
34
+
35
+ /** Zag's tooltip machine with the options mapped onto it, for any view layer. */
36
+ export function tooltipController(opts: TooltipControllerOptions = {}): TooltipController {
37
+ return {
38
+ machine: tooltip.machine,
39
+ props: {
40
+ id: nextUid('vela-tooltip'),
41
+ openDelay: opts.openDelay ?? 0,
42
+ closeDelay: opts.closeDelay ?? 0,
43
+ interactive: opts.interactive ?? false,
44
+ ids: opts.triggerId ? { trigger: opts.triggerId } : undefined,
45
+ positioning: zagPositioning(opts, opts.placement ? splitPlacement(opts.placement) : { side: 'top', align: 'center' }),
46
+ } satisfies Partial<tooltip.Props>,
47
+ connect: (service: TooltipService): TooltipApi => tooltip.connect(service, normalizeProps),
48
+ };
49
+ }
50
+
51
+ export const tooltipDef = defineComponent({
52
+ name: 'tooltip',
53
+ parts: ['positioner', 'content'],
54
+ settings: { radius: t.radius.md },
55
+ });
56
+
57
+ export interface TooltipOptions extends TooltipControllerOptions {
58
+ content: string | Node | (() => Node);
59
+ host?: HTMLElement;
60
+ }
61
+
62
+ export interface TooltipProps extends TooltipOptions {
63
+ trigger: HTMLElement;
64
+ }
65
+
66
+ const injectStyles = defineStyles(
67
+ 'vela-ui-tooltip',
68
+ css`
69
+ @layer vela.ui {
70
+ [data-slot='tooltip-content'] {
71
+ background: ${t.bg};
72
+ color: ${t.fg};
73
+ border: 1px solid ${t.border.soft};
74
+ border-radius: ${tooltipDef.vars.radius};
75
+ box-shadow: ${t.shadow};
76
+ font-size: ${t.fontSize.md};
77
+ line-height: 1.4;
78
+ padding: ${t.space[1]} ${t.space[2]};
79
+ max-width: 280px;
80
+ pointer-events: none;
81
+ z-index: ${t.z.tooltip};
82
+
83
+ &[data-interactive] {
84
+ pointer-events: auto;
85
+ }
86
+ &[data-state='open'] {
87
+ animation: vela-tooltip-in ${t.dur.fast} ease;
88
+ }
89
+ }
90
+ @keyframes vela-tooltip-in {
91
+ from {
92
+ opacity: 0;
93
+ transform: scale(0.97);
94
+ }
95
+ to {
96
+ opacity: 1;
97
+ transform: scale(1);
98
+ }
99
+ }
100
+ }
101
+ `,
102
+ );
103
+
104
+ function resolveHost(trigger: HTMLElement, host?: HTMLElement): HTMLElement {
105
+ return host ?? (trigger.closest('.vela-ui') as HTMLElement | null) ?? trigger.ownerDocument.body;
106
+ }
107
+
108
+ export class Tooltip {
109
+ private readonly positioner: HTMLElement;
110
+ private readonly content: HTMLElement;
111
+ private readonly handle: HandleOf<typeof tooltip.machine>;
112
+ private readonly trigger: HTMLElement;
113
+ private readonly release: () => void;
114
+
115
+ constructor(trigger: HTMLElement, opts: TooltipOptions) {
116
+ this.trigger = trigger;
117
+ const doc = trigger.ownerDocument;
118
+ injectStyles(doc);
119
+ this.content = h('div', { className: 'vela-tooltip', slot: tooltipDef.slot('content') });
120
+ this.positioner = h('div', { className: 'vela-ui-layer', slot: tooltipDef.slot('positioner') }, this.content);
121
+ resolveHost(trigger, opts.host).appendChild(this.positioner);
122
+ this.setContent(opts.content);
123
+
124
+ const ctrl = tooltipController(opts);
125
+ // Scope every spread by this machine's id so several kit components can share a
126
+ // node (e.g. tooltip + menu on one button) without wiping each other's listeners.
127
+ const mid = String(ctrl.props.id);
128
+ if (opts.triggerId) trigger.id = opts.triggerId;
129
+ this.handle = runMachine(ctrl.machine, ctrl.props, (service) => {
130
+ const api = ctrl.connect(service);
131
+ spreadProps(trigger, api.getTriggerProps(), mid);
132
+ spreadProps(this.positioner, api.getPositionerProps(), mid);
133
+ spreadProps(this.content, api.getContentProps(), mid);
134
+ });
135
+ // On the trigger: discarding the trigger's subtree takes the tooltip, and the layer it
136
+ // mounted elsewhere, with it.
137
+ this.release = ownTeardown(trigger, () => {
138
+ this.handle.stop();
139
+ this.positioner.remove();
140
+ // Zag leaves aria-describedby etc. on the trigger; harmless, but drop the hook.
141
+ this.trigger.removeAttribute('data-scope');
142
+ });
143
+ }
144
+
145
+ setContent(content: string | Node | (() => Node)): void {
146
+ this.content.replaceChildren(typeof content === 'function' ? content() : content);
147
+ }
148
+
149
+ destroy(): void {
150
+ this.release();
151
+ }
152
+ }
153
+
154
+ export function createTooltip(props: TooltipProps): Tooltip {
155
+ return new Tooltip(props.trigger, props);
156
+ }
@@ -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.