@luxalgo/vela-ui 0.3.0 → 0.4.1

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