@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
@@ -1,20 +1,24 @@
1
- // Button: a <button> with optional icons before and after its text. `default` is filled with
2
- // the selection color (OK, Apply), `outline` bordered (the dialogs' Cancel), `secondary` a
3
- // sunken fill, `ghost` bare until hovered (toolbar and header icons), `destructive` tinted
4
- // with the danger color, `link` text that underlines on hover, `up` and `down` filled with the
5
- // market's colors (buy and sell). Heights: `xs` 26px, `sm` 30px
6
- // (panels), `default` 34px (dialog footers), `lg` 40px (touch); the `icon*` sizes are the
7
- // same heights, square, for an icon alone, which `label` names.
1
+ // Button: a <button> with optional icons and key caps before and after its text (`↵ Open`,
2
+ // `Esc Close` in a picker's footer). `default` is filled with the selection color (OK, Apply),
3
+ // `outline` bordered (the dialogs' Cancel), `secondary` a sunken fill, `ghost` bare until
4
+ // hovered (toolbar and header icons), `destructive` tinted with the danger color, `link` text
5
+ // that underlines on hover, `up` and `down` filled with the market's colors (buy and sell),
6
+ // `favorite` a bare star whose pressed state takes the favorites color. Heights: `xs` 26px,
7
+ // `sm` 30px (panels), `default` 34px (dialog footers), `lg` 40px (touch); the `icon*` sizes
8
+ // are the same heights, square, for an icon alone, which `label` names. A toggle button
9
+ // (`pressed`, `defaultPressed` or `onPressedChange`) says its state through `aria-pressed`;
10
+ // uncontrolled it flips on a press, given `pressed` it only asks, until `setPressed` moves it.
8
11
  import { iconEl } from '@luxalgo/vela-ui/icons';
9
12
  import { css, defineStyles } from '@luxalgo/vela-ui';
10
13
  import { defineComponent } from '@luxalgo/vela-ui';
11
14
  import { h } from '@luxalgo/vela-ui';
12
15
  import { t } from '@luxalgo/vela-ui';
13
16
  import { variants, type VariantProps } from '@luxalgo/vela-ui';
17
+ import { createKbd } from '@luxalgo/vela-ui/kbd';
14
18
 
15
19
  export const buttonDef = defineComponent({
16
20
  name: 'button',
17
- parts: ['root', 'icon'],
21
+ parts: ['root', 'icon', 'keys'],
18
22
  settings: {
19
23
  radius: t.radius.md,
20
24
  height: '34px',
@@ -28,7 +32,7 @@ export const buttonDef = defineComponent({
28
32
  });
29
33
 
30
34
  export const buttonVariants = variants({
31
- variant: ['default', 'outline', 'secondary', 'ghost', 'destructive', 'link', 'up', 'down'],
35
+ variant: ['default', 'outline', 'secondary', 'ghost', 'destructive', 'link', 'up', 'down', 'favorite'],
32
36
  size: ['default', 'xs', 'sm', 'lg', 'icon', 'icon-xs', 'icon-sm', 'icon-lg'],
33
37
  });
34
38
 
@@ -38,14 +42,27 @@ export type ButtonSize = (typeof buttonVariants.spec.size)[number];
38
42
  export interface ButtonProps extends VariantProps<typeof buttonVariants> {
39
43
  /** The visible text (the `icon*` sizes show none; `label` names them). */
40
44
  text?: string;
41
- /** Icon id from the kit's registry, before the text (the icon of an `icon*` size). */
45
+ /** Icon id from the kit's registry, before the text (the icon of an `icon*` size).
46
+ * `favorite` defaults to `star`. */
42
47
  icon?: string;
48
+ /** The icon while pressed (`star-filled` for `star`). `favorite` defaults to `star-filled`. */
49
+ iconPressed?: string;
43
50
  /** Icon id after the text (a menu trigger's chevron). */
44
51
  iconEnd?: string;
52
+ /** Key caps before the text, one per key (`['↑', '↓']`). */
53
+ keys?: readonly string[];
54
+ /** Key caps after the text. */
55
+ keysEnd?: readonly string[];
45
56
  /** Accessible name; required for the `icon*` sizes, defaults to `text`. */
46
57
  label?: string;
47
- /** A toggle button's state (`aria-pressed`). Omitted: not a toggle. */
58
+ /** A controlled toggle's state (`aria-pressed`): a press only calls `onPressedChange`, and
59
+ * `setPressed` moves it. Omitted with `defaultPressed` and `onPressedChange`: not a toggle. */
48
60
  pressed?: boolean;
61
+ /** An uncontrolled toggle's first state: a press flips it and calls `onPressedChange`. */
62
+ defaultPressed?: boolean;
63
+ /** A press asked for this state (a toggle without `pressed` or `defaultPressed` starts
64
+ * unpressed). */
65
+ onPressedChange?: (pressed: boolean) => void;
49
66
  disabled?: boolean;
50
67
  onClick?: (e: MouseEvent) => void;
51
68
  /** Extra class names (layout hooks). */
@@ -55,6 +72,7 @@ export interface ButtonProps extends VariantProps<typeof buttonVariants> {
55
72
  export interface ButtonHandle {
56
73
  readonly el: HTMLButtonElement;
57
74
  readonly pressed: boolean | undefined;
75
+ /** Show a toggle's state, without calling `onPressedChange`. */
58
76
  setPressed(pressed: boolean): void;
59
77
  setDisabled(disabled: boolean): void;
60
78
  /** New text (or, for an `icon*` size, a new accessible name). */
@@ -107,6 +125,21 @@ export const BUTTON_CSS = css`
107
125
  &[data-size='icon-xs'] > [data-icon] {
108
126
  font-size: 12px;
109
127
  }
128
+ & > [data-keys] {
129
+ flex: none;
130
+ gap: 2px;
131
+ }
132
+ & > [data-keys]:where(:not([hidden])) {
133
+ display: inline-flex;
134
+ }
135
+ /* A key cap's own colors vanish on a fill that matches them (the solid
136
+ variants): inside a button it takes the button's text and draws only its outline. */
137
+ & > [data-keys] > [data-slot='kbd'] {
138
+ border-color: ${t.border};
139
+ background: transparent;
140
+ color: inherit;
141
+ box-shadow: none;
142
+ }
110
143
  &[data-size='sm'],
111
144
  &[data-size='icon-sm'] {
112
145
  height: ${buttonDef.vars['height-sm']};
@@ -225,43 +258,85 @@ export const BUTTON_CSS = css`
225
258
  background: ${t.active};
226
259
  color: ${t.fg.bright};
227
260
  }
261
+ &[data-variant='favorite'] {
262
+ border-color: transparent;
263
+ color: ${t.fg.muted};
264
+ }
265
+ &[data-variant='favorite']:not(:disabled):hover {
266
+ border-color: transparent;
267
+ }
268
+ &[data-variant='favorite'][data-pressed] {
269
+ background: transparent;
270
+ color: ${t.highlight};
271
+ }
272
+ &[data-variant='favorite'][data-pressed]:where(:not(:disabled)):hover {
273
+ background: ${t.hover};
274
+ color: ${t.highlight};
275
+ }
228
276
  }
229
277
  }
230
278
  `;
231
279
 
232
280
  const injectStyles = defineStyles(BUTTON_STYLE_ID, BUTTON_CSS);
233
281
 
282
+ const iconPart = (id: string, side: 'inline-start' | 'inline-end'): HTMLElement => {
283
+ const el = iconEl(id, document);
284
+ el.dataset.slot = buttonDef.slot('icon');
285
+ el.dataset.icon = side;
286
+ return el;
287
+ };
288
+
289
+ const keysPart = (keys: readonly string[] | undefined, side: 'inline-start' | 'inline-end'): HTMLElement | null =>
290
+ keys?.length ? h('span', { slot: buttonDef.slot('keys'), data: { keys: side } }, ...keys.map((key) => createKbd({ text: key }).el)) : null;
291
+
234
292
  class ButtonImpl implements ButtonHandle {
235
293
  readonly el: HTMLButtonElement;
236
294
  private readonly iconOnly: boolean;
237
295
  private readonly textNode: Text | null;
296
+ private readonly toggle: boolean;
297
+ private readonly controlled: boolean;
298
+ private readonly icon: string | undefined;
299
+ private readonly iconPressed: string | undefined;
300
+ private iconStart: HTMLElement | null;
301
+ private shownIcon: string | undefined;
238
302
  private label: string | undefined;
239
303
  private pressedState: boolean | undefined;
240
304
 
241
305
  constructor(props: ButtonProps) {
242
306
  injectStyles(document);
243
307
  const { variant, size } = buttonVariants.resolve(props);
308
+ const favorite = variant === 'favorite';
244
309
  this.iconOnly = size.startsWith('icon');
245
310
  this.label = props.label ?? props.text;
246
- this.pressedState = props.pressed;
247
- const icon = (id: string, side: 'inline-start' | 'inline-end'): HTMLElement => {
248
- const el = iconEl(id, document);
249
- el.dataset.slot = buttonDef.slot('icon');
250
- el.dataset.icon = side;
251
- return el;
252
- };
311
+ this.controlled = props.pressed !== undefined;
312
+ this.toggle = this.controlled || props.defaultPressed !== undefined || props.onPressedChange !== undefined;
313
+ this.pressedState = props.pressed ?? props.defaultPressed ?? (this.toggle ? false : undefined);
314
+ this.icon = props.icon ?? (favorite ? 'star' : undefined);
315
+ this.iconPressed = props.iconPressed ?? (favorite ? 'star-filled' : undefined);
316
+ this.iconStart = this.icon ? iconPart(this.icon, 'inline-start') : null;
317
+ this.shownIcon = this.icon;
253
318
  this.textNode = !this.iconOnly && props.text !== undefined ? document.createTextNode(props.text) : null;
319
+ const onClick = (e: MouseEvent): void => {
320
+ if (this.toggle) {
321
+ const next = !this.pressedState;
322
+ if (!this.controlled) this.setPressed(next);
323
+ props.onPressedChange?.(next);
324
+ }
325
+ props.onClick?.(e);
326
+ };
254
327
  this.el = h(
255
328
  'button',
256
329
  {
257
330
  className: props.className ? `vela-btn ${props.className}` : 'vela-btn',
258
331
  slot: buttonDef.slot('root'),
259
332
  attrs: { type: 'button', ...buttonVariants({ variant, size }) },
260
- on: props.onClick ? { click: props.onClick } : undefined,
333
+ on: this.toggle || props.onClick ? { click: onClick } : undefined,
261
334
  },
262
- props.icon ? icon(props.icon, 'inline-start') : null,
335
+ this.iconStart,
336
+ this.iconOnly ? null : keysPart(props.keys, 'inline-start'),
263
337
  this.textNode,
264
- props.iconEnd && !this.iconOnly ? icon(props.iconEnd, 'inline-end') : null,
338
+ this.iconOnly ? null : keysPart(props.keysEnd, 'inline-end'),
339
+ props.iconEnd && !this.iconOnly ? iconPart(props.iconEnd, 'inline-end') : null,
265
340
  );
266
341
  this.el.disabled = props.disabled ?? false;
267
342
  this.paint();
@@ -290,6 +365,15 @@ class ButtonImpl implements ButtonHandle {
290
365
 
291
366
  private paint(): void {
292
367
  const el = this.el;
368
+ if (this.iconStart && this.iconPressed) {
369
+ const icon = this.pressedState ? this.iconPressed : this.icon!;
370
+ if (icon !== this.shownIcon) {
371
+ const next = iconPart(icon, 'inline-start');
372
+ this.iconStart.replaceWith(next);
373
+ this.iconStart = next;
374
+ this.shownIcon = icon;
375
+ }
376
+ }
293
377
  el.toggleAttribute('data-pressed', this.pressedState === true);
294
378
  if (this.pressedState === undefined) el.removeAttribute('aria-pressed');
295
379
  else el.setAttribute('aria-pressed', String(this.pressedState));
@@ -7,6 +7,7 @@ import { iconAt } from '@luxalgo/vela-ui/icons';
7
7
  import type { ChartTheme } from '@luxalgo/vela-ui/tokens';
8
8
  import { css, defineStyles } from '@luxalgo/vela-ui';
9
9
  import { defineComponent } from '@luxalgo/vela-ui';
10
+ import { addSlot } from '@luxalgo/vela-ui';
10
11
  import { t } from '@luxalgo/vela-ui';
11
12
  import { Popover, type PlacementProps, type PopoverBoundary } from '@luxalgo/vela-ui/popover';
12
13
 
@@ -185,6 +186,17 @@ export const CALLOUT_CSS = css`
185
186
 
186
187
  const injectStyles = defineStyles(CALLOUT_STYLE_ID, CALLOUT_CSS);
187
188
 
189
+ /** A part of the deployed panel, as {@link calloutPart} gives it. */
190
+ export type CalloutPanelPart = Exclude<(typeof calloutBubbleDef.parts)[number], 'root'>;
191
+
192
+ /** Make `el` a part of a deployed panel, with the panel's look: for a panel built without a
193
+ * bubble, such as a chart's timeline-mark popup. Returns `el`. */
194
+ export function calloutPart<E extends HTMLElement>(el: E, part: CalloutPanelPart): E {
195
+ injectStyles(el.ownerDocument);
196
+ addSlot(el, calloutBubbleDef.slot(part));
197
+ return el;
198
+ }
199
+
188
200
  export class CalloutBubble {
189
201
  readonly el: HTMLElement;
190
202
  private spec: CalloutBubbleSpec;
@@ -294,11 +306,10 @@ export class CalloutBubble {
294
306
 
295
307
  private buildPanel(body: HTMLElement, panel: CalloutPanel): void {
296
308
  const doc = body.ownerDocument;
297
- const part = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, name: (typeof calloutBubbleDef.parts)[number]): HTMLElementTagNameMap[K] => {
309
+ const part = <K extends keyof HTMLElementTagNameMap>(tag: K, className: string, name: CalloutPanelPart): HTMLElementTagNameMap[K] => {
298
310
  const el = doc.createElement(tag);
299
311
  el.className = className;
300
- el.dataset.slot = calloutBubbleDef.slot(name);
301
- return el;
312
+ return calloutPart(el, name);
302
313
  };
303
314
  const root = part('div', 'vela-callout-panel', 'panel');
304
315
  if (panel.title) {
@@ -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 '@luxalgo/vela-ui';
10
+ import { defineComponent } from '@luxalgo/vela-ui';
11
+ import { append, h, type Child } from '@luxalgo/vela-ui';
12
+ import { t } from '@luxalgo/vela-ui';
13
+ import { variants, type VariantProps } from '@luxalgo/vela-ui';
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
+ }