@prunellejs/ui 0.0.0-stage → 0.1.1

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.
@@ -0,0 +1,126 @@
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, signal } from 'prunelle'
5
+
6
+ export interface DataTableOptions<T> {
7
+ rows: () => readonly T[]
8
+ getKey: (row: T) => string | number
9
+ searchText?: (row: T) => string
10
+ pageSize?: number | undefined
11
+ }
12
+
13
+ export interface DataTableState<T> {
14
+ readonly query: Signal<string>
15
+ readonly sortBy: Signal<keyof T | undefined>
16
+ readonly sortDir: Signal<'asc' | 'desc'>
17
+ readonly page: Signal<number>
18
+ readonly selected: Signal<Set<string | number>>
19
+ readonly filtered: ReadonlySignal<T[]>
20
+ readonly visible: ReadonlySignal<T[]>
21
+ readonly totalPages: ReadonlySignal<number>
22
+ toggleSort(col: keyof T): void
23
+ toggleSelect(key: string | number): void
24
+ isSelected(key: string | number): boolean
25
+ clearSelection(): void
26
+ }
27
+
28
+ export function createDataTable<T>(options: DataTableOptions<T>): DataTableState<T> {
29
+ const query = signal('')
30
+ const sortBy = signal<keyof T | undefined>(undefined)
31
+ const sortDir = signal<'asc' | 'desc'>('asc')
32
+ const rawPage = signal(1)
33
+ const selected = signal<Set<string | number>>(new Set())
34
+ const rowSelected = new Map<string | number, Signal<boolean>>()
35
+ const pageSize = options.pageSize ?? 25
36
+
37
+ const filtered = computed<T[]>(() => {
38
+ const q = query.value.trim().toLowerCase()
39
+ const base = options.rows()
40
+ if (rowSelected.size > 0) {
41
+ const live = new Set(base.map(options.getKey))
42
+ for (const k of rowSelected.keys()) {
43
+ if (!live.has(k))
44
+ rowSelected.delete(k)
45
+ }
46
+ }
47
+ let list = q === ''
48
+ ? [...base]
49
+ : base.filter(row => (options.searchText ? options.searchText(row) : JSON.stringify(row)).toLowerCase().includes(q))
50
+
51
+ const col = sortBy.value
52
+ if (col !== undefined) {
53
+ const mul = sortDir.value === 'asc' ? 1 : -1
54
+ list = list.sort((a, b) => {
55
+ const av = a[col]
56
+ const bv = b[col]
57
+ return av === bv ? 0 : av == null ? 1 : bv == null ? -1 : av < bv ? -mul : mul
58
+ })
59
+ }
60
+ return list
61
+ })
62
+
63
+ const totalPages = computed(() => Math.max(1, Math.ceil(filtered.value.length / pageSize)))
64
+ const clampedPage = computed(() => Math.min(Math.max(1, rawPage.value), totalPages.value))
65
+ const page: Signal<number> = {
66
+ get value() { return clampedPage.value },
67
+ set value(v: number) { rawPage.value = Math.min(Math.max(1, Math.trunc(v)), totalPages.peek()) },
68
+ peek: () => clampedPage.peek(),
69
+ }
70
+
71
+ const visible = computed<T[]>(() => {
72
+ const start = (clampedPage.value - 1) * pageSize
73
+ return filtered.value.slice(start, start + pageSize)
74
+ })
75
+
76
+ return {
77
+ query,
78
+ sortBy,
79
+ sortDir,
80
+ page,
81
+ selected,
82
+ filtered,
83
+ visible,
84
+ totalPages,
85
+ toggleSort(col) {
86
+ if (sortBy.value === col) {
87
+ sortDir.value = sortDir.value === 'asc' ? 'desc' : 'asc'
88
+ }
89
+ else {
90
+ sortBy.value = col
91
+ sortDir.value = 'asc'
92
+ }
93
+ },
94
+ toggleSelect(key) {
95
+ const next = new Set(selected.peek())
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
105
+ },
106
+ isSelected(key) {
107
+ let perRow = rowSelected.get(key)
108
+ if (perRow === undefined) {
109
+ perRow = signal(selected.peek().has(key))
110
+ rowSelected.set(key, perRow)
111
+ }
112
+ return perRow.value
113
+ },
114
+ clearSelection() {
115
+ const prev = selected.peek()
116
+ if (prev.size === 0)
117
+ return
118
+ selected.value = new Set()
119
+ for (const k of prev) {
120
+ const perRow = rowSelected.get(k)
121
+ if (perRow !== undefined)
122
+ perRow.value = false
123
+ }
124
+ },
125
+ }
126
+ }
@@ -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,68 @@
1
+ // Browser-scoped reactive toast store with automatic & 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 ToastStore {
15
+ i: Signal<ToastItem[]>
16
+ t: Map<string, ReturnType<typeof setTimeout>>
17
+ s: number
18
+ }
19
+
20
+ const TOAST_KEY = Symbol.for('pru.ui.toasts')
21
+ let emptyToasts: Signal<ToastItem[]> | undefined
22
+
23
+ function getToastStore(): ToastStore | undefined {
24
+ if (typeof document === 'undefined')
25
+ return undefined
26
+ const host = document as unknown as Record<symbol, ToastStore | undefined>
27
+ return (host[TOAST_KEY] ??= { i: signal<ToastItem[]>([]), t: new Map(), s: 0 })
28
+ }
29
+
30
+ function pushToast(tone: Tone, title: string, description?: string, durationMs = 4500): string {
31
+ const s = getToastStore()
32
+ if (!s)
33
+ return ''
34
+ const id = `t-${++s.s}`
35
+ 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
+ return id
39
+ }
40
+
41
+ export const toast = {
42
+ get items(): Signal<ToastItem[]> {
43
+ return getToastStore()?.i ?? (emptyToasts ??= signal<ToastItem[]>([]))
44
+ },
45
+ show: (title: string, description?: string, durationMs?: number) => pushToast('neutral', title, description, durationMs),
46
+ info: (title: string, description?: string, durationMs?: number) => pushToast('accent', title, description, durationMs),
47
+ success: (title: string, description?: string, durationMs?: number) => pushToast('success', title, description, durationMs),
48
+ warning: (title: string, description?: string, durationMs?: number) => pushToast('warning', title, description, durationMs),
49
+ danger: (title: string, description?: string, durationMs?: number) => pushToast('danger', title, description, durationMs),
50
+ error: (title: string, description?: string, durationMs?: number) => pushToast('danger', title, description, durationMs),
51
+ dismiss(id: string) {
52
+ const s = getToastStore()
53
+ if (!s)
54
+ return
55
+ clearTimeout(s.t.get(id))
56
+ s.t.delete(id)
57
+ s.i.value = s.i.value.filter(x => x.id !== id)
58
+ },
59
+ clear() {
60
+ const s = getToastStore()
61
+ if (!s)
62
+ return
63
+ for (const h of s.t.values())
64
+ clearTimeout(h)
65
+ s.t.clear()
66
+ s.i.value = []
67
+ },
68
+ }
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.