@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/item.ts CHANGED
@@ -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 '@luxalgo/vela-ui';
11
- import { defineComponent } from '@luxalgo/vela-ui';
12
- import { append, h, type Child } 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
-
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 '@luxalgo/vela-ui';
11
+ import { defineComponent } from '@luxalgo/vela-ui';
12
+ import { append, h, type Child } 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
+
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
+ }