@n1kname/components 0.29.0 → 0.30.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/package.json +4 -2
- package/src/DataTable.astro +210 -4
- package/src/Field.astro +46 -0
- package/src/FilterChips.astro +33 -3
- package/src/IconButton.astro +29 -0
- package/src/Notice.astro +15 -0
- package/src/Segmented.astro +7 -4
- package/src/Toast.astro +13 -0
- package/src/chips.js +10 -2
- package/src/controls.css +233 -0
- package/src/controls.js +41 -0
- package/src/datatable.js +24 -0
- package/src/segmented.js +11 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n1kname/components",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.30.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -26,7 +26,9 @@
|
|
|
26
26
|
"./mapstage.js": "./src/mapstage.js",
|
|
27
27
|
"./match.css": "./src/match.css",
|
|
28
28
|
"./loadfailed.js": "./src/loadfailed.js",
|
|
29
|
-
"./hexmap.js": "./src/hexmap.js"
|
|
29
|
+
"./hexmap.js": "./src/hexmap.js",
|
|
30
|
+
"./controls.css": "./src/controls.css",
|
|
31
|
+
"./controls.js": "./src/controls.js"
|
|
30
32
|
},
|
|
31
33
|
"dependencies": {
|
|
32
34
|
"@n1kname/charts": "0.12.0"
|
package/src/DataTable.astro
CHANGED
|
@@ -21,18 +21,31 @@ interface Props {
|
|
|
21
21
|
caption?: string;
|
|
22
22
|
/** Names the table for a screen reader without a visible caption, such as the title of the figure around it. */
|
|
23
23
|
label?: string;
|
|
24
|
-
/** 40px rows by default; compact is 32px, for dense tables
|
|
25
|
-
|
|
24
|
+
/** 40px rows by default; compact is 32px, for dense tables; comfortable is 56px with more air,
|
|
25
|
+
* for a list someone reads row by row (a name and its details, a place, a date). */
|
|
26
|
+
density?: 'default' | 'compact' | 'comfortable';
|
|
27
|
+
/** Below 640px each row becomes a card instead of scrolling sideways: the first cell across the
|
|
28
|
+
* top beside the last (the row's actions), the cells between on a line under them. A cell's
|
|
29
|
+
* `.stack-label` shows only then; a cell with class `stack-hide` hides then. */
|
|
30
|
+
stack?: boolean;
|
|
31
|
+
/** Starts in the loading state: placeholder rows in the table's shape, shown after 300ms so a
|
|
32
|
+
* fast load doesn't flash. setRows() or setState() from datatable.js ends it. */
|
|
33
|
+
loading?: boolean;
|
|
34
|
+
/** How many placeholder rows while loading. */
|
|
35
|
+
loadingRows?: number;
|
|
26
36
|
/** A long table scrolls in a region of its own this tall (such as "min(70vh, 640px)"), with its
|
|
27
37
|
* header staying in view, so what follows the table stays in place. Leave out to show every row. */
|
|
28
38
|
height?: string;
|
|
29
39
|
id?: string;
|
|
30
40
|
}
|
|
31
|
-
const { columns, rows = [], caption, label, density = 'default', height, id } = Astro.props;
|
|
41
|
+
const { columns, rows = [], caption, label, density = 'default', stack = false, loading = false, loadingRows = 6, height, id } = Astro.props;
|
|
42
|
+
const hasEmpty = Astro.slots.has('empty');
|
|
43
|
+
const hasFailed = Astro.slots.has('failed');
|
|
32
44
|
const num = (v: number) => new Intl.NumberFormat('nl-NL').format(v);
|
|
33
45
|
const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) : typeof c === 'string' ? c : '');
|
|
34
46
|
---
|
|
35
47
|
|
|
48
|
+
<div class:list={['data-table-wrap', { stacked: stack }]} data-state={loading ? 'loading' : rows.length === 0 && hasEmpty ? 'empty' : 'ready'}>
|
|
36
49
|
<div class:list={['scroll', { tall: height }]} tabindex="0" role="region" aria-label={caption ?? label ?? 'Table'} style={height ? `--height:${height}` : undefined}>
|
|
37
50
|
<table class:list={['data-table', density]} id={id} data-data-table>
|
|
38
51
|
{caption && <caption>{caption}</caption>}
|
|
@@ -51,7 +64,15 @@ const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) :
|
|
|
51
64
|
}
|
|
52
65
|
</tr>
|
|
53
66
|
</thead>
|
|
54
|
-
<tbody>
|
|
67
|
+
<tbody aria-busy={loading ? 'true' : undefined}>
|
|
68
|
+
{loading &&
|
|
69
|
+
Array.from({ length: loadingRows }, () => (
|
|
70
|
+
<tr class="placeholder" aria-hidden="true">
|
|
71
|
+
{columns.map((c) => (
|
|
72
|
+
<td class:list={{ left: c.align === 'left', wrap: c.wrap }}><span class="bar" /></td>
|
|
73
|
+
))}
|
|
74
|
+
</tr>
|
|
75
|
+
))}
|
|
55
76
|
{
|
|
56
77
|
rows.map((row) => (
|
|
57
78
|
<tr>
|
|
@@ -70,6 +91,9 @@ const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) :
|
|
|
70
91
|
</tbody>
|
|
71
92
|
</table>
|
|
72
93
|
</div>
|
|
94
|
+
{hasEmpty && <div class="state empty" data-state-for="empty"><slot name="empty" /></div>}
|
|
95
|
+
{hasFailed && <div class="state failed" data-state-for="failed"><slot name="failed" /></div>}
|
|
96
|
+
</div>
|
|
73
97
|
|
|
74
98
|
<script>
|
|
75
99
|
import { makeSortable } from './datatable.js';
|
|
@@ -187,6 +211,85 @@ const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) :
|
|
|
187
211
|
height: 32px;
|
|
188
212
|
}
|
|
189
213
|
|
|
214
|
+
.comfortable :global(td) {
|
|
215
|
+
height: 56px;
|
|
216
|
+
padding-block: var(--sp-3);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
.comfortable :global(td.wrap) {
|
|
220
|
+
padding-block: var(--sp-3);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/* Loading: placeholder rows in the table's shape, faded in after 300ms so a fast load shows
|
|
224
|
+
* nothing at all. Empty and failed replace the body with their slot. */
|
|
225
|
+
.state {
|
|
226
|
+
display: none;
|
|
227
|
+
padding-block: var(--sp-4);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
.data-table-wrap[data-state='empty'] .state.empty,
|
|
231
|
+
.data-table-wrap[data-state='failed'] .state.failed {
|
|
232
|
+
display: block;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
.data-table-wrap[data-state='empty'] tbody,
|
|
236
|
+
.data-table-wrap[data-state='failed'] tbody {
|
|
237
|
+
display: none;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
.data-table :global(tr.placeholder) {
|
|
241
|
+
opacity: 0;
|
|
242
|
+
animation: placeholder-in var(--motion-base) var(--ease-out) 300ms forwards;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
.data-table :global(tr.placeholder:hover) {
|
|
246
|
+
background: none;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
.data-table :global(tr.placeholder .bar) {
|
|
250
|
+
display: inline-block;
|
|
251
|
+
width: 60%;
|
|
252
|
+
max-width: 14ch;
|
|
253
|
+
height: 0.7em;
|
|
254
|
+
border-radius: var(--radius-sm);
|
|
255
|
+
background: color-mix(in oklab, var(--c-text) 9%, transparent);
|
|
256
|
+
vertical-align: middle;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
.data-table :global(tr.placeholder td.left .bar) {
|
|
260
|
+
width: 80%;
|
|
261
|
+
max-width: 24ch;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
@keyframes placeholder-in {
|
|
265
|
+
to {
|
|
266
|
+
opacity: 1;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
271
|
+
.data-table :global(tr.placeholder .bar) {
|
|
272
|
+
background: linear-gradient(90deg, color-mix(in oklab, var(--c-text) 9%, transparent) 30%, color-mix(in oklab, var(--c-text) 15%, transparent) 50%, color-mix(in oklab, var(--c-text) 9%, transparent) 70%);
|
|
273
|
+
background-size: 300% 100%;
|
|
274
|
+
animation: placeholder-sweep 1.6s linear infinite;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
@keyframes placeholder-sweep {
|
|
279
|
+
from {
|
|
280
|
+
background-position: 100% 0;
|
|
281
|
+
}
|
|
282
|
+
to {
|
|
283
|
+
background-position: 0 0;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
@media (prefers-reduced-motion: reduce) {
|
|
288
|
+
.data-table :global(tr.placeholder) {
|
|
289
|
+
animation-duration: 1ms;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
190
293
|
.data-table :global(td:last-child) {
|
|
191
294
|
padding-right: 0;
|
|
192
295
|
}
|
|
@@ -218,3 +321,106 @@ const show = (c: Cell) => (c === null ? '–' : typeof c === 'number' ? num(c) :
|
|
|
218
321
|
color: var(--c-text-muted);
|
|
219
322
|
}
|
|
220
323
|
</style>
|
|
324
|
+
|
|
325
|
+
<style is:global>
|
|
326
|
+
/* Stacked (the `stack` prop): below 640px each row becomes a card, the first cell across the top
|
|
327
|
+
* beside the last, the cells between on one line under them, each with its .stack-label. Global,
|
|
328
|
+
* because rows written by setRows() have no scope; table.data-table outranks the scoped
|
|
329
|
+
* min-width whichever loads last. New in 0.30.0. */
|
|
330
|
+
.stack-label {
|
|
331
|
+
display: none;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
@media (max-width: 639px) {
|
|
335
|
+
/* table.data-table outranks the component's own min-width, whichever loads last. */
|
|
336
|
+
.data-table-wrap.stacked table.data-table {
|
|
337
|
+
display: block;
|
|
338
|
+
width: 100%;
|
|
339
|
+
min-width: 0;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
.data-table-wrap.stacked .data-table thead {
|
|
343
|
+
position: absolute;
|
|
344
|
+
width: 1px;
|
|
345
|
+
height: 1px;
|
|
346
|
+
overflow: hidden;
|
|
347
|
+
clip-path: inset(50%);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
.data-table-wrap.stacked .data-table tbody,
|
|
351
|
+
.data-table-wrap.stacked .data-table tr {
|
|
352
|
+
display: block;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
.data-table-wrap.stacked .data-table tbody tr {
|
|
356
|
+
display: flex;
|
|
357
|
+
flex-wrap: wrap;
|
|
358
|
+
align-items: center;
|
|
359
|
+
gap: var(--sp-1) var(--sp-3);
|
|
360
|
+
padding: var(--sp-3) 0;
|
|
361
|
+
border-bottom: 1px solid var(--c-border);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/* The line break between the top line and the facts under it. */
|
|
365
|
+
.data-table-wrap.stacked .data-table tbody tr::before {
|
|
366
|
+
content: '';
|
|
367
|
+
order: 2;
|
|
368
|
+
flex-basis: 100%;
|
|
369
|
+
height: 0;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
.data-table-wrap.stacked .data-table tbody td {
|
|
373
|
+
display: block;
|
|
374
|
+
height: auto;
|
|
375
|
+
padding: 0;
|
|
376
|
+
border: 0;
|
|
377
|
+
order: 3;
|
|
378
|
+
text-align: left;
|
|
379
|
+
white-space: normal;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
.data-table-wrap.stacked .data-table tbody td:first-child {
|
|
383
|
+
order: 0;
|
|
384
|
+
flex: 1 1 0;
|
|
385
|
+
min-width: 0;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
.data-table-wrap.stacked .data-table tbody td:last-child {
|
|
389
|
+
order: 1;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
.data-table-wrap.stacked .data-table tbody td.wrap {
|
|
393
|
+
min-width: 0;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
.data-table-wrap.stacked .data-table tbody td:empty {
|
|
397
|
+
display: none;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
.data-table-wrap.stacked .data-table caption {
|
|
401
|
+
display: block;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/* A placeholder cell in a card has no width of its own to take a share of. */
|
|
405
|
+
.data-table-wrap.stacked .data-table tr.placeholder td .bar {
|
|
406
|
+
width: 9ch;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
.data-table-wrap.stacked .data-table tr.placeholder td:first-child .bar {
|
|
410
|
+
width: 70%;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
.stack-label {
|
|
414
|
+
display: inline;
|
|
415
|
+
color: var(--c-text-muted);
|
|
416
|
+
font-family: var(--font-body);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/* A cell with nothing worth saying in a card (an empty score, "New") hides when stacked. */
|
|
421
|
+
@media (max-width: 639px) {
|
|
422
|
+
.data-table-wrap.stacked .data-table tbody td.stack-hide {
|
|
423
|
+
display: none;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
</style>
|
package/src/Field.astro
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* A labelled input, select or textarea. The label sits above, a hint or an error under it, tied to
|
|
4
|
+
* the field for a screen reader. 44px tall, and 16px text so iOS doesn't zoom in. For a search box
|
|
5
|
+
* with a clear button, use SearchField.
|
|
6
|
+
*/
|
|
7
|
+
import './controls.css';
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
label: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
id?: string;
|
|
13
|
+
as?: 'input' | 'select' | 'textarea';
|
|
14
|
+
/** For an input: text, email, url, number, date… */
|
|
15
|
+
type?: string;
|
|
16
|
+
value?: string;
|
|
17
|
+
placeholder?: string;
|
|
18
|
+
/** For a select. */
|
|
19
|
+
options?: { value: string; label: string }[];
|
|
20
|
+
/** A line under the field: what to put in it. */
|
|
21
|
+
hint?: string;
|
|
22
|
+
/** What's wrong with what's in it. Marks the field invalid. */
|
|
23
|
+
error?: string;
|
|
24
|
+
required?: boolean;
|
|
25
|
+
/** For a textarea. */
|
|
26
|
+
rows?: number;
|
|
27
|
+
autocomplete?: string;
|
|
28
|
+
}
|
|
29
|
+
const slug = (t: string) => t.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
|
|
30
|
+
const { label, name, id = `field-${slug(name ?? Astro.props.label)}`, as = 'input', type = 'text', value, placeholder, options = [], hint, error, required, rows = 4, autocomplete } = Astro.props;
|
|
31
|
+
const described = [hint && `${id}-hint`, error && `${id}-error`].filter(Boolean).join(' ') || undefined;
|
|
32
|
+
const common = { id, name, required, 'aria-describedby': described, 'aria-invalid': error ? 'true' : undefined, class: 'field' };
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
<div class="field-group">
|
|
36
|
+
<label class="field-label" for={id}>{label}</label>
|
|
37
|
+
{as === 'select' ? (
|
|
38
|
+
<select {...common}>{options.map((o) => <option value={o.value} selected={o.value === value}>{o.label}</option>)}</select>
|
|
39
|
+
) : as === 'textarea' ? (
|
|
40
|
+
<textarea {...common} rows={rows} placeholder={placeholder}>{value}</textarea>
|
|
41
|
+
) : (
|
|
42
|
+
<input {...common} type={type} value={value} placeholder={placeholder} autocomplete={autocomplete} />
|
|
43
|
+
)}
|
|
44
|
+
{hint && <p class="field-hint" id={`${id}-hint`}>{hint}</p>}
|
|
45
|
+
{error && <p class="field-error" id={`${id}-error`}>{error}</p>}
|
|
46
|
+
</div>
|
package/src/FilterChips.astro
CHANGED
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
* One of several values to filter by, each a chip with a swatch and, under a search, how many
|
|
4
4
|
* items it has. The chosen one takes a tint of its own colour, never an inverted fill; states fade
|
|
5
5
|
* into each other. A second press goes back to All. Fires `chips-change`; fill or update it with
|
|
6
|
-
* setChips() from chips.js.
|
|
6
|
+
* setChips() from chips.js. An option can carry an icon instead of the swatch.
|
|
7
7
|
*/
|
|
8
|
+
import { ICONS, type IconName } from './icons';
|
|
8
9
|
interface Option {
|
|
9
10
|
value: string;
|
|
10
11
|
label: string;
|
|
11
12
|
/** A token, such as var(--c-chart-3), for the swatch and the tint. */
|
|
12
13
|
color?: string;
|
|
14
|
+
/** An icon from the set in place of the swatch, in the chip's colour: what kind of thing it is. */
|
|
15
|
+
icon?: IconName;
|
|
13
16
|
count?: number;
|
|
14
17
|
}
|
|
15
18
|
interface Props {
|
|
@@ -29,7 +32,11 @@ const list = all ? [{ value: 'all', label: all, count: options.every((o) => o.co
|
|
|
29
32
|
{
|
|
30
33
|
list.map((o) => (
|
|
31
34
|
<button type="button" data-value={o.value} aria-pressed={String(o.value === selected)} disabled={o.count === 0 && o.value !== selected} style={`--chip:${(o as Option).color ?? 'var(--c-accent)'}`}>
|
|
32
|
-
{(o as Option).
|
|
35
|
+
{(o as Option).icon ? (
|
|
36
|
+
<svg class="chip-icon" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" set:html={ICONS[(o as Option).icon as IconName]} />
|
|
37
|
+
) : (
|
|
38
|
+
(o as Option).color && <i class="chip-swatch" style={`background:${(o as Option).color}`} />
|
|
39
|
+
)}
|
|
33
40
|
{o.label}
|
|
34
41
|
{o.count !== undefined && <span class="n">{o.count}</span>}
|
|
35
42
|
</button>
|
|
@@ -97,6 +104,28 @@ const list = all ? [{ value: 'all', label: all, count: options.every((o) => o.co
|
|
|
97
104
|
font-variant-numeric: tabular-nums;
|
|
98
105
|
}
|
|
99
106
|
|
|
107
|
+
/* On a chosen chip's tint, muted measures 4.2 to 4.4:1 on chart-1 and chart-2 in light mode;
|
|
108
|
+
* secondary stays quieter than the label and passes. */
|
|
109
|
+
.filter-chips :global(button[aria-pressed='true'] .n) {
|
|
110
|
+
color: var(--c-text-secondary);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
.filter-chips :global(.chip-icon) {
|
|
114
|
+
flex: none;
|
|
115
|
+
color: var(--chip);
|
|
116
|
+
transition: transform var(--motion-spring) var(--ease-spring);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
.filter-chips :global(button[aria-pressed='true'] .chip-icon) {
|
|
120
|
+
transform: scale(1.12);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* The first counts to arrive land at once: chips don't fade in from disabled after a load. */
|
|
124
|
+
.filter-chips:global(.settling) :global(button),
|
|
125
|
+
.filter-chips:global(.settling) :global(.chip-swatch) {
|
|
126
|
+
transition: none;
|
|
127
|
+
}
|
|
128
|
+
|
|
100
129
|
.filter-chips :global(.chip-swatch) {
|
|
101
130
|
display: inline-block;
|
|
102
131
|
width: 8px;
|
|
@@ -111,7 +140,8 @@ const list = all ? [{ value: 'all', label: all, count: options.every((o) => o.co
|
|
|
111
140
|
|
|
112
141
|
@media (prefers-reduced-motion: reduce) {
|
|
113
142
|
.filter-chips :global(button),
|
|
114
|
-
.filter-chips :global(.chip-swatch)
|
|
143
|
+
.filter-chips :global(.chip-swatch),
|
|
144
|
+
.filter-chips :global(.chip-icon) {
|
|
115
145
|
transition: none;
|
|
116
146
|
}
|
|
117
147
|
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* A square button with only an icon, named for a screen reader by `label`. With `pressed` it's a
|
|
4
|
+
* toggle (a favourite or not): aria-pressed, a tint and a filled icon when on. With
|
|
5
|
+
* `href` it's a link. 36px, 44px on a touch screen. For rows written at runtime, iconButtonHTML()
|
|
6
|
+
* from controls.js writes the same markup.
|
|
7
|
+
*/
|
|
8
|
+
import './controls.css';
|
|
9
|
+
import Icon from './Icon.astro';
|
|
10
|
+
import type { IconName } from './icons';
|
|
11
|
+
|
|
12
|
+
interface Props {
|
|
13
|
+
icon: IconName;
|
|
14
|
+
label: string;
|
|
15
|
+
/** Leave out for a plain button; true or false makes it a toggle. */
|
|
16
|
+
pressed?: boolean;
|
|
17
|
+
href?: string;
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
id?: string;
|
|
20
|
+
class?: string;
|
|
21
|
+
}
|
|
22
|
+
const { icon, label, pressed, href, disabled, id, class: className } = Astro.props;
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
{href ? (
|
|
26
|
+
<a class:list={['icon-button', className]} href={href} aria-label={label} title={label} id={id}><Icon name={icon} /></a>
|
|
27
|
+
) : (
|
|
28
|
+
<button type="button" class:list={['icon-button', className]} aria-label={label} title={label} aria-pressed={pressed === undefined ? undefined : String(pressed)} disabled={disabled} id={id}><Icon name={icon} /></button>
|
|
29
|
+
)}
|
package/src/Notice.astro
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* A one-line caveat above the content it's about: the figures are a day old, a sensor is down.
|
|
4
|
+
* Quieter than EmptyState kind="error": the content still shows. Warning by default; info for
|
|
5
|
+
* something to know that isn't a problem.
|
|
6
|
+
*/
|
|
7
|
+
import './controls.css';
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
tone?: 'warning' | 'info';
|
|
11
|
+
}
|
|
12
|
+
const { tone = 'warning' } = Astro.props;
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
<p class:list={['notice', { info: tone === 'info' }]}><slot /></p>
|
package/src/Segmented.astro
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
---
|
|
2
2
|
/**
|
|
3
3
|
* One of a few fixed options side by side, such as Top 20 / 50 / 100 / 200. The marker slides to
|
|
4
|
-
* the chosen one on the spring. Fires `segment-change` with its value
|
|
5
|
-
*
|
|
4
|
+
* the chosen one on the spring. Fires `segment-change` with its value; setSegmented() from
|
|
5
|
+
* segmented.js sets it from code, or chooses none. For a value with many options, or with
|
|
6
|
+
* counts, use FilterChips.
|
|
6
7
|
*/
|
|
7
8
|
interface Props {
|
|
8
9
|
/** What is chosen, for a screen reader: "How many". */
|
|
9
10
|
label: string;
|
|
10
11
|
options: { value: string; label: string }[];
|
|
11
|
-
|
|
12
|
+
/** null for none chosen yet: no marker until one is. */
|
|
13
|
+
value?: string | null;
|
|
12
14
|
id?: string;
|
|
13
15
|
}
|
|
14
|
-
const { label, options, value = options[0]?.value, id } = Astro.props;
|
|
16
|
+
const { label, options, value = options[0]?.value ?? null, id } = Astro.props;
|
|
15
17
|
---
|
|
16
18
|
|
|
17
19
|
<div class="segmented" role="group" aria-label={label} id={id} data-segmented>
|
|
@@ -41,6 +43,7 @@ const { label, options, value = options[0]?.value, id } = Astro.props;
|
|
|
41
43
|
left: 3px;
|
|
42
44
|
border-radius: calc(var(--radius-md) - 3px);
|
|
43
45
|
background: var(--c-surface);
|
|
46
|
+
transition: opacity var(--motion-base) var(--ease-out);
|
|
44
47
|
box-shadow:
|
|
45
48
|
0 0 0 1px var(--c-border-strong),
|
|
46
49
|
0 1px 2px rgb(0 0 0 / 0.08);
|
package/src/Toast.astro
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
---
|
|
2
|
+
/**
|
|
3
|
+
* A short message after an action, at the bottom of the window, with an optional Undo. Put one on
|
|
4
|
+
* a page and call showToast() from controls.js; it's announced politely and goes after five
|
|
5
|
+
* seconds, or stays while the pointer or focus is on it.
|
|
6
|
+
*/
|
|
7
|
+
import './controls.css';
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
<div class="toast" role="status" aria-live="polite" hidden data-toast>
|
|
11
|
+
<span class="toast-text"></span>
|
|
12
|
+
<button type="button" hidden></button>
|
|
13
|
+
</div>
|
package/src/chips.js
CHANGED
|
@@ -2,23 +2,31 @@
|
|
|
2
2
|
// chosen one goes back to All. Fires `chips-change` with the value ("all" for All).
|
|
3
3
|
//
|
|
4
4
|
// import { setChips } from '@n1kname/components/chips.js';
|
|
5
|
-
// setChips(chips, [{ value: 'cycling', label: 'Cycling', color: 'var(--c-chart-3)', count: 312 }, …], 'all');
|
|
5
|
+
// setChips(chips, [{ value: 'cycling', label: 'Cycling', color: 'var(--c-chart-3)', icon: 'bike', count: 312 }, …], 'all');
|
|
6
6
|
// chips.addEventListener('chips-change', (e) => show(e.detail.value));
|
|
7
7
|
//
|
|
8
8
|
// Call setChips again with new counts (say, under a search): with the same values it only
|
|
9
9
|
// updates the counts and states, so focus stays where it is. A value with nothing is disabled
|
|
10
10
|
// unless it is the one chosen.
|
|
11
|
+
import { ICONS } from './icons';
|
|
12
|
+
|
|
11
13
|
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
12
14
|
|
|
13
15
|
const button = (o, selected) =>
|
|
14
16
|
`<button type="button" data-value="${esc(o.value)}" aria-pressed="${selected === o.value}"${o.count === 0 && selected !== o.value ? ' disabled' : ''} style="--chip:${o.color ?? 'var(--c-accent)'}">` +
|
|
15
|
-
`${o.color ? `<i class="chip-swatch" style="background:${o.color}"></i>` : ''}${esc(o.label)}${o.count === undefined ? '' : ` <span class="n">${o.count}</span>`}</button>`;
|
|
17
|
+
`${o.icon ? `<svg class="chip-icon" xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[o.icon] ?? ''}</svg>` : o.color ? `<i class="chip-swatch" style="background:${o.color}"></i>` : ''}${esc(o.label)}${o.count === undefined ? '' : ` <span class="n">${o.count}</span>`}</button>`;
|
|
16
18
|
|
|
17
19
|
export const setChips = (root, options, selected = 'all') => {
|
|
18
20
|
const all = root.dataset.all;
|
|
19
21
|
const list = all ? [{ value: 'all', label: all, count: options.every((o) => o.count !== undefined) ? options.reduce((s, o) => s + o.count, 0) : undefined }, ...options] : options;
|
|
20
22
|
const now = [...root.querySelectorAll('button[data-value]')].map((b) => b.dataset.value);
|
|
21
23
|
if (now.join('|') === list.map((o) => o.value).join('|')) {
|
|
24
|
+
// The first counts after a load land without the fade, so chips don't seem to switch on.
|
|
25
|
+
if (!root.dataset.counted) {
|
|
26
|
+
root.dataset.counted = '';
|
|
27
|
+
root.classList.add('settling');
|
|
28
|
+
requestAnimationFrame(() => requestAnimationFrame(() => root.classList.remove('settling')));
|
|
29
|
+
}
|
|
22
30
|
root.querySelectorAll('button[data-value]').forEach((b, i) => {
|
|
23
31
|
const o = list[i];
|
|
24
32
|
const n = b.querySelector('.n');
|
package/src/controls.css
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Controls for pages that change things: Field, IconButton, Toast and Notice. Plain classes, not
|
|
3
|
+
* scoped, so rows a page writes at runtime (setRows, iconButtonHTML) get them too. The components
|
|
4
|
+
* import this file; a page that only writes the markup imports it itself:
|
|
5
|
+
*
|
|
6
|
+
* import '@n1kname/components/controls.css';
|
|
7
|
+
*
|
|
8
|
+
* New in 0.30.0.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/* ---- Field ---- */
|
|
12
|
+
.field-label {
|
|
13
|
+
display: block;
|
|
14
|
+
margin-bottom: var(--sp-1);
|
|
15
|
+
font-size: var(--text-label);
|
|
16
|
+
font-weight: 600;
|
|
17
|
+
color: var(--c-text-secondary);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.field {
|
|
21
|
+
box-sizing: border-box;
|
|
22
|
+
width: 100%;
|
|
23
|
+
min-width: 0;
|
|
24
|
+
min-height: 44px;
|
|
25
|
+
padding: 0 var(--sp-3);
|
|
26
|
+
border: 1px solid var(--c-border-strong);
|
|
27
|
+
border-radius: var(--radius-md);
|
|
28
|
+
background: var(--c-surface);
|
|
29
|
+
color: var(--c-text);
|
|
30
|
+
font: inherit;
|
|
31
|
+
/* 16px stops iOS zooming into the field. */
|
|
32
|
+
font-size: max(16px, var(--text-body-sm));
|
|
33
|
+
transition: border-color var(--motion-hover) var(--ease-hover);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
textarea.field {
|
|
37
|
+
padding-block: var(--sp-2);
|
|
38
|
+
line-height: var(--leading-body-sm, 1.5);
|
|
39
|
+
resize: vertical;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
select.field {
|
|
43
|
+
appearance: none;
|
|
44
|
+
padding-right: var(--sp-7);
|
|
45
|
+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
|
|
46
|
+
background-repeat: no-repeat;
|
|
47
|
+
background-position: right var(--sp-3) center;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.field::placeholder {
|
|
51
|
+
color: var(--c-text-muted);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
.field:hover {
|
|
55
|
+
border-color: var(--c-text-muted);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.field:focus-visible {
|
|
59
|
+
outline: 2px solid var(--c-focus);
|
|
60
|
+
outline-offset: 1px;
|
|
61
|
+
border-color: transparent;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.field[aria-invalid='true'] {
|
|
65
|
+
border-color: var(--c-status-critical);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.field-error {
|
|
69
|
+
margin: var(--sp-1) 0 0;
|
|
70
|
+
font-size: var(--text-caption);
|
|
71
|
+
line-height: var(--leading-caption);
|
|
72
|
+
color: var(--c-status-critical);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.field-hint {
|
|
76
|
+
margin: var(--sp-1) 0 0;
|
|
77
|
+
font-size: var(--text-caption);
|
|
78
|
+
line-height: var(--leading-caption);
|
|
79
|
+
color: var(--c-text-muted);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/* ---- IconButton ---- */
|
|
83
|
+
.icon-button {
|
|
84
|
+
display: inline-grid;
|
|
85
|
+
place-items: center;
|
|
86
|
+
flex: none;
|
|
87
|
+
width: 36px;
|
|
88
|
+
height: 36px;
|
|
89
|
+
padding: 0;
|
|
90
|
+
border: 1px solid transparent;
|
|
91
|
+
border-radius: var(--radius-sm);
|
|
92
|
+
background: transparent;
|
|
93
|
+
color: var(--c-text-muted);
|
|
94
|
+
cursor: pointer;
|
|
95
|
+
transition:
|
|
96
|
+
background-color var(--motion-hover) var(--ease-hover),
|
|
97
|
+
border-color var(--motion-hover) var(--ease-hover),
|
|
98
|
+
color var(--motion-hover) var(--ease-hover),
|
|
99
|
+
transform var(--motion-fast) var(--ease-out);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
@media (pointer: coarse) {
|
|
103
|
+
.icon-button {
|
|
104
|
+
width: 44px;
|
|
105
|
+
height: 44px;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.icon-button:hover {
|
|
110
|
+
background: var(--c-surface-hover);
|
|
111
|
+
color: var(--c-text);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.icon-button[aria-pressed='true'] {
|
|
115
|
+
background: var(--c-surface-tint);
|
|
116
|
+
border-color: var(--c-accent-edge);
|
|
117
|
+
color: var(--c-text-accent);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.icon-button[aria-pressed='true'] svg {
|
|
121
|
+
fill: currentColor;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.icon-button:focus-visible {
|
|
125
|
+
outline: 2px solid var(--c-focus);
|
|
126
|
+
outline-offset: 2px;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.icon-button:active:not(:disabled) {
|
|
130
|
+
transform: scale(0.94);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.icon-button:disabled {
|
|
134
|
+
opacity: 0.5;
|
|
135
|
+
cursor: progress;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
@media (prefers-reduced-motion: reduce) {
|
|
139
|
+
.icon-button:active:not(:disabled) {
|
|
140
|
+
transform: none;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* ---- Toast ---- */
|
|
145
|
+
.toast {
|
|
146
|
+
position: fixed;
|
|
147
|
+
left: 50%;
|
|
148
|
+
bottom: max(var(--sp-5), env(safe-area-inset-bottom));
|
|
149
|
+
z-index: var(--z-panel);
|
|
150
|
+
display: flex;
|
|
151
|
+
align-items: center;
|
|
152
|
+
gap: var(--sp-4);
|
|
153
|
+
max-width: calc(100vw - 2 * var(--sp-4));
|
|
154
|
+
padding: var(--sp-2) var(--sp-2) var(--sp-2) var(--sp-4);
|
|
155
|
+
border-radius: var(--radius-md);
|
|
156
|
+
background: var(--c-text);
|
|
157
|
+
color: var(--c-bg);
|
|
158
|
+
font-size: var(--text-body-sm);
|
|
159
|
+
box-shadow: 0 8px 24px rgb(0 0 0 / 0.18);
|
|
160
|
+
translate: -50% 0;
|
|
161
|
+
transition:
|
|
162
|
+
opacity var(--motion-base) var(--ease-out),
|
|
163
|
+
translate var(--motion-spring) var(--ease-spring);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.toast[hidden] {
|
|
167
|
+
display: flex !important;
|
|
168
|
+
opacity: 0;
|
|
169
|
+
translate: -50% 12px;
|
|
170
|
+
pointer-events: none;
|
|
171
|
+
visibility: hidden;
|
|
172
|
+
transition:
|
|
173
|
+
opacity var(--motion-base) var(--ease-standard),
|
|
174
|
+
translate var(--motion-base) var(--ease-standard),
|
|
175
|
+
visibility 0s var(--motion-base);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
.toast-text {
|
|
179
|
+
overflow: hidden;
|
|
180
|
+
text-overflow: ellipsis;
|
|
181
|
+
white-space: nowrap;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.toast button {
|
|
185
|
+
flex: none;
|
|
186
|
+
min-height: 36px;
|
|
187
|
+
padding: 0 var(--sp-3);
|
|
188
|
+
border: 0;
|
|
189
|
+
border-radius: var(--radius-sm);
|
|
190
|
+
background: transparent;
|
|
191
|
+
color: inherit;
|
|
192
|
+
font: inherit;
|
|
193
|
+
font-weight: 600;
|
|
194
|
+
text-decoration: underline;
|
|
195
|
+
text-underline-offset: 3px;
|
|
196
|
+
cursor: pointer;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
.toast button:hover {
|
|
200
|
+
background: color-mix(in oklab, var(--c-bg) 14%, transparent);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
.toast button:focus-visible {
|
|
204
|
+
outline: 2px solid var(--c-bg);
|
|
205
|
+
outline-offset: 1px;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
@media (prefers-reduced-motion: reduce) {
|
|
209
|
+
.toast,
|
|
210
|
+
.toast[hidden] {
|
|
211
|
+
translate: -50% 0;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/* ---- Notice ---- */
|
|
216
|
+
.notice {
|
|
217
|
+
margin: 0;
|
|
218
|
+
padding: var(--sp-3) var(--sp-4);
|
|
219
|
+
border-left: 3px solid var(--c-status-warning);
|
|
220
|
+
border-radius: var(--radius-sm);
|
|
221
|
+
background: var(--c-surface);
|
|
222
|
+
font-size: var(--text-body-sm);
|
|
223
|
+
line-height: var(--leading-body-sm, 1.5);
|
|
224
|
+
color: var(--c-text-secondary);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
.notice.info {
|
|
228
|
+
border-left-color: var(--c-accent);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
.notice strong {
|
|
232
|
+
color: var(--c-text);
|
|
233
|
+
}
|
package/src/controls.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Drives Toast, and writes IconButton markup for rows made at runtime.
|
|
2
|
+
//
|
|
3
|
+
// import { showToast, iconButtonHTML } from '@n1kname/components/controls.js';
|
|
4
|
+
// showToast('Removed', { action: 'Undo', onAction: () => restore(id) });
|
|
5
|
+
// cell.innerHTML = iconButtonHTML({ icon: 'heart', label: 'Favourite', pressed: false });
|
|
6
|
+
import { ICONS } from './icons';
|
|
7
|
+
|
|
8
|
+
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
9
|
+
|
|
10
|
+
export const iconButtonHTML = ({ icon, label, pressed, size = 18, attrs = '' }) =>
|
|
11
|
+
`<button type="button" class="icon-button" aria-label="${esc(label)}" title="${esc(label)}"` +
|
|
12
|
+
`${pressed === undefined ? '' : ` aria-pressed="${pressed}"`}${attrs ? ` ${attrs}` : ''}>` +
|
|
13
|
+
`<svg xmlns="http://www.w3.org/2000/svg" width="${size}" height="${size}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="${size < 20 ? 1.75 : 2}" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[icon] ?? ''}</svg></button>`;
|
|
14
|
+
|
|
15
|
+
let timer;
|
|
16
|
+
export const showToast = (text, { action, onAction, timeout = 5000 } = {}) => {
|
|
17
|
+
const root = document.querySelector('[data-toast]');
|
|
18
|
+
if (!root) return;
|
|
19
|
+
const button = root.querySelector('button');
|
|
20
|
+
root.querySelector('.toast-text').textContent = text;
|
|
21
|
+
button.hidden = !action;
|
|
22
|
+
button.textContent = action ?? '';
|
|
23
|
+
button.onclick = () => {
|
|
24
|
+
hide();
|
|
25
|
+
onAction?.();
|
|
26
|
+
};
|
|
27
|
+
const hide = () => {
|
|
28
|
+
clearTimeout(timer);
|
|
29
|
+
root.hidden = true;
|
|
30
|
+
};
|
|
31
|
+
const arm = () => {
|
|
32
|
+
clearTimeout(timer);
|
|
33
|
+
timer = setTimeout(hide, timeout);
|
|
34
|
+
};
|
|
35
|
+
// Stays while someone is on it, so Undo can't vanish under the pointer.
|
|
36
|
+
root.onpointerenter = root.onfocusin = () => clearTimeout(timer);
|
|
37
|
+
root.onpointerleave = root.onfocusout = arm;
|
|
38
|
+
root.hidden = false;
|
|
39
|
+
arm();
|
|
40
|
+
return hide;
|
|
41
|
+
};
|
package/src/datatable.js
CHANGED
|
@@ -17,6 +17,9 @@ export const setRows = (table, rows, rowClass = () => '') => {
|
|
|
17
17
|
return `<tr class="${rowClass(rows[r], r)}">${tds.join('')}</tr>`;
|
|
18
18
|
})
|
|
19
19
|
.join('');
|
|
20
|
+
body.removeAttribute('aria-busy');
|
|
21
|
+
const wrap = table.closest('.data-table-wrap');
|
|
22
|
+
if (wrap) wrap.dataset.state = rows.length === 0 && wrap.querySelector('[data-state-for="empty"]') ? 'empty' : 'ready';
|
|
20
23
|
};
|
|
21
24
|
|
|
22
25
|
// Sorting: each header button cycles descending, then ascending. Rows without a value stay last.
|
|
@@ -58,3 +61,24 @@ export const setColumns = (table, columns) => {
|
|
|
58
61
|
.join('');
|
|
59
62
|
makeSortable(table);
|
|
60
63
|
};
|
|
64
|
+
|
|
65
|
+
// The table's state, for data that arrives at runtime: 'loading' writes placeholder rows in the
|
|
66
|
+
// table's shape (shown after 300ms, so a fast load shows nothing), 'empty' and 'failed' show the
|
|
67
|
+
// DataTable's `empty` or `failed` slot instead of the body, 'ready' shows the rows. setRows()
|
|
68
|
+
// moves to 'ready', or to 'empty' when there are no rows and the table has an empty slot.
|
|
69
|
+
export const setState = (table, state, { rows = 6 } = {}) => {
|
|
70
|
+
const wrap = table.closest('.data-table-wrap');
|
|
71
|
+
const body = table.tBodies[0];
|
|
72
|
+
if (state === 'loading') {
|
|
73
|
+
const head = [...table.tHead.rows[0].cells];
|
|
74
|
+
const scope = [...table.attributes].find((a) => a.name.startsWith('data-astro-cid'))?.name;
|
|
75
|
+
const s = scope ? ` ${scope}` : '';
|
|
76
|
+
const cell = (th) => `<td class="${['left', 'wrap'].filter((c) => th.classList.contains(c)).join(' ')}"${s}><span class="bar"${s}></span></td>`;
|
|
77
|
+
body.innerHTML = Array.from({ length: rows }, () => `<tr class="placeholder" aria-hidden="true"${s}>${head.map(cell).join('')}</tr>`).join('');
|
|
78
|
+
body.setAttribute('aria-busy', 'true');
|
|
79
|
+
} else {
|
|
80
|
+
body.removeAttribute('aria-busy');
|
|
81
|
+
body.querySelectorAll('tr.placeholder').forEach((tr) => tr.remove());
|
|
82
|
+
}
|
|
83
|
+
if (wrap) wrap.dataset.state = state;
|
|
84
|
+
};
|
package/src/segmented.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Drives Segmented: the marker slides to the chosen option on the spring, and `segment-change`
|
|
2
|
-
// fires with its value.
|
|
2
|
+
// fires with its value. setSegmented(root, value) chooses one from code, or none with null (the
|
|
3
|
+
// marker fades out), without firing the event.
|
|
3
4
|
const reduced = () => matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
4
5
|
const token = (name, fallback) => getComputedStyle(document.documentElement).getPropertyValue(name).trim() || fallback;
|
|
5
6
|
const ms = (name, fallback) => {
|
|
@@ -12,6 +13,7 @@ export const wireSegmented = (root) => {
|
|
|
12
13
|
const marker = root.querySelector('.marker');
|
|
13
14
|
const place = (animate) => {
|
|
14
15
|
const on = root.querySelector('[aria-pressed="true"]');
|
|
16
|
+
marker.style.opacity = on ? '1' : '0';
|
|
15
17
|
if (!on) return;
|
|
16
18
|
const r = root.getBoundingClientRect();
|
|
17
19
|
const b = on.getBoundingClientRect();
|
|
@@ -27,5 +29,13 @@ export const wireSegmented = (root) => {
|
|
|
27
29
|
root.dispatchEvent(new CustomEvent('segment-change', { detail: { value: b.dataset.value } }));
|
|
28
30
|
});
|
|
29
31
|
new ResizeObserver(() => place(false)).observe(root);
|
|
32
|
+
root._place = place;
|
|
30
33
|
place(false);
|
|
31
34
|
};
|
|
35
|
+
|
|
36
|
+
export const setSegmented = (root, value) => {
|
|
37
|
+
const was = root.querySelector('[aria-pressed="true"]');
|
|
38
|
+
root.querySelectorAll('button[data-value]').forEach((x) => x.setAttribute('aria-pressed', String(value !== null && x.dataset.value === value)));
|
|
39
|
+
// From nothing chosen, the marker appears in place rather than sliding in from the left.
|
|
40
|
+
root._place?.(Boolean(was));
|
|
41
|
+
};
|