@luxalgo/vela-ui 0.1.0 → 0.3.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 (124) hide show
  1. package/CHANGELOG.md +89 -0
  2. package/README.md +5 -2
  3. package/dist/badge.js +1 -1
  4. package/dist/bar-chart.js +1 -1
  5. package/dist/button.d.ts +18 -4
  6. package/dist/button.js +4 -3
  7. package/dist/calendar.d.ts +3 -1
  8. package/dist/calendar.js +1 -1
  9. package/dist/callout-bubble.d.ts +6 -1
  10. package/dist/callout-bubble.js +12 -8
  11. package/dist/card.d.ts +52 -0
  12. package/dist/card.js +179 -0
  13. package/dist/checkbox.js +5 -5
  14. package/dist/{chunk-X4YPSIW6.js → chunk-5BJV5FY5.js} +1 -1
  15. package/dist/{chunk-FCO3YZLT.js → chunk-ALAHGSLC.js} +1 -1
  16. package/dist/{chunk-5QHXOAZA.js → chunk-B2EZB5ZG.js} +4 -4
  17. package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
  18. package/dist/chunk-DXDCKZ2M.js +117 -0
  19. package/dist/{chunk-6NXY62JS.js → chunk-FYVQO5TN.js} +1 -1
  20. package/dist/{chunk-V6TTTMTE.js → chunk-GVRX3UAV.js} +4 -1
  21. package/dist/{chunk-XJAPCCOJ.js → chunk-IOOYDSSK.js} +4 -4
  22. package/dist/{chunk-OBLIX3VN.js → chunk-KKS6NEGI.js} +2 -2
  23. package/dist/{chunk-SAPURP46.js → chunk-OJ2SEMN6.js} +3 -3
  24. package/dist/{chunk-2Q4JRINP.js → chunk-OQXFQMZM.js} +3 -3
  25. package/dist/{chunk-AJ2IYSOI.js → chunk-TBXUF5YX.js} +35 -3
  26. package/dist/{chunk-ZB3BZHSN.js → chunk-UEO6W37Z.js} +4 -4
  27. package/dist/{chunk-CH3DV6FD.js → chunk-WWHNUKSZ.js} +5 -5
  28. package/dist/chunk-XBEL4K33.js +142 -0
  29. package/dist/collapsible.d.ts +49 -0
  30. package/dist/collapsible.js +106 -0
  31. package/dist/color-picker.js +5 -5
  32. package/dist/context-menu.js +5 -5
  33. package/dist/css.d.ts +3 -1
  34. package/dist/css.js +1 -1
  35. package/dist/data-table.d.ts +39 -2
  36. package/dist/data-table.js +1 -1
  37. package/dist/date-picker.js +3 -3
  38. package/dist/dialog.d.ts +8 -0
  39. package/dist/dialog.js +15 -5
  40. package/dist/donut.js +1 -1
  41. package/dist/drawer.js +2 -2
  42. package/dist/dropdown-menu.js +4 -4
  43. package/dist/empty.js +2 -2
  44. package/dist/field.js +13 -13
  45. package/dist/glyph-select.js +5 -5
  46. package/dist/h-Bl37qUis.d.ts +24 -0
  47. package/dist/hover-card.d.ts +1 -1
  48. package/dist/hover-card.js +3 -3
  49. package/dist/icons.js +1 -1
  50. package/dist/index.d.ts +10 -2
  51. package/dist/index.js +3 -3
  52. package/dist/input-group.js +4 -3
  53. package/dist/input.js +2 -2
  54. package/dist/item.d.ts +72 -0
  55. package/dist/item.js +131 -0
  56. package/dist/kbd.js +2 -2
  57. package/dist/line-chart.js +1 -1
  58. package/dist/manifest.json +1336 -65
  59. package/dist/month-grid.d.ts +45 -0
  60. package/dist/month-grid.js +157 -0
  61. package/dist/number-input.js +5 -5
  62. package/dist/popover.js +3 -3
  63. package/dist/scatter-chart.js +1 -1
  64. package/dist/select.js +7 -7
  65. package/dist/slider.d.ts +46 -0
  66. package/dist/slider.js +111 -0
  67. package/dist/sort-button.d.ts +41 -0
  68. package/dist/sort-button.js +88 -0
  69. package/dist/sparkline.js +1 -1
  70. package/dist/stat.js +1 -1
  71. package/dist/switch.js +3 -3
  72. package/dist/tabs.d.ts +3 -1
  73. package/dist/tabs.js +13 -8
  74. package/dist/text-area.js +2 -2
  75. package/dist/theme-Ct-q58xz.d.ts +341 -0
  76. package/dist/theme.d.ts +42 -181
  77. package/dist/theme.js +2 -2
  78. package/dist/theme.schema.json +541 -1
  79. package/dist/toast.d.ts +49 -0
  80. package/dist/toast.js +463 -0
  81. package/dist/toggle-group.js +4 -4
  82. package/dist/tooltip.js +2 -2
  83. package/dist/treemap.d.ts +80 -0
  84. package/dist/treemap.js +470 -0
  85. package/docs/theming.md +6 -0
  86. package/docs/writing-a-component.md +4 -1
  87. package/llms.txt +11 -3
  88. package/package.json +1 -1
  89. package/registry/button.ts +105 -21
  90. package/registry/callout-bubble.ts +14 -3
  91. package/registry/card.ts +379 -0
  92. package/registry/collapsible.ts +271 -0
  93. package/registry/dialog.ts +16 -0
  94. package/registry/dropdown-menu.ts +3 -3
  95. package/registry/item.ts +376 -0
  96. package/registry/month-grid.ts +328 -0
  97. package/registry/slider.ts +259 -0
  98. package/registry/sort-button.ts +189 -0
  99. package/registry/tabs.ts +38 -4
  100. package/registry/toast.ts +965 -0
  101. package/registry/treemap.ts +847 -0
  102. package/skills/using-vela-ui-components/SKILL.md +14 -9
  103. package/src/components/button.ts +105 -21
  104. package/src/components/callout-bubble.ts +14 -3
  105. package/src/components/card.ts +379 -0
  106. package/src/components/collapsible.ts +271 -0
  107. package/src/components/dialog.ts +16 -0
  108. package/src/components/dropdown-menu.ts +3 -3
  109. package/src/components/item.ts +376 -0
  110. package/src/components/month-grid.ts +328 -0
  111. package/src/components/slider.ts +259 -0
  112. package/src/components/sort-button.ts +189 -0
  113. package/src/components/tabs.ts +38 -4
  114. package/src/components/toast.ts +965 -0
  115. package/src/components/treemap.ts +847 -0
  116. package/src/lib/calendar.ts +5 -0
  117. package/src/lib/component-index.ts +34 -2
  118. package/src/lib/css.ts +6 -2
  119. package/src/lib/icons.ts +7 -0
  120. package/src/lib/scope.ts +135 -0
  121. package/src/lib/theme.ts +21 -5
  122. package/dist/chunk-EZQXTO6K.js +0 -89
  123. package/dist/chunk-IDA2BT7D.js +0 -29
  124. package/dist/data-table-DtU3FX9j.d.ts +0 -61
@@ -0,0 +1,379 @@
1
+ // Card: a surface for one subject (a panel of figures, a chart, a summary): a header with an
2
+ // eyebrow over the title and a description under it, an action at its end (buttons, a toggle
3
+ // group), then the content and a footer past a line. `default` is a bordered surface, `inset` a
4
+ // sunken tile for figures inside another card; `sm` is tighter. The title is a heading, so a
5
+ // screen reader can move from card to card. With `onSelect` the whole card is clickable while a
6
+ // press on a control inside it stays that control's: to the keyboard and screen readers its
7
+ // title is the button (the card itself when it has no title), as a card often holds buttons of
8
+ // its own and a button cannot hold other buttons.
9
+ import { css, defineStyles } from '../lib/css';
10
+ import { defineComponent } from '../lib/define';
11
+ import { append, h, type Child } from '../lib/h';
12
+ import { t } from '../lib/t';
13
+ import { variants, type VariantProps } from '../lib/variants';
14
+
15
+ export const cardDef = defineComponent({
16
+ name: 'card',
17
+ parts: ['root', 'header', 'heading', 'eyebrow', 'title', 'trigger', 'description', 'action', 'content', 'footer'],
18
+ settings: {
19
+ radius: t.radius.lg,
20
+ padding: '16px',
21
+ 'padding-sm': '10px 12px',
22
+ gap: '12px',
23
+ 'gap-sm': '8px',
24
+ background: t.surface,
25
+ border: t.border,
26
+ 'inset-background': `color-mix(in srgb, ${t.fg.bright} 2.5%, ${t.surface})`,
27
+ },
28
+ });
29
+
30
+ export const cardVariants = variants({ variant: ['default', 'inset'], size: ['default', 'sm'] });
31
+
32
+ export type CardVariant = (typeof cardVariants.spec.variant)[number];
33
+ export type CardSize = (typeof cardVariants.spec.size)[number];
34
+
35
+ export interface CardProps extends VariantProps<typeof cardVariants> {
36
+ /** Small capitals over the title: what kind of card it is ("Market pulse"). */
37
+ eyebrow?: string | Node;
38
+ /** The heading: text, or an element (a name with a badge after it). */
39
+ title?: string | Node;
40
+ /** The muted line under the title. */
41
+ description?: string | Node;
42
+ /** At the header's end: its buttons, a toggle group. */
43
+ action?: Child;
44
+ /** The body. More can be appended to the handle's `content` later. */
45
+ content?: Child;
46
+ /** Under the body, past a line: notes, a source, a time. */
47
+ footer?: Child;
48
+ /** The title's heading level (default 3). */
49
+ headingLevel?: 2 | 3 | 4 | 5 | 6;
50
+ /** The current card (the one a detail view shows). */
51
+ selected?: boolean;
52
+ disabled?: boolean;
53
+ /** Makes the card clickable: a click on it, or Enter or Space on its title, runs it. */
54
+ onSelect?: (e: MouseEvent | KeyboardEvent) => void;
55
+ /** Extra class names (layout hooks). */
56
+ className?: string;
57
+ }
58
+
59
+ export interface CardHandle {
60
+ readonly el: HTMLElement;
61
+ /** The body: append the card's content here. */
62
+ readonly content: HTMLElement;
63
+ /** A new title; null removes it. */
64
+ setTitle(title: string | Node | null): void;
65
+ /** A new description; null removes it. */
66
+ setDescription(description: string | Node | null): void;
67
+ /** A new footer; null removes it. */
68
+ setFooter(footer: Child | null): void;
69
+ setSelected(selected: boolean): void;
70
+ setDisabled(disabled: boolean): void;
71
+ destroy(): void;
72
+ }
73
+
74
+ /** A press on one of these inside a card belongs to it, not to the card. */
75
+ const CONTROL = 'button, a[href], input, select, textarea, label, summary, [role="button"], [role="checkbox"], [role="switch"], [role="menuitem"], [role="option"], [role="link"], [role="tab"], [role="radio"], [contenteditable]:not([contenteditable="false"])';
76
+
77
+ const injectStyles = defineStyles(
78
+ 'vela-ui-card',
79
+ css`
80
+ @layer vela.ui {
81
+ [data-slot='card'] {
82
+ position: relative;
83
+ box-sizing: border-box;
84
+ min-width: 0;
85
+ padding: ${cardDef.vars.padding};
86
+ border: 1px solid ${cardDef.vars.border};
87
+ border-radius: ${cardDef.vars.radius};
88
+ background: ${cardDef.vars.background};
89
+ color: ${t.fg};
90
+ font-size: 13px;
91
+ text-align: start;
92
+ transition:
93
+ background ${t.dur.fast} ease,
94
+ border-color ${t.dur.fast} ease;
95
+
96
+ &:where(:not([hidden])) {
97
+ display: flex;
98
+ flex-direction: column;
99
+ gap: ${cardDef.vars.gap};
100
+ }
101
+ &[data-size='sm'] {
102
+ gap: ${cardDef.vars['gap-sm']};
103
+ padding: ${cardDef.vars['padding-sm']};
104
+ font-size: 12px;
105
+ }
106
+ &[data-variant='inset'] {
107
+ background: ${cardDef.vars['inset-background']};
108
+ }
109
+ &[data-clickable] {
110
+ cursor: pointer;
111
+ -webkit-tap-highlight-color: transparent;
112
+ }
113
+ &[data-clickable]:not([data-disabled]):hover {
114
+ border-color: ${t.border.strong};
115
+ }
116
+ &[data-selected],
117
+ &[data-selected][data-clickable]:hover {
118
+ border-color: ${t.fg.bright};
119
+ }
120
+ &:focus-visible {
121
+ outline: 2px solid ${t.focus};
122
+ outline-offset: 2px;
123
+ }
124
+ &[data-disabled] {
125
+ cursor: default;
126
+ opacity: 0.5;
127
+ }
128
+ }
129
+ [data-slot='card-header'] {
130
+ gap: 12px;
131
+ min-width: 0;
132
+
133
+ &:where(:not([hidden])) {
134
+ display: flex;
135
+ align-items: flex-start;
136
+ }
137
+ }
138
+ [data-slot='card-heading'] {
139
+ flex: 1 1 auto;
140
+ min-width: 0;
141
+
142
+ &:where(:not([hidden])) {
143
+ display: flex;
144
+ flex-direction: column;
145
+ gap: 2px;
146
+ }
147
+ }
148
+ [data-slot='card-eyebrow'] {
149
+ color: ${t.fg.muted};
150
+ font-size: 10px;
151
+ font-weight: 500;
152
+ letter-spacing: 0.08em;
153
+ line-height: 14px;
154
+ text-transform: uppercase;
155
+ }
156
+ [data-slot='card-title'] {
157
+ margin: 0;
158
+ color: ${t.fg.bright};
159
+ font-size: 15px;
160
+ font-weight: 600;
161
+ line-height: 1.3;
162
+ overflow-wrap: anywhere;
163
+
164
+ &:is([data-size='sm'] *) {
165
+ font-size: 13px;
166
+ }
167
+ }
168
+ [data-slot='card-trigger'] {
169
+ all: unset;
170
+ border-radius: ${t.radius.sm};
171
+ cursor: pointer;
172
+
173
+ &:focus-visible {
174
+ outline: 2px solid ${t.focus};
175
+ outline-offset: 2px;
176
+ }
177
+ &:disabled {
178
+ cursor: default;
179
+ }
180
+ }
181
+ [data-slot='card-description'] {
182
+ margin: 0;
183
+ color: ${t.fg.muted};
184
+ font-size: 12px;
185
+ line-height: 1.45;
186
+ }
187
+ [data-slot='card-action'] {
188
+ flex: none;
189
+ gap: 6px;
190
+ margin-inline-start: auto;
191
+
192
+ &:where(:not([hidden])) {
193
+ display: inline-flex;
194
+ align-items: center;
195
+ }
196
+ }
197
+ [data-slot='card-content'] {
198
+ flex: 1 1 auto;
199
+ min-width: 0;
200
+ }
201
+ [data-slot='card-footer'] {
202
+ flex-wrap: wrap;
203
+ gap: 8px 16px;
204
+ padding-top: 10px;
205
+ border-top: 1px solid ${cardDef.vars.border};
206
+ color: ${t.fg.muted};
207
+ font-size: 11px;
208
+ line-height: 1.5;
209
+
210
+ &:where(:not([hidden])) {
211
+ display: flex;
212
+ align-items: center;
213
+ justify-content: space-between;
214
+ }
215
+ }
216
+ }
217
+ `,
218
+ );
219
+
220
+ class CardImpl implements CardHandle {
221
+ readonly el: HTMLElement;
222
+ readonly content: HTMLElement;
223
+ private header: HTMLElement | null = null;
224
+ private heading: HTMLElement | null = null;
225
+ private titleEl: HTMLElement | null = null;
226
+ private trigger: HTMLButtonElement | null = null;
227
+ private descriptionEl: HTMLElement | null = null;
228
+ private footerEl: HTMLElement | null = null;
229
+ private disabled = false;
230
+ private selected = false;
231
+ private readonly onClick = (e: MouseEvent): void => {
232
+ if (this.disabled) return;
233
+ const control = e.target instanceof Element ? e.target.closest(CONTROL) : null;
234
+ if (control && control !== this.trigger && control !== this.el && this.el.contains(control)) return;
235
+ this.props.onSelect?.(e);
236
+ };
237
+ private readonly onKey = (e: KeyboardEvent): void => {
238
+ if (this.disabled || e.target !== this.el || (e.key !== 'Enter' && e.key !== ' ')) return;
239
+ e.preventDefault();
240
+ this.props.onSelect?.(e);
241
+ };
242
+
243
+ constructor(private readonly props: CardProps) {
244
+ injectStyles(document);
245
+ this.content = h('div', { slot: cardDef.slot('content') });
246
+ append(this.content, [props.content]);
247
+ this.el = h(
248
+ 'div',
249
+ { className: props.className, slot: cardDef.slot('root'), attrs: cardVariants(props), data: { clickable: props.onSelect ? true : null } },
250
+ this.content,
251
+ );
252
+ if (props.eyebrow !== undefined) this.ensureHeading().prepend(h('div', { slot: cardDef.slot('eyebrow') }, props.eyebrow));
253
+ if (props.title !== undefined) this.setTitle(props.title);
254
+ if (props.description !== undefined) this.setDescription(props.description);
255
+ if (props.action !== undefined && props.action !== null) {
256
+ const action = h('div', { slot: cardDef.slot('action') });
257
+ append(action, [props.action]);
258
+ this.ensureHeader().append(action);
259
+ }
260
+ if (props.footer !== undefined) this.setFooter(props.footer);
261
+ if (props.onSelect) this.el.addEventListener('click', this.onClick);
262
+ this.disabled = props.disabled ?? false;
263
+ this.selected = props.selected ?? false;
264
+ this.syncControl();
265
+ this.setDisabled(this.disabled);
266
+ this.setSelected(this.selected);
267
+ }
268
+
269
+ setTitle(title: string | Node | null): void {
270
+ if (title === null) {
271
+ this.titleEl?.remove();
272
+ this.titleEl = null;
273
+ this.trigger = null;
274
+ this.syncControl();
275
+ return;
276
+ }
277
+ if (!this.titleEl) {
278
+ this.titleEl = h(`h${this.props.headingLevel ?? 3}` as 'h3', { slot: cardDef.slot('title') });
279
+ const heading = this.ensureHeading();
280
+ const eyebrow = heading.querySelector(`:scope > [data-slot="${cardDef.slot('eyebrow')}"]`);
281
+ if (eyebrow) eyebrow.after(this.titleEl);
282
+ else heading.prepend(this.titleEl);
283
+ if (this.props.onSelect) {
284
+ this.trigger = h('button', { slot: cardDef.slot('trigger'), attrs: { type: 'button' } });
285
+ this.titleEl.append(this.trigger);
286
+ }
287
+ this.syncControl();
288
+ }
289
+ (this.trigger ?? this.titleEl).replaceChildren(title);
290
+ }
291
+
292
+ setDescription(description: string | Node | null): void {
293
+ if (description === null) {
294
+ this.descriptionEl?.remove();
295
+ this.descriptionEl = null;
296
+ return;
297
+ }
298
+ this.descriptionEl ??= this.ensureHeading().appendChild(h('p', { slot: cardDef.slot('description') }));
299
+ this.descriptionEl.replaceChildren(description);
300
+ }
301
+
302
+ setFooter(footer: Child | null): void {
303
+ if (footer === null) {
304
+ this.footerEl?.remove();
305
+ this.footerEl = null;
306
+ return;
307
+ }
308
+ this.footerEl ??= this.el.appendChild(h('div', { slot: cardDef.slot('footer') }));
309
+ this.footerEl.replaceChildren();
310
+ append(this.footerEl, [footer]);
311
+ }
312
+
313
+ setSelected(selected: boolean): void {
314
+ this.selected = selected;
315
+ this.el.toggleAttribute('data-selected', selected);
316
+ this.el.removeAttribute('aria-current');
317
+ this.trigger?.removeAttribute('aria-current');
318
+ if (selected) this.control()?.setAttribute('aria-current', 'true');
319
+ }
320
+
321
+ setDisabled(disabled: boolean): void {
322
+ this.disabled = disabled;
323
+ this.el.toggleAttribute('data-disabled', disabled);
324
+ if (this.trigger) this.trigger.disabled = disabled;
325
+ else if (this.props.onSelect) {
326
+ this.el.tabIndex = disabled ? -1 : 0;
327
+ if (disabled) this.el.setAttribute('aria-disabled', 'true');
328
+ else this.el.removeAttribute('aria-disabled');
329
+ }
330
+ }
331
+
332
+ destroy(): void {
333
+ this.el.removeEventListener('click', this.onClick);
334
+ this.el.removeEventListener('keydown', this.onKey);
335
+ this.el.remove();
336
+ }
337
+
338
+ /** What the keyboard and screen readers press: the title's button, or the card itself without a title. */
339
+ private control(): HTMLElement | null {
340
+ if (!this.props.onSelect) return null;
341
+ return this.trigger ?? this.el;
342
+ }
343
+
344
+ /** Give the card the button's role while it has no title to carry it, and take it back once it has one. */
345
+ private syncControl(): void {
346
+ if (!this.props.onSelect) return;
347
+ if (this.trigger === null) {
348
+ this.el.setAttribute('role', 'button');
349
+ this.el.addEventListener('keydown', this.onKey);
350
+ } else {
351
+ this.el.removeAttribute('role');
352
+ this.el.removeAttribute('tabindex');
353
+ this.el.removeAttribute('aria-disabled');
354
+ this.el.removeEventListener('keydown', this.onKey);
355
+ }
356
+ this.setDisabled(this.disabled);
357
+ this.setSelected(this.selected);
358
+ }
359
+
360
+ private ensureHeader(): HTMLElement {
361
+ if (!this.header) {
362
+ this.header = h('div', { slot: cardDef.slot('header') });
363
+ this.el.prepend(this.header);
364
+ }
365
+ return this.header;
366
+ }
367
+
368
+ private ensureHeading(): HTMLElement {
369
+ if (!this.heading) {
370
+ this.heading = h('div', { slot: cardDef.slot('heading') });
371
+ this.ensureHeader().prepend(this.heading);
372
+ }
373
+ return this.heading;
374
+ }
375
+ }
376
+
377
+ export function createCard(props: CardProps = {}): CardHandle {
378
+ return new CardImpl(props);
379
+ }
@@ -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
+ }