@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.
- package/LICENSE +21 -0
- package/llms.txt +78 -0
- package/package.json +32 -4
- package/src/CommandPalette.tsx +262 -0
- package/src/DataTable.tsx +244 -0
- package/src/Dialog.tsx +181 -0
- package/src/Drawer.tsx +4 -0
- package/src/Menu.tsx +117 -0
- package/src/Select.tsx +125 -0
- package/src/Tabs.tsx +64 -0
- package/src/Toaster.tsx +105 -0
- package/src/behaviors.ts +385 -0
- package/src/field-context.ts +21 -0
- package/src/fold.ts +7 -0
- package/src/index.ts +72 -0
- 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/platform-fallback.ts +36 -0
- package/src/plugin.ts +188 -0
- package/src/primitives.tsx +238 -0
- package/src/static.tsx +324 -0
- package/src/styles/bare.css +25 -0
- 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 +179 -0
- package/src/styles/grammars/editorial.css +164 -0
- package/src/styles/grammars/orbit.css +269 -0
- package/src/styles/grammars/prunelle.css +504 -0
- package/src/styles/grammars/soft.css +160 -0
- package/src/styles/grammars/workshop.css +172 -0
- 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 +163 -0
- package/src/tabs-state.ts +39 -0
- package/src/toast.ts +110 -0
- package/README.md +0 -3
package/src/behaviors.ts
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
// Headless signal behavior hooks (zero CSS, zero DOM lock-in — standard TSX `{...spread}`).
|
|
2
|
+
// DataTable and Tabs state live in ./table-state.ts and ./tabs-state.ts so lightweight pages don't download them.
|
|
3
|
+
|
|
4
|
+
import type { Signal } from 'prunelle'
|
|
5
|
+
import { effect, onCleanup, signal, useId } from 'prunelle'
|
|
6
|
+
import { useFieldContext } from './field-context'
|
|
7
|
+
|
|
8
|
+
export { createDataTable } from './table-state'
|
|
9
|
+
export type { DataTableOptions, DataTableState } from './table-state'
|
|
10
|
+
export { createTabs } from './tabs-state'
|
|
11
|
+
export type { TabsBehavior } from './tabs-state'
|
|
12
|
+
export { toast } from './toast'
|
|
13
|
+
export type { ToastItem } from './toast'
|
|
14
|
+
export { useFieldContext }
|
|
15
|
+
|
|
16
|
+
// ---- Safe href validation & SPA-friendly navigation ----
|
|
17
|
+
|
|
18
|
+
const UNSAFE_HREF_RE = /^(?:javascript|vbscript|data):/i
|
|
19
|
+
|
|
20
|
+
/** Returns a safe navigable URL (rejecting `javascript:`, `vbscript:`, and `data:` schemes even with control-char obfuscation), or `undefined`. */
|
|
21
|
+
export function safeHref(href: string | undefined): string | undefined {
|
|
22
|
+
if (href === undefined)
|
|
23
|
+
return undefined
|
|
24
|
+
const trimmed = href.trim()
|
|
25
|
+
if (trimmed.length === 0)
|
|
26
|
+
return undefined
|
|
27
|
+
let cleaned = ''
|
|
28
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
29
|
+
const code = trimmed.charCodeAt(i)
|
|
30
|
+
if (code > 0x20 && code !== 0x7F)
|
|
31
|
+
cleaned += trimmed[i]
|
|
32
|
+
}
|
|
33
|
+
return UNSAFE_HREF_RE.test(cleaned) ? undefined : trimmed
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Navigates to a safe href with one synthetic anchor click, which `@prunellejs/spa` can intercept and the browser follows otherwise; `location.assign` only when there is no body to hold the anchor. */
|
|
37
|
+
export function navigateSafeHref(href: string | undefined): boolean {
|
|
38
|
+
const safe = safeHref(href)
|
|
39
|
+
if (safe === undefined)
|
|
40
|
+
return false
|
|
41
|
+
if (typeof document !== 'undefined' && document.body !== null) {
|
|
42
|
+
const a = document.createElement('a')
|
|
43
|
+
a.href = safe
|
|
44
|
+
a.hidden = true
|
|
45
|
+
document.body.appendChild(a)
|
|
46
|
+
try {
|
|
47
|
+
a.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, button: 0 }))
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
a.remove()
|
|
51
|
+
}
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
54
|
+
if (typeof location !== 'undefined')
|
|
55
|
+
location.assign(safe)
|
|
56
|
+
return true
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ---- Stable hydration-safe unique IDs for ARIA wiring ----
|
|
60
|
+
|
|
61
|
+
export function nextUiId(prefix: string): string {
|
|
62
|
+
return useId(prefix)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ---- Shared listbox / menu keyboard navigation ----
|
|
66
|
+
|
|
67
|
+
export function findFirstEnabledIndex<T>(items: readonly T[], isDisabled?: (item: T) => boolean): number {
|
|
68
|
+
return findNextEnabledIndex(items, -1, 1, isDisabled)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function findLastEnabledIndex<T>(items: readonly T[], isDisabled?: (item: T) => boolean): number {
|
|
72
|
+
return findNextEnabledIndex(items, items.length, -1, isDisabled)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function findNextEnabledIndex<T>(
|
|
76
|
+
items: readonly T[],
|
|
77
|
+
current: number,
|
|
78
|
+
delta: 1 | -1,
|
|
79
|
+
isDisabled?: (item: T) => boolean,
|
|
80
|
+
): number {
|
|
81
|
+
const len = items.length
|
|
82
|
+
if (len === 0)
|
|
83
|
+
return -1
|
|
84
|
+
let idx = current < 0 || current >= len ? (delta === 1 ? -1 : len) : current
|
|
85
|
+
for (let step = 0; step < len; step++) {
|
|
86
|
+
idx = (idx + delta + len) % len
|
|
87
|
+
if (!isDisabled?.(items[idx]!))
|
|
88
|
+
return idx
|
|
89
|
+
}
|
|
90
|
+
return -1
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface ListNavigationOptions<T> {
|
|
94
|
+
items: () => readonly T[]
|
|
95
|
+
activeIndex: Signal<number>
|
|
96
|
+
isDisabled?: ((item: T) => boolean) | undefined
|
|
97
|
+
onSelect: (item: T, index: number) => void
|
|
98
|
+
onClose: (returnFocus?: boolean) => void
|
|
99
|
+
selectOnSpace?: boolean | undefined
|
|
100
|
+
homeEnd?: boolean | undefined
|
|
101
|
+
closeOnTab?: boolean | undefined
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Shared keyboard handler for ARIA menu / listbox / combobox popups (ArrowUp/Down, Home/End, Enter/Space, Escape, Tab). */
|
|
105
|
+
export function handleListKeyDown<T>(e: KeyboardEvent, opts: ListNavigationOptions<T>): boolean {
|
|
106
|
+
const list = opts.items()
|
|
107
|
+
if (e.key === 'Escape') {
|
|
108
|
+
opts.onClose(true)
|
|
109
|
+
return true
|
|
110
|
+
}
|
|
111
|
+
if (e.key === 'Tab') {
|
|
112
|
+
if (opts.closeOnTab === false)
|
|
113
|
+
return false
|
|
114
|
+
opts.onClose(false)
|
|
115
|
+
return true
|
|
116
|
+
}
|
|
117
|
+
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
118
|
+
e.preventDefault()
|
|
119
|
+
opts.activeIndex.value = findNextEnabledIndex(list, opts.activeIndex.value, e.key === 'ArrowDown' ? 1 : -1, opts.isDisabled)
|
|
120
|
+
return true
|
|
121
|
+
}
|
|
122
|
+
if (opts.homeEnd !== false && (e.key === 'Home' || e.key === 'End')) {
|
|
123
|
+
e.preventDefault()
|
|
124
|
+
opts.activeIndex.value = (e.key === 'Home' ? findFirstEnabledIndex : findLastEnabledIndex)(list, opts.isDisabled)
|
|
125
|
+
return true
|
|
126
|
+
}
|
|
127
|
+
if (e.key === 'Enter' || (opts.selectOnSpace !== false && e.key === ' ')) {
|
|
128
|
+
const idx = opts.activeIndex.value
|
|
129
|
+
const active = idx >= 0 && idx < list.length ? list[idx] : undefined
|
|
130
|
+
if (active !== undefined && !opts.isDisabled?.(active)) {
|
|
131
|
+
e.preventDefault()
|
|
132
|
+
opts.onSelect(active, idx)
|
|
133
|
+
return true
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface ListPopupOptions<T> {
|
|
140
|
+
items: () => readonly T[]
|
|
141
|
+
itemId: (idx: number) => string
|
|
142
|
+
isDisabled: (item: T) => boolean
|
|
143
|
+
onSelect: (item: T, idx: number) => void
|
|
144
|
+
initialIndex?: () => number
|
|
145
|
+
focusActive?: boolean | undefined
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ListPopupState {
|
|
149
|
+
readonly open: Signal<boolean>
|
|
150
|
+
readonly highlighted: Signal<number>
|
|
151
|
+
setRoot(el: HTMLElement | undefined): void
|
|
152
|
+
close(returnFocus?: boolean): void
|
|
153
|
+
toggle(e: MouseEvent): void
|
|
154
|
+
onTriggerKeyDown(e: KeyboardEvent): void
|
|
155
|
+
focusActive(): void
|
|
156
|
+
activeDescendantId(): string | undefined
|
|
157
|
+
/** The popup's `ref`: shows it in the top layer, anchored to the trigger. */
|
|
158
|
+
mountPopup(el: HTMLElement | undefined): void
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// In the top layer no overflow ancestor clips the popup, and the trigger as its `source` is the anchor CSS places it against and a click the browser does not count as outside.
|
|
162
|
+
function showAnchored(el: HTMLElement, trigger: HTMLElement): void {
|
|
163
|
+
if (el.isConnected && !el.matches(':popover-open')) {
|
|
164
|
+
try {
|
|
165
|
+
el.showPopover({ source: trigger })
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
el.showPopover()
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Shared popup open/close and keyboard list navigation for Menu and Select; the browser dismisses the popup on an outside click. */
|
|
174
|
+
export function createListPopup<T>(opts: ListPopupOptions<T>): ListPopupState {
|
|
175
|
+
const open = signal(false)
|
|
176
|
+
const highlighted = signal(findFirstEnabledIndex(opts.items(), opts.isDisabled))
|
|
177
|
+
let rootEl: HTMLElement | undefined
|
|
178
|
+
let lastOpenEvent: Event | undefined
|
|
179
|
+
// On browsers without `showPopover({ source })`, clicking the trigger light-dismisses the popover before `click` fires; suppress that click so it stays closed.
|
|
180
|
+
let pointerDownOnTrigger = false
|
|
181
|
+
let pointerDown = false
|
|
182
|
+
let suppressNextClick = false
|
|
183
|
+
let clearSuppressTimer: ReturnType<typeof setTimeout> | undefined
|
|
184
|
+
|
|
185
|
+
const scheduleSuppressReset = () => {
|
|
186
|
+
clearTimeout(clearSuppressTimer)
|
|
187
|
+
clearSuppressTimer = setTimeout(() => {
|
|
188
|
+
clearSuppressTimer = undefined
|
|
189
|
+
pointerDownOnTrigger = false
|
|
190
|
+
suppressNextClick = false
|
|
191
|
+
}, 50)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const activeDescendantId = (): string | undefined => {
|
|
195
|
+
const idx = highlighted.value
|
|
196
|
+
const list = opts.items()
|
|
197
|
+
return !open.value || idx < 0 || idx >= list.length || opts.isDisabled(list[idx]!) ? undefined : opts.itemId(idx)
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const focusActive = () => {
|
|
201
|
+
const id = opts.focusActive ? activeDescendantId() : undefined
|
|
202
|
+
if (!id)
|
|
203
|
+
return
|
|
204
|
+
const el = document.getElementById(id)
|
|
205
|
+
if (el)
|
|
206
|
+
el.focus()
|
|
207
|
+
else
|
|
208
|
+
queueMicrotask(() => document.getElementById(id)?.focus())
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const openWith = (last: boolean) => {
|
|
212
|
+
open.value = true
|
|
213
|
+
const init = opts.initialIndex?.()
|
|
214
|
+
highlighted.value = init !== undefined && init >= 0
|
|
215
|
+
? init
|
|
216
|
+
: (last ? findLastEnabledIndex : findFirstEnabledIndex)(opts.items(), opts.isDisabled)
|
|
217
|
+
focusActive()
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const close = (returnFocus = false) => {
|
|
221
|
+
open.value = false
|
|
222
|
+
if (returnFocus)
|
|
223
|
+
rootEl?.querySelector<HTMLElement>('[data-part="trigger"]')?.focus()
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
effect(() => {
|
|
227
|
+
if (!open.value)
|
|
228
|
+
return
|
|
229
|
+
const onPointerDown = (e: Event) => {
|
|
230
|
+
const trigger = rootEl?.querySelector('[data-part="trigger"]')
|
|
231
|
+
pointerDownOnTrigger = trigger !== null && trigger !== undefined && trigger.contains(e.target as Node)
|
|
232
|
+
if (!pointerDownOnTrigger)
|
|
233
|
+
return
|
|
234
|
+
pointerDown = true
|
|
235
|
+
suppressNextClick = false
|
|
236
|
+
const onUp = () => {
|
|
237
|
+
pointerDown = false
|
|
238
|
+
document.removeEventListener('pointerup', onUp, true)
|
|
239
|
+
document.removeEventListener('pointercancel', onUp, true)
|
|
240
|
+
scheduleSuppressReset()
|
|
241
|
+
}
|
|
242
|
+
document.addEventListener('pointerup', onUp, true)
|
|
243
|
+
document.addEventListener('pointercancel', onUp, true)
|
|
244
|
+
}
|
|
245
|
+
const onKey = (e: KeyboardEvent) => {
|
|
246
|
+
if (e === lastOpenEvent)
|
|
247
|
+
return
|
|
248
|
+
const prev = highlighted.peek()
|
|
249
|
+
handleListKeyDown(e, {
|
|
250
|
+
items: opts.items,
|
|
251
|
+
activeIndex: highlighted,
|
|
252
|
+
isDisabled: opts.isDisabled,
|
|
253
|
+
onSelect: opts.onSelect,
|
|
254
|
+
onClose: close,
|
|
255
|
+
selectOnSpace: true,
|
|
256
|
+
})
|
|
257
|
+
if (open.peek() && highlighted.peek() !== prev)
|
|
258
|
+
focusActive()
|
|
259
|
+
}
|
|
260
|
+
document.addEventListener('pointerdown', onPointerDown, true)
|
|
261
|
+
document.addEventListener('keydown', onKey)
|
|
262
|
+
onCleanup(() => {
|
|
263
|
+
document.removeEventListener('pointerdown', onPointerDown, true)
|
|
264
|
+
document.removeEventListener('keydown', onKey)
|
|
265
|
+
})
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
open,
|
|
270
|
+
highlighted,
|
|
271
|
+
setRoot(el) { rootEl = el },
|
|
272
|
+
close,
|
|
273
|
+
toggle(e) {
|
|
274
|
+
e.stopPropagation()
|
|
275
|
+
if (open.value) {
|
|
276
|
+
close()
|
|
277
|
+
}
|
|
278
|
+
else if (suppressNextClick) {
|
|
279
|
+
suppressNextClick = false
|
|
280
|
+
pointerDownOnTrigger = false
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
openWith(false)
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
onTriggerKeyDown(e) {
|
|
287
|
+
if (!open.value && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
288
|
+
e.preventDefault()
|
|
289
|
+
e.stopPropagation()
|
|
290
|
+
lastOpenEvent = e
|
|
291
|
+
openWith(e.key === 'ArrowUp')
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
focusActive,
|
|
295
|
+
activeDescendantId,
|
|
296
|
+
mountPopup(el) {
|
|
297
|
+
if (el === undefined)
|
|
298
|
+
return
|
|
299
|
+
el.addEventListener('toggle', (e) => {
|
|
300
|
+
if ((e as ToggleEvent).newState !== 'closed')
|
|
301
|
+
return
|
|
302
|
+
if (open.peek() && pointerDownOnTrigger) {
|
|
303
|
+
suppressNextClick = true
|
|
304
|
+
if (!pointerDown)
|
|
305
|
+
scheduleSuppressReset()
|
|
306
|
+
}
|
|
307
|
+
close()
|
|
308
|
+
})
|
|
309
|
+
queueMicrotask(() => {
|
|
310
|
+
showAnchored(el, rootEl!.querySelector<HTMLElement>('[data-part="trigger"]')!)
|
|
311
|
+
focusActive()
|
|
312
|
+
})
|
|
313
|
+
},
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ---- Shared native <dialog> sync & backdrop click helpers ----
|
|
318
|
+
|
|
319
|
+
/** Syncs a native `<dialog>` element's modal state, handling initial `open={signal(true)}` on mount/hydration. */
|
|
320
|
+
export function syncDialogOpen(
|
|
321
|
+
el: HTMLDialogElement | undefined,
|
|
322
|
+
isOpen: Signal<boolean>,
|
|
323
|
+
wantOpen: boolean,
|
|
324
|
+
onOpened?: () => void,
|
|
325
|
+
): void {
|
|
326
|
+
if (el === undefined)
|
|
327
|
+
return
|
|
328
|
+
if (wantOpen) {
|
|
329
|
+
if (!el.open) {
|
|
330
|
+
try {
|
|
331
|
+
el.showModal()
|
|
332
|
+
onOpened?.()
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
queueMicrotask(() => {
|
|
336
|
+
if (isOpen.peek() && !el.open && el.isConnected) {
|
|
337
|
+
el.showModal()
|
|
338
|
+
onOpened?.()
|
|
339
|
+
}
|
|
340
|
+
})
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
else if (el.open) {
|
|
345
|
+
el.close()
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Returns true only when a click on `<dialog>` targeted the backdrop outside its inner panel. */
|
|
350
|
+
export function isDialogBackdropClick(e: MouseEvent, dialogEl: HTMLDialogElement | undefined): boolean {
|
|
351
|
+
if (dialogEl === undefined || e.target !== dialogEl)
|
|
352
|
+
return false
|
|
353
|
+
const rect = dialogEl.getBoundingClientRect()
|
|
354
|
+
if (rect.width === 0 && rect.height === 0)
|
|
355
|
+
return true
|
|
356
|
+
return e.clientX < rect.left || e.clientX > rect.right || e.clientY < rect.top || e.clientY > rect.bottom
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// ---- Dialog behavior ----
|
|
360
|
+
|
|
361
|
+
export interface DialogBehavior {
|
|
362
|
+
readonly open: Signal<boolean>
|
|
363
|
+
show(): void
|
|
364
|
+
close(): void
|
|
365
|
+
toggle(): void
|
|
366
|
+
readonly triggerProps: Record<string, unknown>
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export function createDialog(initial = false): DialogBehavior {
|
|
370
|
+
const open = signal(initial)
|
|
371
|
+
return {
|
|
372
|
+
open,
|
|
373
|
+
show: () => { open.value = true },
|
|
374
|
+
close: () => { open.value = false },
|
|
375
|
+
toggle: () => { open.value = !open.value },
|
|
376
|
+
get triggerProps() {
|
|
377
|
+
return {
|
|
378
|
+
'type': 'button',
|
|
379
|
+
'aria-haspopup': 'dialog',
|
|
380
|
+
'aria-expanded': open.value ? 'true' : 'false',
|
|
381
|
+
'onClick': () => { open.value = true },
|
|
382
|
+
}
|
|
383
|
+
},
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context } from 'prunelle'
|
|
2
|
+
import { useContext } from 'prunelle'
|
|
3
|
+
|
|
4
|
+
export interface FieldControlWiring {
|
|
5
|
+
id: string
|
|
6
|
+
describedBy: string | undefined
|
|
7
|
+
invalid: boolean
|
|
8
|
+
required: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const FIELD_CONTEXT_ID = Symbol.for('pru.ui.field')
|
|
12
|
+
const FIELD_CONTEXT_REF = { id: FIELD_CONTEXT_ID, defaultValue: undefined } as Context<FieldControlWiring | undefined>
|
|
13
|
+
|
|
14
|
+
export function useFieldContext(): FieldControlWiring | undefined {
|
|
15
|
+
try {
|
|
16
|
+
return useContext(FIELD_CONTEXT_REF)
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/fold.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Case and diacritic folding shared by the DataTable search and the CommandPalette filter, so "emile" finds "Émile".
|
|
2
|
+
|
|
3
|
+
const MARKS_RE = /\p{M}/gu
|
|
4
|
+
|
|
5
|
+
export function foldText(text: string): string {
|
|
6
|
+
return text.normalize('NFD').replace(MARKS_RE, '').toLowerCase()
|
|
7
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// @prunellejs/ui — Platform-first headless primitives + pure CSS Style Grammars.
|
|
2
|
+
// Zero JS recipes, zero Tailwind lock-in, zero custom compiler syntax.
|
|
3
|
+
|
|
4
|
+
export {
|
|
5
|
+
createDataTable,
|
|
6
|
+
createDialog,
|
|
7
|
+
createTabs,
|
|
8
|
+
findFirstEnabledIndex,
|
|
9
|
+
findLastEnabledIndex,
|
|
10
|
+
findNextEnabledIndex,
|
|
11
|
+
handleListKeyDown,
|
|
12
|
+
isDialogBackdropClick,
|
|
13
|
+
nextUiId,
|
|
14
|
+
syncDialogOpen,
|
|
15
|
+
} from './behaviors'
|
|
16
|
+
export type { DataTableOptions, DataTableState, DialogBehavior, ListNavigationOptions, TabsBehavior } from './behaviors'
|
|
17
|
+
export { toast } from './toast'
|
|
18
|
+
export type { ToastItem } from './toast'
|
|
19
|
+
export { CommandPalette } from './CommandPalette'
|
|
20
|
+
export type { CommandItem, CommandPaletteProps, CommandSource } from './CommandPalette'
|
|
21
|
+
export { DataTable } from './DataTable'
|
|
22
|
+
export type { DataTableColumn, DataTableProps } from './DataTable'
|
|
23
|
+
export { ControlledDialog, Dialog } from './Dialog'
|
|
24
|
+
export type { ControlledDialogProps, DialogProps } from './Dialog'
|
|
25
|
+
export { ControlledDrawer, Drawer } from './Drawer'
|
|
26
|
+
export type { ControlledDrawerProps, DrawerProps } from './Drawer'
|
|
27
|
+
export { Menu } from './Menu'
|
|
28
|
+
export type { MenuItemDef, MenuProps } from './Menu'
|
|
29
|
+
export {
|
|
30
|
+
Accordion,
|
|
31
|
+
AccordionItem,
|
|
32
|
+
Badge,
|
|
33
|
+
Card,
|
|
34
|
+
CardBody,
|
|
35
|
+
CardFooter,
|
|
36
|
+
CardHeader,
|
|
37
|
+
Checkbox,
|
|
38
|
+
Grid,
|
|
39
|
+
Popover,
|
|
40
|
+
Progress,
|
|
41
|
+
RadioGroup,
|
|
42
|
+
Sidebar,
|
|
43
|
+
Split,
|
|
44
|
+
Switch,
|
|
45
|
+
} from './primitives'
|
|
46
|
+
export type { CheckControlProps, PopoverProps, ProgressProps, RadioGroupProps, RadioOption } from './primitives'
|
|
47
|
+
export { Select } from './Select'
|
|
48
|
+
export type { SelectOption, SelectProps } from './Select'
|
|
49
|
+
export {
|
|
50
|
+
Button,
|
|
51
|
+
Cluster,
|
|
52
|
+
Field,
|
|
53
|
+
Notice,
|
|
54
|
+
Stack,
|
|
55
|
+
} from './static'
|
|
56
|
+
export type {
|
|
57
|
+
ButtonProps,
|
|
58
|
+
ButtonVariant,
|
|
59
|
+
FieldControlWiring,
|
|
60
|
+
FieldProps,
|
|
61
|
+
Size,
|
|
62
|
+
SpaceToken,
|
|
63
|
+
SpaceValue,
|
|
64
|
+
Tone,
|
|
65
|
+
TrackSize,
|
|
66
|
+
TrackValue,
|
|
67
|
+
} from './static'
|
|
68
|
+
export { FieldContext, useFieldContext } from './static'
|
|
69
|
+
export { Tabs } from './Tabs'
|
|
70
|
+
export type { TabDef, TabsProps } from './Tabs'
|
|
71
|
+
export { Toaster } from './Toaster'
|
|
72
|
+
export type { ToasterProps } from './Toaster'
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const placeholder = 'Tapez une commande ou cherchez…'
|
|
2
|
+
export const input = 'Rechercher une commande'
|
|
3
|
+
export const palette = 'Palette de commandes'
|
|
4
|
+
export const trigger = 'Rechercher (⌘K)'
|
|
5
|
+
export const empty = 'Aucune commande ne correspond.'
|
|
6
|
+
export const noResults = 'Aucun résultat'
|
|
7
|
+
export const result = 'résultat'
|
|
8
|
+
export const results = 'résultats'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const placeholder = 'Choisir…'
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const placeholder = 'Filtrer les lignes…'
|
|
2
|
+
export const search = 'Rechercher'
|
|
3
|
+
export const selectColumn = 'Sélection'
|
|
4
|
+
export const selectRow = 'Sélectionner la ligne'
|
|
5
|
+
export const selectAll = 'Sélectionner toutes les lignes'
|
|
6
|
+
export const ascending = 'croissant'
|
|
7
|
+
export const descending = 'décroissant'
|
|
8
|
+
export const rows = 'lignes'
|
|
9
|
+
export const selected = 'sélectionnées'
|
|
10
|
+
export const page = 'Page'
|
|
11
|
+
export const prev = 'Précédente'
|
|
12
|
+
export const next = 'Suivante'
|
|
13
|
+
export const empty = 'Aucune ligne ne correspond.'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const dismiss = 'Fermer'
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const placeholder = 'Type a command or search...'
|
|
2
|
+
export const input = 'Search commands'
|
|
3
|
+
export const palette = 'Command palette'
|
|
4
|
+
export const trigger = 'Search (⌘K)'
|
|
5
|
+
export const empty = 'No matching commands.'
|
|
6
|
+
export const noResults = 'No results'
|
|
7
|
+
export const result = 'result'
|
|
8
|
+
export const results = 'results'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const placeholder = 'Select…'
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const placeholder = 'Filter rows...'
|
|
2
|
+
export const search = 'Search'
|
|
3
|
+
export const selectColumn = 'Select'
|
|
4
|
+
export const selectRow = 'Select row'
|
|
5
|
+
export const selectAll = 'Select all rows'
|
|
6
|
+
export const ascending = 'ascending'
|
|
7
|
+
export const descending = 'descending'
|
|
8
|
+
export const rows = 'rows'
|
|
9
|
+
export const selected = 'selected'
|
|
10
|
+
export const page = 'Page'
|
|
11
|
+
export const prev = 'Prev'
|
|
12
|
+
export const next = 'Next'
|
|
13
|
+
export const empty = 'No matching rows.'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const dismiss = 'Dismiss'
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// What a browser lacking invoker commands (Safari before 26.2), `closedby` (Safari before 27.3) or anchor positioning (Safari before 26, Firefox before 147) needs for the static Dialog and Drawer, `commandfor` buttons and the Menu, Select and Popover popups: only that browser loads it.
|
|
2
|
+
|
|
3
|
+
// Below the trigger, or above it and right-aligned where the viewport has no room, as `position-try-fallbacks` would; again on scroll, as an anchor would follow.
|
|
4
|
+
const PLACE_POPUP = 'const S=["[data-ui=menu] [role=menu]","[data-ui=select] [role=listbox]","[data-ui=popover][popover]"],'
|
|
5
|
+
+ 'P=t=>{const b=t.closest("[data-ui=menu],[data-ui=select]")?.querySelector("[data-part=trigger]")??document.querySelector(`[popovertarget="${t.id}"]`);if(!b)return;'
|
|
6
|
+
+ 'const r=b.getBoundingClientRect(),h=t.offsetHeight,w=t.offsetWidth,y=r.bottom+4+h>innerHeight&&r.top-4-h>=0?r.top-4-h:r.bottom+4,x=r.left+w>innerWidth?Math.max(0,r.right-w):r.left;'
|
|
7
|
+
+ 'Object.assign(t.style,{positionArea:"none",positionTryFallbacks:"none",margin:"0",inset:`${y}px auto auto ${x}px`,minWidth:t.matches("[data-ui=popover]")?"":`${r.width}px`})};'
|
|
8
|
+
+ 'A&&(document.addEventListener("toggle",e=>e.newState==="open"&&e.target.matches(S.join())&&P(e.target),true),'
|
|
9
|
+
+ 'addEventListener("scroll",()=>document.querySelectorAll(S.map(s=>`${s}:popover-open`).join()).forEach(P),{capture:true,passive:true}));'
|
|
10
|
+
|
|
11
|
+
/** Opens and closes what a `commandfor` button names, closes a `closedby="any"` dialog on a click pressed and released on its backdrop, and places the popups against their trigger. */
|
|
12
|
+
export const FALLBACK_JS = 'const C=!("command"in HTMLButtonElement.prototype),B=!("closedBy"in HTMLDialogElement.prototype),A=!CSS.supports("position-area:block-end");let D;'
|
|
13
|
+
+ PLACE_POPUP
|
|
14
|
+
+ 'document.addEventListener("pointerdown",e=>D=e.target,true);'
|
|
15
|
+
+ 'document.addEventListener("click",e=>{const t=e.target;'
|
|
16
|
+
+ 'if(C){const b=t.closest&&t.closest("button[commandfor]"),d=b&&document.getElementById(b.getAttribute("commandfor"));'
|
|
17
|
+
+ 'if(d){const c=b.getAttribute("command");c==="show-modal"?d.open||d.showModal():c==="close"||c==="request-close"?d.close():c==="toggle-popover"?d.togglePopover():c==="show-popover"?d.showPopover():c==="hide-popover"&&d.hidePopover();return}}'
|
|
18
|
+
+ 'if(B&&t===D&&t instanceof HTMLDialogElement&&t.open&&t.getAttribute("closedby")==="any"){const r=t.getBoundingClientRect();'
|
|
19
|
+
+ '(e.clientX<r.left||e.clientX>r.right||e.clientY<r.top||e.clientY>r.bottom)&&t.close()}})'
|
|
20
|
+
|
|
21
|
+
/** The fallback's file under the asset prefix, named by its content so a browser keeps it for good. */
|
|
22
|
+
export const FALLBACK_FILE = `kui-fallback-${new Bun.CryptoHasher('sha256').update(FALLBACK_JS).digest('hex').slice(0, 8)}.js`
|
|
23
|
+
|
|
24
|
+
/** Inline on a page with a `commandfor` button or a popup: imports the fallback only where a feature is missing, so other browsers fetch nothing. */
|
|
25
|
+
export const FALLBACK_GUARD = 'if(!("command"in HTMLButtonElement.prototype&&"closedBy"in HTMLDialogElement.prototype&&CSS.supports("position-area:block-end")))import(document.querySelector("script[data-kui-fallback]").dataset.kuiFallback)'
|
|
26
|
+
|
|
27
|
+
const NEEDS_FALLBACK_RE = /<button\b[^>]*\scommandfor=|\sdata-ui="(?:menu|select|popover)"/
|
|
28
|
+
|
|
29
|
+
/** `html` with the guard before `</head>` when a button in it carries `commandfor` or it holds a Menu, Select or Popover, pointing at the fallback under `assetPrefix`. */
|
|
30
|
+
export function withPlatformFallback(html: string, assetPrefix: string): string {
|
|
31
|
+
if (!NEEDS_FALLBACK_RE.test(html) || html.includes('data-kui-fallback'))
|
|
32
|
+
return html
|
|
33
|
+
const tag = `<script type="module" data-kui-fallback="${assetPrefix.replace(/\/$/, '')}/${FALLBACK_FILE}">${FALLBACK_GUARD}</script>`
|
|
34
|
+
const head = html.indexOf('</head>')
|
|
35
|
+
return head === -1 ? `${tag}${html}` : `${html.slice(0, head)}${tag}${html.slice(head)}`
|
|
36
|
+
}
|