@luxalgo/vela-ui 0.2.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.
- package/CHANGELOG.md +41 -0
- 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 +4 -4
- package/dist/{chunk-WGULXRWD.js → chunk-B2EZB5ZG.js} +3 -3
- package/dist/{chunk-G7IQLY3A.js → chunk-C3PXY2SW.js} +5 -0
- package/dist/{chunk-CC3DDNFC.js → chunk-DXDCKZ2M.js} +4 -4
- package/dist/{chunk-WBGSYCV2.js → chunk-IOOYDSSK.js} +3 -3
- package/dist/{chunk-B5ZSRUDQ.js → chunk-KKS6NEGI.js} +2 -2
- package/dist/{chunk-7VMN7NKJ.js → chunk-OJ2SEMN6.js} +3 -3
- package/dist/{chunk-MJ4RUGX7.js → chunk-OQXFQMZM.js} +2 -2
- package/dist/{chunk-V2Q4MRMS.js → chunk-TBXUF5YX.js} +12 -0
- package/dist/{chunk-WXH3EFV5.js → chunk-UEO6W37Z.js} +3 -3
- package/dist/{chunk-Z5PINFQO.js → chunk-WWHNUKSZ.js} +4 -4
- package/dist/collapsible.d.ts +1 -1
- package/dist/collapsible.js +3 -3
- package/dist/color-picker.js +4 -4
- package/dist/context-menu.js +4 -4
- 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 +14 -4
- package/dist/donut.d.ts +1 -1
- package/dist/donut.js +1 -1
- package/dist/drawer.d.ts +1 -1
- package/dist/drawer.js +1 -1
- package/dist/dropdown-menu.d.ts +1 -1
- package/dist/dropdown-menu.js +4 -4
- package/dist/empty.d.ts +1 -1
- package/dist/empty.js +2 -2
- package/dist/field.js +12 -12
- 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 +3 -1
- 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 +618 -1
- package/dist/month-grid.js +1 -1
- package/dist/number-input.d.ts +1 -1
- package/dist/number-input.js +4 -4
- package/dist/popover.js +3 -3
- package/dist/scatter-chart.d.ts +1 -1
- package/dist/scatter-chart.js +1 -1
- package/dist/select.js +6 -6
- package/dist/slider.d.ts +1 -1
- package/dist/slider.js +1 -1
- package/dist/sort-button.js +3 -3
- package/dist/sparkline.d.ts +1 -1
- package/dist/sparkline.js +1 -1
- package/dist/stat.d.ts +1 -1
- package/dist/stat.js +1 -1
- package/dist/switch.d.ts +1 -1
- package/dist/switch.js +2 -2
- package/dist/tabs.d.ts +1 -1
- package/dist/tabs.js +3 -3
- package/dist/text-area.d.ts +1 -1
- package/dist/text-area.js +2 -2
- package/dist/theme-Ct-q58xz.d.ts +341 -0
- package/dist/theme.d.ts +42 -192
- package/dist/theme.js +1 -1
- package/dist/theme.schema.json +292 -1
- 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 +3 -3
- package/dist/tooltip.js +1 -1
- package/dist/treemap.d.ts +80 -0
- package/dist/treemap.js +470 -0
- package/llms.txt +5 -2
- package/package.json +1 -1
- package/registry/button.ts +8 -0
- package/registry/card.ts +379 -0
- package/registry/dialog.ts +16 -0
- package/registry/toast.ts +965 -0
- package/registry/treemap.ts +847 -0
- package/skills/using-vela-ui-components/SKILL.md +4 -2
- package/src/components/button.ts +8 -0
- package/src/components/card.ts +379 -0
- package/src/components/dialog.ts +16 -0
- package/src/components/toast.ts +965 -0
- package/src/components/treemap.ts +847 -0
- package/src/lib/component-index.ts +12 -0
- package/src/lib/icons.ts +7 -0
- package/src/lib/theme.ts +6 -0
- 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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: using-vela-ui-components
|
|
3
|
-
description: Builds interface pieces with @luxalgo/vela-ui, the component kit behind Vela charts - buttons, tabs, toggle groups, badges, dialogs, drawers, dropdown and context menus, tooltips, hover cards, popovers, collapsible sections, list rows, form controls (checkbox, switch, slider, input, select, number input, date and color pickers) and data components (data table, sort buttons, month grid, stat, sparkline, bar, line and scatter charts). Use when adding or wiring these components on a page or next to a Vela chart, when a menu, dialog or tooltip from the kit renders unstyled or in the wrong place, or when the user wants UI that matches their Vela chart, even if they do not name the kit.
|
|
3
|
+
description: Builds interface pieces with @luxalgo/vela-ui, the component kit behind Vela charts - buttons, tabs, toggle groups, badges, dialogs, drawers, dropdown and context menus, tooltips, hover cards, popovers, toasts, collapsible sections, cards, list rows, form controls (checkbox, switch, slider, input, select, number input, date and color pickers) and data components (data table, sort buttons, month grid, stat, sparkline, bar, line and scatter charts, treemap). Use when adding or wiring these components on a page or next to a Vela chart, when a menu, dialog or tooltip from the kit renders unstyled or in the wrong place, or when the user wants UI that matches their Vela chart, even if they do not name the kit.
|
|
4
4
|
license: Apache-2.0
|
|
5
5
|
---
|
|
6
6
|
|
|
@@ -47,10 +47,12 @@ To find a component, its props, parts and an example, search
|
|
|
47
47
|
| A label or a card on hover | `tooltip`, `hover-card` |
|
|
48
48
|
| A modal or a sheet | `dialog`, `drawer` |
|
|
49
49
|
| Floating content of your own | `popover` |
|
|
50
|
+
| A notice that leaves on its own (saved, failed, an alert fired) | `toast`: raise it with `toast()` / `toast.success(…)` from anywhere, shown by one `createToaster()` |
|
|
50
51
|
| A section that folds | `collapsible` |
|
|
52
|
+
| A panel or tile of figures, a chart with its title | `card` (`inset` for tiles inside a card; `onSelect` makes it clickable) |
|
|
51
53
|
| Rows of a list (a watchlist, alerts, results) | `item`, stacked by `createItemGroup` |
|
|
52
54
|
| A form | `input`, `input-group`, `text-area`, `number-input`, `select`, `checkbox`, `switch`, `slider`, `date-picker`, `color-picker` |
|
|
53
|
-
| Data | `data-table`, `sort-button` (sorts a list that is not a table), `month-grid` (a figure per day), `sparkline`, `line-chart`, `bar-chart`, `scatter-chart`, `donut`, `empty` |
|
|
55
|
+
| Data | `data-table`, `sort-button` (sorts a list that is not a table), `month-grid` (a figure per day), `sparkline`, `line-chart`, `bar-chart`, `scatter-chart`, `donut`, `treemap` (weighted tiles tinted by a value, in groups that can zoom), `empty` |
|
|
54
56
|
|
|
55
57
|
## Rules that prevent the common bugs
|
|
56
58
|
|
package/src/components/button.ts
CHANGED
|
@@ -132,6 +132,14 @@ export const BUTTON_CSS = css`
|
|
|
132
132
|
& > [data-keys]:where(:not([hidden])) {
|
|
133
133
|
display: inline-flex;
|
|
134
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
|
+
}
|
|
135
143
|
&[data-size='sm'],
|
|
136
144
|
&[data-size='icon-sm'] {
|
|
137
145
|
height: ${buttonDef.vars['height-sm']};
|
|
@@ -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
|
+
}
|
package/src/components/dialog.ts
CHANGED
|
@@ -7,6 +7,8 @@ import { iconEl } from '../lib/icons';
|
|
|
7
7
|
import { css, defineStyles } from '../lib/css';
|
|
8
8
|
import { defineComponent } from '../lib/define';
|
|
9
9
|
import { disposeWithin } from '../lib/dispose';
|
|
10
|
+
import { msg, textOf, type Translator } from '../lib/i18n';
|
|
11
|
+
import { kitTranslator } from '../lib/kit-text';
|
|
10
12
|
import { t } from '../lib/t';
|
|
11
13
|
import { injectStyles as injectSheet } from '../lib/styles';
|
|
12
14
|
import { announceSurface } from '../lib/surface-events';
|
|
@@ -83,10 +85,15 @@ export interface DialogOptions extends DialogControllerOptions {
|
|
|
83
85
|
className?: string;
|
|
84
86
|
/** Close when the backdrop itself is pressed (not when a portaled popover is). */
|
|
85
87
|
closeOnBackdrop?: boolean;
|
|
88
|
+
/** The translator of the close button's name ("Close"). Default: the kit build's, else
|
|
89
|
+
* English. */
|
|
90
|
+
i18n?: Translator;
|
|
86
91
|
}
|
|
87
92
|
|
|
88
93
|
export type DialogProps = DialogOptions;
|
|
89
94
|
|
|
95
|
+
const CLOSE = msg('ui.dialog.close', 'Close');
|
|
96
|
+
|
|
90
97
|
const injectStyles = defineStyles(
|
|
91
98
|
'vela-ui-dialog-5',
|
|
92
99
|
css`
|
|
@@ -286,6 +293,7 @@ export class Dialog {
|
|
|
286
293
|
readonly positioner: HTMLElement;
|
|
287
294
|
readonly backdrop: HTMLElement;
|
|
288
295
|
readonly footer: HTMLElement | null;
|
|
296
|
+
private readonly closeButton: HTMLElement;
|
|
289
297
|
private readonly handle: HandleOf<typeof dialog.machine>;
|
|
290
298
|
private readonly ctrl: DialogController;
|
|
291
299
|
|
|
@@ -357,6 +365,8 @@ export class Dialog {
|
|
|
357
365
|
this.titleEl = title;
|
|
358
366
|
const close = part(doc.createElement('button'), 'close');
|
|
359
367
|
close.className = 'vela-dialog-close';
|
|
368
|
+
this.closeButton = close;
|
|
369
|
+
this.setI18n(opts.i18n ?? kitTranslator());
|
|
360
370
|
close.appendChild(iconEl('close', doc));
|
|
361
371
|
header.append(title, close);
|
|
362
372
|
|
|
@@ -487,6 +497,12 @@ export class Dialog {
|
|
|
487
497
|
announceSurface(this.panel, open, 'dialog', opener);
|
|
488
498
|
}
|
|
489
499
|
|
|
500
|
+
/** Speak the dialog's own words (the close button's name) through `i18n`, for a language
|
|
501
|
+
* that changes while the dialog lives; null is English. */
|
|
502
|
+
setI18n(i18n: Translator | null): void {
|
|
503
|
+
this.closeButton.setAttribute('aria-label', textOf(i18n, CLOSE));
|
|
504
|
+
}
|
|
505
|
+
|
|
490
506
|
get open(): boolean {
|
|
491
507
|
return this.ctrl.connect(this.handle.service).open;
|
|
492
508
|
}
|