@luxalgo/vela-ui 0.2.0 → 0.4.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.
- package/CHANGELOG.md +153 -91
- package/LICENSE +201 -201
- package/NOTICE +28 -28
- package/README.md +86 -86
- package/cli/engine.mjs +282 -282
- package/cli/index.mjs +154 -154
- package/dist/badge.js +1 -1
- package/dist/bar-chart.d.ts +1 -1
- package/dist/bar-chart.js +1 -1
- package/dist/button.d.ts +1 -1
- package/dist/button.js +4 -4
- package/dist/callout-bubble.d.ts +1 -1
- package/dist/callout-bubble.js +4 -4
- package/dist/card.d.ts +52 -0
- package/dist/card.js +179 -0
- package/dist/checkbox.js +5 -5
- package/dist/{chunk-B5ZSRUDQ.js → chunk-BWSOMG3G.js} +2 -2
- package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
- package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
- package/dist/chunk-GUIYFQ27.js +30 -0
- package/dist/{chunk-WGULXRWD.js → chunk-KUWTI2PE.js} +4 -4
- package/dist/{chunk-Z5PINFQO.js → chunk-QGWAN3EP.js} +5 -5
- package/dist/{chunk-V2Q4MRMS.js → chunk-RH53NQGE.js} +30 -4
- package/dist/{chunk-WBGSYCV2.js → chunk-SKFAYTDK.js} +4 -4
- package/dist/{chunk-MJ4RUGX7.js → chunk-TGJNHUYV.js} +3 -3
- package/dist/{chunk-WXH3EFV5.js → chunk-VC7SVSDK.js} +4 -4
- package/dist/{chunk-7VMN7NKJ.js → chunk-YT2BSP7O.js} +3 -3
- package/dist/collapsible.d.ts +1 -1
- package/dist/collapsible.js +3 -3
- package/dist/color-picker.js +5 -5
- package/dist/context-menu.js +5 -5
- package/dist/data-table.d.ts +1 -1
- package/dist/data-table.js +1 -1
- package/dist/date-picker.d.ts +1 -1
- package/dist/date-picker.js +2 -2
- package/dist/dialog.d.ts +9 -1
- package/dist/dialog.js +15 -5
- package/dist/donut.d.ts +1 -1
- package/dist/donut.js +1 -1
- package/dist/drawer.d.ts +1 -1
- package/dist/drawer.js +2 -2
- package/dist/dropdown-menu.d.ts +1 -1
- package/dist/dropdown-menu.js +5 -5
- package/dist/empty.d.ts +1 -1
- package/dist/empty.js +2 -2
- package/dist/field.js +13 -13
- package/dist/{stat.d.ts → figure.d.ts} +9 -9
- package/dist/figure.js +41 -0
- package/dist/glyph-select.js +5 -5
- package/dist/hover-card.d.ts +1 -1
- package/dist/hover-card.js +3 -3
- package/dist/icons.js +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -3
- package/dist/input-group.d.ts +1 -1
- package/dist/input-group.js +4 -4
- package/dist/input.js +2 -2
- package/dist/item.d.ts +1 -1
- package/dist/item.js +2 -2
- package/dist/kbd.d.ts +1 -1
- package/dist/kbd.js +2 -2
- package/dist/line-chart.d.ts +1 -1
- package/dist/line-chart.js +1 -1
- package/dist/manifest.json +691 -74
- package/dist/month-grid.js +1 -1
- package/dist/number-input.d.ts +1 -1
- package/dist/number-input.js +5 -5
- package/dist/popover.js +3 -3
- package/dist/scatter-chart.d.ts +1 -1
- package/dist/scatter-chart.js +1 -1
- package/dist/select.d.ts +1 -1
- package/dist/select.js +7 -7
- package/dist/slider.d.ts +1 -1
- package/dist/slider.js +2 -2
- package/dist/sort-button.js +4 -4
- package/dist/sparkline.d.ts +1 -1
- package/dist/sparkline.js +1 -1
- package/dist/switch.d.ts +1 -1
- package/dist/switch.js +3 -3
- package/dist/tabs.d.ts +1 -1
- package/dist/tabs.js +4 -4
- package/dist/text-area.d.ts +1 -1
- package/dist/text-area.js +2 -2
- package/dist/theme-TefB8vtl.d.ts +341 -0
- package/dist/theme.d.ts +42 -192
- package/dist/theme.js +1 -1
- package/dist/theme.schema.json +321 -30
- package/dist/toast.d.ts +49 -0
- package/dist/toast.js +463 -0
- package/dist/toggle-group.d.ts +1 -1
- package/dist/toggle-group.js +4 -4
- package/dist/tooltip.js +2 -2
- package/dist/treemap.d.ts +80 -0
- package/dist/treemap.js +470 -0
- package/dist/zag.d.ts +11 -2
- package/dist/zag.js +1 -1
- package/docs/theming.md +217 -217
- package/docs/what-to-change.md +20 -20
- package/docs/writing-a-component.md +250 -250
- package/llms.txt +6 -3
- package/package.json +90 -90
- package/registry/badge.ts +119 -119
- package/registry/bar-chart.ts +215 -215
- package/registry/button.ts +387 -379
- package/registry/callout-bubble.ts +347 -347
- package/registry/card.ts +379 -0
- package/registry/checkbox.ts +234 -234
- package/registry/collapsible.ts +271 -271
- package/registry/color-picker.ts +582 -582
- package/registry/context-menu.ts +208 -208
- package/registry/data-table.ts +215 -215
- package/registry/date-picker.ts +483 -483
- package/registry/dialog.ts +551 -535
- package/registry/donut.ts +145 -145
- package/registry/drawer.ts +636 -636
- package/registry/dropdown-menu.ts +762 -762
- package/registry/empty.ts +91 -91
- package/registry/field.ts +584 -584
- package/registry/{stat.ts → figure.ts} +96 -96
- package/registry/glyph-select.ts +268 -268
- package/registry/hover-card.ts +222 -222
- package/registry/input-group.ts +229 -229
- package/registry/input.ts +181 -181
- package/registry/item.ts +376 -376
- package/registry/kbd.ts +64 -64
- package/registry/line-chart.ts +218 -218
- package/registry/month-grid.ts +328 -328
- package/registry/number-input.ts +443 -443
- package/registry/popover.ts +368 -368
- package/registry/scatter-chart.ts +221 -221
- package/registry/select.ts +620 -620
- package/registry/slider.ts +259 -259
- package/registry/sort-button.ts +189 -189
- package/registry/sparkline.ts +114 -114
- package/registry/switch.ts +205 -205
- package/registry/tabs.ts +426 -426
- package/registry/text-area.ts +186 -186
- package/registry/toast.ts +965 -0
- package/registry/toggle-group.ts +295 -295
- package/registry/tooltip.ts +156 -156
- package/registry/treemap.ts +847 -0
- package/skills/theming-vela-ui/SKILL.md +137 -137
- package/skills/using-vela-ui-components/SKILL.md +139 -137
- package/skills/writing-vela-ui-components/SKILL.md +163 -163
- package/src/components/badge.ts +119 -119
- package/src/components/bar-chart.ts +215 -215
- package/src/components/button.ts +387 -379
- package/src/components/callout-bubble.ts +347 -347
- package/src/components/card.ts +379 -0
- package/src/components/checkbox.ts +234 -234
- package/src/components/collapsible.ts +271 -271
- package/src/components/color-picker.ts +582 -582
- package/src/components/context-menu.ts +208 -208
- package/src/components/data-table.ts +215 -215
- package/src/components/date-picker.ts +483 -483
- package/src/components/dialog.ts +551 -535
- package/src/components/donut.ts +145 -145
- package/src/components/drawer.ts +636 -636
- package/src/components/dropdown-menu.ts +762 -762
- package/src/components/empty.ts +91 -91
- package/src/components/field.ts +584 -584
- package/src/components/{stat.ts → figure.ts} +96 -96
- package/src/components/glyph-select.ts +268 -268
- package/src/components/hover-card.ts +222 -222
- package/src/components/input-group.ts +229 -229
- package/src/components/input.ts +181 -181
- package/src/components/item.ts +376 -376
- package/src/components/kbd.ts +64 -64
- package/src/components/line-chart.ts +218 -218
- package/src/components/month-grid.ts +328 -328
- package/src/components/number-input.ts +443 -443
- package/src/components/popover.ts +368 -368
- package/src/components/scatter-chart.ts +221 -221
- package/src/components/select.ts +620 -620
- package/src/components/slider.ts +259 -259
- package/src/components/sort-button.ts +189 -189
- package/src/components/sparkline.ts +114 -114
- package/src/components/switch.ts +205 -205
- package/src/components/tabs.ts +426 -426
- package/src/components/text-area.ts +186 -186
- package/src/components/toast.ts +965 -0
- package/src/components/toggle-group.ts +295 -295
- package/src/components/tooltip.ts +156 -156
- package/src/components/treemap.ts +847 -0
- package/src/index.ts +24 -24
- package/src/lib/calendar.ts +117 -117
- package/src/lib/chart.ts +72 -72
- package/src/lib/color.ts +89 -89
- package/src/lib/component-index.ts +163 -151
- package/src/lib/css.ts +48 -48
- package/src/lib/define.ts +45 -45
- package/src/lib/dispose.ts +36 -36
- package/src/lib/h.ts +55 -55
- package/src/lib/headless.ts +16 -16
- package/src/lib/i18n.ts +30 -30
- package/src/lib/icons.ts +244 -237
- package/src/lib/kit-text.ts +26 -26
- package/src/lib/page.ts +24 -24
- package/src/lib/palette.ts +81 -81
- package/src/lib/placement.ts +188 -188
- package/src/lib/scope.ts +135 -135
- package/src/lib/styles.ts +53 -53
- package/src/lib/surface-events.ts +31 -31
- package/src/lib/surface-exit.ts +87 -87
- package/src/lib/t.ts +68 -68
- package/src/lib/theme.ts +684 -664
- package/src/lib/tokens.ts +110 -110
- package/src/lib/variants.ts +32 -32
- package/src/lib/zag.ts +61 -46
- package/stylelint/index.mjs +60 -60
- package/stylelint/lint.mjs +81 -81
- package/stylelint/plugin.mjs +115 -115
- package/dist/chunk-P4MTEMT7.js +0 -23
- package/dist/stat.js +0 -41
- package/dist/{chunk-U3EH42YY.js → chunk-5BJV5FY5.js} +1 -1
- package/dist/{chunk-E2FE6V2N.js → chunk-ALAHGSLC.js} +1 -1
- package/dist/{chunk-C3ICYMI3.js → chunk-FYVQO5TN.js} +1 -1
package/registry/card.ts
ADDED
|
@@ -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
|
+
}
|