@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
@@ -1,620 +1,629 @@
1
- // Select: a trigger button and a themed option list on a popover, with an overlay scrollbar
2
- // for long lists. `md` is the shared 100px kit column and ellipsizes the trigger; `sm` is a
3
- // compact toolbar select that hugs its widest option. The open list shows full labels.
4
- import type { ChartTheme } from '../lib/tokens';
5
- import { iconEl } from '../lib/icons';
6
- import { css, defineStyles } from '../lib/css';
7
- import { defineComponent } from '../lib/define';
8
- import { ownTeardown } from '../lib/dispose';
9
- import { addSlot, h } from '../lib/h';
10
- import { t } from '../lib/t';
11
- import { variants } from '../lib/variants';
12
- import { nextUid, normalizeProps, runMachine, spreadProps } from '../lib/zag';
13
- import * as zagListbox from '@zag-js/listbox';
14
- import { Popover, closeOpenPopovers, openPopoverTrigger, type PlacementProps, type PopoverBoundary, type PopoverOptions } from './popover';
15
-
16
- export type SelectSize = 'sm' | 'md';
17
-
18
- export interface SelectOption {
19
- value: string;
20
- label: string;
21
- }
22
-
23
- export interface SelectControllerOptions {
24
- options: readonly SelectOption[];
25
- value?: string;
26
- /** The value it starts with when `value` is not given. */
27
- defaultValue?: string;
28
- /** Shown while no option is picked: the value stays `''` until the user picks one. Without
29
- * it, the first option is picked to start with. */
30
- placeholder?: string;
31
- size?: SelectSize;
32
- /** Stretch the trigger to its parent. Off: the shared 100px kit column (ellipsis). */
33
- fill?: boolean;
34
- disabled?: boolean;
35
- /** Called when the user picks an option. */
36
- onValueChange?: (value: string, label: string) => void;
37
- /** @deprecated Use `onValueChange`. */
38
- onChange?: (value: string, label: string) => void;
39
- }
40
-
41
- export interface SelectController {
42
- options: readonly SelectOption[];
43
- value: string;
44
- size: SelectSize;
45
- fill: boolean;
46
- disabled: boolean;
47
- setValue(v: string): void;
48
- labelOf(value: string): string;
49
- pick(value: string): { value: string; label: string };
50
- }
51
-
52
- /** The select's options and current value, without DOM. */
53
- export function selectController(opts: SelectControllerOptions): SelectController {
54
- const options = opts.options;
55
- let value = opts.value ?? opts.defaultValue ?? (opts.placeholder !== undefined ? '' : (options[0]?.value ?? ''));
56
- const size = opts.size ?? 'md';
57
- return {
58
- options,
59
- get value() {
60
- return value;
61
- },
62
- size,
63
- fill: opts.fill ?? size !== 'sm',
64
- disabled: opts.disabled ?? false,
65
- setValue(v: string) {
66
- value = v;
67
- },
68
- labelOf(v: string) {
69
- return options.find((o) => o.value === v)?.label ?? v;
70
- },
71
- pick(v: string) {
72
- value = v;
73
- const label = options.find((o) => o.value === v)?.label ?? v;
74
- opts.onValueChange?.(v, label);
75
- opts.onChange?.(v, label);
76
- return { value: v, label };
77
- },
78
- };
79
- }
80
-
81
- export const selectDef = defineComponent({
82
- name: 'select',
83
- parts: ['root', 'trigger', 'value', 'icon', 'sizer', 'native', 'content', 'viewport', 'item', 'rail', 'thumb'],
84
- settings: { radius: t.radius.md, 'content-radius': t.radius.md, 'thumb-radius': '2px', 'focus-ring': `0 0 0 3px ${t.focus.soft}` },
85
- });
86
-
87
- export const selectVariants = variants({ size: ['md', 'sm'] });
88
-
89
- /** Where the list opens and how (shadcn's placement props, defaults below and start-aligned,
90
- * 4px away, as wide as the trigger at least). */
91
- export interface SelectListPopoverOpts extends PlacementProps {
92
- theme?: ChartTheme;
93
- matchWidth?: boolean;
94
- /** @deprecated Use `collisionBoundary`. */
95
- boundary?: PopoverBoundary;
96
- boundaryInset?: number;
97
- /** @deprecated Use `sideOffset`. */
98
- gap?: number;
99
- host?: HTMLElement;
100
- position?: PopoverOptions['position'];
101
- zIndex?: string | number;
102
- size?: SelectSize;
103
- /** Move the focus into the open list, for the arrow keys and typeahead (and back to the
104
- * trigger when it closes). Off by default: a trigger the user types in keeps the focus. */
105
- focusList?: boolean;
106
- onClose?: () => void;
107
- }
108
-
109
- export interface SelectOptions extends SelectControllerOptions, PlacementProps {
110
- id?: string;
111
- /** Submitted with its form under this name: a hidden native select carries the value. */
112
- name?: string;
113
- /** A form does not submit while no option is picked (with `placeholder`). */
114
- required?: boolean;
115
- theme?: ChartTheme;
116
- /** The open list's other options (its host, layer, width). Its placement props are the
117
- * select's own; set here, they give way to those. */
118
- list?: SelectListPopoverOpts;
119
- }
120
-
121
- export type SelectProps = SelectOptions;
122
-
123
- export const SELECT_STYLE_ID = 'vela-ui-select-3';
124
-
125
- export const SELECT_CSS = css`
126
- @layer vela.ui {
127
- [data-slot='select-trigger'] {
128
- display: flex;
129
- align-items: center;
130
- width: 100%;
131
- min-width: 0;
132
- box-sizing: border-box;
133
- height: 34px;
134
- padding-block: 0;
135
- padding-inline: 8px 26px;
136
- background: transparent;
137
- border: 1px solid ${t.border.strong};
138
- border-radius: ${selectDef.vars.radius};
139
- color: ${t.fg.bright};
140
- font-size: 14px;
141
- font-family: inherit;
142
- cursor: pointer;
143
- text-align: start;
144
- outline: 2px solid transparent;
145
- transition:
146
- border-color ${t.dur.fast} ease,
147
- box-shadow ${t.dur.fast} ease;
148
-
149
- &:hover {
150
- border-color: ${t.fg.muted};
151
- }
152
- &:focus {
153
- border-color: ${t.focus};
154
- box-shadow: ${selectDef.vars['focus-ring']};
155
- }
156
- }
157
- [data-slot='select-value'] {
158
- flex: 1;
159
- min-width: 0;
160
- overflow: hidden;
161
- text-overflow: ellipsis;
162
- white-space: nowrap;
163
- }
164
- [data-slot='select-icon'] {
165
- position: absolute;
166
- inset-inline-end: 8px;
167
- top: 50%;
168
- transform: translateY(-50%);
169
- pointer-events: none;
170
- display: flex;
171
- opacity: 0.55;
172
- line-height: 0;
173
- color: inherit;
174
-
175
- & > span,
176
- & svg {
177
- width: 12px;
178
- height: 12px;
179
- display: block;
180
- }
181
- }
182
- [data-slot='select-sizer'] {
183
- visibility: hidden;
184
- height: 0;
185
- overflow: hidden;
186
- font-size: 13px;
187
-
188
- & span {
189
- display: block;
190
- height: 0;
191
- white-space: nowrap;
192
- padding-block: 0;
193
- padding-inline: 8px 26px;
194
- border-inline: 1px solid transparent;
195
- }
196
- }
197
- [data-slot='select-viewport'] {
198
- width: max-content;
199
- min-width: 100%;
200
- max-height: none;
201
- overflow: hidden;
202
- }
203
- [data-slot='select-rail'] {
204
- position: relative;
205
- flex: none;
206
- width: 3px;
207
- margin-block: 2px;
208
- margin-inline: 0 1px;
209
- pointer-events: none;
210
- }
211
- [data-slot='select-thumb'] {
212
- width: 3px;
213
- border-radius: ${selectDef.vars['thumb-radius']};
214
- background: ${t.scroll};
215
- }
216
- [data-slot='select-item'] {
217
- display: block;
218
- width: auto;
219
- min-width: 100%;
220
- white-space: nowrap;
221
- text-align: start;
222
- padding: 7px 10px;
223
- border: none;
224
- border-radius: ${t.radius.sm};
225
- background: transparent;
226
- color: inherit;
227
- cursor: pointer;
228
- font: inherit;
229
- font-weight: 400;
230
-
231
- &:hover,
232
- &[data-highlighted] {
233
- background: ${t.hover};
234
- }
235
- &[data-checked] {
236
- background: ${t.hover.strong};
237
- color: ${t.fg.bright};
238
- }
239
- &[data-checked]:hover,
240
- &[data-checked][data-highlighted] {
241
- background: ${t.hover.strong};
242
- }
243
- }
244
- /* The highlighted option shows where the keyboard is; the list itself draws no ring. */
245
- [data-slot='select-viewport']:focus {
246
- outline: 2px solid transparent;
247
- }
248
- [data-slot='select-value'][data-placeholder] {
249
- color: ${t.fg.muted};
250
- }
251
- /* Seen by forms and their validation, never by the user. */
252
- [data-slot='select-native'] {
253
- position: absolute;
254
- width: 1px;
255
- height: 1px;
256
- margin: -1px;
257
- padding: 0;
258
- border: 0;
259
- overflow: hidden;
260
- clip-path: inset(50%);
261
- white-space: nowrap;
262
- pointer-events: none;
263
- }
264
- [data-slot='select'] {
265
- position: relative;
266
- min-width: 0;
267
-
268
- &:where(:not([hidden])) {
269
- display: inline-block;
270
- }
271
- &[data-fill] {
272
- width: 100%;
273
- }
274
- /* The closed trigger matches NumberInput and TextField (100px); long labels
275
- ellipsize, and the open list still sizes to the longest item. */
276
- &:not([data-fill]) {
277
- width: 100px;
278
- flex: none;
279
- justify-self: start;
280
- }
281
- &[data-size='sm'] [data-slot='select-trigger'] {
282
- height: 28px;
283
- background: ${t.surface.elev};
284
- border-radius: ${t.radius.sm};
285
- color: ${t.fg};
286
- font-size: 13px;
287
- box-shadow: none;
288
- }
289
- &[data-size='sm'] [data-slot='select-trigger']:focus {
290
- box-shadow: none;
291
- border-color: ${t.fg.muted};
292
- }
293
- /* Compact toolbar selects still hug the widest option (no 100px kit column). */
294
- &[data-size='sm']:not([data-fill]) {
295
- width: auto;
296
- max-width: 200px;
297
- }
298
- }
299
- [data-slot~='select-content'] {
300
- width: max-content;
301
- background: ${t.bg};
302
- color: ${t.fg};
303
- border: none;
304
- border-radius: ${selectDef.vars['content-radius']};
305
- box-shadow: ${t.shadow};
306
- font: 14px ${t.font};
307
- padding: 4px;
308
- overflow: hidden;
309
-
310
- &[data-size='sm'] {
311
- font-size: 13px;
312
- background: ${t.surface.overlay};
313
- }
314
- &[data-scroll] {
315
- display: flex;
316
- align-items: stretch;
317
- gap: 2px;
318
- }
319
- &[data-scroll] [data-slot='select-viewport'] {
320
- flex: 1 1 auto;
321
- min-width: min-content;
322
- max-height: 240px;
323
- overflow-y: auto;
324
- scrollbar-width: none;
325
- }
326
- &[data-scroll] [data-slot='select-viewport']::-webkit-scrollbar {
327
- display: none;
328
- width: 0;
329
- height: 0;
330
- }
331
- }
332
- }
333
- `;
334
-
335
- const injectStyles = defineStyles(SELECT_STYLE_ID, SELECT_CSS);
336
-
337
- function syncThumb(list: HTMLElement, thumb: HTMLElement): void {
338
- const view = list.clientHeight;
339
- const total = list.scrollHeight;
340
- if (total <= view) {
341
- thumb.style.height = '0';
342
- return;
343
- }
344
- const thumbH = Math.max(20, (view / total) * view);
345
- const top = (list.scrollTop / (total - view)) * (view - thumbH);
346
- thumb.style.height = `${Math.round(thumbH)}px`;
347
- thumb.style.transform = `translateY(${Math.round(top)}px`;
348
- }
349
-
350
- /** Fill `menu` with the options, a listbox (Zag's listbox machine: the arrow keys, Home/End,
351
- * Enter and typeahead while it has the focus). A click picks at once; one pick per fill.
352
- * Call {@link decorateSelectScroll} after the menu is on screen; call the returned function
353
- * when the menu goes away. */
354
- export function fillSelectList(menu: HTMLElement, options: readonly SelectOption[], current: string, onPick: (value: string, label: string) => void): () => void {
355
- injectStyles(menu.ownerDocument);
356
- addSlot(menu, selectDef.slot('content'));
357
- let picked = false;
358
- const choose = (value: string): void => {
359
- const option = options.find((o) => o.value === value);
360
- if (picked || !option) return;
361
- picked = true;
362
- onPick(option.value, option.label);
363
- };
364
- const items = options.map((p) =>
365
- h('button', {
366
- className: 'vela-select-item',
367
- slot: selectDef.slot('item'),
368
- attrs: { type: 'button', tabindex: -1 },
369
- data: { checked: p.value === current ? '1' : null },
370
- text: p.label,
371
- on: {
372
- click: (e) => {
373
- e.stopPropagation();
374
- choose(p.value);
375
- },
376
- },
377
- }),
378
- );
379
- const viewport = h('div', { className: 'vela-select-items', slot: selectDef.slot('viewport') }, items);
380
- menu.replaceChildren(viewport);
381
- const mid = nextUid('vela-select-list');
382
- const known = options.some((o) => o.value === current);
383
- const machine = runMachine(
384
- zagListbox.machine,
385
- {
386
- id: mid,
387
- collection: zagListbox.collection({ items: options, itemToValue: (o) => o.value, itemToString: (o) => o.label }),
388
- selectionMode: 'single',
389
- defaultValue: known ? [current] : [],
390
- defaultHighlightedValue: known ? current : null,
391
- // The keyboard's pick (Enter) arrives here, a beat after the key.
392
- onSelect: ({ value }) => choose(value),
393
- },
394
- (service) => {
395
- const api = zagListbox.connect(service, normalizeProps);
396
- spreadProps(viewport, api.getContentProps(), mid);
397
- options.forEach((item, i) => spreadProps(items[i]!, api.getItemProps({ item, highlightOnHover: true }), mid));
398
- },
399
- );
400
- // After the machine's own handler: the picking key ends here, so it can't also press the
401
- // trigger the focus returns to.
402
- viewport.addEventListener('keydown', (e) => {
403
- if (e.key === 'Enter' || e.key === ' ') e.preventDefault();
404
- });
405
- return () => machine.stop();
406
- }
407
-
408
- /** Overlay scrollbar + scroll-into-view — needs a laid-out list (after the popover is shown). */
409
- export function decorateSelectScroll(menu: HTMLElement): void {
410
- const list = menu.querySelector<HTMLElement>(`[data-slot='${selectDef.slot('viewport')}']`);
411
- if (!list) return;
412
- if (list.scrollHeight > 240) {
413
- menu.classList.add('is-scroll');
414
- menu.dataset.scroll = '';
415
- const thumb = h('div', { className: 'vela-select-thumb', slot: selectDef.slot('thumb') });
416
- menu.appendChild(h('div', { className: 'vela-select-rail', slot: selectDef.slot('rail') }, thumb));
417
- list.addEventListener('scroll', () => syncThumb(list, thumb));
418
- syncThumb(list, thumb);
419
- }
420
- menu.querySelector<HTMLElement>(`[data-slot='${selectDef.slot('item')}'][data-checked]`)?.scrollIntoView({ block: 'nearest' });
421
- list.dispatchEvent(new Event('scroll'));
422
- }
423
-
424
- /** Open (or re-click close) a themed option list under `trigger`. */
425
- export function toggleSelectList(
426
- trigger: HTMLElement,
427
- options: readonly SelectOption[],
428
- current: string,
429
- onPick: (value: string, label: string) => void,
430
- opts: SelectListPopoverOpts = {},
431
- ): Popover | null {
432
- if (openPopoverTrigger() === trigger) {
433
- closeOpenPopovers();
434
- return null;
435
- }
436
- return openSelectList(trigger, options, current, onPick, opts);
437
- }
438
-
439
- export function openSelectList(
440
- trigger: HTMLElement,
441
- options: readonly SelectOption[],
442
- current: string,
443
- onPick: (value: string, label: string) => void,
444
- opts: SelectListPopoverOpts = {},
445
- ): Popover {
446
- injectStyles(trigger.ownerDocument);
447
- closeOpenPopovers();
448
- let dispose: (() => void) | null = null;
449
- const pop = new Popover({
450
- trigger,
451
- theme: opts.theme,
452
- matchWidth: opts.matchWidth ?? true,
453
- collisionBoundary: opts.collisionBoundary ?? opts.boundary,
454
- boundaryInset: opts.boundaryInset,
455
- side: opts.side,
456
- sideOffset: opts.sideOffset ?? opts.gap ?? 4,
457
- align: opts.align ?? 'start',
458
- alignOffset: opts.alignOffset,
459
- avoidCollisions: opts.avoidCollisions,
460
- collisionPadding: opts.collisionPadding,
461
- host: opts.host,
462
- position: opts.position,
463
- zIndex: opts.zIndex,
464
- className: 'vela-select-list',
465
- slot: selectDef.slot('content'),
466
- onClose: () => {
467
- dispose?.();
468
- dispose = null;
469
- const active = trigger.ownerDocument.activeElement;
470
- if (opts.focusList && (!active || active === trigger.ownerDocument.body || pop.el.contains(active))) trigger.focus({ preventScroll: true });
471
- opts.onClose?.();
472
- },
473
- content: (el) => {
474
- if (opts.size) el.dataset.size = opts.size;
475
- dispose = fillSelectList(el, options, current, (value, label) => {
476
- pop.hide();
477
- onPick(value, label);
478
- });
479
- },
480
- });
481
- pop.show();
482
- decorateSelectScroll(pop.el);
483
- pop.reposition();
484
- if (opts.focusList) pop.el.querySelector<HTMLElement>(`[data-slot='${selectDef.slot('viewport')}']`)?.focus({ preventScroll: true });
485
- return pop;
486
- }
487
-
488
- export class Select {
489
- readonly el: HTMLElement;
490
- private readonly trigger: HTMLButtonElement;
491
- private readonly labelEl: HTMLElement;
492
- private readonly ctrl: SelectController;
493
- private readonly listOpts: SelectListPopoverOpts;
494
- private readonly placeholder: string | undefined;
495
- private readonly native: HTMLSelectElement | null;
496
- private readonly release: () => void;
497
- private list: Popover | null = null;
498
-
499
- constructor(opts: SelectOptions) {
500
- const doc = opts.list?.host?.ownerDocument ?? document;
501
- injectStyles(doc);
502
- this.ctrl = selectController(opts);
503
- const placed = Object.fromEntries(
504
- (['side', 'align', 'sideOffset', 'alignOffset', 'avoidCollisions', 'collisionPadding', 'collisionBoundary'] as const).filter((k) => opts[k] !== undefined).map((k) => [k, opts[k]]),
505
- ) as PlacementProps;
506
- this.listOpts = { ...opts.list, ...placed, theme: opts.theme ?? opts.list?.theme, size: this.ctrl.size };
507
-
508
- this.placeholder = opts.placeholder;
509
- const label = h('span', { className: 'vela-select-label', slot: selectDef.slot('value') });
510
- const btn = h(
511
- 'button',
512
- {
513
- className: 'vela-select-trigger',
514
- slot: selectDef.slot('trigger'),
515
- attrs: { type: 'button', id: opts.id, 'aria-haspopup': 'listbox', 'aria-expanded': 'false', 'aria-required': opts.required ? 'true' : undefined },
516
- },
517
- label,
518
- h('span', { className: 'vela-select-chevron', slot: selectDef.slot('icon') }, iconEl('chevron-down', doc)),
519
- );
520
- btn.disabled = this.ctrl.disabled;
521
- // Compact toolbar selects hug the widest option. md uses a fixed 100px
522
- // column and ellipsizes the trigger; the open list still shows full labels.
523
- const sizer =
524
- this.ctrl.size === 'sm'
525
- ? h(
526
- 'div',
527
- { className: 'vela-select-sizer', slot: selectDef.slot('sizer'), attrs: { 'aria-hidden': 'true' } },
528
- this.ctrl.options.map((o) => h('span', { text: o.label })),
529
- )
530
- : null;
531
- this.native =
532
- opts.name !== undefined
533
- ? h(
534
- 'select',
535
- { slot: selectDef.slot('native'), attrs: { name: opts.name, 'aria-hidden': 'true', tabindex: -1 } },
536
- opts.placeholder !== undefined ? h('option', { attrs: { value: '' } }) : null,
537
- this.ctrl.options.map((o) => h('option', { attrs: { value: o.value }, text: o.label })),
538
- )
539
- : null;
540
- if (this.native) {
541
- this.native.required = opts.required === true;
542
- this.native.disabled = this.ctrl.disabled;
543
- }
544
- this.el = h('div', { className: 'vela-select', slot: selectDef.slot('root'), attrs: selectVariants({ size: this.ctrl.size }), data: { fill: this.ctrl.fill } }, btn, sizer, this.native);
545
-
546
- // Suppress the default mousedown focus: when this press outside-dismisses an open
547
- // popover, Chrome's focus action can scroll the dialog body under the pointer.
548
- btn.addEventListener('pointerdown', (e) => {
549
- e.preventDefault();
550
- btn.focus({ preventScroll: true });
551
- });
552
- btn.addEventListener('click', (e) => {
553
- e.stopPropagation();
554
- this.toggle();
555
- });
556
- btn.addEventListener('keydown', (e) => {
557
- if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && !this.list) {
558
- e.preventDefault();
559
- this.toggle();
560
- }
561
- });
562
-
563
- this.trigger = btn;
564
- this.labelEl = label;
565
- this.paint();
566
- this.release = ownTeardown(this.el, () => {
567
- this.list?.hide();
568
- this.list = null;
569
- });
570
- }
571
-
572
- get value(): string {
573
- return this.ctrl.value;
574
- }
575
-
576
- setValue(v: string): void {
577
- this.ctrl.setValue(v);
578
- this.paint();
579
- }
580
-
581
- /** The trigger shows the picked option's label, or the placeholder while none is picked. */
582
- private paint(): void {
583
- const v = this.ctrl.value;
584
- const empty = this.placeholder !== undefined && !this.ctrl.options.some((o) => o.value === v);
585
- this.labelEl.textContent = empty ? (this.placeholder ?? '') : this.ctrl.labelOf(v);
586
- this.labelEl.toggleAttribute('data-placeholder', empty);
587
- if (this.native) this.native.value = empty ? '' : v;
588
- }
589
-
590
- toggle(): void {
591
- this.list = toggleSelectList(
592
- this.trigger,
593
- this.ctrl.options,
594
- this.ctrl.value,
595
- (value) => {
596
- this.ctrl.pick(value);
597
- this.paint();
598
- this.list = null;
599
- },
600
- {
601
- ...this.listOpts,
602
- focusList: true,
603
- onClose: () => {
604
- this.list = null;
605
- this.trigger.setAttribute('aria-expanded', 'false');
606
- this.listOpts.onClose?.();
607
- },
608
- },
609
- );
610
- this.trigger.setAttribute('aria-expanded', String(this.list !== null));
611
- }
612
-
613
- destroy(): void {
614
- this.release();
615
- }
616
- }
617
-
618
- export function createSelect(props: SelectProps): Select {
619
- return new Select(props);
620
- }
1
+ // Select: a trigger button and a themed option list on a popover, with an overlay scrollbar
2
+ // for long lists. `md` is the shared 100px kit column and ellipsizes the trigger; `sm` is a
3
+ // compact toolbar select that hugs its widest option. The open list shows full labels.
4
+ import type { ChartTheme } from '../lib/tokens';
5
+ import { iconEl } from '../lib/icons';
6
+ import { css, defineStyles } from '../lib/css';
7
+ import { defineComponent } from '../lib/define';
8
+ import { ownTeardown } from '../lib/dispose';
9
+ import { addSlot, h } from '../lib/h';
10
+ import { t } from '../lib/t';
11
+ import { variants } from '../lib/variants';
12
+ import { nextUid, normalizeProps, runMachine, spreadProps } from '../lib/zag';
13
+ import * as zagListbox from '@zag-js/listbox';
14
+ import { Popover, closeOpenPopovers, openPopoverTrigger, type PlacementProps, type PopoverBoundary, type PopoverOptions } from './popover';
15
+
16
+ export type SelectSize = 'sm' | 'md';
17
+
18
+ export interface SelectOption {
19
+ value: string;
20
+ label: string;
21
+ /** Shown in the list but not selectable (dimmed, skipped by the keyboard). */
22
+ disabled?: boolean;
23
+ /** The option's tooltip, e.g. why it is disabled. */
24
+ title?: string;
25
+ }
26
+
27
+ export interface SelectControllerOptions {
28
+ options: readonly SelectOption[];
29
+ value?: string;
30
+ /** The value it starts with when `value` is not given. */
31
+ defaultValue?: string;
32
+ /** Shown while no option is picked: the value stays `''` until the user picks one. Without
33
+ * it, the first option is picked to start with. */
34
+ placeholder?: string;
35
+ size?: SelectSize;
36
+ /** Stretch the trigger to its parent. Off: the shared 100px kit column (ellipsis). */
37
+ fill?: boolean;
38
+ disabled?: boolean;
39
+ /** Called when the user picks an option. */
40
+ onValueChange?: (value: string, label: string) => void;
41
+ /** @deprecated Use `onValueChange`. */
42
+ onChange?: (value: string, label: string) => void;
43
+ }
44
+
45
+ export interface SelectController {
46
+ options: readonly SelectOption[];
47
+ value: string;
48
+ size: SelectSize;
49
+ fill: boolean;
50
+ disabled: boolean;
51
+ setValue(v: string): void;
52
+ labelOf(value: string): string;
53
+ pick(value: string): { value: string; label: string };
54
+ }
55
+
56
+ /** The select's options and current value, without DOM. */
57
+ export function selectController(opts: SelectControllerOptions): SelectController {
58
+ const options = opts.options;
59
+ let value = opts.value ?? opts.defaultValue ?? (opts.placeholder !== undefined ? '' : (options[0]?.value ?? ''));
60
+ const size = opts.size ?? 'md';
61
+ return {
62
+ options,
63
+ get value() {
64
+ return value;
65
+ },
66
+ size,
67
+ fill: opts.fill ?? size !== 'sm',
68
+ disabled: opts.disabled ?? false,
69
+ setValue(v: string) {
70
+ value = v;
71
+ },
72
+ labelOf(v: string) {
73
+ return options.find((o) => o.value === v)?.label ?? v;
74
+ },
75
+ pick(v: string) {
76
+ value = v;
77
+ const label = options.find((o) => o.value === v)?.label ?? v;
78
+ opts.onValueChange?.(v, label);
79
+ opts.onChange?.(v, label);
80
+ return { value: v, label };
81
+ },
82
+ };
83
+ }
84
+
85
+ export const selectDef = defineComponent({
86
+ name: 'select',
87
+ parts: ['root', 'trigger', 'value', 'icon', 'sizer', 'native', 'content', 'viewport', 'item', 'rail', 'thumb'],
88
+ settings: { radius: t.radius.md, 'content-radius': t.radius.md, 'thumb-radius': '2px', 'focus-ring': `0 0 0 3px ${t.focus.soft}` },
89
+ });
90
+
91
+ export const selectVariants = variants({ size: ['md', 'sm'] });
92
+
93
+ /** Where the list opens and how (shadcn's placement props, defaults below and start-aligned,
94
+ * 4px away, as wide as the trigger at least). */
95
+ export interface SelectListPopoverOpts extends PlacementProps {
96
+ theme?: ChartTheme;
97
+ matchWidth?: boolean;
98
+ /** @deprecated Use `collisionBoundary`. */
99
+ boundary?: PopoverBoundary;
100
+ boundaryInset?: number;
101
+ /** @deprecated Use `sideOffset`. */
102
+ gap?: number;
103
+ host?: HTMLElement;
104
+ position?: PopoverOptions['position'];
105
+ zIndex?: string | number;
106
+ size?: SelectSize;
107
+ /** Move the focus into the open list, for the arrow keys and typeahead (and back to the
108
+ * trigger when it closes). Off by default: a trigger the user types in keeps the focus. */
109
+ focusList?: boolean;
110
+ onClose?: () => void;
111
+ }
112
+
113
+ export interface SelectOptions extends SelectControllerOptions, PlacementProps {
114
+ id?: string;
115
+ /** Submitted with its form under this name: a hidden native select carries the value. */
116
+ name?: string;
117
+ /** A form does not submit while no option is picked (with `placeholder`). */
118
+ required?: boolean;
119
+ theme?: ChartTheme;
120
+ /** The open list's other options (its host, layer, width). Its placement props are the
121
+ * select's own; set here, they give way to those. */
122
+ list?: SelectListPopoverOpts;
123
+ }
124
+
125
+ export type SelectProps = SelectOptions;
126
+
127
+ export const SELECT_STYLE_ID = 'vela-ui-select-3';
128
+
129
+ export const SELECT_CSS = css`
130
+ @layer vela.ui {
131
+ [data-slot='select-trigger'] {
132
+ display: flex;
133
+ align-items: center;
134
+ width: 100%;
135
+ min-width: 0;
136
+ box-sizing: border-box;
137
+ height: 34px;
138
+ padding-block: 0;
139
+ padding-inline: 8px 26px;
140
+ background: transparent;
141
+ border: 1px solid ${t.border.strong};
142
+ border-radius: ${selectDef.vars.radius};
143
+ color: ${t.fg.bright};
144
+ font-size: 14px;
145
+ font-family: inherit;
146
+ cursor: pointer;
147
+ text-align: start;
148
+ outline: 2px solid transparent;
149
+ transition:
150
+ border-color ${t.dur.fast} ease,
151
+ box-shadow ${t.dur.fast} ease;
152
+
153
+ &:hover {
154
+ border-color: ${t.fg.muted};
155
+ }
156
+ &:focus {
157
+ border-color: ${t.focus};
158
+ box-shadow: ${selectDef.vars['focus-ring']};
159
+ }
160
+ }
161
+ [data-slot='select-value'] {
162
+ flex: 1;
163
+ min-width: 0;
164
+ overflow: hidden;
165
+ text-overflow: ellipsis;
166
+ white-space: nowrap;
167
+ }
168
+ [data-slot='select-icon'] {
169
+ position: absolute;
170
+ inset-inline-end: 8px;
171
+ top: 50%;
172
+ transform: translateY(-50%);
173
+ pointer-events: none;
174
+ display: flex;
175
+ opacity: 0.55;
176
+ line-height: 0;
177
+ color: inherit;
178
+
179
+ & > span,
180
+ & svg {
181
+ width: 12px;
182
+ height: 12px;
183
+ display: block;
184
+ }
185
+ }
186
+ [data-slot='select-sizer'] {
187
+ visibility: hidden;
188
+ height: 0;
189
+ overflow: hidden;
190
+ font-size: 13px;
191
+
192
+ & span {
193
+ display: block;
194
+ height: 0;
195
+ white-space: nowrap;
196
+ padding-block: 0;
197
+ padding-inline: 8px 26px;
198
+ border-inline: 1px solid transparent;
199
+ }
200
+ }
201
+ [data-slot='select-viewport'] {
202
+ width: max-content;
203
+ min-width: 100%;
204
+ max-height: none;
205
+ overflow: hidden;
206
+ }
207
+ [data-slot='select-rail'] {
208
+ position: relative;
209
+ flex: none;
210
+ width: 3px;
211
+ margin-block: 2px;
212
+ margin-inline: 0 1px;
213
+ pointer-events: none;
214
+ }
215
+ [data-slot='select-thumb'] {
216
+ width: 3px;
217
+ border-radius: ${selectDef.vars['thumb-radius']};
218
+ background: ${t.scroll};
219
+ }
220
+ [data-slot='select-item'] {
221
+ display: block;
222
+ width: auto;
223
+ min-width: 100%;
224
+ white-space: nowrap;
225
+ text-align: start;
226
+ padding: 7px 10px;
227
+ border: none;
228
+ border-radius: ${t.radius.sm};
229
+ background: transparent;
230
+ color: inherit;
231
+ cursor: pointer;
232
+ font: inherit;
233
+ font-weight: 400;
234
+
235
+ &:hover,
236
+ &[data-highlighted] {
237
+ background: ${t.hover};
238
+ }
239
+ &[data-checked] {
240
+ background: ${t.hover.strong};
241
+ color: ${t.fg.bright};
242
+ }
243
+ &[data-checked]:hover,
244
+ &[data-checked][data-highlighted] {
245
+ background: ${t.hover.strong};
246
+ }
247
+ &[data-disabled] {
248
+ opacity: 0.4;
249
+ cursor: default;
250
+ background: transparent;
251
+ }
252
+ }
253
+ /* The highlighted option shows where the keyboard is; the list itself draws no ring. */
254
+ [data-slot='select-viewport']:focus {
255
+ outline: 2px solid transparent;
256
+ }
257
+ [data-slot='select-value'][data-placeholder] {
258
+ color: ${t.fg.muted};
259
+ }
260
+ /* Seen by forms and their validation, never by the user. */
261
+ [data-slot='select-native'] {
262
+ position: absolute;
263
+ width: 1px;
264
+ height: 1px;
265
+ margin: -1px;
266
+ padding: 0;
267
+ border: 0;
268
+ overflow: hidden;
269
+ clip-path: inset(50%);
270
+ white-space: nowrap;
271
+ pointer-events: none;
272
+ }
273
+ [data-slot='select'] {
274
+ position: relative;
275
+ min-width: 0;
276
+
277
+ &:where(:not([hidden])) {
278
+ display: inline-block;
279
+ }
280
+ &[data-fill] {
281
+ width: 100%;
282
+ }
283
+ /* The closed trigger matches NumberInput and TextField (100px); long labels
284
+ ellipsize, and the open list still sizes to the longest item. */
285
+ &:not([data-fill]) {
286
+ width: 100px;
287
+ flex: none;
288
+ justify-self: start;
289
+ }
290
+ &[data-size='sm'] [data-slot='select-trigger'] {
291
+ height: 28px;
292
+ background: ${t.surface.elev};
293
+ border-radius: ${t.radius.sm};
294
+ color: ${t.fg};
295
+ font-size: 13px;
296
+ box-shadow: none;
297
+ }
298
+ &[data-size='sm'] [data-slot='select-trigger']:focus {
299
+ box-shadow: none;
300
+ border-color: ${t.fg.muted};
301
+ }
302
+ /* Compact toolbar selects still hug the widest option (no 100px kit column). */
303
+ &[data-size='sm']:not([data-fill]) {
304
+ width: auto;
305
+ max-width: 200px;
306
+ }
307
+ }
308
+ [data-slot~='select-content'] {
309
+ width: max-content;
310
+ background: ${t.bg};
311
+ color: ${t.fg};
312
+ border: none;
313
+ border-radius: ${selectDef.vars['content-radius']};
314
+ box-shadow: ${t.shadow};
315
+ font: 14px ${t.font};
316
+ padding: 4px;
317
+ overflow: hidden;
318
+
319
+ &[data-size='sm'] {
320
+ font-size: 13px;
321
+ background: ${t.surface.overlay};
322
+ }
323
+ &[data-scroll] {
324
+ display: flex;
325
+ align-items: stretch;
326
+ gap: 2px;
327
+ }
328
+ &[data-scroll] [data-slot='select-viewport'] {
329
+ flex: 1 1 auto;
330
+ min-width: min-content;
331
+ max-height: 240px;
332
+ overflow-y: auto;
333
+ scrollbar-width: none;
334
+ }
335
+ &[data-scroll] [data-slot='select-viewport']::-webkit-scrollbar {
336
+ display: none;
337
+ width: 0;
338
+ height: 0;
339
+ }
340
+ }
341
+ }
342
+ `;
343
+
344
+ const injectStyles = defineStyles(SELECT_STYLE_ID, SELECT_CSS);
345
+
346
+ function syncThumb(list: HTMLElement, thumb: HTMLElement): void {
347
+ const view = list.clientHeight;
348
+ const total = list.scrollHeight;
349
+ if (total <= view) {
350
+ thumb.style.height = '0';
351
+ return;
352
+ }
353
+ const thumbH = Math.max(20, (view / total) * view);
354
+ const top = (list.scrollTop / (total - view)) * (view - thumbH);
355
+ thumb.style.height = `${Math.round(thumbH)}px`;
356
+ thumb.style.transform = `translateY(${Math.round(top)}px`;
357
+ }
358
+
359
+ /** Fill `menu` with the options, a listbox (Zag's listbox machine: the arrow keys, Home/End,
360
+ * Enter and typeahead while it has the focus). A click picks at once; one pick per fill.
361
+ * Call {@link decorateSelectScroll} after the menu is on screen; call the returned function
362
+ * when the menu goes away. */
363
+ export function fillSelectList(menu: HTMLElement, options: readonly SelectOption[], current: string, onPick: (value: string, label: string) => void): () => void {
364
+ injectStyles(menu.ownerDocument);
365
+ addSlot(menu, selectDef.slot('content'));
366
+ let picked = false;
367
+ const choose = (value: string): void => {
368
+ const option = options.find((o) => o.value === value);
369
+ if (picked || !option || option.disabled) return;
370
+ picked = true;
371
+ onPick(option.value, option.label);
372
+ };
373
+ const items = options.map((p) =>
374
+ h('button', {
375
+ className: 'vela-select-item',
376
+ slot: selectDef.slot('item'),
377
+ attrs: { type: 'button', tabindex: -1, title: p.title },
378
+ data: { checked: p.value === current ? '1' : null },
379
+ text: p.label,
380
+ on: {
381
+ click: (e) => {
382
+ e.stopPropagation();
383
+ choose(p.value);
384
+ },
385
+ },
386
+ }),
387
+ );
388
+ const viewport = h('div', { className: 'vela-select-items', slot: selectDef.slot('viewport') }, items);
389
+ menu.replaceChildren(viewport);
390
+ const mid = nextUid('vela-select-list');
391
+ const known = options.some((o) => o.value === current);
392
+ const machine = runMachine(
393
+ zagListbox.machine,
394
+ {
395
+ id: mid,
396
+ collection: zagListbox.collection({ items: options, itemToValue: (o) => o.value, itemToString: (o) => o.label, isItemDisabled: (o) => o.disabled === true }),
397
+ selectionMode: 'single',
398
+ defaultValue: known ? [current] : [],
399
+ defaultHighlightedValue: known ? current : null,
400
+ // The keyboard's pick (Enter) arrives here, a beat after the key.
401
+ onSelect: ({ value }) => choose(value),
402
+ },
403
+ (service) => {
404
+ const api = zagListbox.connect(service, normalizeProps);
405
+ spreadProps(viewport, api.getContentProps(), mid);
406
+ options.forEach((item, i) => spreadProps(items[i]!, api.getItemProps({ item, highlightOnHover: true }), mid));
407
+ },
408
+ );
409
+ // After the machine's own handler: the picking key ends here, so it can't also press the
410
+ // trigger the focus returns to.
411
+ viewport.addEventListener('keydown', (e) => {
412
+ if (e.key === 'Enter' || e.key === ' ') e.preventDefault();
413
+ });
414
+ return () => machine.stop();
415
+ }
416
+
417
+ /** Overlay scrollbar + scroll-into-view — needs a laid-out list (after the popover is shown). */
418
+ export function decorateSelectScroll(menu: HTMLElement): void {
419
+ const list = menu.querySelector<HTMLElement>(`[data-slot='${selectDef.slot('viewport')}']`);
420
+ if (!list) return;
421
+ if (list.scrollHeight > 240) {
422
+ menu.classList.add('is-scroll');
423
+ menu.dataset.scroll = '';
424
+ const thumb = h('div', { className: 'vela-select-thumb', slot: selectDef.slot('thumb') });
425
+ menu.appendChild(h('div', { className: 'vela-select-rail', slot: selectDef.slot('rail') }, thumb));
426
+ list.addEventListener('scroll', () => syncThumb(list, thumb));
427
+ syncThumb(list, thumb);
428
+ }
429
+ menu.querySelector<HTMLElement>(`[data-slot='${selectDef.slot('item')}'][data-checked]`)?.scrollIntoView({ block: 'nearest' });
430
+ list.dispatchEvent(new Event('scroll'));
431
+ }
432
+
433
+ /** Open (or re-click close) a themed option list under `trigger`. */
434
+ export function toggleSelectList(
435
+ trigger: HTMLElement,
436
+ options: readonly SelectOption[],
437
+ current: string,
438
+ onPick: (value: string, label: string) => void,
439
+ opts: SelectListPopoverOpts = {},
440
+ ): Popover | null {
441
+ if (openPopoverTrigger() === trigger) {
442
+ closeOpenPopovers();
443
+ return null;
444
+ }
445
+ return openSelectList(trigger, options, current, onPick, opts);
446
+ }
447
+
448
+ export function openSelectList(
449
+ trigger: HTMLElement,
450
+ options: readonly SelectOption[],
451
+ current: string,
452
+ onPick: (value: string, label: string) => void,
453
+ opts: SelectListPopoverOpts = {},
454
+ ): Popover {
455
+ injectStyles(trigger.ownerDocument);
456
+ closeOpenPopovers();
457
+ let dispose: (() => void) | null = null;
458
+ const pop = new Popover({
459
+ trigger,
460
+ theme: opts.theme,
461
+ matchWidth: opts.matchWidth ?? true,
462
+ collisionBoundary: opts.collisionBoundary ?? opts.boundary,
463
+ boundaryInset: opts.boundaryInset,
464
+ side: opts.side,
465
+ sideOffset: opts.sideOffset ?? opts.gap ?? 4,
466
+ align: opts.align ?? 'start',
467
+ alignOffset: opts.alignOffset,
468
+ avoidCollisions: opts.avoidCollisions,
469
+ collisionPadding: opts.collisionPadding,
470
+ host: opts.host,
471
+ position: opts.position,
472
+ zIndex: opts.zIndex,
473
+ className: 'vela-select-list',
474
+ slot: selectDef.slot('content'),
475
+ onClose: () => {
476
+ dispose?.();
477
+ dispose = null;
478
+ const active = trigger.ownerDocument.activeElement;
479
+ if (opts.focusList && (!active || active === trigger.ownerDocument.body || pop.el.contains(active))) trigger.focus({ preventScroll: true });
480
+ opts.onClose?.();
481
+ },
482
+ content: (el) => {
483
+ if (opts.size) el.dataset.size = opts.size;
484
+ dispose = fillSelectList(el, options, current, (value, label) => {
485
+ pop.hide();
486
+ onPick(value, label);
487
+ });
488
+ },
489
+ });
490
+ pop.show();
491
+ decorateSelectScroll(pop.el);
492
+ pop.reposition();
493
+ if (opts.focusList) pop.el.querySelector<HTMLElement>(`[data-slot='${selectDef.slot('viewport')}']`)?.focus({ preventScroll: true });
494
+ return pop;
495
+ }
496
+
497
+ export class Select {
498
+ readonly el: HTMLElement;
499
+ private readonly trigger: HTMLButtonElement;
500
+ private readonly labelEl: HTMLElement;
501
+ private readonly ctrl: SelectController;
502
+ private readonly listOpts: SelectListPopoverOpts;
503
+ private readonly placeholder: string | undefined;
504
+ private readonly native: HTMLSelectElement | null;
505
+ private readonly release: () => void;
506
+ private list: Popover | null = null;
507
+
508
+ constructor(opts: SelectOptions) {
509
+ const doc = opts.list?.host?.ownerDocument ?? document;
510
+ injectStyles(doc);
511
+ this.ctrl = selectController(opts);
512
+ const placed = Object.fromEntries(
513
+ (['side', 'align', 'sideOffset', 'alignOffset', 'avoidCollisions', 'collisionPadding', 'collisionBoundary'] as const).filter((k) => opts[k] !== undefined).map((k) => [k, opts[k]]),
514
+ ) as PlacementProps;
515
+ this.listOpts = { ...opts.list, ...placed, theme: opts.theme ?? opts.list?.theme, size: this.ctrl.size };
516
+
517
+ this.placeholder = opts.placeholder;
518
+ const label = h('span', { className: 'vela-select-label', slot: selectDef.slot('value') });
519
+ const btn = h(
520
+ 'button',
521
+ {
522
+ className: 'vela-select-trigger',
523
+ slot: selectDef.slot('trigger'),
524
+ attrs: { type: 'button', id: opts.id, 'aria-haspopup': 'listbox', 'aria-expanded': 'false', 'aria-required': opts.required ? 'true' : undefined },
525
+ },
526
+ label,
527
+ h('span', { className: 'vela-select-chevron', slot: selectDef.slot('icon') }, iconEl('chevron-down', doc)),
528
+ );
529
+ btn.disabled = this.ctrl.disabled;
530
+ // Compact toolbar selects hug the widest option. md uses a fixed 100px
531
+ // column and ellipsizes the trigger; the open list still shows full labels.
532
+ const sizer =
533
+ this.ctrl.size === 'sm'
534
+ ? h(
535
+ 'div',
536
+ { className: 'vela-select-sizer', slot: selectDef.slot('sizer'), attrs: { 'aria-hidden': 'true' } },
537
+ this.ctrl.options.map((o) => h('span', { text: o.label })),
538
+ )
539
+ : null;
540
+ this.native =
541
+ opts.name !== undefined
542
+ ? h(
543
+ 'select',
544
+ { slot: selectDef.slot('native'), attrs: { name: opts.name, 'aria-hidden': 'true', tabindex: -1 } },
545
+ opts.placeholder !== undefined ? h('option', { attrs: { value: '' } }) : null,
546
+ this.ctrl.options.map((o) => h('option', { attrs: { value: o.value, disabled: o.disabled }, text: o.label })),
547
+ )
548
+ : null;
549
+ if (this.native) {
550
+ this.native.required = opts.required === true;
551
+ this.native.disabled = this.ctrl.disabled;
552
+ }
553
+ this.el = h('div', { className: 'vela-select', slot: selectDef.slot('root'), attrs: selectVariants({ size: this.ctrl.size }), data: { fill: this.ctrl.fill } }, btn, sizer, this.native);
554
+
555
+ // Suppress the default mousedown focus: when this press outside-dismisses an open
556
+ // popover, Chrome's focus action can scroll the dialog body under the pointer.
557
+ btn.addEventListener('pointerdown', (e) => {
558
+ e.preventDefault();
559
+ btn.focus({ preventScroll: true });
560
+ });
561
+ btn.addEventListener('click', (e) => {
562
+ e.stopPropagation();
563
+ this.toggle();
564
+ });
565
+ btn.addEventListener('keydown', (e) => {
566
+ if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && !this.list) {
567
+ e.preventDefault();
568
+ this.toggle();
569
+ }
570
+ });
571
+
572
+ this.trigger = btn;
573
+ this.labelEl = label;
574
+ this.paint();
575
+ this.release = ownTeardown(this.el, () => {
576
+ this.list?.hide();
577
+ this.list = null;
578
+ });
579
+ }
580
+
581
+ get value(): string {
582
+ return this.ctrl.value;
583
+ }
584
+
585
+ setValue(v: string): void {
586
+ this.ctrl.setValue(v);
587
+ this.paint();
588
+ }
589
+
590
+ /** The trigger shows the picked option's label, or the placeholder while none is picked. */
591
+ private paint(): void {
592
+ const v = this.ctrl.value;
593
+ const empty = this.placeholder !== undefined && !this.ctrl.options.some((o) => o.value === v);
594
+ this.labelEl.textContent = empty ? (this.placeholder ?? '') : this.ctrl.labelOf(v);
595
+ this.labelEl.toggleAttribute('data-placeholder', empty);
596
+ if (this.native) this.native.value = empty ? '' : v;
597
+ }
598
+
599
+ toggle(): void {
600
+ this.list = toggleSelectList(
601
+ this.trigger,
602
+ this.ctrl.options,
603
+ this.ctrl.value,
604
+ (value) => {
605
+ this.ctrl.pick(value);
606
+ this.paint();
607
+ this.list = null;
608
+ },
609
+ {
610
+ ...this.listOpts,
611
+ focusList: true,
612
+ onClose: () => {
613
+ this.list = null;
614
+ this.trigger.setAttribute('aria-expanded', 'false');
615
+ this.listOpts.onClose?.();
616
+ },
617
+ },
618
+ );
619
+ this.trigger.setAttribute('aria-expanded', String(this.list !== null));
620
+ }
621
+
622
+ destroy(): void {
623
+ this.release();
624
+ }
625
+ }
626
+
627
+ export function createSelect(props: SelectProps): Select {
628
+ return new Select(props);
629
+ }