@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
@@ -1,376 +1,376 @@
1
- // Item: one row of a list (a watchlist, a list of alerts, search results): media (an icon, a
2
- // logo, an avatar, a star), a title over a description, then at its end the meta (figures,
3
- // badges) and the actions (buttons). With `onSelect` the whole row is clickable, while a press
4
- // on a control inside it stays that control's. To the keyboard (Enter, Space) and to screen
5
- // readers the row is a button; a row with actions makes its text the button instead, as a
6
- // button cannot hold other buttons. `selected` marks the current row; `revealActions` keeps the actions
7
- // out of sight until the row is hovered or holds focus, while the meta stays. `default` is
8
- // bare until hovered, `outline` bordered, `muted` on a sunken fill; `sm` is the compact row of
9
- // dense panels. ItemGroup stacks rows, with a line between them when `separated`.
10
- import { css, defineStyles } from '../lib/css';
11
- import { defineComponent } from '../lib/define';
12
- import { append, h, type Child } from '../lib/h';
13
- import { iconEl } from '../lib/icons';
14
- import { t } from '../lib/t';
15
- import { variants, type VariantProps } from '../lib/variants';
16
-
17
- export const itemDef = defineComponent({
18
- name: 'item',
19
- parts: ['root', 'media', 'content', 'title', 'description', 'meta', 'actions', 'group'],
20
- settings: { radius: t.radius.md, gap: '10px' },
21
- });
22
-
23
- export const itemVariants = variants({ variant: ['default', 'outline', 'muted'], size: ['default', 'sm'] });
24
-
25
- export type ItemVariant = (typeof itemVariants.spec.variant)[number];
26
- export type ItemSize = (typeof itemVariants.spec.size)[number];
27
-
28
- export interface ItemProps extends VariantProps<typeof itemVariants> {
29
- /** The first line: text, or an element (a ticker with a chip after it). */
30
- title: string | Node;
31
- /** The muted line under the title. */
32
- description?: string | Node;
33
- /** Before the text: an icon id from the kit's registry, or an element (a logo, an
34
- * avatar, a favorite star). */
35
- media?: string | Node;
36
- /** After the text, always shown: figures and badges (a price and its change). */
37
- meta?: Child;
38
- /** At the row's end: its buttons. */
39
- actions?: Child;
40
- /** Show the actions only while the row is hovered or holds focus (on touch screens they
41
- * always show). The meta stays. */
42
- revealActions?: boolean;
43
- /** The current row (the open symbol, the highlighted result). */
44
- selected?: boolean;
45
- disabled?: boolean;
46
- /** Makes the row clickable: a click, Enter or Space runs it. */
47
- onSelect?: (e: MouseEvent | KeyboardEvent) => void;
48
- /** The row's role (default `button` when it has `onSelect` and no actions, its text being
49
- * the button when it has actions); `option` in a listbox, where `selected` is
50
- * `aria-selected`. */
51
- role?: string;
52
- /** Extra class names (layout hooks). */
53
- className?: string;
54
- }
55
-
56
- export interface ItemHandle {
57
- readonly el: HTMLElement;
58
- setSelected(selected: boolean): void;
59
- setDisabled(disabled: boolean): void;
60
- setTitle(title: string | Node): void;
61
- /** A new description; null removes it. */
62
- setDescription(description: string | Node | null): void;
63
- destroy(): void;
64
- }
65
-
66
- export interface ItemGroupProps {
67
- /** The rows, as elements or as the handles `createItem` returns. */
68
- items?: ReadonlyArray<HTMLElement | ItemHandle>;
69
- /** A line between the rows, edge to edge (default: separate rounded rows). */
70
- separated?: boolean;
71
- /** The list's accessible name. */
72
- label?: string;
73
- /** The group's role: default `group` when it has a `label` (the name needs a role to be
74
- * read), none otherwise; `listbox` for rows whose role is `option`. */
75
- role?: string;
76
- /** Extra class names (layout hooks). */
77
- className?: string;
78
- }
79
-
80
- export interface ItemGroupHandle {
81
- readonly el: HTMLElement;
82
- /** Replace the rows. */
83
- setItems(items: ReadonlyArray<HTMLElement | ItemHandle>): void;
84
- destroy(): void;
85
- }
86
-
87
- /** A press on one of these inside a row belongs to it, not to the row. */
88
- const CONTROL = 'button, a[href], input, select, textarea, label, summary, [role="button"], [role="checkbox"], [role="switch"], [role="menuitem"], [role="option"], [role="link"], [contenteditable]:not([contenteditable="false"])';
89
-
90
- const injectStyles = defineStyles(
91
- 'vela-ui-item',
92
- css`
93
- @layer vela.ui {
94
- [data-slot='item'] {
95
- position: relative;
96
- box-sizing: border-box;
97
- min-width: 0;
98
- padding: 8px 10px;
99
- border: 1px solid transparent;
100
- border-radius: ${itemDef.vars.radius};
101
- color: ${t.fg};
102
- font-size: 13px;
103
- text-align: start;
104
- transition:
105
- background ${t.dur.fast} ease,
106
- border-color ${t.dur.fast} ease;
107
-
108
- &:where(:not([hidden])) {
109
- display: flex;
110
- align-items: center;
111
- gap: ${itemDef.vars.gap};
112
- }
113
- &[data-size='sm'] {
114
- gap: 8px;
115
- padding: 4px 8px;
116
- font-size: 12px;
117
- }
118
- &[data-variant='outline'] {
119
- border-color: ${t.border};
120
- }
121
- &[data-variant='muted'] {
122
- background: ${t.surface.sunken};
123
- }
124
- &[data-clickable] {
125
- cursor: pointer;
126
- user-select: none;
127
- -webkit-tap-highlight-color: transparent;
128
- }
129
- &[data-clickable]:not([data-disabled]):hover {
130
- background: ${t.hover};
131
- }
132
- &[data-selected],
133
- &[data-selected][data-clickable]:hover {
134
- background: ${t.active};
135
- }
136
- &:focus-visible {
137
- outline: 2px solid ${t.focus};
138
- outline-offset: -2px;
139
- }
140
- &[data-disabled] {
141
- cursor: default;
142
- opacity: 0.5;
143
- }
144
- }
145
- [data-slot='item-media'] {
146
- flex: none;
147
- color: ${t.fg.muted};
148
-
149
- &:where(:not([hidden])) {
150
- display: inline-flex;
151
- align-items: center;
152
- justify-content: center;
153
- }
154
- & > [data-icon] {
155
- width: 1em;
156
- height: 1em;
157
- font-size: 16px;
158
- line-height: 0;
159
- }
160
- & > [data-icon] > svg {
161
- width: 100%;
162
- height: 100%;
163
- }
164
- }
165
- [data-slot='item-content'] {
166
- flex: 1 1 auto;
167
- min-width: 0;
168
-
169
- &:where(:not([hidden])) {
170
- display: flex;
171
- flex-direction: column;
172
- gap: 2px;
173
- }
174
- &[role='button']:focus-visible {
175
- outline: 2px solid ${t.focus};
176
- outline-offset: 2px;
177
- border-radius: ${t.radius.sm};
178
- }
179
- }
180
- [data-slot='item-title'],
181
- [data-slot='item-description'] {
182
- min-width: 0;
183
- overflow: hidden;
184
- text-overflow: ellipsis;
185
- white-space: nowrap;
186
- }
187
- [data-slot='item-title'] {
188
- color: ${t.fg.bright};
189
- font-weight: 500;
190
- line-height: 18px;
191
- }
192
- [data-slot='item-description'] {
193
- color: ${t.fg.muted};
194
- font-size: 12px;
195
- line-height: 16px;
196
-
197
- &:is([data-size='sm'] *) {
198
- font-size: 11px;
199
- line-height: 14px;
200
- }
201
- }
202
- [data-slot='item-meta'] {
203
- flex: none;
204
- gap: 8px;
205
- color: ${t.fg};
206
- font-variant-numeric: tabular-nums;
207
- text-align: end;
208
- white-space: nowrap;
209
-
210
- &:where(:not([hidden])) {
211
- display: inline-flex;
212
- align-items: center;
213
- }
214
- }
215
- [data-slot='item-actions'] {
216
- flex: none;
217
- gap: 4px;
218
-
219
- &:where(:not([hidden])) {
220
- display: inline-flex;
221
- align-items: center;
222
- }
223
- &:is([data-reveal-actions] > *) {
224
- opacity: 0;
225
- transition: opacity ${t.dur.fast} ease;
226
- }
227
- &:is([data-reveal-actions]:hover > *, [data-reveal-actions]:focus-within > *) {
228
- opacity: 1;
229
- }
230
- @media (hover: none) {
231
- &:is([data-reveal-actions] > *) {
232
- opacity: 1;
233
- }
234
- }
235
- }
236
- [data-slot='item-group'] {
237
- min-width: 0;
238
-
239
- &:where(:not([hidden])) {
240
- display: flex;
241
- flex-direction: column;
242
- gap: 2px;
243
- }
244
- &[data-separated] {
245
- gap: 0;
246
- }
247
- &[data-separated] > [data-slot='item'] {
248
- border-radius: 0;
249
- border-bottom-color: ${t.border.faint};
250
- }
251
- }
252
- }
253
- `,
254
- );
255
-
256
- class ItemImpl implements ItemHandle {
257
- readonly el: HTMLElement;
258
- private readonly content: HTMLElement;
259
- private readonly titleEl: HTMLElement;
260
- private descriptionEl: HTMLElement | null = null;
261
- /** What takes the focus, the role and the states: the row, or its text in a row with actions. */
262
- private readonly control: HTMLElement;
263
- private readonly option: boolean;
264
- private disabled = false;
265
- private readonly onClick = (e: MouseEvent): void => {
266
- if (this.disabled) return;
267
- const control = e.target instanceof Element ? e.target.closest(CONTROL) : null;
268
- if (control && control !== this.control && control !== this.el && this.el.contains(control)) return;
269
- this.props.onSelect?.(e);
270
- };
271
- private readonly onKey = (e: KeyboardEvent): void => {
272
- if (this.disabled || e.target !== this.control || (e.key !== 'Enter' && e.key !== ' ')) return;
273
- e.preventDefault();
274
- this.props.onSelect?.(e);
275
- };
276
-
277
- constructor(private readonly props: ItemProps) {
278
- injectStyles(document);
279
- const clickable = !!props.onSelect;
280
- const textControl = clickable && props.role === undefined && props.actions !== undefined && props.actions !== null;
281
- const role = props.role ?? (clickable ? 'button' : null);
282
- this.option = role === 'option';
283
- let media: HTMLElement | null = null;
284
- if (props.media !== undefined) {
285
- media = h('div', { slot: itemDef.slot('media') });
286
- if (typeof props.media === 'string') {
287
- const icon = iconEl(props.media, document);
288
- icon.dataset.icon = '';
289
- media.appendChild(icon);
290
- } else media.appendChild(props.media);
291
- }
292
- this.titleEl = h('div', { slot: itemDef.slot('title') }, props.title);
293
- this.content = h('div', { slot: itemDef.slot('content'), attrs: textControl ? { role, tabindex: '0' } : undefined }, this.titleEl);
294
- const trailing = (part: 'meta' | 'actions', children: Child): HTMLElement | null => {
295
- if (children === undefined || children === null) return null;
296
- const el = h('div', { slot: itemDef.slot(part) });
297
- append(el, [children]);
298
- return el;
299
- };
300
- this.el = h(
301
- 'div',
302
- {
303
- className: props.className,
304
- slot: itemDef.slot('root'),
305
- attrs: { ...itemVariants(props), role: textControl ? null : role, tabindex: clickable && !textControl ? '0' : null },
306
- data: { clickable: clickable || null, 'reveal-actions': props.revealActions || null },
307
- },
308
- media,
309
- this.content,
310
- trailing('meta', props.meta),
311
- trailing('actions', props.actions),
312
- );
313
- this.control = textControl ? this.content : this.el;
314
- if (props.description !== undefined) this.setDescription(props.description);
315
- if (clickable) {
316
- this.el.addEventListener('click', this.onClick);
317
- this.control.addEventListener('keydown', this.onKey);
318
- }
319
- this.setSelected(props.selected ?? false);
320
- this.setDisabled(props.disabled ?? false);
321
- }
322
-
323
- setSelected(selected: boolean): void {
324
- this.el.toggleAttribute('data-selected', selected);
325
- if (this.option) this.control.setAttribute('aria-selected', String(selected));
326
- else if (selected) this.control.setAttribute('aria-current', 'true');
327
- else this.control.removeAttribute('aria-current');
328
- }
329
-
330
- setDisabled(disabled: boolean): void {
331
- this.disabled = disabled;
332
- this.el.toggleAttribute('data-disabled', disabled);
333
- if (disabled) this.control.setAttribute('aria-disabled', 'true');
334
- else this.control.removeAttribute('aria-disabled');
335
- if (this.props.onSelect) this.control.tabIndex = disabled ? -1 : 0;
336
- }
337
-
338
- setTitle(title: string | Node): void {
339
- this.titleEl.replaceChildren(title);
340
- }
341
-
342
- setDescription(description: string | Node | null): void {
343
- if (description === null) {
344
- this.descriptionEl?.remove();
345
- this.descriptionEl = null;
346
- return;
347
- }
348
- this.descriptionEl ??= this.content.appendChild(h('div', { slot: itemDef.slot('description') }));
349
- this.descriptionEl.replaceChildren(description);
350
- }
351
-
352
- destroy(): void {
353
- this.el.removeEventListener('click', this.onClick);
354
- this.control.removeEventListener('keydown', this.onKey);
355
- }
356
- }
357
-
358
- export function createItem(props: ItemProps): ItemHandle {
359
- return new ItemImpl(props);
360
- }
361
-
362
- /** Rows stacked as one list. */
363
- export function createItemGroup(props: ItemGroupProps = {}): ItemGroupHandle {
364
- injectStyles(document);
365
- const el = h('div', {
366
- className: props.className,
367
- slot: itemDef.slot('group'),
368
- attrs: { role: props.role ?? (props.label ? 'group' : null), 'aria-label': props.label ?? null },
369
- data: { separated: props.separated || null },
370
- });
371
- const setItems = (items: ReadonlyArray<HTMLElement | ItemHandle>): void => {
372
- el.replaceChildren(...items.map((item) => (item instanceof HTMLElement ? item : item.el)));
373
- };
374
- setItems(props.items ?? []);
375
- return { el, setItems, destroy: () => el.remove() };
376
- }
1
+ // Item: one row of a list (a watchlist, a list of alerts, search results): media (an icon, a
2
+ // logo, an avatar, a star), a title over a description, then at its end the meta (figures,
3
+ // badges) and the actions (buttons). With `onSelect` the whole row is clickable, while a press
4
+ // on a control inside it stays that control's. To the keyboard (Enter, Space) and to screen
5
+ // readers the row is a button; a row with actions makes its text the button instead, as a
6
+ // button cannot hold other buttons. `selected` marks the current row; `revealActions` keeps the actions
7
+ // out of sight until the row is hovered or holds focus, while the meta stays. `default` is
8
+ // bare until hovered, `outline` bordered, `muted` on a sunken fill; `sm` is the compact row of
9
+ // dense panels. ItemGroup stacks rows, with a line between them when `separated`.
10
+ import { css, defineStyles } from '../lib/css';
11
+ import { defineComponent } from '../lib/define';
12
+ import { append, h, type Child } from '../lib/h';
13
+ import { iconEl } from '../lib/icons';
14
+ import { t } from '../lib/t';
15
+ import { variants, type VariantProps } from '../lib/variants';
16
+
17
+ export const itemDef = defineComponent({
18
+ name: 'item',
19
+ parts: ['root', 'media', 'content', 'title', 'description', 'meta', 'actions', 'group'],
20
+ settings: { radius: t.radius.md, gap: '10px' },
21
+ });
22
+
23
+ export const itemVariants = variants({ variant: ['default', 'outline', 'muted'], size: ['default', 'sm'] });
24
+
25
+ export type ItemVariant = (typeof itemVariants.spec.variant)[number];
26
+ export type ItemSize = (typeof itemVariants.spec.size)[number];
27
+
28
+ export interface ItemProps extends VariantProps<typeof itemVariants> {
29
+ /** The first line: text, or an element (a ticker with a chip after it). */
30
+ title: string | Node;
31
+ /** The muted line under the title. */
32
+ description?: string | Node;
33
+ /** Before the text: an icon id from the kit's registry, or an element (a logo, an
34
+ * avatar, a favorite star). */
35
+ media?: string | Node;
36
+ /** After the text, always shown: figures and badges (a price and its change). */
37
+ meta?: Child;
38
+ /** At the row's end: its buttons. */
39
+ actions?: Child;
40
+ /** Show the actions only while the row is hovered or holds focus (on touch screens they
41
+ * always show). The meta stays. */
42
+ revealActions?: boolean;
43
+ /** The current row (the open symbol, the highlighted result). */
44
+ selected?: boolean;
45
+ disabled?: boolean;
46
+ /** Makes the row clickable: a click, Enter or Space runs it. */
47
+ onSelect?: (e: MouseEvent | KeyboardEvent) => void;
48
+ /** The row's role (default `button` when it has `onSelect` and no actions, its text being
49
+ * the button when it has actions); `option` in a listbox, where `selected` is
50
+ * `aria-selected`. */
51
+ role?: string;
52
+ /** Extra class names (layout hooks). */
53
+ className?: string;
54
+ }
55
+
56
+ export interface ItemHandle {
57
+ readonly el: HTMLElement;
58
+ setSelected(selected: boolean): void;
59
+ setDisabled(disabled: boolean): void;
60
+ setTitle(title: string | Node): void;
61
+ /** A new description; null removes it. */
62
+ setDescription(description: string | Node | null): void;
63
+ destroy(): void;
64
+ }
65
+
66
+ export interface ItemGroupProps {
67
+ /** The rows, as elements or as the handles `createItem` returns. */
68
+ items?: ReadonlyArray<HTMLElement | ItemHandle>;
69
+ /** A line between the rows, edge to edge (default: separate rounded rows). */
70
+ separated?: boolean;
71
+ /** The list's accessible name. */
72
+ label?: string;
73
+ /** The group's role: default `group` when it has a `label` (the name needs a role to be
74
+ * read), none otherwise; `listbox` for rows whose role is `option`. */
75
+ role?: string;
76
+ /** Extra class names (layout hooks). */
77
+ className?: string;
78
+ }
79
+
80
+ export interface ItemGroupHandle {
81
+ readonly el: HTMLElement;
82
+ /** Replace the rows. */
83
+ setItems(items: ReadonlyArray<HTMLElement | ItemHandle>): void;
84
+ destroy(): void;
85
+ }
86
+
87
+ /** A press on one of these inside a row belongs to it, not to the row. */
88
+ const CONTROL = 'button, a[href], input, select, textarea, label, summary, [role="button"], [role="checkbox"], [role="switch"], [role="menuitem"], [role="option"], [role="link"], [contenteditable]:not([contenteditable="false"])';
89
+
90
+ const injectStyles = defineStyles(
91
+ 'vela-ui-item',
92
+ css`
93
+ @layer vela.ui {
94
+ [data-slot='item'] {
95
+ position: relative;
96
+ box-sizing: border-box;
97
+ min-width: 0;
98
+ padding: 8px 10px;
99
+ border: 1px solid transparent;
100
+ border-radius: ${itemDef.vars.radius};
101
+ color: ${t.fg};
102
+ font-size: 13px;
103
+ text-align: start;
104
+ transition:
105
+ background ${t.dur.fast} ease,
106
+ border-color ${t.dur.fast} ease;
107
+
108
+ &:where(:not([hidden])) {
109
+ display: flex;
110
+ align-items: center;
111
+ gap: ${itemDef.vars.gap};
112
+ }
113
+ &[data-size='sm'] {
114
+ gap: 8px;
115
+ padding: 4px 8px;
116
+ font-size: 12px;
117
+ }
118
+ &[data-variant='outline'] {
119
+ border-color: ${t.border};
120
+ }
121
+ &[data-variant='muted'] {
122
+ background: ${t.surface.sunken};
123
+ }
124
+ &[data-clickable] {
125
+ cursor: pointer;
126
+ user-select: none;
127
+ -webkit-tap-highlight-color: transparent;
128
+ }
129
+ &[data-clickable]:not([data-disabled]):hover {
130
+ background: ${t.hover};
131
+ }
132
+ &[data-selected],
133
+ &[data-selected][data-clickable]:hover {
134
+ background: ${t.active};
135
+ }
136
+ &:focus-visible {
137
+ outline: 2px solid ${t.focus};
138
+ outline-offset: -2px;
139
+ }
140
+ &[data-disabled] {
141
+ cursor: default;
142
+ opacity: 0.5;
143
+ }
144
+ }
145
+ [data-slot='item-media'] {
146
+ flex: none;
147
+ color: ${t.fg.muted};
148
+
149
+ &:where(:not([hidden])) {
150
+ display: inline-flex;
151
+ align-items: center;
152
+ justify-content: center;
153
+ }
154
+ & > [data-icon] {
155
+ width: 1em;
156
+ height: 1em;
157
+ font-size: 16px;
158
+ line-height: 0;
159
+ }
160
+ & > [data-icon] > svg {
161
+ width: 100%;
162
+ height: 100%;
163
+ }
164
+ }
165
+ [data-slot='item-content'] {
166
+ flex: 1 1 auto;
167
+ min-width: 0;
168
+
169
+ &:where(:not([hidden])) {
170
+ display: flex;
171
+ flex-direction: column;
172
+ gap: 2px;
173
+ }
174
+ &[role='button']:focus-visible {
175
+ outline: 2px solid ${t.focus};
176
+ outline-offset: 2px;
177
+ border-radius: ${t.radius.sm};
178
+ }
179
+ }
180
+ [data-slot='item-title'],
181
+ [data-slot='item-description'] {
182
+ min-width: 0;
183
+ overflow: hidden;
184
+ text-overflow: ellipsis;
185
+ white-space: nowrap;
186
+ }
187
+ [data-slot='item-title'] {
188
+ color: ${t.fg.bright};
189
+ font-weight: 500;
190
+ line-height: 18px;
191
+ }
192
+ [data-slot='item-description'] {
193
+ color: ${t.fg.muted};
194
+ font-size: 12px;
195
+ line-height: 16px;
196
+
197
+ &:is([data-size='sm'] *) {
198
+ font-size: 11px;
199
+ line-height: 14px;
200
+ }
201
+ }
202
+ [data-slot='item-meta'] {
203
+ flex: none;
204
+ gap: 8px;
205
+ color: ${t.fg};
206
+ font-variant-numeric: tabular-nums;
207
+ text-align: end;
208
+ white-space: nowrap;
209
+
210
+ &:where(:not([hidden])) {
211
+ display: inline-flex;
212
+ align-items: center;
213
+ }
214
+ }
215
+ [data-slot='item-actions'] {
216
+ flex: none;
217
+ gap: 4px;
218
+
219
+ &:where(:not([hidden])) {
220
+ display: inline-flex;
221
+ align-items: center;
222
+ }
223
+ &:is([data-reveal-actions] > *) {
224
+ opacity: 0;
225
+ transition: opacity ${t.dur.fast} ease;
226
+ }
227
+ &:is([data-reveal-actions]:hover > *, [data-reveal-actions]:focus-within > *) {
228
+ opacity: 1;
229
+ }
230
+ @media (hover: none) {
231
+ &:is([data-reveal-actions] > *) {
232
+ opacity: 1;
233
+ }
234
+ }
235
+ }
236
+ [data-slot='item-group'] {
237
+ min-width: 0;
238
+
239
+ &:where(:not([hidden])) {
240
+ display: flex;
241
+ flex-direction: column;
242
+ gap: 2px;
243
+ }
244
+ &[data-separated] {
245
+ gap: 0;
246
+ }
247
+ &[data-separated] > [data-slot='item'] {
248
+ border-radius: 0;
249
+ border-bottom-color: ${t.border.faint};
250
+ }
251
+ }
252
+ }
253
+ `,
254
+ );
255
+
256
+ class ItemImpl implements ItemHandle {
257
+ readonly el: HTMLElement;
258
+ private readonly content: HTMLElement;
259
+ private readonly titleEl: HTMLElement;
260
+ private descriptionEl: HTMLElement | null = null;
261
+ /** What takes the focus, the role and the states: the row, or its text in a row with actions. */
262
+ private readonly control: HTMLElement;
263
+ private readonly option: boolean;
264
+ private disabled = false;
265
+ private readonly onClick = (e: MouseEvent): void => {
266
+ if (this.disabled) return;
267
+ const control = e.target instanceof Element ? e.target.closest(CONTROL) : null;
268
+ if (control && control !== this.control && control !== this.el && this.el.contains(control)) return;
269
+ this.props.onSelect?.(e);
270
+ };
271
+ private readonly onKey = (e: KeyboardEvent): void => {
272
+ if (this.disabled || e.target !== this.control || (e.key !== 'Enter' && e.key !== ' ')) return;
273
+ e.preventDefault();
274
+ this.props.onSelect?.(e);
275
+ };
276
+
277
+ constructor(private readonly props: ItemProps) {
278
+ injectStyles(document);
279
+ const clickable = !!props.onSelect;
280
+ const textControl = clickable && props.role === undefined && props.actions !== undefined && props.actions !== null;
281
+ const role = props.role ?? (clickable ? 'button' : null);
282
+ this.option = role === 'option';
283
+ let media: HTMLElement | null = null;
284
+ if (props.media !== undefined) {
285
+ media = h('div', { slot: itemDef.slot('media') });
286
+ if (typeof props.media === 'string') {
287
+ const icon = iconEl(props.media, document);
288
+ icon.dataset.icon = '';
289
+ media.appendChild(icon);
290
+ } else media.appendChild(props.media);
291
+ }
292
+ this.titleEl = h('div', { slot: itemDef.slot('title') }, props.title);
293
+ this.content = h('div', { slot: itemDef.slot('content'), attrs: textControl ? { role, tabindex: '0' } : undefined }, this.titleEl);
294
+ const trailing = (part: 'meta' | 'actions', children: Child): HTMLElement | null => {
295
+ if (children === undefined || children === null) return null;
296
+ const el = h('div', { slot: itemDef.slot(part) });
297
+ append(el, [children]);
298
+ return el;
299
+ };
300
+ this.el = h(
301
+ 'div',
302
+ {
303
+ className: props.className,
304
+ slot: itemDef.slot('root'),
305
+ attrs: { ...itemVariants(props), role: textControl ? null : role, tabindex: clickable && !textControl ? '0' : null },
306
+ data: { clickable: clickable || null, 'reveal-actions': props.revealActions || null },
307
+ },
308
+ media,
309
+ this.content,
310
+ trailing('meta', props.meta),
311
+ trailing('actions', props.actions),
312
+ );
313
+ this.control = textControl ? this.content : this.el;
314
+ if (props.description !== undefined) this.setDescription(props.description);
315
+ if (clickable) {
316
+ this.el.addEventListener('click', this.onClick);
317
+ this.control.addEventListener('keydown', this.onKey);
318
+ }
319
+ this.setSelected(props.selected ?? false);
320
+ this.setDisabled(props.disabled ?? false);
321
+ }
322
+
323
+ setSelected(selected: boolean): void {
324
+ this.el.toggleAttribute('data-selected', selected);
325
+ if (this.option) this.control.setAttribute('aria-selected', String(selected));
326
+ else if (selected) this.control.setAttribute('aria-current', 'true');
327
+ else this.control.removeAttribute('aria-current');
328
+ }
329
+
330
+ setDisabled(disabled: boolean): void {
331
+ this.disabled = disabled;
332
+ this.el.toggleAttribute('data-disabled', disabled);
333
+ if (disabled) this.control.setAttribute('aria-disabled', 'true');
334
+ else this.control.removeAttribute('aria-disabled');
335
+ if (this.props.onSelect) this.control.tabIndex = disabled ? -1 : 0;
336
+ }
337
+
338
+ setTitle(title: string | Node): void {
339
+ this.titleEl.replaceChildren(title);
340
+ }
341
+
342
+ setDescription(description: string | Node | null): void {
343
+ if (description === null) {
344
+ this.descriptionEl?.remove();
345
+ this.descriptionEl = null;
346
+ return;
347
+ }
348
+ this.descriptionEl ??= this.content.appendChild(h('div', { slot: itemDef.slot('description') }));
349
+ this.descriptionEl.replaceChildren(description);
350
+ }
351
+
352
+ destroy(): void {
353
+ this.el.removeEventListener('click', this.onClick);
354
+ this.control.removeEventListener('keydown', this.onKey);
355
+ }
356
+ }
357
+
358
+ export function createItem(props: ItemProps): ItemHandle {
359
+ return new ItemImpl(props);
360
+ }
361
+
362
+ /** Rows stacked as one list. */
363
+ export function createItemGroup(props: ItemGroupProps = {}): ItemGroupHandle {
364
+ injectStyles(document);
365
+ const el = h('div', {
366
+ className: props.className,
367
+ slot: itemDef.slot('group'),
368
+ attrs: { role: props.role ?? (props.label ? 'group' : null), 'aria-label': props.label ?? null },
369
+ data: { separated: props.separated || null },
370
+ });
371
+ const setItems = (items: ReadonlyArray<HTMLElement | ItemHandle>): void => {
372
+ el.replaceChildren(...items.map((item) => (item instanceof HTMLElement ? item : item.el)));
373
+ };
374
+ setItems(props.items ?? []);
375
+ return { el, setItems, destroy: () => el.remove() };
376
+ }