@luxalgo/vela-ui 0.1.0 → 0.2.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 (122) hide show
  1. package/CHANGELOG.md +48 -0
  2. package/README.md +5 -2
  3. package/dist/badge.js +2 -2
  4. package/dist/bar-chart.d.ts +1 -1
  5. package/dist/bar-chart.js +2 -2
  6. package/dist/button.d.ts +18 -4
  7. package/dist/button.js +4 -3
  8. package/dist/calendar.d.ts +3 -1
  9. package/dist/calendar.js +1 -1
  10. package/dist/callout-bubble.d.ts +7 -2
  11. package/dist/callout-bubble.js +12 -8
  12. package/dist/checkbox.js +4 -4
  13. package/dist/{chunk-SAPURP46.js → chunk-7VMN7NKJ.js} +3 -3
  14. package/dist/{chunk-OBLIX3VN.js → chunk-B5ZSRUDQ.js} +3 -3
  15. package/dist/{chunk-6NXY62JS.js → chunk-C3ICYMI3.js} +2 -2
  16. package/dist/chunk-CC3DDNFC.js +117 -0
  17. package/dist/{chunk-FCO3YZLT.js → chunk-E2FE6V2N.js} +2 -2
  18. package/dist/{chunk-V6TTTMTE.js → chunk-GVRX3UAV.js} +4 -1
  19. package/dist/{chunk-2Q4JRINP.js → chunk-MJ4RUGX7.js} +4 -4
  20. package/dist/{chunk-X4YPSIW6.js → chunk-U3EH42YY.js} +2 -2
  21. package/dist/{chunk-AJ2IYSOI.js → chunk-V2Q4MRMS.js} +23 -3
  22. package/dist/{chunk-XJAPCCOJ.js → chunk-WBGSYCV2.js} +3 -3
  23. package/dist/{chunk-5QHXOAZA.js → chunk-WGULXRWD.js} +4 -4
  24. package/dist/{chunk-ZB3BZHSN.js → chunk-WXH3EFV5.js} +3 -3
  25. package/dist/chunk-XBEL4K33.js +142 -0
  26. package/dist/{chunk-CH3DV6FD.js → chunk-Z5PINFQO.js} +4 -4
  27. package/dist/collapsible.d.ts +49 -0
  28. package/dist/collapsible.js +106 -0
  29. package/dist/color-picker.js +6 -6
  30. package/dist/context-menu.js +5 -5
  31. package/dist/css.d.ts +3 -1
  32. package/dist/css.js +1 -1
  33. package/dist/data-table.d.ts +39 -2
  34. package/dist/data-table.js +2 -2
  35. package/dist/date-picker.d.ts +1 -1
  36. package/dist/date-picker.js +3 -3
  37. package/dist/dialog.d.ts +1 -1
  38. package/dist/dialog.js +5 -5
  39. package/dist/donut.d.ts +1 -1
  40. package/dist/donut.js +2 -2
  41. package/dist/drawer.d.ts +1 -1
  42. package/dist/drawer.js +3 -3
  43. package/dist/dropdown-menu.d.ts +1 -1
  44. package/dist/dropdown-menu.js +4 -4
  45. package/dist/empty.d.ts +1 -1
  46. package/dist/empty.js +2 -2
  47. package/dist/field.js +12 -12
  48. package/dist/glyph-select.js +5 -5
  49. package/dist/h-Bl37qUis.d.ts +24 -0
  50. package/dist/hover-card.d.ts +1 -1
  51. package/dist/hover-card.js +3 -3
  52. package/dist/index.d.ts +7 -1
  53. package/dist/index.js +3 -3
  54. package/dist/input-group.d.ts +1 -1
  55. package/dist/input-group.js +4 -3
  56. package/dist/input.js +3 -3
  57. package/dist/item.d.ts +72 -0
  58. package/dist/item.js +131 -0
  59. package/dist/kbd.d.ts +1 -1
  60. package/dist/kbd.js +3 -3
  61. package/dist/line-chart.d.ts +1 -1
  62. package/dist/line-chart.js +2 -2
  63. package/dist/manifest.json +665 -11
  64. package/dist/month-grid.d.ts +45 -0
  65. package/dist/month-grid.js +157 -0
  66. package/dist/number-input.d.ts +1 -1
  67. package/dist/number-input.js +4 -4
  68. package/dist/popover.js +4 -4
  69. package/dist/scatter-chart.d.ts +1 -1
  70. package/dist/scatter-chart.js +2 -2
  71. package/dist/select.js +6 -6
  72. package/dist/slider.d.ts +46 -0
  73. package/dist/slider.js +111 -0
  74. package/dist/sort-button.d.ts +41 -0
  75. package/dist/sort-button.js +88 -0
  76. package/dist/sparkline.d.ts +1 -1
  77. package/dist/sparkline.js +2 -2
  78. package/dist/stat.d.ts +1 -1
  79. package/dist/stat.js +2 -2
  80. package/dist/switch.d.ts +1 -1
  81. package/dist/switch.js +3 -3
  82. package/dist/tabs.d.ts +3 -1
  83. package/dist/tabs.js +12 -7
  84. package/dist/text-area.d.ts +1 -1
  85. package/dist/text-area.js +3 -3
  86. package/dist/theme.d.ts +17 -6
  87. package/dist/theme.js +2 -2
  88. package/dist/theme.schema.json +250 -1
  89. package/dist/toggle-group.d.ts +1 -1
  90. package/dist/toggle-group.js +3 -3
  91. package/dist/tooltip.js +3 -3
  92. package/docs/theming.md +6 -0
  93. package/docs/writing-a-component.md +4 -1
  94. package/llms.txt +8 -3
  95. package/package.json +1 -1
  96. package/registry/button.ts +97 -21
  97. package/registry/callout-bubble.ts +14 -3
  98. package/registry/collapsible.ts +271 -0
  99. package/registry/dropdown-menu.ts +3 -3
  100. package/registry/item.ts +376 -0
  101. package/registry/month-grid.ts +328 -0
  102. package/registry/slider.ts +259 -0
  103. package/registry/sort-button.ts +189 -0
  104. package/registry/tabs.ts +38 -4
  105. package/skills/using-vela-ui-components/SKILL.md +12 -9
  106. package/src/components/button.ts +97 -21
  107. package/src/components/callout-bubble.ts +14 -3
  108. package/src/components/collapsible.ts +271 -0
  109. package/src/components/dropdown-menu.ts +3 -3
  110. package/src/components/item.ts +376 -0
  111. package/src/components/month-grid.ts +328 -0
  112. package/src/components/slider.ts +259 -0
  113. package/src/components/sort-button.ts +189 -0
  114. package/src/components/tabs.ts +38 -4
  115. package/src/lib/calendar.ts +5 -0
  116. package/src/lib/component-index.ts +22 -2
  117. package/src/lib/css.ts +6 -2
  118. package/src/lib/scope.ts +135 -0
  119. package/src/lib/theme.ts +15 -5
  120. package/dist/chunk-EZQXTO6K.js +0 -89
  121. package/dist/chunk-IDA2BT7D.js +0 -29
  122. package/dist/data-table-DtU3FX9j.d.ts +0 -61
@@ -0,0 +1,271 @@
1
+ // Collapsible: a section that shows or hides its content. Its own trigger is a header (a title,
2
+ // an optional count, a chevron) that toggles it on a click, Enter or Space; or the host hands
3
+ // it a trigger of its own, such as a row's chevron button, and places the content where it
4
+ // likes. `default` is a section header with the chevron at its end (a panel's drawers),
5
+ // `label` a small uppercase group label with the chevron before it (a list's groups). The
6
+ // content is `hidden` while closed.
7
+ import { css, defineStyles } from '../lib/css';
8
+ import { defineComponent } from '../lib/define';
9
+ import { h } from '../lib/h';
10
+ import { iconEl } from '../lib/icons';
11
+ import { t } from '../lib/t';
12
+ import { variants, type VariantProps } from '../lib/variants';
13
+ import { nextUid } from '../lib/zag';
14
+
15
+ export const collapsibleDef = defineComponent({
16
+ name: 'collapsible',
17
+ parts: ['root', 'trigger', 'icon', 'title', 'count', 'chevron', 'content'],
18
+ settings: { 'trigger-height': '36px', radius: t.radius.md },
19
+ });
20
+
21
+ export const collapsibleVariants = variants({ variant: ['default', 'label'] });
22
+
23
+ export type CollapsibleVariant = (typeof collapsibleVariants.spec.variant)[number];
24
+
25
+ export interface CollapsibleProps extends VariantProps<typeof collapsibleVariants> {
26
+ /** The header's text. Without `trigger`, the collapsible draws its header with it. */
27
+ title?: string;
28
+ /** After the title, muted: a count, a total. Null shows none. */
29
+ count?: string | number | null;
30
+ /** Icon id from the kit's registry, before the title. */
31
+ icon?: string;
32
+ /** A host element that toggles the content instead of the header (a row's chevron
33
+ * button): it gets `aria-expanded`, `aria-controls` and `data-state`, and a click on it
34
+ * toggles. The root then holds only the content. */
35
+ trigger?: HTMLElement;
36
+ /** The content, or a function that fills the content element. */
37
+ content?: Node | ((content: HTMLElement) => void);
38
+ /** Open when created (default closed). */
39
+ defaultOpen?: boolean;
40
+ /** Opened or closed by the user. */
41
+ onOpenChange?: (open: boolean) => void;
42
+ disabled?: boolean;
43
+ /** Extra class names (layout hooks). */
44
+ className?: string;
45
+ }
46
+
47
+ export interface CollapsibleHandle {
48
+ readonly el: HTMLElement;
49
+ /** What toggles it: its header, or the host's `trigger`. */
50
+ readonly trigger: HTMLElement;
51
+ /** The element that holds the content. */
52
+ readonly content: HTMLElement;
53
+ readonly open: boolean;
54
+ /** Open or close it without calling `onOpenChange`. */
55
+ setOpen(open: boolean): void;
56
+ /** A new header text (its own header only). */
57
+ setTitle(title: string): void;
58
+ /** A new count (its own header only); null hides it. */
59
+ setCount(count: string | number | null): void;
60
+ setDisabled(disabled: boolean): void;
61
+ destroy(): void;
62
+ }
63
+
64
+ const injectStyles = defineStyles(
65
+ 'vela-ui-collapsible',
66
+ css`
67
+ @layer vela.ui {
68
+ [data-slot='collapsible'] {
69
+ min-width: 0;
70
+ }
71
+ [data-slot='collapsible-trigger']:where(:not([hidden])) {
72
+ all: unset;
73
+ box-sizing: border-box;
74
+ display: flex;
75
+ align-items: center;
76
+ gap: 8px;
77
+ width: 100%;
78
+ cursor: pointer;
79
+ user-select: none;
80
+ -webkit-tap-highlight-color: transparent;
81
+
82
+ &:focus-visible {
83
+ outline: 2px solid ${t.focus};
84
+ outline-offset: -2px;
85
+ }
86
+ &[data-disabled] {
87
+ cursor: default;
88
+ opacity: 0.5;
89
+ }
90
+ &[data-variant='default'] {
91
+ min-height: ${collapsibleDef.vars['trigger-height']};
92
+ padding: 0 12px;
93
+ border-radius: ${collapsibleDef.vars.radius};
94
+ color: ${t.fg.bright};
95
+ font-size: 13px;
96
+ font-weight: 600;
97
+ transition: background ${t.dur.fast} ease;
98
+ }
99
+ &[data-variant='default']:not([data-disabled]):hover {
100
+ background: ${t.hover};
101
+ }
102
+ &[data-variant='label'] {
103
+ gap: 6px;
104
+ padding: 10px 4px 4px;
105
+ color: ${t.fg.muted};
106
+ font-size: 10px;
107
+ font-weight: 700;
108
+ letter-spacing: 0.08em;
109
+ text-transform: uppercase;
110
+ transition: color ${t.dur.fast} ease;
111
+ }
112
+ &[data-variant='label']:not([data-disabled]):hover {
113
+ color: ${t.fg};
114
+ }
115
+ }
116
+ [data-slot='collapsible-title'] {
117
+ min-width: 0;
118
+ overflow: hidden;
119
+ text-overflow: ellipsis;
120
+ white-space: nowrap;
121
+ }
122
+ [data-slot='collapsible-count'] {
123
+ flex: none;
124
+ color: ${t.fg.muted};
125
+ font-size: 12px;
126
+ font-weight: 400;
127
+ font-variant-numeric: tabular-nums;
128
+
129
+ &:is([data-variant='label'] > *) {
130
+ color: ${t.fg.faint};
131
+ font-size: inherit;
132
+ font-weight: 600;
133
+ letter-spacing: 0;
134
+ }
135
+ }
136
+ [data-slot~='collapsible-icon'],
137
+ [data-slot~='collapsible-chevron'] {
138
+ flex: none;
139
+ width: 1em;
140
+ height: 1em;
141
+ font-size: 16px;
142
+ line-height: 0;
143
+
144
+ & > svg {
145
+ width: 100%;
146
+ height: 100%;
147
+ }
148
+ }
149
+ [data-slot='collapsible-chevron'] {
150
+ font-size: 14px;
151
+ color: ${t.fg.muted};
152
+ transition: transform ${t.dur.med} ease;
153
+
154
+ &:is([data-variant='default'] > *) {
155
+ margin-inline-start: auto;
156
+ }
157
+ &:is([data-variant='label'] > *) {
158
+ font-size: 12px;
159
+ color: inherit;
160
+ }
161
+ &:is([data-variant='default'][data-state='open'] > *) {
162
+ transform: rotate(180deg);
163
+ }
164
+ &:is([data-variant='label'][data-state='closed'] > *) {
165
+ transform: rotate(-90deg);
166
+ }
167
+ &:is([data-variant='label'][data-state='closed'] > *):dir(rtl) {
168
+ transform: rotate(90deg);
169
+ }
170
+ }
171
+ }
172
+ `,
173
+ );
174
+
175
+ class CollapsibleImpl implements CollapsibleHandle {
176
+ readonly el: HTMLElement;
177
+ readonly trigger: HTMLElement;
178
+ readonly content: HTMLElement;
179
+ private readonly own: boolean;
180
+ private readonly titleEl: HTMLElement | null;
181
+ private countEl: HTMLElement | null = null;
182
+ private isOpen: boolean;
183
+ private disabled: boolean;
184
+ private readonly onClick = (): void => {
185
+ if (this.disabled) return;
186
+ this.paint(!this.isOpen);
187
+ this.props.onOpenChange?.(this.isOpen);
188
+ };
189
+
190
+ constructor(private readonly props: CollapsibleProps) {
191
+ injectStyles(document);
192
+ const variant = collapsibleVariants.resolve(props).variant;
193
+ this.isOpen = props.defaultOpen ?? false;
194
+ this.disabled = props.disabled ?? false;
195
+ const id = nextUid('vela-collapsible');
196
+ this.content = h('div', { slot: collapsibleDef.slot('content'), attrs: { id } });
197
+ if (props.content instanceof Node) this.content.appendChild(props.content);
198
+ else props.content?.(this.content);
199
+ this.own = !props.trigger;
200
+ if (props.trigger) {
201
+ this.trigger = props.trigger;
202
+ this.titleEl = null;
203
+ } else {
204
+ let icon: HTMLElement | null = null;
205
+ if (props.icon) {
206
+ icon = iconEl(props.icon, document);
207
+ icon.dataset.slot = collapsibleDef.slot('icon');
208
+ }
209
+ const chevron = iconEl('chevron-down', document);
210
+ chevron.dataset.slot = collapsibleDef.slot('chevron');
211
+ this.titleEl = h('span', { slot: collapsibleDef.slot('title'), text: props.title ?? '' });
212
+ this.trigger = h(
213
+ 'button',
214
+ { slot: collapsibleDef.slot('trigger'), data: { variant }, attrs: { type: 'button' } },
215
+ variant === 'label' ? chevron : null,
216
+ icon,
217
+ this.titleEl,
218
+ variant === 'label' ? null : chevron,
219
+ );
220
+ this.setCount(props.count ?? null);
221
+ }
222
+ this.trigger.setAttribute('aria-controls', id);
223
+ this.trigger.addEventListener('click', this.onClick);
224
+ this.el = h('div', { className: props.className, slot: collapsibleDef.slot('root'), data: { variant } }, this.own ? this.trigger : null, this.content);
225
+ this.paint(this.isOpen);
226
+ this.setDisabled(this.disabled);
227
+ }
228
+
229
+ get open(): boolean {
230
+ return this.isOpen;
231
+ }
232
+
233
+ setOpen(open: boolean): void {
234
+ this.paint(open);
235
+ }
236
+
237
+ setTitle(title: string): void {
238
+ if (this.titleEl) this.titleEl.textContent = title;
239
+ }
240
+
241
+ setCount(count: string | number | null): void {
242
+ if (!this.own) return;
243
+ this.countEl?.remove();
244
+ this.countEl = count === null || count === '' ? null : h('span', { slot: collapsibleDef.slot('count'), text: String(count) });
245
+ if (this.countEl) this.titleEl!.after(this.countEl);
246
+ }
247
+
248
+ setDisabled(disabled: boolean): void {
249
+ this.disabled = disabled;
250
+ this.trigger.toggleAttribute('data-disabled', disabled);
251
+ if (this.trigger instanceof HTMLButtonElement) this.trigger.disabled = disabled;
252
+ else if (disabled) this.trigger.setAttribute('aria-disabled', 'true');
253
+ else this.trigger.removeAttribute('aria-disabled');
254
+ }
255
+
256
+ destroy(): void {
257
+ this.trigger.removeEventListener('click', this.onClick);
258
+ }
259
+
260
+ private paint(open: boolean): void {
261
+ this.isOpen = open;
262
+ const state = open ? 'open' : 'closed';
263
+ this.trigger.setAttribute('aria-expanded', String(open));
264
+ for (const el of [this.trigger, this.content, this.el]) el?.setAttribute('data-state', state);
265
+ this.content.hidden = !open;
266
+ }
267
+ }
268
+
269
+ export function createCollapsible(props: CollapsibleProps = {}): CollapsibleHandle {
270
+ return new CollapsibleImpl(props);
271
+ }
@@ -310,12 +310,12 @@ const injectStyles = defineStyles(
310
310
  background ${t.dur.med} ease;
311
311
  }
312
312
  &[data-on] {
313
- background: var(${t.accent.bright.name}, ${t.accent});
314
- border-color: var(${t.accent.bright.name}, ${t.accent});
313
+ background: ${t.selected.bg};
314
+ border-color: ${t.selected.bg};
315
315
  }
316
316
  &[data-on]::after {
317
317
  transform: translateX(16px);
318
- background: ${t.bg};
318
+ background: ${t.selected.fg};
319
319
  }
320
320
  }
321
321
  [data-slot~='dropdown-menu-separator'] {
@@ -0,0 +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
+ }