@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
@@ -1,379 +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
- }
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
+ }