@prunellejs/ui 0.1.1 → 0.7.2
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/LICENSE +21 -0
- package/llms.txt +19 -11
- package/package.json +2 -2
- package/src/CommandPalette.tsx +130 -75
- package/src/DataTable.tsx +137 -58
- package/src/Dialog.tsx +30 -14
- package/src/Menu.tsx +10 -8
- package/src/Select.tsx +19 -8
- package/src/Tabs.tsx +2 -1
- package/src/Toaster.tsx +79 -5
- package/src/behaviors.ts +104 -6
- package/src/fold.ts +7 -0
- package/src/index.ts +1 -1
- package/src/labels/dialog.ts +3 -0
- package/src/labels/fr/dialog.ts +3 -0
- package/src/labels/fr/palette.ts +8 -0
- package/src/labels/fr/select.ts +1 -0
- package/src/labels/fr/table.ts +13 -0
- package/src/labels/fr/toaster.ts +1 -0
- package/src/labels/palette.ts +8 -0
- package/src/labels/select.ts +1 -0
- package/src/labels/table.ts +13 -0
- package/src/labels/toaster.ts +1 -0
- package/src/plugin.ts +114 -31
- package/src/primitives.tsx +3 -2
- package/src/static.tsx +11 -6
- package/src/styles/bare.css +22 -312
- package/src/styles/base.css +61 -0
- package/src/styles/components/accordion.css +10 -0
- package/src/styles/components/badge.css +6 -0
- package/src/styles/components/button.css +11 -0
- package/src/styles/components/card.css +5 -0
- package/src/styles/components/checkbox.css +11 -0
- package/src/styles/components/command.css +25 -0
- package/src/styles/components/consent.css +38 -0
- package/src/styles/components/datatable.css +9 -0
- package/src/styles/components/dialog.css +16 -0
- package/src/styles/components/drawer.css +29 -0
- package/src/styles/components/field.css +8 -0
- package/src/styles/components/layout.css +69 -0
- package/src/styles/components/menu.css +21 -0
- package/src/styles/components/notice.css +5 -0
- package/src/styles/components/popover.css +8 -0
- package/src/styles/components/progress.css +14 -0
- package/src/styles/components/radio-group.css +11 -0
- package/src/styles/components/select.css +25 -0
- package/src/styles/components/switch.css +42 -0
- package/src/styles/components/tabs.css +4 -0
- package/src/styles/components/toast.css +28 -0
- package/src/styles/grammars/console.css +114 -256
- package/src/styles/grammars/editorial.css +101 -271
- package/src/styles/grammars/orbit.css +121 -390
- package/src/styles/grammars/prunelle.css +209 -357
- package/src/styles/grammars/soft.css +106 -240
- package/src/styles/grammars/workshop.css +110 -251
- package/src/styles/scan.ts +144 -0
- package/src/styles/split.ts +429 -0
- package/src/styles/theme.css +22 -0
- package/src/styles/themed/accordion.css +20 -0
- package/src/styles/themed/badge.css +20 -0
- package/src/styles/themed/button.css +24 -0
- package/src/styles/themed/card.css +23 -0
- package/src/styles/themed/checkbox.css +4 -0
- package/src/styles/themed/command.css +33 -0
- package/src/styles/themed/consent.css +33 -0
- package/src/styles/themed/datatable.css +28 -0
- package/src/styles/themed/dialog.css +13 -0
- package/src/styles/themed/drawer.css +13 -0
- package/src/styles/themed/field.css +22 -0
- package/src/styles/themed/menu.css +21 -0
- package/src/styles/themed/notice.css +17 -0
- package/src/styles/themed/popover.css +11 -0
- package/src/styles/themed/radio-group.css +12 -0
- package/src/styles/themed/select.css +32 -0
- package/src/styles/themed/switch.css +23 -0
- package/src/styles/themed/tabs.css +20 -0
- package/src/styles/themed/toast.css +18 -0
- package/src/table-state.ts +53 -16
- package/src/toast.ts +50 -8
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="field"] {
|
|
3
|
+
gap: var(--ui-fld-gap, 0.35rem);
|
|
4
|
+
|
|
5
|
+
& [data-part="label"] { font: var(--ui-fld-lf, 600 0.8rem/1.2 var(--ui-font-body)); color: var(--ui-fld-lfg, var(--ui-ink)); }
|
|
6
|
+
& :is(input:not([type="checkbox"], [type="radio"]), textarea, select) {
|
|
7
|
+
min-block-size: max(var(--ui-touch-min, 0px), var(--ui-in-h, 0px));
|
|
8
|
+
font: var(--ui-in-f, 400 0.95rem/1.4 var(--ui-font-body));
|
|
9
|
+
font-size: max(var(--ui-input-floor, 0px), var(--ui-in-fs, 0.95rem));
|
|
10
|
+
color: var(--ui-ink);
|
|
11
|
+
background: var(--ui-in-bg, var(--ui-surface));
|
|
12
|
+
border: var(--ui-in-bd, 1px solid var(--ui-line-strong));
|
|
13
|
+
border-radius: var(--ui-in-r, var(--ui-radius-inner, var(--ui-radius)));
|
|
14
|
+
padding: var(--ui-in-pad, 0.5rem 0.75rem);
|
|
15
|
+
transition: border-color var(--ui-motion), box-shadow var(--ui-motion);
|
|
16
|
+
}
|
|
17
|
+
& :is(input:not([type="checkbox"], [type="radio"]), textarea, select):focus { border-color: var(--ui-in-fbd, var(--ui-accent)); }
|
|
18
|
+
& [aria-invalid="true"] { border-color: var(--ui-danger); }
|
|
19
|
+
& [data-part="hint"] { margin: 0; font-size: var(--ui-fld-ms, 0.82rem); color: var(--ui-muted); }
|
|
20
|
+
& [data-part="error"] { margin: 0; font-size: var(--ui-fld-ms, 0.82rem); color: var(--ui-danger); }
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="menu"] [data-part="content"] {
|
|
3
|
+
background: var(--ui-pop-bg, var(--ui-surface));
|
|
4
|
+
color: var(--ui-ink);
|
|
5
|
+
border: var(--ui-pop-bd, 1px solid var(--ui-line));
|
|
6
|
+
border-radius: var(--ui-radius);
|
|
7
|
+
box-shadow: var(--ui-pop-sh, none);
|
|
8
|
+
padding: var(--ui-pop-pad, 0.35rem);
|
|
9
|
+
min-inline-size: var(--ui-pop-w, 12rem);
|
|
10
|
+
}
|
|
11
|
+
[data-ui="menu"] [data-part="item"] {
|
|
12
|
+
background: transparent;
|
|
13
|
+
border: 0;
|
|
14
|
+
border-radius: var(--ui-it-r, var(--ui-radius-inner, 0));
|
|
15
|
+
padding: var(--ui-it-pad, 0.45rem 0.65rem);
|
|
16
|
+
font: var(--ui-it-f, 500 0.88rem/1.3 var(--ui-font-body));
|
|
17
|
+
color: var(--ui-ink);
|
|
18
|
+
&:hover, &[data-highlighted="true"] { background: var(--ui-it-hbg, var(--ui-surface)); box-shadow: var(--ui-it-hind, inset 2px 0 0 var(--ui-accent)); }
|
|
19
|
+
&[data-selected="true"] { color: var(--ui-accent); font-weight: 600; }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="notice"] {
|
|
3
|
+
--ui-tone: var(--ui-accent);
|
|
4
|
+
border-style: solid;
|
|
5
|
+
border-width: var(--ui-notice-bw, 0);
|
|
6
|
+
border-color: var(--ui-line, transparent);
|
|
7
|
+
border-radius: var(--ui-radius);
|
|
8
|
+
background: var(--ui-notice-bg, var(--ui-surface));
|
|
9
|
+
color: var(--ui-notice-fg, var(--ui-ink));
|
|
10
|
+
padding: var(--ui-notice-pad, 0.75rem 1.05rem);
|
|
11
|
+
|
|
12
|
+
&[data-tone="accent"] { --ui-tone: var(--ui-accent); }
|
|
13
|
+
&[data-tone="success"] { --ui-tone: var(--ui-success); }
|
|
14
|
+
&[data-tone="warning"] { --ui-tone: var(--ui-warning); }
|
|
15
|
+
&[data-tone="danger"] { --ui-tone: var(--ui-danger); }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="popover"][popover] {
|
|
3
|
+
background: var(--ui-pop-bg, var(--ui-surface));
|
|
4
|
+
color: var(--ui-ink);
|
|
5
|
+
border: var(--ui-pop-bd, 1px solid var(--ui-line));
|
|
6
|
+
border-radius: var(--ui-radius);
|
|
7
|
+
box-shadow: var(--ui-pop-sh, none);
|
|
8
|
+
padding: var(--ui-pop-pad, 0.35rem);
|
|
9
|
+
min-inline-size: var(--ui-pop-w, 12rem);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="radio-group"] input { accent-color: var(--ui-chk-acc, var(--ui-accent-fill, var(--ui-accent))); }
|
|
3
|
+
[data-ui="radio-group"] [data-part="legend"] {
|
|
4
|
+
font: var(--ui-rg-lf, 600 0.8rem/1.2 var(--ui-font-body));
|
|
5
|
+
text-transform: var(--ui-rg-lc, none);
|
|
6
|
+
letter-spacing: var(--ui-rg-lt, normal);
|
|
7
|
+
color: var(--ui-rg-lfg, var(--ui-muted));
|
|
8
|
+
margin-block-end: var(--ui-rg-lmb, 0.45rem);
|
|
9
|
+
}
|
|
10
|
+
[data-ui="radio-group"] [data-part="option"] { font-size: var(--ui-chk-fs, inherit); }
|
|
11
|
+
[data-ui="radio-group"] [data-part="hint"] { font-size: var(--ui-rg-hs, 0.8rem); color: var(--ui-muted); }
|
|
12
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="select"] [data-part="trigger"] {
|
|
3
|
+
gap: 0.85rem;
|
|
4
|
+
min-inline-size: var(--ui-pop-w, 12rem);
|
|
5
|
+
min-block-size: max(var(--ui-touch-min, 0px), var(--ui-sel-h, 0px));
|
|
6
|
+
background: var(--ui-sel-bg, var(--ui-surface));
|
|
7
|
+
color: var(--ui-ink);
|
|
8
|
+
border: var(--ui-sel-bd, 1px solid var(--ui-line-strong));
|
|
9
|
+
border-radius: var(--ui-sel-r, var(--ui-radius-inner, var(--ui-radius)));
|
|
10
|
+
padding: var(--ui-sel-pad, 0.5rem 0.75rem);
|
|
11
|
+
font: var(--ui-sel-f, 500 0.88rem/1.3 var(--ui-font-body));
|
|
12
|
+
}
|
|
13
|
+
[data-ui="select"] [data-part="listbox"] {
|
|
14
|
+
background: var(--ui-pop-bg, var(--ui-surface));
|
|
15
|
+
color: var(--ui-ink);
|
|
16
|
+
border: var(--ui-pop-bd, 1px solid var(--ui-line));
|
|
17
|
+
border-radius: var(--ui-radius);
|
|
18
|
+
box-shadow: var(--ui-pop-sh, none);
|
|
19
|
+
padding: var(--ui-pop-pad, 0.35rem);
|
|
20
|
+
min-inline-size: var(--ui-pop-w, 12rem);
|
|
21
|
+
}
|
|
22
|
+
[data-ui="select"] [data-part="option"] {
|
|
23
|
+
background: transparent;
|
|
24
|
+
border: 0;
|
|
25
|
+
border-radius: var(--ui-it-r, var(--ui-radius-inner, 0));
|
|
26
|
+
padding: var(--ui-it-pad, 0.45rem 0.65rem);
|
|
27
|
+
font: var(--ui-it-f, 500 0.88rem/1.3 var(--ui-font-body));
|
|
28
|
+
color: var(--ui-ink);
|
|
29
|
+
&:hover, &[data-highlighted="true"] { background: var(--ui-it-hbg, var(--ui-surface)); box-shadow: var(--ui-it-hind, inset 2px 0 0 var(--ui-accent)); }
|
|
30
|
+
&[data-selected="true"] { color: var(--ui-accent); font-weight: 600; }
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="switch"] { font-size: var(--ui-chk-fs, inherit); }
|
|
3
|
+
[data-ui="switch"] [data-part="track"] {
|
|
4
|
+
border: var(--ui-sw-bd, 1px solid var(--ui-line-strong));
|
|
5
|
+
border-radius: var(--ui-sw-r, var(--ui-radius-inner, var(--ui-radius)));
|
|
6
|
+
background: var(--ui-sw-bg, var(--ui-paper));
|
|
7
|
+
&::after {
|
|
8
|
+
background: var(--ui-sw-th, var(--ui-muted));
|
|
9
|
+
border-radius: var(--ui-sw-thr, 0);
|
|
10
|
+
box-shadow: var(--ui-sw-thsh, none);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
[data-ui="switch"] input:checked + [data-part="track"] {
|
|
14
|
+
border-color: var(--ui-sw-onbd, var(--ui-sw-onbg, var(--ui-accent-fill, var(--ui-accent))));
|
|
15
|
+
background: var(--ui-sw-onbg, var(--ui-accent-fill, var(--ui-accent)));
|
|
16
|
+
&::after { background: var(--ui-sw-onth, var(--ui-on-accent)); }
|
|
17
|
+
}
|
|
18
|
+
@media (forced-colors: active) {
|
|
19
|
+
:root [data-ui="switch"] [data-part="track"]::after { background: ButtonText; }
|
|
20
|
+
:root [data-ui="switch"] input:checked + [data-part="track"] { background: Highlight; border-color: Highlight; }
|
|
21
|
+
:root [data-ui="switch"] input:checked + [data-part="track"]::after { background: HighlightText; }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="tabs"] [data-part="list"] {
|
|
3
|
+
gap: var(--ui-tab-gap, 0.25rem);
|
|
4
|
+
border-bottom: var(--ui-tab-lbb, none);
|
|
5
|
+
}
|
|
6
|
+
[data-ui="tabs"] [data-part="trigger"] {
|
|
7
|
+
background: transparent;
|
|
8
|
+
border: var(--ui-tab-bd, 0);
|
|
9
|
+
padding: var(--ui-tab-pad, 0.5rem 0.85rem);
|
|
10
|
+
font: var(--ui-tab-f, 500 0.86rem/1 var(--ui-font-body));
|
|
11
|
+
color: var(--ui-tab-fg, var(--ui-muted));
|
|
12
|
+
transition: color var(--ui-motion);
|
|
13
|
+
&:hover { color: var(--ui-ink); }
|
|
14
|
+
&[data-state="active"] { color: var(--ui-tab-afg, var(--ui-ink)); background: var(--ui-tab-abg, transparent); box-shadow: var(--ui-tab-aind, inset 0 -2px 0 var(--ui-accent)); }
|
|
15
|
+
}
|
|
16
|
+
[data-ui="tabs"] [data-part="panel"] { padding: var(--ui-tab-ppad, 1rem 0 0); }
|
|
17
|
+
@media (forced-colors: active) {
|
|
18
|
+
:root [data-ui="tabs"] [data-part="trigger"][data-state="active"] { text-decoration: underline 2px; text-underline-offset: 0.3em; }
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="toast"] {
|
|
3
|
+
--ui-tone: var(--ui-accent);
|
|
4
|
+
background: var(--ui-tst-bg, var(--ui-surface));
|
|
5
|
+
color: var(--ui-tst-fg, var(--ui-ink));
|
|
6
|
+
border-style: solid;
|
|
7
|
+
border-width: var(--ui-tst-bw, 1px);
|
|
8
|
+
border-color: var(--ui-tst-bc, var(--ui-line));
|
|
9
|
+
border-radius: var(--ui-radius);
|
|
10
|
+
box-shadow: var(--ui-tst-sh, none);
|
|
11
|
+
padding: var(--ui-tst-pad, 0.75rem 1rem);
|
|
12
|
+
|
|
13
|
+
&[data-tone="accent"] { --ui-tone: var(--ui-accent); }
|
|
14
|
+
&[data-tone="success"] { --ui-tone: var(--ui-success); }
|
|
15
|
+
&[data-tone="warning"] { --ui-tone: var(--ui-warning); }
|
|
16
|
+
&[data-tone="danger"] { --ui-tone: var(--ui-danger); }
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/table-state.ts
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
// Headless DataTable behavior (O(1) selection, clamped pagination, leak-free row signal eviction).
|
|
2
2
|
|
|
3
3
|
import type { ReadonlySignal, Signal } from 'prunelle'
|
|
4
|
-
import { computed, signal } from 'prunelle'
|
|
4
|
+
import { computed, effect, signal, untrack } from 'prunelle'
|
|
5
|
+
import { foldText } from './fold'
|
|
5
6
|
|
|
6
7
|
export interface DataTableOptions<T> {
|
|
7
8
|
rows: () => readonly T[]
|
|
8
9
|
getKey: (row: T) => string | number
|
|
9
10
|
searchText?: (row: T) => string
|
|
10
11
|
pageSize?: number | undefined
|
|
12
|
+
/** Language of the sort order; `<html lang>` by default. */
|
|
13
|
+
locale?: string | undefined
|
|
14
|
+
/** Called with the selected keys each time the selection changes. */
|
|
15
|
+
onSelect?: ((keys: Array<string | number>) => void) | undefined
|
|
11
16
|
}
|
|
12
17
|
|
|
13
18
|
export interface DataTableState<T> {
|
|
@@ -21,6 +26,8 @@ export interface DataTableState<T> {
|
|
|
21
26
|
readonly totalPages: ReadonlySignal<number>
|
|
22
27
|
toggleSort(col: keyof T): void
|
|
23
28
|
toggleSelect(key: string | number): void
|
|
29
|
+
/** Selects (`on`) or clears every key given, notifying only the rows that change. */
|
|
30
|
+
setSelected(keys: Iterable<string | number>, on: boolean): void
|
|
24
31
|
isSelected(key: string | number): boolean
|
|
25
32
|
clearSelection(): void
|
|
26
33
|
}
|
|
@@ -33,20 +40,28 @@ export function createDataTable<T>(options: DataTableOptions<T>): DataTableState
|
|
|
33
40
|
const selected = signal<Set<string | number>>(new Set())
|
|
34
41
|
const rowSelected = new Map<string | number, Signal<boolean>>()
|
|
35
42
|
const pageSize = options.pageSize ?? 25
|
|
43
|
+
let collator: Intl.Collator | undefined
|
|
44
|
+
|
|
45
|
+
const compare = (a: unknown, b: unknown): number => {
|
|
46
|
+
if (typeof a === 'number' && typeof b === 'number')
|
|
47
|
+
return a - b
|
|
48
|
+
collator ??= new Intl.Collator(options.locale ?? (typeof document === 'undefined' ? undefined : document.documentElement.lang || undefined), { numeric: true, sensitivity: 'base' })
|
|
49
|
+
return collator.compare(String(a), String(b))
|
|
50
|
+
}
|
|
36
51
|
|
|
37
52
|
const filtered = computed<T[]>(() => {
|
|
38
|
-
const q = query.value.trim()
|
|
53
|
+
const q = foldText(query.value.trim())
|
|
39
54
|
const base = options.rows()
|
|
40
55
|
if (rowSelected.size > 0) {
|
|
41
|
-
const
|
|
56
|
+
const alive = live.value
|
|
42
57
|
for (const k of rowSelected.keys()) {
|
|
43
|
-
if (!
|
|
58
|
+
if (!alive.has(k))
|
|
44
59
|
rowSelected.delete(k)
|
|
45
60
|
}
|
|
46
61
|
}
|
|
47
62
|
let list = q === ''
|
|
48
63
|
? [...base]
|
|
49
|
-
: base.filter(row => (options.searchText ? options.searchText(row) : JSON.stringify(row)).
|
|
64
|
+
: base.filter(row => foldText(options.searchText ? options.searchText(row) : JSON.stringify(row)).includes(q))
|
|
50
65
|
|
|
51
66
|
const col = sortBy.value
|
|
52
67
|
if (col !== undefined) {
|
|
@@ -54,7 +69,7 @@ export function createDataTable<T>(options: DataTableOptions<T>): DataTableState
|
|
|
54
69
|
list = list.sort((a, b) => {
|
|
55
70
|
const av = a[col]
|
|
56
71
|
const bv = b[col]
|
|
57
|
-
return av === bv ? 0 : av == null ? 1 : bv == null ? -1 : av
|
|
72
|
+
return av === bv ? 0 : av == null ? 1 : bv == null ? -1 : compare(av, bv) * mul
|
|
58
73
|
})
|
|
59
74
|
}
|
|
60
75
|
return list
|
|
@@ -73,6 +88,35 @@ export function createDataTable<T>(options: DataTableOptions<T>): DataTableState
|
|
|
73
88
|
return filtered.value.slice(start, start + pageSize)
|
|
74
89
|
})
|
|
75
90
|
|
|
91
|
+
const setSelected = (keys: Iterable<string | number>, on: boolean) => {
|
|
92
|
+
const next = new Set(selected.peek())
|
|
93
|
+
let changed = false
|
|
94
|
+
for (const key of keys) {
|
|
95
|
+
if (next.has(key) !== on)
|
|
96
|
+
changed = true
|
|
97
|
+
if (on)
|
|
98
|
+
next.add(key)
|
|
99
|
+
else
|
|
100
|
+
next.delete(key)
|
|
101
|
+
const perRow = rowSelected.get(key)
|
|
102
|
+
if (perRow !== undefined)
|
|
103
|
+
perRow.value = on
|
|
104
|
+
}
|
|
105
|
+
if (!changed)
|
|
106
|
+
return
|
|
107
|
+
selected.value = next
|
|
108
|
+
options.onSelect?.([...next])
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Rows that leave the data leave the selection.
|
|
112
|
+
const live = computed(() => new Set(options.rows().map(options.getKey)))
|
|
113
|
+
effect(() => {
|
|
114
|
+
const keys = live.value
|
|
115
|
+
const gone = [...selected.peek()].filter(k => !keys.has(k))
|
|
116
|
+
if (gone.length > 0)
|
|
117
|
+
untrack(() => setSelected(gone, false))
|
|
118
|
+
})
|
|
119
|
+
|
|
76
120
|
return {
|
|
77
121
|
query,
|
|
78
122
|
sortBy,
|
|
@@ -92,17 +136,9 @@ export function createDataTable<T>(options: DataTableOptions<T>): DataTableState
|
|
|
92
136
|
}
|
|
93
137
|
},
|
|
94
138
|
toggleSelect(key) {
|
|
95
|
-
|
|
96
|
-
const willSelect = !next.has(key)
|
|
97
|
-
if (willSelect)
|
|
98
|
-
next.add(key)
|
|
99
|
-
else
|
|
100
|
-
next.delete(key)
|
|
101
|
-
selected.value = next
|
|
102
|
-
const perRow = rowSelected.get(key)
|
|
103
|
-
if (perRow !== undefined)
|
|
104
|
-
perRow.value = willSelect
|
|
139
|
+
setSelected([key], !selected.peek().has(key))
|
|
105
140
|
},
|
|
141
|
+
setSelected,
|
|
106
142
|
isSelected(key) {
|
|
107
143
|
let perRow = rowSelected.get(key)
|
|
108
144
|
if (perRow === undefined) {
|
|
@@ -121,6 +157,7 @@ export function createDataTable<T>(options: DataTableOptions<T>): DataTableState
|
|
|
121
157
|
if (perRow !== undefined)
|
|
122
158
|
perRow.value = false
|
|
123
159
|
}
|
|
160
|
+
options.onSelect?.([])
|
|
124
161
|
},
|
|
125
162
|
}
|
|
126
163
|
}
|
package/src/toast.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Browser-scoped reactive toast store with automatic & cancellable dismiss timers.
|
|
1
|
+
// Browser-scoped reactive toast store with automatic, pausable & cancellable dismiss timers.
|
|
2
2
|
|
|
3
3
|
import type { Signal } from 'prunelle'
|
|
4
4
|
import type { Tone } from './static'
|
|
@@ -11,9 +11,15 @@ export interface ToastItem {
|
|
|
11
11
|
tone: Tone
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
interface ToastTimer {
|
|
15
|
+
h: ReturnType<typeof setTimeout> | undefined
|
|
16
|
+
left: number
|
|
17
|
+
at: number
|
|
18
|
+
}
|
|
19
|
+
|
|
14
20
|
interface ToastStore {
|
|
15
21
|
i: Signal<ToastItem[]>
|
|
16
|
-
t: Map<string,
|
|
22
|
+
t: Map<string, ToastTimer>
|
|
17
23
|
s: number
|
|
18
24
|
}
|
|
19
25
|
|
|
@@ -27,17 +33,53 @@ function getToastStore(): ToastStore | undefined {
|
|
|
27
33
|
return (host[TOAST_KEY] ??= { i: signal<ToastItem[]>([]), t: new Map(), s: 0 })
|
|
28
34
|
}
|
|
29
35
|
|
|
30
|
-
|
|
36
|
+
// An error stays until dismissed; the others stay long enough to read, about 60 ms per character.
|
|
37
|
+
function readingTime(tone: Tone, title: string, description = ''): number {
|
|
38
|
+
if (tone === 'danger')
|
|
39
|
+
return 0
|
|
40
|
+
return Math.min(15_000, Math.max(4500, 1000 + (title.length + description.length) * 60))
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function startTimer(timer: ToastTimer, id: string) {
|
|
44
|
+
timer.at = Date.now()
|
|
45
|
+
timer.h = setTimeout(() => toast.dismiss(id), timer.left)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function pushToast(tone: Tone, title: string, description?: string, durationMs = readingTime(tone, title, description)): string {
|
|
31
49
|
const s = getToastStore()
|
|
32
50
|
if (!s)
|
|
33
51
|
return ''
|
|
34
52
|
const id = `t-${++s.s}`
|
|
53
|
+
// The timer exists before the item shows, so a Toaster that is hovered or focused pauses it at once.
|
|
54
|
+
if (durationMs > 0) {
|
|
55
|
+
const timer: ToastTimer = { h: undefined, left: durationMs, at: 0 }
|
|
56
|
+
s.t.set(id, timer)
|
|
57
|
+
startTimer(timer, id)
|
|
58
|
+
}
|
|
35
59
|
s.i.value = [...s.i.value, { id, title, description, tone }]
|
|
36
|
-
if (durationMs > 0)
|
|
37
|
-
s.t.set(id, setTimeout(() => toast.dismiss(id), durationMs))
|
|
38
60
|
return id
|
|
39
61
|
}
|
|
40
62
|
|
|
63
|
+
/** Stops every running dismiss timer, keeping the time each toast had left (the Toaster calls it on hover and focus). */
|
|
64
|
+
export function pauseToasts() {
|
|
65
|
+
const now = Date.now()
|
|
66
|
+
for (const timer of getToastStore()?.t.values() ?? []) {
|
|
67
|
+
if (timer.h === undefined)
|
|
68
|
+
continue
|
|
69
|
+
clearTimeout(timer.h)
|
|
70
|
+
timer.h = undefined
|
|
71
|
+
timer.left = Math.max(0, timer.left - (now - timer.at))
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Restarts each paused dismiss timer with the time it had left. */
|
|
76
|
+
export function resumeToasts() {
|
|
77
|
+
for (const [id, timer] of getToastStore()?.t ?? []) {
|
|
78
|
+
if (timer.h === undefined)
|
|
79
|
+
startTimer(timer, id)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
41
83
|
export const toast = {
|
|
42
84
|
get items(): Signal<ToastItem[]> {
|
|
43
85
|
return getToastStore()?.i ?? (emptyToasts ??= signal<ToastItem[]>([]))
|
|
@@ -52,7 +94,7 @@ export const toast = {
|
|
|
52
94
|
const s = getToastStore()
|
|
53
95
|
if (!s)
|
|
54
96
|
return
|
|
55
|
-
clearTimeout(s.t.get(id))
|
|
97
|
+
clearTimeout(s.t.get(id)?.h)
|
|
56
98
|
s.t.delete(id)
|
|
57
99
|
s.i.value = s.i.value.filter(x => x.id !== id)
|
|
58
100
|
},
|
|
@@ -60,8 +102,8 @@ export const toast = {
|
|
|
60
102
|
const s = getToastStore()
|
|
61
103
|
if (!s)
|
|
62
104
|
return
|
|
63
|
-
for (const
|
|
64
|
-
clearTimeout(h)
|
|
105
|
+
for (const timer of s.t.values())
|
|
106
|
+
clearTimeout(timer.h)
|
|
65
107
|
s.t.clear()
|
|
66
108
|
s.i.value = []
|
|
67
109
|
},
|