@prunellejs/ui 0.0.0-stage → 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.
Files changed (84) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +78 -0
  3. package/package.json +32 -4
  4. package/src/CommandPalette.tsx +262 -0
  5. package/src/DataTable.tsx +244 -0
  6. package/src/Dialog.tsx +181 -0
  7. package/src/Drawer.tsx +4 -0
  8. package/src/Menu.tsx +117 -0
  9. package/src/Select.tsx +125 -0
  10. package/src/Tabs.tsx +64 -0
  11. package/src/Toaster.tsx +105 -0
  12. package/src/behaviors.ts +385 -0
  13. package/src/field-context.ts +21 -0
  14. package/src/fold.ts +7 -0
  15. package/src/index.ts +72 -0
  16. package/src/labels/dialog.ts +3 -0
  17. package/src/labels/fr/dialog.ts +3 -0
  18. package/src/labels/fr/palette.ts +8 -0
  19. package/src/labels/fr/select.ts +1 -0
  20. package/src/labels/fr/table.ts +13 -0
  21. package/src/labels/fr/toaster.ts +1 -0
  22. package/src/labels/palette.ts +8 -0
  23. package/src/labels/select.ts +1 -0
  24. package/src/labels/table.ts +13 -0
  25. package/src/labels/toaster.ts +1 -0
  26. package/src/platform-fallback.ts +36 -0
  27. package/src/plugin.ts +188 -0
  28. package/src/primitives.tsx +238 -0
  29. package/src/static.tsx +324 -0
  30. package/src/styles/bare.css +25 -0
  31. package/src/styles/base.css +61 -0
  32. package/src/styles/components/accordion.css +10 -0
  33. package/src/styles/components/badge.css +6 -0
  34. package/src/styles/components/button.css +11 -0
  35. package/src/styles/components/card.css +5 -0
  36. package/src/styles/components/checkbox.css +11 -0
  37. package/src/styles/components/command.css +25 -0
  38. package/src/styles/components/consent.css +38 -0
  39. package/src/styles/components/datatable.css +9 -0
  40. package/src/styles/components/dialog.css +16 -0
  41. package/src/styles/components/drawer.css +29 -0
  42. package/src/styles/components/field.css +8 -0
  43. package/src/styles/components/layout.css +69 -0
  44. package/src/styles/components/menu.css +21 -0
  45. package/src/styles/components/notice.css +5 -0
  46. package/src/styles/components/popover.css +8 -0
  47. package/src/styles/components/progress.css +14 -0
  48. package/src/styles/components/radio-group.css +11 -0
  49. package/src/styles/components/select.css +25 -0
  50. package/src/styles/components/switch.css +42 -0
  51. package/src/styles/components/tabs.css +4 -0
  52. package/src/styles/components/toast.css +28 -0
  53. package/src/styles/grammars/console.css +179 -0
  54. package/src/styles/grammars/editorial.css +164 -0
  55. package/src/styles/grammars/orbit.css +269 -0
  56. package/src/styles/grammars/prunelle.css +504 -0
  57. package/src/styles/grammars/soft.css +160 -0
  58. package/src/styles/grammars/workshop.css +172 -0
  59. package/src/styles/scan.ts +144 -0
  60. package/src/styles/split.ts +429 -0
  61. package/src/styles/theme.css +22 -0
  62. package/src/styles/themed/accordion.css +20 -0
  63. package/src/styles/themed/badge.css +20 -0
  64. package/src/styles/themed/button.css +24 -0
  65. package/src/styles/themed/card.css +23 -0
  66. package/src/styles/themed/checkbox.css +4 -0
  67. package/src/styles/themed/command.css +33 -0
  68. package/src/styles/themed/consent.css +33 -0
  69. package/src/styles/themed/datatable.css +28 -0
  70. package/src/styles/themed/dialog.css +13 -0
  71. package/src/styles/themed/drawer.css +13 -0
  72. package/src/styles/themed/field.css +22 -0
  73. package/src/styles/themed/menu.css +21 -0
  74. package/src/styles/themed/notice.css +17 -0
  75. package/src/styles/themed/popover.css +11 -0
  76. package/src/styles/themed/radio-group.css +12 -0
  77. package/src/styles/themed/select.css +32 -0
  78. package/src/styles/themed/switch.css +23 -0
  79. package/src/styles/themed/tabs.css +20 -0
  80. package/src/styles/themed/toast.css +18 -0
  81. package/src/table-state.ts +163 -0
  82. package/src/tabs-state.ts +39 -0
  83. package/src/toast.ts +110 -0
  84. package/README.md +0 -3
@@ -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
+ }
@@ -0,0 +1,163 @@
1
+ // Headless DataTable behavior (O(1) selection, clamped pagination, leak-free row signal eviction).
2
+
3
+ import type { ReadonlySignal, Signal } from 'prunelle'
4
+ import { computed, effect, signal, untrack } from 'prunelle'
5
+ import { foldText } from './fold'
6
+
7
+ export interface DataTableOptions<T> {
8
+ rows: () => readonly T[]
9
+ getKey: (row: T) => string | number
10
+ searchText?: (row: T) => string
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
16
+ }
17
+
18
+ export interface DataTableState<T> {
19
+ readonly query: Signal<string>
20
+ readonly sortBy: Signal<keyof T | undefined>
21
+ readonly sortDir: Signal<'asc' | 'desc'>
22
+ readonly page: Signal<number>
23
+ readonly selected: Signal<Set<string | number>>
24
+ readonly filtered: ReadonlySignal<T[]>
25
+ readonly visible: ReadonlySignal<T[]>
26
+ readonly totalPages: ReadonlySignal<number>
27
+ toggleSort(col: keyof T): void
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
31
+ isSelected(key: string | number): boolean
32
+ clearSelection(): void
33
+ }
34
+
35
+ export function createDataTable<T>(options: DataTableOptions<T>): DataTableState<T> {
36
+ const query = signal('')
37
+ const sortBy = signal<keyof T | undefined>(undefined)
38
+ const sortDir = signal<'asc' | 'desc'>('asc')
39
+ const rawPage = signal(1)
40
+ const selected = signal<Set<string | number>>(new Set())
41
+ const rowSelected = new Map<string | number, Signal<boolean>>()
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
+ }
51
+
52
+ const filtered = computed<T[]>(() => {
53
+ const q = foldText(query.value.trim())
54
+ const base = options.rows()
55
+ if (rowSelected.size > 0) {
56
+ const alive = live.value
57
+ for (const k of rowSelected.keys()) {
58
+ if (!alive.has(k))
59
+ rowSelected.delete(k)
60
+ }
61
+ }
62
+ let list = q === ''
63
+ ? [...base]
64
+ : base.filter(row => foldText(options.searchText ? options.searchText(row) : JSON.stringify(row)).includes(q))
65
+
66
+ const col = sortBy.value
67
+ if (col !== undefined) {
68
+ const mul = sortDir.value === 'asc' ? 1 : -1
69
+ list = list.sort((a, b) => {
70
+ const av = a[col]
71
+ const bv = b[col]
72
+ return av === bv ? 0 : av == null ? 1 : bv == null ? -1 : compare(av, bv) * mul
73
+ })
74
+ }
75
+ return list
76
+ })
77
+
78
+ const totalPages = computed(() => Math.max(1, Math.ceil(filtered.value.length / pageSize)))
79
+ const clampedPage = computed(() => Math.min(Math.max(1, rawPage.value), totalPages.value))
80
+ const page: Signal<number> = {
81
+ get value() { return clampedPage.value },
82
+ set value(v: number) { rawPage.value = Math.min(Math.max(1, Math.trunc(v)), totalPages.peek()) },
83
+ peek: () => clampedPage.peek(),
84
+ }
85
+
86
+ const visible = computed<T[]>(() => {
87
+ const start = (clampedPage.value - 1) * pageSize
88
+ return filtered.value.slice(start, start + pageSize)
89
+ })
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
+
120
+ return {
121
+ query,
122
+ sortBy,
123
+ sortDir,
124
+ page,
125
+ selected,
126
+ filtered,
127
+ visible,
128
+ totalPages,
129
+ toggleSort(col) {
130
+ if (sortBy.value === col) {
131
+ sortDir.value = sortDir.value === 'asc' ? 'desc' : 'asc'
132
+ }
133
+ else {
134
+ sortBy.value = col
135
+ sortDir.value = 'asc'
136
+ }
137
+ },
138
+ toggleSelect(key) {
139
+ setSelected([key], !selected.peek().has(key))
140
+ },
141
+ setSelected,
142
+ isSelected(key) {
143
+ let perRow = rowSelected.get(key)
144
+ if (perRow === undefined) {
145
+ perRow = signal(selected.peek().has(key))
146
+ rowSelected.set(key, perRow)
147
+ }
148
+ return perRow.value
149
+ },
150
+ clearSelection() {
151
+ const prev = selected.peek()
152
+ if (prev.size === 0)
153
+ return
154
+ selected.value = new Set()
155
+ for (const k of prev) {
156
+ const perRow = rowSelected.get(k)
157
+ if (perRow !== undefined)
158
+ perRow.value = false
159
+ }
160
+ options.onSelect?.([])
161
+ },
162
+ }
163
+ }
@@ -0,0 +1,39 @@
1
+ // Tabs behavior (roving arrow-key + Home/End navigation).
2
+
3
+ import type { Signal } from 'prunelle'
4
+ import { selector, signal } from 'prunelle'
5
+
6
+ export interface TabsBehavior {
7
+ readonly active: Signal<string>
8
+ select(id: string): void
9
+ isActive(id: string): boolean
10
+ onKeyDown(e: KeyboardEvent, ids: readonly string[]): void
11
+ }
12
+
13
+ export function createTabs(initial: string): TabsBehavior {
14
+ const active = signal(initial)
15
+ const sel = selector(() => active.value)
16
+
17
+ return {
18
+ active,
19
+ select(id: string) { active.value = id },
20
+ isActive: (id: string) => sel.is(id),
21
+ onKeyDown(e: KeyboardEvent, ids: readonly string[]) {
22
+ const idx = ids.indexOf(active.value)
23
+ if (idx === -1 || ids.length === 0)
24
+ return
25
+ let next = -1
26
+ if (e.key === 'ArrowRight') next = (idx + 1) % ids.length
27
+ else if (e.key === 'ArrowLeft') next = (idx - 1 + ids.length) % ids.length
28
+ else if (e.key === 'Home') next = 0
29
+ else if (e.key === 'End') next = ids.length - 1
30
+ if (next !== -1) {
31
+ e.preventDefault()
32
+ active.value = ids[next]!
33
+ const tablist = (e.currentTarget as HTMLElement | null)?.closest('[role="tablist"]')
34
+ const buttons = tablist?.querySelectorAll<HTMLElement>('[role="tab"]')
35
+ buttons?.[next]?.focus()
36
+ }
37
+ },
38
+ }
39
+ }
package/src/toast.ts ADDED
@@ -0,0 +1,110 @@
1
+ // Browser-scoped reactive toast store with automatic, pausable & cancellable dismiss timers.
2
+
3
+ import type { Signal } from 'prunelle'
4
+ import type { Tone } from './static'
5
+ import { signal } from 'prunelle'
6
+
7
+ export interface ToastItem {
8
+ id: string
9
+ title: string
10
+ description?: string | undefined
11
+ tone: Tone
12
+ }
13
+
14
+ interface ToastTimer {
15
+ h: ReturnType<typeof setTimeout> | undefined
16
+ left: number
17
+ at: number
18
+ }
19
+
20
+ interface ToastStore {
21
+ i: Signal<ToastItem[]>
22
+ t: Map<string, ToastTimer>
23
+ s: number
24
+ }
25
+
26
+ const TOAST_KEY = Symbol.for('pru.ui.toasts')
27
+ let emptyToasts: Signal<ToastItem[]> | undefined
28
+
29
+ function getToastStore(): ToastStore | undefined {
30
+ if (typeof document === 'undefined')
31
+ return undefined
32
+ const host = document as unknown as Record<symbol, ToastStore | undefined>
33
+ return (host[TOAST_KEY] ??= { i: signal<ToastItem[]>([]), t: new Map(), s: 0 })
34
+ }
35
+
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 {
49
+ const s = getToastStore()
50
+ if (!s)
51
+ return ''
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
+ }
59
+ s.i.value = [...s.i.value, { id, title, description, tone }]
60
+ return id
61
+ }
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
+
83
+ export const toast = {
84
+ get items(): Signal<ToastItem[]> {
85
+ return getToastStore()?.i ?? (emptyToasts ??= signal<ToastItem[]>([]))
86
+ },
87
+ show: (title: string, description?: string, durationMs?: number) => pushToast('neutral', title, description, durationMs),
88
+ info: (title: string, description?: string, durationMs?: number) => pushToast('accent', title, description, durationMs),
89
+ success: (title: string, description?: string, durationMs?: number) => pushToast('success', title, description, durationMs),
90
+ warning: (title: string, description?: string, durationMs?: number) => pushToast('warning', title, description, durationMs),
91
+ danger: (title: string, description?: string, durationMs?: number) => pushToast('danger', title, description, durationMs),
92
+ error: (title: string, description?: string, durationMs?: number) => pushToast('danger', title, description, durationMs),
93
+ dismiss(id: string) {
94
+ const s = getToastStore()
95
+ if (!s)
96
+ return
97
+ clearTimeout(s.t.get(id)?.h)
98
+ s.t.delete(id)
99
+ s.i.value = s.i.value.filter(x => x.id !== id)
100
+ },
101
+ clear() {
102
+ const s = getToastStore()
103
+ if (!s)
104
+ return
105
+ for (const timer of s.t.values())
106
+ clearTimeout(timer.h)
107
+ s.t.clear()
108
+ s.i.value = []
109
+ },
110
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.