@luxalgo/vela-ui 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/CHANGELOG.md +153 -132
  2. package/LICENSE +201 -201
  3. package/NOTICE +28 -28
  4. package/README.md +86 -86
  5. package/cli/engine.mjs +282 -282
  6. package/cli/index.mjs +154 -154
  7. package/dist/callout-bubble.js +2 -2
  8. package/dist/checkbox.js +2 -2
  9. package/dist/{chunk-KKS6NEGI.js → chunk-BWSOMG3G.js} +1 -1
  10. package/dist/chunk-GUIYFQ27.js +30 -0
  11. package/dist/{chunk-B2EZB5ZG.js → chunk-KUWTI2PE.js} +2 -2
  12. package/dist/{chunk-WWHNUKSZ.js → chunk-QGWAN3EP.js} +2 -2
  13. package/dist/{chunk-TBXUF5YX.js → chunk-RH53NQGE.js} +18 -4
  14. package/dist/{chunk-IOOYDSSK.js → chunk-SKFAYTDK.js} +1 -1
  15. package/dist/{chunk-OQXFQMZM.js → chunk-TGJNHUYV.js} +2 -2
  16. package/dist/{chunk-UEO6W37Z.js → chunk-VC7SVSDK.js} +1 -1
  17. package/dist/{chunk-OJ2SEMN6.js → chunk-YT2BSP7O.js} +1 -1
  18. package/dist/collapsible.js +1 -1
  19. package/dist/color-picker.js +4 -4
  20. package/dist/context-menu.js +3 -3
  21. package/dist/dialog.js +3 -3
  22. package/dist/donut.d.ts +1 -1
  23. package/dist/drawer.js +1 -1
  24. package/dist/dropdown-menu.js +3 -3
  25. package/dist/field.js +8 -8
  26. package/dist/{stat.d.ts → figure.d.ts} +9 -9
  27. package/dist/figure.js +41 -0
  28. package/dist/glyph-select.js +3 -3
  29. package/dist/index.d.ts +2 -2
  30. package/dist/index.js +1 -1
  31. package/dist/manifest.json +74 -74
  32. package/dist/number-input.js +2 -2
  33. package/dist/popover.js +2 -2
  34. package/dist/select.d.ts +1 -1
  35. package/dist/select.js +4 -4
  36. package/dist/slider.js +1 -1
  37. package/dist/sort-button.js +1 -1
  38. package/dist/switch.js +1 -1
  39. package/dist/tabs.js +1 -1
  40. package/dist/{theme-Ct-q58xz.d.ts → theme-TefB8vtl.d.ts} +2 -2
  41. package/dist/theme.d.ts +2 -2
  42. package/dist/theme.js +1 -1
  43. package/dist/theme.schema.json +30 -30
  44. package/dist/toast.d.ts +2 -2
  45. package/dist/toast.js +1 -1
  46. package/dist/toggle-group.js +1 -1
  47. package/dist/tooltip.js +1 -1
  48. package/dist/zag.d.ts +11 -2
  49. package/dist/zag.js +1 -1
  50. package/docs/theming.md +217 -217
  51. package/docs/what-to-change.md +20 -20
  52. package/docs/writing-a-component.md +250 -250
  53. package/llms.txt +3 -3
  54. package/package.json +90 -90
  55. package/registry/badge.ts +119 -119
  56. package/registry/bar-chart.ts +215 -215
  57. package/registry/button.ts +387 -387
  58. package/registry/callout-bubble.ts +347 -347
  59. package/registry/card.ts +379 -379
  60. package/registry/checkbox.ts +234 -234
  61. package/registry/collapsible.ts +271 -271
  62. package/registry/color-picker.ts +582 -582
  63. package/registry/context-menu.ts +208 -208
  64. package/registry/data-table.ts +215 -215
  65. package/registry/date-picker.ts +483 -483
  66. package/registry/dialog.ts +551 -551
  67. package/registry/donut.ts +145 -145
  68. package/registry/drawer.ts +636 -636
  69. package/registry/dropdown-menu.ts +762 -762
  70. package/registry/empty.ts +91 -91
  71. package/registry/field.ts +584 -584
  72. package/registry/{stat.ts → figure.ts} +96 -96
  73. package/registry/glyph-select.ts +268 -268
  74. package/registry/hover-card.ts +222 -222
  75. package/registry/input-group.ts +229 -229
  76. package/registry/input.ts +181 -181
  77. package/registry/item.ts +376 -376
  78. package/registry/kbd.ts +64 -64
  79. package/registry/line-chart.ts +218 -218
  80. package/registry/month-grid.ts +328 -328
  81. package/registry/number-input.ts +443 -443
  82. package/registry/popover.ts +368 -368
  83. package/registry/scatter-chart.ts +221 -221
  84. package/registry/select.ts +620 -620
  85. package/registry/slider.ts +259 -259
  86. package/registry/sort-button.ts +189 -189
  87. package/registry/sparkline.ts +114 -114
  88. package/registry/switch.ts +205 -205
  89. package/registry/tabs.ts +426 -426
  90. package/registry/text-area.ts +186 -186
  91. package/registry/toast.ts +965 -965
  92. package/registry/toggle-group.ts +295 -295
  93. package/registry/tooltip.ts +156 -156
  94. package/registry/treemap.ts +847 -847
  95. package/skills/theming-vela-ui/SKILL.md +137 -137
  96. package/skills/using-vela-ui-components/SKILL.md +139 -139
  97. package/skills/writing-vela-ui-components/SKILL.md +163 -163
  98. package/src/components/badge.ts +119 -119
  99. package/src/components/bar-chart.ts +215 -215
  100. package/src/components/button.ts +387 -387
  101. package/src/components/callout-bubble.ts +347 -347
  102. package/src/components/card.ts +379 -379
  103. package/src/components/checkbox.ts +234 -234
  104. package/src/components/collapsible.ts +271 -271
  105. package/src/components/color-picker.ts +582 -582
  106. package/src/components/context-menu.ts +208 -208
  107. package/src/components/data-table.ts +215 -215
  108. package/src/components/date-picker.ts +483 -483
  109. package/src/components/dialog.ts +551 -551
  110. package/src/components/donut.ts +145 -145
  111. package/src/components/drawer.ts +636 -636
  112. package/src/components/dropdown-menu.ts +762 -762
  113. package/src/components/empty.ts +91 -91
  114. package/src/components/field.ts +584 -584
  115. package/src/components/{stat.ts → figure.ts} +96 -96
  116. package/src/components/glyph-select.ts +268 -268
  117. package/src/components/hover-card.ts +222 -222
  118. package/src/components/input-group.ts +229 -229
  119. package/src/components/input.ts +181 -181
  120. package/src/components/item.ts +376 -376
  121. package/src/components/kbd.ts +64 -64
  122. package/src/components/line-chart.ts +218 -218
  123. package/src/components/month-grid.ts +328 -328
  124. package/src/components/number-input.ts +443 -443
  125. package/src/components/popover.ts +368 -368
  126. package/src/components/scatter-chart.ts +221 -221
  127. package/src/components/select.ts +620 -620
  128. package/src/components/slider.ts +259 -259
  129. package/src/components/sort-button.ts +189 -189
  130. package/src/components/sparkline.ts +114 -114
  131. package/src/components/switch.ts +205 -205
  132. package/src/components/tabs.ts +426 -426
  133. package/src/components/text-area.ts +186 -186
  134. package/src/components/toast.ts +965 -965
  135. package/src/components/toggle-group.ts +295 -295
  136. package/src/components/tooltip.ts +156 -156
  137. package/src/components/treemap.ts +847 -847
  138. package/src/index.ts +24 -24
  139. package/src/lib/calendar.ts +117 -117
  140. package/src/lib/chart.ts +72 -72
  141. package/src/lib/color.ts +89 -89
  142. package/src/lib/component-index.ts +163 -163
  143. package/src/lib/css.ts +48 -48
  144. package/src/lib/define.ts +45 -45
  145. package/src/lib/dispose.ts +36 -36
  146. package/src/lib/h.ts +55 -55
  147. package/src/lib/headless.ts +16 -16
  148. package/src/lib/i18n.ts +30 -30
  149. package/src/lib/icons.ts +244 -244
  150. package/src/lib/kit-text.ts +26 -26
  151. package/src/lib/page.ts +24 -24
  152. package/src/lib/palette.ts +81 -81
  153. package/src/lib/placement.ts +188 -188
  154. package/src/lib/scope.ts +135 -135
  155. package/src/lib/styles.ts +53 -53
  156. package/src/lib/surface-events.ts +31 -31
  157. package/src/lib/surface-exit.ts +87 -87
  158. package/src/lib/t.ts +68 -68
  159. package/src/lib/theme.ts +684 -670
  160. package/src/lib/tokens.ts +110 -110
  161. package/src/lib/variants.ts +32 -32
  162. package/src/lib/zag.ts +61 -46
  163. package/stylelint/index.mjs +60 -60
  164. package/stylelint/lint.mjs +81 -81
  165. package/stylelint/plugin.mjs +115 -115
  166. package/dist/chunk-P4MTEMT7.js +0 -23
  167. package/dist/stat.js +0 -41
package/registry/tabs.ts CHANGED
@@ -1,426 +1,426 @@
1
- // Tabs: a tab list over Zag's tabs machine (WAI-ARIA tabs: roving focus, the arrow keys along
2
- // the orientation, Home/End, automatic or manual activation). Clicks and the arrow keys
3
- // select; the caller shows what the selected tab stands for, through panels it attaches or as
4
- // a filter of one view. `default` puts the tabs on one tinted track, `line` underlines the
5
- // selected one (the indicator dialog), `filled` is the symbol picker's market strip, `soft`
6
- // the drawings drawer's category strip, `nav` a vertical settings rail. A tab with a
7
- // `description` reads on two lines, its label over it (a day strip: the weekday, then the date).
8
- import * as tabs from '@zag-js/tabs';
9
- import { css, defineStyles } from '@luxalgo/vela-ui';
10
- import { defineComponent } from '@luxalgo/vela-ui';
11
- import { ownTeardown } from '@luxalgo/vela-ui/dispose';
12
- import { h } from '@luxalgo/vela-ui';
13
- import { iconEl } from '@luxalgo/vela-ui/icons';
14
- import { t } from '@luxalgo/vela-ui';
15
- import { variants, type VariantProps } from '@luxalgo/vela-ui';
16
- import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
17
-
18
- export const tabsDef = defineComponent({
19
- name: 'tabs',
20
- parts: ['list', 'trigger', 'icon', 'count', 'label', 'description', 'content'],
21
- settings: { 'track-radius': '8px', 'trigger-radius': t.radius.md, 'filled-radius': '5px', 'soft-radius': '8px', 'count-radius': '8px' },
22
- });
23
-
24
- export const tabsVariants = variants({ variant: ['default', 'line', 'filled', 'soft', 'nav'] });
25
-
26
- export type TabsVariant = (typeof tabsVariants.spec.variant)[number];
27
-
28
- export interface TabItem {
29
- value: string;
30
- label: string;
31
- disabled?: boolean;
32
- /** Out of the strip for now (its content does not apply); the arrow keys skip it. */
33
- hidden?: boolean;
34
- /** Icon id from the kit's registry, before the label. */
35
- icon?: string;
36
- /** A count after the label (open orders, trades); null shows none. */
37
- count?: number | null;
38
- /** A second line under the label (a day strip's date under its weekday). */
39
- description?: string;
40
- }
41
-
42
- export interface TabsProps extends VariantProps<typeof tabsVariants> {
43
- items: readonly TabItem[];
44
- /** The selected tab (default: the first available one). */
45
- defaultValue?: string;
46
- onValueChange?: (value: string) => void;
47
- /** Default `horizontal` (`nav` reads best vertical). */
48
- orientation?: 'horizontal' | 'vertical';
49
- /** `automatic` (default): moving focus with the arrow keys selects. `manual`: Enter or
50
- * Space selects the focused tab. */
51
- activationMode?: 'automatic' | 'manual';
52
- /** The text direction the arrow keys follow (default `ltr`). */
53
- dir?: 'ltr' | 'rtl';
54
- /** Extra class names on the list (the host's layout of the strip). */
55
- className?: string;
56
- /** Extra class names on every trigger. */
57
- triggerClassName?: string;
58
- }
59
-
60
- export interface TabsHandle {
61
- readonly el: HTMLElement;
62
- /** The selected tab. */
63
- readonly value: string | null;
64
- /** Show a tab as selected without calling `onValueChange`. */
65
- setValue(value: string | null): void;
66
- setItems(items: readonly TabItem[]): void;
67
- /** Hide or show a tab. Hiding the selected tab selects the first available one (and
68
- * calls `onValueChange`). */
69
- setHidden(value: string, hidden: boolean): void;
70
- /** A tab's count (null: none). */
71
- setCount(value: string, count: number | null): void;
72
- /** The trigger of a tab (null: unknown value). */
73
- trigger(value: string): HTMLButtonElement | null;
74
- /** Make `el` the panel of tab `value`: it gets the tabpanel role, its link to the trigger,
75
- * and `hidden` while another tab is selected. */
76
- panel(value: string, el: HTMLElement): void;
77
- destroy(): void;
78
- }
79
-
80
- /** A tab a user can reach: neither disabled nor hidden. */
81
- export const tabAvailable = (item: TabItem): boolean => !item.disabled && !item.hidden;
82
-
83
- const countEl = (count: number | null | undefined): HTMLElement | null => (count == null ? null : h('span', { slot: tabsDef.slot('count'), text: String(count) }));
84
-
85
- export const TABS_STYLE_ID = 'vela-ui-tabs';
86
-
87
- export const TABS_CSS = css`
88
- @layer vela.ui {
89
- [data-slot='tabs-icon'] {
90
- margin-inline-end: 6px;
91
- }
92
- [data-slot='tabs-count'] {
93
- min-width: 16px;
94
- height: 16px;
95
- margin-inline-start: 6px;
96
- padding: 0 5px;
97
- border-radius: ${tabsDef.vars['count-radius']};
98
- background: ${t.hover};
99
- color: ${t.fg.muted};
100
- font-size: 10px;
101
- font-weight: 600;
102
- font-variant-numeric: tabular-nums;
103
-
104
- &:where(:not([hidden])) {
105
- display: inline-flex;
106
- align-items: center;
107
- justify-content: center;
108
- }
109
- }
110
- [data-slot='tabs-list'] {
111
- display: flex;
112
-
113
- &[data-orientation='vertical'] {
114
- flex-direction: column;
115
- }
116
- &[data-variant='default'] {
117
- display: inline-flex;
118
- gap: 2px;
119
- padding: 3px;
120
- box-sizing: border-box;
121
- border-radius: ${tabsDef.vars['track-radius']};
122
- background: color-mix(in srgb, ${t.fg.bright} 6%, transparent);
123
- }
124
- &[data-variant='default'][data-orientation='horizontal'] {
125
- height: 32px;
126
- align-items: stretch;
127
- }
128
- &[data-variant='line'] {
129
- gap: 12px;
130
- border-bottom: 1px solid ${t.border};
131
- }
132
- &[data-variant='nav'] {
133
- gap: 2px;
134
- }
135
- }
136
- [data-slot='tabs-trigger']:where(:not([hidden])) {
137
- all: unset;
138
- cursor: pointer;
139
- white-space: nowrap;
140
- -webkit-tap-highlight-color: transparent;
141
-
142
- &[data-disabled] {
143
- cursor: default;
144
- opacity: 0.45;
145
- }
146
- &:focus-visible {
147
- outline: 2px solid ${t.focus};
148
- outline-offset: -2px;
149
- }
150
- &[data-variant='default'] {
151
- display: inline-flex;
152
- align-items: center;
153
- justify-content: center;
154
- padding: 0 10px;
155
- border-radius: ${tabsDef.vars['trigger-radius']};
156
- color: ${t.fg.muted};
157
- font-size: 13px;
158
- font-weight: 500;
159
- transition:
160
- background ${t.dur.fast} ease,
161
- color ${t.dur.fast} ease;
162
- }
163
- &[data-variant='default'][data-orientation='vertical'] {
164
- justify-content: flex-start;
165
- padding: 6px 10px;
166
- }
167
- &[data-variant='default']:not([data-selected], [data-disabled]):hover {
168
- color: ${t.fg.bright};
169
- }
170
- &[data-variant='default'][data-selected] {
171
- background: color-mix(in srgb, ${t.fg.bright} 12%, transparent);
172
- color: ${t.fg.bright};
173
- }
174
- &[data-variant='line'] {
175
- padding-bottom: 8px;
176
- margin-bottom: -1px;
177
- border-bottom: 2px solid transparent;
178
- font-size: 13px;
179
- font-weight: 600;
180
- line-height: 20px;
181
- color: ${t.fg.muted};
182
- transition:
183
- color ${t.dur.fast} ease,
184
- border-color ${t.dur.fast} ease;
185
- }
186
- &[data-variant='line']:not([data-selected], [data-disabled]):hover {
187
- color: ${t.fg};
188
- }
189
- &[data-variant='line'][data-selected] {
190
- color: ${t.fg};
191
- border-bottom-color: ${t.fg.bright};
192
- }
193
- &[data-variant='filled'] {
194
- padding: 3px 10px;
195
- border-radius: ${tabsDef.vars['filled-radius']};
196
- color: ${t.fg.muted};
197
- font-size: 13px;
198
- font-weight: 600;
199
- }
200
- &[data-variant='filled']:not([data-selected], [data-disabled]):hover {
201
- color: ${t.fg};
202
- }
203
- &[data-variant='filled'][data-selected] {
204
- background: ${t.selected.bg};
205
- color: ${t.selected.fg};
206
- }
207
- &[data-variant='soft'] {
208
- flex: none;
209
- min-height: 36px;
210
- padding: 0 12px;
211
- display: inline-flex;
212
- align-items: center;
213
- border-radius: ${tabsDef.vars['soft-radius']};
214
- font-size: 13px;
215
- font-weight: 600;
216
- color: ${t.fg.muted};
217
- }
218
- &[data-variant='soft'][data-selected] {
219
- color: ${t.fg.bright};
220
- background: ${t.hover};
221
- }
222
- &[data-variant='nav'] {
223
- padding: 9px 12px;
224
- border-radius: ${t.radius.md};
225
- color: ${t.fg.muted};
226
- font-size: 13px;
227
- font-weight: 600;
228
- transition:
229
- background ${t.dur.fast} ease,
230
- color ${t.dur.fast} ease;
231
- }
232
- &[data-variant='nav']:not([data-selected], [data-disabled]):hover {
233
- background: ${t.hover};
234
- color: ${t.fg.bright};
235
- }
236
- &[data-variant='nav'][data-selected] {
237
- background: ${t.active};
238
- color: ${t.fg.bright};
239
- }
240
- &[data-description] {
241
- display: inline-flex;
242
- flex-direction: column;
243
- align-items: center;
244
- justify-content: center;
245
- gap: 2px;
246
- }
247
- &[data-description]:not([data-variant='line']) {
248
- padding-block: 6px;
249
- }
250
- }
251
- [data-slot='tabs-list'][data-variant='default']:has(> [data-description]) {
252
- height: auto;
253
- }
254
- [data-slot='tabs-label'] {
255
- display: inline-flex;
256
- align-items: center;
257
- line-height: 16px;
258
- }
259
- [data-slot='tabs-description'] {
260
- font-size: 11px;
261
- font-weight: 400;
262
- line-height: 14px;
263
- opacity: 0.85;
264
- }
265
- }
266
- `;
267
-
268
- const injectStyles = defineStyles(TABS_STYLE_ID, TABS_CSS);
269
-
270
- class TabsImpl implements TabsHandle {
271
- readonly el: HTMLElement;
272
- private readonly machine: HandleOf<typeof tabs.machine>;
273
- private readonly mid: string;
274
- private readonly release: () => void;
275
- private readonly variant: TabsVariant;
276
- private readonly triggerClassName: string | undefined;
277
- private items: TabItem[];
278
- private readonly triggers = new Map<string, HTMLButtonElement>();
279
- private readonly panels = new Map<string, HTMLElement>();
280
- /** The selection as of the last write or report: the machine applies events on a
281
- * microtask, so its own value lags a write. */
282
- private current: string | null;
283
- /** A host write the machine has yet to report: that report is not the user's change. */
284
- private silentNext: string | undefined;
285
-
286
- constructor(props: TabsProps) {
287
- injectStyles(document);
288
- this.variant = tabsVariants.resolve(props).variant;
289
- this.triggerClassName = props.triggerClassName;
290
- this.items = [...props.items];
291
- this.current = props.defaultValue ?? this.items.find(tabAvailable)?.value ?? null;
292
- this.mid = nextUid('vela-tabs');
293
- this.el = h('div', {
294
- className: props.className ? `vela-tabs ${props.className}` : 'vela-tabs',
295
- slot: tabsDef.slot('list'),
296
- data: { variant: this.variant },
297
- });
298
- this.build();
299
- this.machine = runMachine(
300
- tabs.machine,
301
- {
302
- id: this.mid,
303
- defaultValue: this.current,
304
- orientation: props.orientation ?? 'horizontal',
305
- activationMode: props.activationMode ?? 'automatic',
306
- dir: props.dir ?? 'ltr',
307
- loopFocus: true,
308
- onValueChange: ({ value }) => {
309
- if (value == null) return;
310
- this.current = value;
311
- if (this.silentNext === value) {
312
- this.silentNext = undefined;
313
- return;
314
- }
315
- props.onValueChange?.(value);
316
- },
317
- },
318
- (service) => this.render(service),
319
- );
320
- this.release = ownTeardown(this.el, () => this.machine.stop());
321
- }
322
-
323
- get value(): string | null {
324
- return this.current;
325
- }
326
-
327
- setValue(value: string | null): void {
328
- if (value === this.current) return;
329
- this.current = value;
330
- if (value === null) {
331
- this.api().clearValue();
332
- return;
333
- }
334
- this.silentNext = value;
335
- this.api().setValue(value);
336
- }
337
-
338
- setItems(items: readonly TabItem[]): void {
339
- this.items = [...items];
340
- this.build();
341
- if (this.current === null || !this.items.some((i) => i.value === this.current)) this.setValue(this.items.find(tabAvailable)?.value ?? null);
342
- this.machine.flush();
343
- }
344
-
345
- setHidden(value: string, hidden: boolean): void {
346
- this.items = this.items.map((i) => (i.value === value ? { ...i, hidden } : i));
347
- if (hidden && this.current === value) {
348
- const next = this.items.find(tabAvailable)?.value;
349
- if (next !== undefined) {
350
- this.current = next;
351
- this.api().setValue(next);
352
- }
353
- }
354
- this.machine.flush();
355
- }
356
-
357
- setCount(value: string, count: number | null): void {
358
- const b = this.triggers.get(value);
359
- if (!b) return;
360
- this.items = this.items.map((i) => (i.value === value ? { ...i, count } : i));
361
- b.querySelector(`[data-slot="${tabsDef.slot('count')}"]`)?.remove();
362
- const el = countEl(count);
363
- if (el) (b.querySelector(`[data-slot="${tabsDef.slot('label')}"]`) ?? b).appendChild(el);
364
- }
365
-
366
- trigger(value: string): HTMLButtonElement | null {
367
- return this.triggers.get(value) ?? null;
368
- }
369
-
370
- panel(value: string, el: HTMLElement): void {
371
- el.dataset.slot = tabsDef.slot('content');
372
- this.panels.set(value, el);
373
- this.machine.flush();
374
- }
375
-
376
- destroy(): void {
377
- this.release();
378
- }
379
-
380
- private api(): tabs.Api {
381
- return tabs.connect(this.machine.service, normalizeProps);
382
- }
383
-
384
- private build(): void {
385
- this.triggers.clear();
386
- const className = this.triggerClassName ? `vela-tab ${this.triggerClassName}` : 'vela-tab';
387
- this.el.replaceChildren(
388
- ...this.items.map((item) => {
389
- let icon: HTMLElement | null = null;
390
- if (item.icon) {
391
- icon = iconEl(item.icon, document);
392
- icon.dataset.slot = tabsDef.slot('icon');
393
- }
394
- const b = item.description
395
- ? h(
396
- 'button',
397
- { className, slot: tabsDef.slot('trigger'), data: { variant: this.variant, description: true } },
398
- h('span', { slot: tabsDef.slot('label') }, icon, item.label, countEl(item.count)),
399
- h('span', { slot: tabsDef.slot('description'), text: item.description }),
400
- )
401
- : h('button', { className, slot: tabsDef.slot('trigger'), data: { variant: this.variant } }, icon, item.label, countEl(item.count));
402
- this.triggers.set(item.value, b);
403
- return b;
404
- }),
405
- );
406
- }
407
-
408
- private render(service: tabs.Service): void {
409
- const api = tabs.connect(service, normalizeProps);
410
- spreadProps(this.el, api.getListProps(), this.mid);
411
- for (const item of this.items) {
412
- const b = this.triggers.get(item.value);
413
- if (!b) continue;
414
- // A hidden tab is a disabled trigger to the machine, so the arrow keys skip it.
415
- const props = api.getTriggerProps({ value: item.value, disabled: !tabAvailable(item) }) as Record<string, unknown>;
416
- if (!this.panels.has(item.value)) delete props['aria-controls'];
417
- spreadProps(b, props, this.mid);
418
- b.hidden = item.hidden === true;
419
- }
420
- for (const [value, el] of this.panels) spreadProps(el, api.getContentProps({ value }), this.mid);
421
- }
422
- }
423
-
424
- export function createTabs(props: TabsProps): TabsHandle {
425
- return new TabsImpl(props);
426
- }
1
+ // Tabs: a tab list over Zag's tabs machine (WAI-ARIA tabs: roving focus, the arrow keys along
2
+ // the orientation, Home/End, automatic or manual activation). Clicks and the arrow keys
3
+ // select; the caller shows what the selected tab stands for, through panels it attaches or as
4
+ // a filter of one view. `default` puts the tabs on one tinted track, `line` underlines the
5
+ // selected one (the indicator dialog), `filled` is the symbol picker's market strip, `soft`
6
+ // the drawings drawer's category strip, `nav` a vertical settings rail. A tab with a
7
+ // `description` reads on two lines, its label over it (a day strip: the weekday, then the date).
8
+ import * as tabs from '@zag-js/tabs';
9
+ import { css, defineStyles } from '@luxalgo/vela-ui';
10
+ import { defineComponent } from '@luxalgo/vela-ui';
11
+ import { ownTeardown } from '@luxalgo/vela-ui/dispose';
12
+ import { h } from '@luxalgo/vela-ui';
13
+ import { iconEl } from '@luxalgo/vela-ui/icons';
14
+ import { t } from '@luxalgo/vela-ui';
15
+ import { variants, type VariantProps } from '@luxalgo/vela-ui';
16
+ import { nextUid, normalizeProps, runMachine, spreadProps, type HandleOf } from '@luxalgo/vela-ui/zag';
17
+
18
+ export const tabsDef = defineComponent({
19
+ name: 'tabs',
20
+ parts: ['list', 'trigger', 'icon', 'count', 'label', 'description', 'content'],
21
+ settings: { 'track-radius': '8px', 'trigger-radius': t.radius.md, 'filled-radius': '5px', 'soft-radius': '8px', 'count-radius': '8px' },
22
+ });
23
+
24
+ export const tabsVariants = variants({ variant: ['default', 'line', 'filled', 'soft', 'nav'] });
25
+
26
+ export type TabsVariant = (typeof tabsVariants.spec.variant)[number];
27
+
28
+ export interface TabItem {
29
+ value: string;
30
+ label: string;
31
+ disabled?: boolean;
32
+ /** Out of the strip for now (its content does not apply); the arrow keys skip it. */
33
+ hidden?: boolean;
34
+ /** Icon id from the kit's registry, before the label. */
35
+ icon?: string;
36
+ /** A count after the label (open orders, trades); null shows none. */
37
+ count?: number | null;
38
+ /** A second line under the label (a day strip's date under its weekday). */
39
+ description?: string;
40
+ }
41
+
42
+ export interface TabsProps extends VariantProps<typeof tabsVariants> {
43
+ items: readonly TabItem[];
44
+ /** The selected tab (default: the first available one). */
45
+ defaultValue?: string;
46
+ onValueChange?: (value: string) => void;
47
+ /** Default `horizontal` (`nav` reads best vertical). */
48
+ orientation?: 'horizontal' | 'vertical';
49
+ /** `automatic` (default): moving focus with the arrow keys selects. `manual`: Enter or
50
+ * Space selects the focused tab. */
51
+ activationMode?: 'automatic' | 'manual';
52
+ /** The text direction the arrow keys follow (default `ltr`). */
53
+ dir?: 'ltr' | 'rtl';
54
+ /** Extra class names on the list (the host's layout of the strip). */
55
+ className?: string;
56
+ /** Extra class names on every trigger. */
57
+ triggerClassName?: string;
58
+ }
59
+
60
+ export interface TabsHandle {
61
+ readonly el: HTMLElement;
62
+ /** The selected tab. */
63
+ readonly value: string | null;
64
+ /** Show a tab as selected without calling `onValueChange`. */
65
+ setValue(value: string | null): void;
66
+ setItems(items: readonly TabItem[]): void;
67
+ /** Hide or show a tab. Hiding the selected tab selects the first available one (and
68
+ * calls `onValueChange`). */
69
+ setHidden(value: string, hidden: boolean): void;
70
+ /** A tab's count (null: none). */
71
+ setCount(value: string, count: number | null): void;
72
+ /** The trigger of a tab (null: unknown value). */
73
+ trigger(value: string): HTMLButtonElement | null;
74
+ /** Make `el` the panel of tab `value`: it gets the tabpanel role, its link to the trigger,
75
+ * and `hidden` while another tab is selected. */
76
+ panel(value: string, el: HTMLElement): void;
77
+ destroy(): void;
78
+ }
79
+
80
+ /** A tab a user can reach: neither disabled nor hidden. */
81
+ export const tabAvailable = (item: TabItem): boolean => !item.disabled && !item.hidden;
82
+
83
+ const countEl = (count: number | null | undefined): HTMLElement | null => (count == null ? null : h('span', { slot: tabsDef.slot('count'), text: String(count) }));
84
+
85
+ export const TABS_STYLE_ID = 'vela-ui-tabs';
86
+
87
+ export const TABS_CSS = css`
88
+ @layer vela.ui {
89
+ [data-slot='tabs-icon'] {
90
+ margin-inline-end: 6px;
91
+ }
92
+ [data-slot='tabs-count'] {
93
+ min-width: 16px;
94
+ height: 16px;
95
+ margin-inline-start: 6px;
96
+ padding: 0 5px;
97
+ border-radius: ${tabsDef.vars['count-radius']};
98
+ background: ${t.hover};
99
+ color: ${t.fg.muted};
100
+ font-size: 10px;
101
+ font-weight: 600;
102
+ font-variant-numeric: tabular-nums;
103
+
104
+ &:where(:not([hidden])) {
105
+ display: inline-flex;
106
+ align-items: center;
107
+ justify-content: center;
108
+ }
109
+ }
110
+ [data-slot='tabs-list'] {
111
+ display: flex;
112
+
113
+ &[data-orientation='vertical'] {
114
+ flex-direction: column;
115
+ }
116
+ &[data-variant='default'] {
117
+ display: inline-flex;
118
+ gap: 2px;
119
+ padding: 3px;
120
+ box-sizing: border-box;
121
+ border-radius: ${tabsDef.vars['track-radius']};
122
+ background: color-mix(in srgb, ${t.fg.bright} 6%, transparent);
123
+ }
124
+ &[data-variant='default'][data-orientation='horizontal'] {
125
+ height: 32px;
126
+ align-items: stretch;
127
+ }
128
+ &[data-variant='line'] {
129
+ gap: 12px;
130
+ border-bottom: 1px solid ${t.border};
131
+ }
132
+ &[data-variant='nav'] {
133
+ gap: 2px;
134
+ }
135
+ }
136
+ [data-slot='tabs-trigger']:where(:not([hidden])) {
137
+ all: unset;
138
+ cursor: pointer;
139
+ white-space: nowrap;
140
+ -webkit-tap-highlight-color: transparent;
141
+
142
+ &[data-disabled] {
143
+ cursor: default;
144
+ opacity: 0.45;
145
+ }
146
+ &:focus-visible {
147
+ outline: 2px solid ${t.focus};
148
+ outline-offset: -2px;
149
+ }
150
+ &[data-variant='default'] {
151
+ display: inline-flex;
152
+ align-items: center;
153
+ justify-content: center;
154
+ padding: 0 10px;
155
+ border-radius: ${tabsDef.vars['trigger-radius']};
156
+ color: ${t.fg.muted};
157
+ font-size: 13px;
158
+ font-weight: 500;
159
+ transition:
160
+ background ${t.dur.fast} ease,
161
+ color ${t.dur.fast} ease;
162
+ }
163
+ &[data-variant='default'][data-orientation='vertical'] {
164
+ justify-content: flex-start;
165
+ padding: 6px 10px;
166
+ }
167
+ &[data-variant='default']:not([data-selected], [data-disabled]):hover {
168
+ color: ${t.fg.bright};
169
+ }
170
+ &[data-variant='default'][data-selected] {
171
+ background: color-mix(in srgb, ${t.fg.bright} 12%, transparent);
172
+ color: ${t.fg.bright};
173
+ }
174
+ &[data-variant='line'] {
175
+ padding-bottom: 8px;
176
+ margin-bottom: -1px;
177
+ border-bottom: 2px solid transparent;
178
+ font-size: 13px;
179
+ font-weight: 600;
180
+ line-height: 20px;
181
+ color: ${t.fg.muted};
182
+ transition:
183
+ color ${t.dur.fast} ease,
184
+ border-color ${t.dur.fast} ease;
185
+ }
186
+ &[data-variant='line']:not([data-selected], [data-disabled]):hover {
187
+ color: ${t.fg};
188
+ }
189
+ &[data-variant='line'][data-selected] {
190
+ color: ${t.fg};
191
+ border-bottom-color: ${t.fg.bright};
192
+ }
193
+ &[data-variant='filled'] {
194
+ padding: 3px 10px;
195
+ border-radius: ${tabsDef.vars['filled-radius']};
196
+ color: ${t.fg.muted};
197
+ font-size: 13px;
198
+ font-weight: 600;
199
+ }
200
+ &[data-variant='filled']:not([data-selected], [data-disabled]):hover {
201
+ color: ${t.fg};
202
+ }
203
+ &[data-variant='filled'][data-selected] {
204
+ background: ${t.selected.bg};
205
+ color: ${t.selected.fg};
206
+ }
207
+ &[data-variant='soft'] {
208
+ flex: none;
209
+ min-height: 36px;
210
+ padding: 0 12px;
211
+ display: inline-flex;
212
+ align-items: center;
213
+ border-radius: ${tabsDef.vars['soft-radius']};
214
+ font-size: 13px;
215
+ font-weight: 600;
216
+ color: ${t.fg.muted};
217
+ }
218
+ &[data-variant='soft'][data-selected] {
219
+ color: ${t.fg.bright};
220
+ background: ${t.hover};
221
+ }
222
+ &[data-variant='nav'] {
223
+ padding: 9px 12px;
224
+ border-radius: ${t.radius.md};
225
+ color: ${t.fg.muted};
226
+ font-size: 13px;
227
+ font-weight: 600;
228
+ transition:
229
+ background ${t.dur.fast} ease,
230
+ color ${t.dur.fast} ease;
231
+ }
232
+ &[data-variant='nav']:not([data-selected], [data-disabled]):hover {
233
+ background: ${t.hover};
234
+ color: ${t.fg.bright};
235
+ }
236
+ &[data-variant='nav'][data-selected] {
237
+ background: ${t.active};
238
+ color: ${t.fg.bright};
239
+ }
240
+ &[data-description] {
241
+ display: inline-flex;
242
+ flex-direction: column;
243
+ align-items: center;
244
+ justify-content: center;
245
+ gap: 2px;
246
+ }
247
+ &[data-description]:not([data-variant='line']) {
248
+ padding-block: 6px;
249
+ }
250
+ }
251
+ [data-slot='tabs-list'][data-variant='default']:has(> [data-description]) {
252
+ height: auto;
253
+ }
254
+ [data-slot='tabs-label'] {
255
+ display: inline-flex;
256
+ align-items: center;
257
+ line-height: 16px;
258
+ }
259
+ [data-slot='tabs-description'] {
260
+ font-size: 11px;
261
+ font-weight: 400;
262
+ line-height: 14px;
263
+ opacity: 0.85;
264
+ }
265
+ }
266
+ `;
267
+
268
+ const injectStyles = defineStyles(TABS_STYLE_ID, TABS_CSS);
269
+
270
+ class TabsImpl implements TabsHandle {
271
+ readonly el: HTMLElement;
272
+ private readonly machine: HandleOf<typeof tabs.machine>;
273
+ private readonly mid: string;
274
+ private readonly release: () => void;
275
+ private readonly variant: TabsVariant;
276
+ private readonly triggerClassName: string | undefined;
277
+ private items: TabItem[];
278
+ private readonly triggers = new Map<string, HTMLButtonElement>();
279
+ private readonly panels = new Map<string, HTMLElement>();
280
+ /** The selection as of the last write or report: the machine applies events on a
281
+ * microtask, so its own value lags a write. */
282
+ private current: string | null;
283
+ /** A host write the machine has yet to report: that report is not the user's change. */
284
+ private silentNext: string | undefined;
285
+
286
+ constructor(props: TabsProps) {
287
+ injectStyles(document);
288
+ this.variant = tabsVariants.resolve(props).variant;
289
+ this.triggerClassName = props.triggerClassName;
290
+ this.items = [...props.items];
291
+ this.current = props.defaultValue ?? this.items.find(tabAvailable)?.value ?? null;
292
+ this.mid = nextUid('vela-tabs');
293
+ this.el = h('div', {
294
+ className: props.className ? `vela-tabs ${props.className}` : 'vela-tabs',
295
+ slot: tabsDef.slot('list'),
296
+ data: { variant: this.variant },
297
+ });
298
+ this.build();
299
+ this.machine = runMachine(
300
+ tabs.machine,
301
+ {
302
+ id: this.mid,
303
+ defaultValue: this.current,
304
+ orientation: props.orientation ?? 'horizontal',
305
+ activationMode: props.activationMode ?? 'automatic',
306
+ dir: props.dir ?? 'ltr',
307
+ loopFocus: true,
308
+ onValueChange: ({ value }) => {
309
+ if (value == null) return;
310
+ this.current = value;
311
+ if (this.silentNext === value) {
312
+ this.silentNext = undefined;
313
+ return;
314
+ }
315
+ props.onValueChange?.(value);
316
+ },
317
+ },
318
+ (service) => this.render(service),
319
+ );
320
+ this.release = ownTeardown(this.el, () => this.machine.stop());
321
+ }
322
+
323
+ get value(): string | null {
324
+ return this.current;
325
+ }
326
+
327
+ setValue(value: string | null): void {
328
+ if (value === this.current) return;
329
+ this.current = value;
330
+ if (value === null) {
331
+ this.api().clearValue();
332
+ return;
333
+ }
334
+ this.silentNext = value;
335
+ this.api().setValue(value);
336
+ }
337
+
338
+ setItems(items: readonly TabItem[]): void {
339
+ this.items = [...items];
340
+ this.build();
341
+ if (this.current === null || !this.items.some((i) => i.value === this.current)) this.setValue(this.items.find(tabAvailable)?.value ?? null);
342
+ this.machine.flush();
343
+ }
344
+
345
+ setHidden(value: string, hidden: boolean): void {
346
+ this.items = this.items.map((i) => (i.value === value ? { ...i, hidden } : i));
347
+ if (hidden && this.current === value) {
348
+ const next = this.items.find(tabAvailable)?.value;
349
+ if (next !== undefined) {
350
+ this.current = next;
351
+ this.api().setValue(next);
352
+ }
353
+ }
354
+ this.machine.flush();
355
+ }
356
+
357
+ setCount(value: string, count: number | null): void {
358
+ const b = this.triggers.get(value);
359
+ if (!b) return;
360
+ this.items = this.items.map((i) => (i.value === value ? { ...i, count } : i));
361
+ b.querySelector(`[data-slot="${tabsDef.slot('count')}"]`)?.remove();
362
+ const el = countEl(count);
363
+ if (el) (b.querySelector(`[data-slot="${tabsDef.slot('label')}"]`) ?? b).appendChild(el);
364
+ }
365
+
366
+ trigger(value: string): HTMLButtonElement | null {
367
+ return this.triggers.get(value) ?? null;
368
+ }
369
+
370
+ panel(value: string, el: HTMLElement): void {
371
+ el.dataset.slot = tabsDef.slot('content');
372
+ this.panels.set(value, el);
373
+ this.machine.flush();
374
+ }
375
+
376
+ destroy(): void {
377
+ this.release();
378
+ }
379
+
380
+ private api(): tabs.Api {
381
+ return tabs.connect(this.machine.service, normalizeProps);
382
+ }
383
+
384
+ private build(): void {
385
+ this.triggers.clear();
386
+ const className = this.triggerClassName ? `vela-tab ${this.triggerClassName}` : 'vela-tab';
387
+ this.el.replaceChildren(
388
+ ...this.items.map((item) => {
389
+ let icon: HTMLElement | null = null;
390
+ if (item.icon) {
391
+ icon = iconEl(item.icon, document);
392
+ icon.dataset.slot = tabsDef.slot('icon');
393
+ }
394
+ const b = item.description
395
+ ? h(
396
+ 'button',
397
+ { className, slot: tabsDef.slot('trigger'), data: { variant: this.variant, description: true } },
398
+ h('span', { slot: tabsDef.slot('label') }, icon, item.label, countEl(item.count)),
399
+ h('span', { slot: tabsDef.slot('description'), text: item.description }),
400
+ )
401
+ : h('button', { className, slot: tabsDef.slot('trigger'), data: { variant: this.variant } }, icon, item.label, countEl(item.count));
402
+ this.triggers.set(item.value, b);
403
+ return b;
404
+ }),
405
+ );
406
+ }
407
+
408
+ private render(service: tabs.Service): void {
409
+ const api = tabs.connect(service, normalizeProps);
410
+ spreadProps(this.el, api.getListProps(), this.mid);
411
+ for (const item of this.items) {
412
+ const b = this.triggers.get(item.value);
413
+ if (!b) continue;
414
+ // A hidden tab is a disabled trigger to the machine, so the arrow keys skip it.
415
+ const props = api.getTriggerProps({ value: item.value, disabled: !tabAvailable(item) }) as Record<string, unknown>;
416
+ if (!this.panels.has(item.value)) delete props['aria-controls'];
417
+ spreadProps(b, props, this.mid);
418
+ b.hidden = item.hidden === true;
419
+ }
420
+ for (const [value, el] of this.panels) spreadProps(el, api.getContentProps({ value }), this.mid);
421
+ }
422
+ }
423
+
424
+ export function createTabs(props: TabsProps): TabsHandle {
425
+ return new TabsImpl(props);
426
+ }