@prunellejs/ui 0.0.0-stage → 0.6.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/llms.txt +76 -0
- package/package.json +32 -4
- package/src/CommandPalette.tsx +260 -0
- package/src/DataTable.tsx +175 -0
- package/src/Dialog.tsx +181 -0
- package/src/Drawer.tsx +4 -0
- package/src/Menu.tsx +117 -0
- package/src/Select.tsx +124 -0
- package/src/Tabs.tsx +63 -0
- package/src/Toaster.tsx +141 -0
- package/src/behaviors.ts +389 -0
- package/src/field-context.ts +21 -0
- package/src/index.ts +72 -0
- package/src/platform-fallback.ts +36 -0
- package/src/plugin.ts +169 -0
- package/src/primitives.tsx +237 -0
- package/src/static.tsx +319 -0
- package/src/styles/bare.css +24 -0
- package/src/styles/base.css +55 -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/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 +178 -0
- package/src/styles/grammars/editorial.css +163 -0
- package/src/styles/grammars/orbit.css +268 -0
- package/src/styles/grammars/prunelle.css +449 -0
- package/src/styles/grammars/soft.css +160 -0
- package/src/styles/grammars/workshop.css +171 -0
- package/src/styles/scan.ts +137 -0
- package/src/styles/split.ts +381 -0
- package/src/styles/theme.css +21 -0
- package/src/styles/themed/accordion.css +20 -0
- package/src/styles/themed/badge.css +20 -0
- package/src/styles/themed/button.css +21 -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/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 +18 -0
- package/src/styles/themed/tabs.css +17 -0
- package/src/styles/themed/toast.css +18 -0
- package/src/table-state.ts +126 -0
- package/src/tabs-state.ts +39 -0
- package/src/toast.ts +68 -0
- package/README.md +0 -3
package/src/Dialog.tsx
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Native `<dialog>` primitives shared by Dialog and Drawer: top layer, Esc and backdrop close, ::backdrop and focus return come from the browser.
|
|
3
|
+
// `Dialog` and `Drawer` open from their trigger's `command`, with no JS; `ControlledDialog` and `ControlledDrawer` also follow an `open` signal, as an island.
|
|
4
|
+
|
|
5
|
+
import type { PrunelleChild, Signal } from 'prunelle'
|
|
6
|
+
import type { ButtonVariant } from './static'
|
|
7
|
+
import { effect } from 'prunelle'
|
|
8
|
+
import { nextUiId, syncDialogOpen } from './behaviors'
|
|
9
|
+
|
|
10
|
+
export interface DialogProps {
|
|
11
|
+
id?: string | undefined
|
|
12
|
+
title?: string | undefined
|
|
13
|
+
ariaLabel?: string | undefined
|
|
14
|
+
description?: string | undefined
|
|
15
|
+
triggerLabel?: string | undefined
|
|
16
|
+
triggerVariant?: ButtonVariant | undefined
|
|
17
|
+
closeLabel?: string | undefined
|
|
18
|
+
/** A Dialog ships no JS: to open or close it from code, use `ControlledDialog` (or `ControlledDrawer`) with this signal. */
|
|
19
|
+
open?: undefined
|
|
20
|
+
children?: PrunelleChild | undefined
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface DrawerProps extends DialogProps {
|
|
24
|
+
side?: 'right' | 'left' | 'bottom' | undefined
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ControlledDialogProps extends Omit<DialogProps, 'open'> {
|
|
28
|
+
/** Opens and closes the dialog; the browser closing it (Esc, backdrop, close button) sets it to `false`. */
|
|
29
|
+
open: Signal<boolean>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ControlledDrawerProps extends ControlledDialogProps {
|
|
33
|
+
side?: 'right' | 'left' | 'bottom' | undefined
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type Kind = 'dialog' | 'drawer'
|
|
37
|
+
type Shared = Omit<DrawerProps, 'open'>
|
|
38
|
+
|
|
39
|
+
interface DialogIds {
|
|
40
|
+
baseId: string
|
|
41
|
+
titleId: string
|
|
42
|
+
descId: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function dialogIds(props: Shared, kind: Kind): DialogIds {
|
|
46
|
+
const baseId = props.id ?? nextUiId(`kui-${kind}`)
|
|
47
|
+
return { baseId, titleId: `${baseId}-title`, descId: `${baseId}-desc` }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function dialogAttrs(props: Shared, kind: Kind, ids: DialogIds) {
|
|
51
|
+
return {
|
|
52
|
+
'id': ids.baseId,
|
|
53
|
+
'closedby': 'any' as const,
|
|
54
|
+
'data-ui': kind,
|
|
55
|
+
'data-side': kind === 'drawer' ? (props.side ?? 'right') : undefined,
|
|
56
|
+
'data-part': 'content',
|
|
57
|
+
'aria-labelledby': props.title ? ids.titleId : undefined,
|
|
58
|
+
'aria-label': !props.title ? (props.ariaLabel ?? (kind === 'drawer' ? 'Drawer' : 'Dialog')) : undefined,
|
|
59
|
+
'aria-describedby': props.description ? ids.descId : undefined,
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function closeButtonAttrs(dialog: Shared, ids: DialogIds) {
|
|
64
|
+
return {
|
|
65
|
+
'data-ui': 'button',
|
|
66
|
+
'data-variant': 'ghost',
|
|
67
|
+
'data-size': 'sm',
|
|
68
|
+
'data-part': 'close',
|
|
69
|
+
'type': 'button' as const,
|
|
70
|
+
'commandfor': ids.baseId,
|
|
71
|
+
'command': 'close' as const,
|
|
72
|
+
'aria-label': dialog.closeLabel ?? 'Close',
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Takes `props` without spreading so lazy `get children()` getters evaluate strictly inside `<div data-part="body">`.
|
|
77
|
+
function DialogPanel(props: { dialog: Shared, ids: DialogIds }) {
|
|
78
|
+
const { dialog, ids } = props
|
|
79
|
+
return (
|
|
80
|
+
<div data-part="panel">
|
|
81
|
+
<header data-part="header" data-layout="split">
|
|
82
|
+
{dialog.title && <h2 id={ids.titleId} data-part="title">{dialog.title}</h2>}
|
|
83
|
+
<button {...closeButtonAttrs(dialog, ids)}>✕</button>
|
|
84
|
+
</header>
|
|
85
|
+
{dialog.description && <p id={ids.descId} data-part="description">{dialog.description}</p>}
|
|
86
|
+
<div data-part="body">{dialog.children}</div>
|
|
87
|
+
</div>
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function ControlledDialogPanel(props: { dialog: Shared, ids: DialogIds, onClose: () => void }) {
|
|
92
|
+
const { dialog, ids, onClose } = props
|
|
93
|
+
return (
|
|
94
|
+
<div data-part="panel">
|
|
95
|
+
<header data-part="header" data-layout="split">
|
|
96
|
+
{dialog.title && <h2 id={ids.titleId} data-part="title">{dialog.title}</h2>}
|
|
97
|
+
<button {...closeButtonAttrs(dialog, ids)} onClick={onClose}>✕</button>
|
|
98
|
+
</header>
|
|
99
|
+
{dialog.description && <p id={ids.descId} data-part="description">{dialog.description}</p>}
|
|
100
|
+
<div data-part="body">{dialog.children}</div>
|
|
101
|
+
</div>
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function renderStaticDialog(props: DrawerProps, kind: Kind) {
|
|
106
|
+
const ids = dialogIds(props, kind)
|
|
107
|
+
return (
|
|
108
|
+
<div data-ui={`${kind}-root`}>
|
|
109
|
+
{props.triggerLabel && (
|
|
110
|
+
<button
|
|
111
|
+
data-ui="button"
|
|
112
|
+
data-variant={props.triggerVariant ?? 'outline'}
|
|
113
|
+
data-part="trigger"
|
|
114
|
+
type="button"
|
|
115
|
+
aria-haspopup="dialog"
|
|
116
|
+
commandfor={ids.baseId}
|
|
117
|
+
command="show-modal"
|
|
118
|
+
>
|
|
119
|
+
{props.triggerLabel}
|
|
120
|
+
</button>
|
|
121
|
+
)}
|
|
122
|
+
<dialog {...dialogAttrs(props, kind, ids)}>
|
|
123
|
+
<DialogPanel dialog={props} ids={ids} />
|
|
124
|
+
</dialog>
|
|
125
|
+
</div>
|
|
126
|
+
)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function renderControlledDialog(props: ControlledDrawerProps, kind: Kind) {
|
|
130
|
+
const ids = dialogIds(props, kind)
|
|
131
|
+
const isOpen = props.open
|
|
132
|
+
let dialogEl: HTMLDialogElement | undefined
|
|
133
|
+
|
|
134
|
+
effect(() => {
|
|
135
|
+
syncDialogOpen(dialogEl, isOpen, isOpen.value)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<div data-ui={`${kind}-root`} data-state={isOpen.value ? 'open' : 'closed'}>
|
|
140
|
+
{props.triggerLabel && (
|
|
141
|
+
<button
|
|
142
|
+
data-ui="button"
|
|
143
|
+
data-variant={props.triggerVariant ?? 'outline'}
|
|
144
|
+
data-part="trigger"
|
|
145
|
+
type="button"
|
|
146
|
+
aria-haspopup="dialog"
|
|
147
|
+
aria-expanded={isOpen.value ? 'true' : 'false'}
|
|
148
|
+
onClick={() => { isOpen.value = true }}
|
|
149
|
+
>
|
|
150
|
+
{props.triggerLabel}
|
|
151
|
+
</button>
|
|
152
|
+
)}
|
|
153
|
+
<dialog
|
|
154
|
+
{...dialogAttrs(props, kind, ids)}
|
|
155
|
+
ref={(el) => {
|
|
156
|
+
dialogEl = el
|
|
157
|
+
syncDialogOpen(dialogEl, isOpen, isOpen.peek())
|
|
158
|
+
}}
|
|
159
|
+
onClose={() => { isOpen.value = false }}
|
|
160
|
+
>
|
|
161
|
+
<ControlledDialogPanel dialog={props} ids={ids} onClose={() => { isOpen.value = false }} />
|
|
162
|
+
</dialog>
|
|
163
|
+
</div>
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function Dialog(props: DialogProps) {
|
|
168
|
+
return renderStaticDialog(props, 'dialog')
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function Drawer(props: DrawerProps) {
|
|
172
|
+
return renderStaticDialog(props, 'drawer')
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function ControlledDialog(props: ControlledDialogProps) {
|
|
176
|
+
return renderControlledDialog(props, 'dialog')
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function ControlledDrawer(props: ControlledDrawerProps) {
|
|
180
|
+
return renderControlledDialog(props, 'drawer')
|
|
181
|
+
}
|
package/src/Drawer.tsx
ADDED
package/src/Menu.tsx
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Accessible dropdown Menu primitive with roving menuitem focus, link items, ArrowDown/Up trigger open, Escape/Tab close & click-outside dismiss.
|
|
3
|
+
|
|
4
|
+
import type { ButtonVariant } from './static'
|
|
5
|
+
import { createListPopup, navigateSafeHref, nextUiId, safeHref } from './behaviors'
|
|
6
|
+
|
|
7
|
+
export interface MenuItemDef {
|
|
8
|
+
id: string
|
|
9
|
+
label: string
|
|
10
|
+
href?: string | undefined
|
|
11
|
+
disabled?: boolean | undefined
|
|
12
|
+
onSelect?: (() => void) | undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface MenuProps {
|
|
16
|
+
id?: string | undefined
|
|
17
|
+
label: string
|
|
18
|
+
items: MenuItemDef[]
|
|
19
|
+
variant?: ButtonVariant | undefined
|
|
20
|
+
onSelect?: ((id: string) => void) | undefined
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function Menu(props: MenuProps) {
|
|
24
|
+
const baseId = props.id ?? nextUiId('kui-menu')
|
|
25
|
+
const triggerId = `${baseId}-trigger`
|
|
26
|
+
const menuId = `${baseId}-menu`
|
|
27
|
+
const itemId = (idx: number) => `${baseId}-item-${idx}`
|
|
28
|
+
const variant = props.variant ?? 'outline'
|
|
29
|
+
const label = props.label
|
|
30
|
+
|
|
31
|
+
const activate = (item: MenuItemDef, navigateHref: boolean) => {
|
|
32
|
+
if (item.disabled)
|
|
33
|
+
return
|
|
34
|
+
item.onSelect?.()
|
|
35
|
+
props.onSelect?.(item.id)
|
|
36
|
+
const href = safeHref(item.href)
|
|
37
|
+
popup.close(href === undefined)
|
|
38
|
+
if (navigateHref && href !== undefined)
|
|
39
|
+
navigateSafeHref(href)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const popup = createListPopup<MenuItemDef>({
|
|
43
|
+
items: () => props.items,
|
|
44
|
+
itemId,
|
|
45
|
+
isDisabled: it => Boolean(it.disabled),
|
|
46
|
+
onSelect: active => activate(active, true),
|
|
47
|
+
focusActive: true,
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div
|
|
52
|
+
data-ui="menu"
|
|
53
|
+
data-state={popup.open.value ? 'open' : 'closed'}
|
|
54
|
+
ref={(el) => { popup.setRoot(el) }}
|
|
55
|
+
>
|
|
56
|
+
<button
|
|
57
|
+
id={triggerId}
|
|
58
|
+
data-ui="button"
|
|
59
|
+
data-variant={variant}
|
|
60
|
+
data-part="trigger"
|
|
61
|
+
type="button"
|
|
62
|
+
aria-haspopup="menu"
|
|
63
|
+
aria-expanded={popup.open.value ? 'true' : 'false'}
|
|
64
|
+
aria-controls={popup.open.value ? menuId : undefined}
|
|
65
|
+
onClick={e => popup.toggle(e)}
|
|
66
|
+
onKeyDown={e => popup.onTriggerKeyDown(e)}
|
|
67
|
+
>
|
|
68
|
+
{label}
|
|
69
|
+
</button>
|
|
70
|
+
{popup.open.value && (
|
|
71
|
+
<div
|
|
72
|
+
id={menuId}
|
|
73
|
+
data-part="content"
|
|
74
|
+
role="menu"
|
|
75
|
+
aria-labelledby={triggerId}
|
|
76
|
+
popover="auto"
|
|
77
|
+
ref={popup.mountPopup}
|
|
78
|
+
>
|
|
79
|
+
{props.items.map((item, idx) => {
|
|
80
|
+
const href = safeHref(item.href)
|
|
81
|
+
return href !== undefined && !item.disabled
|
|
82
|
+
? (
|
|
83
|
+
<a
|
|
84
|
+
key={item.id}
|
|
85
|
+
id={itemId(idx)}
|
|
86
|
+
data-part="item"
|
|
87
|
+
data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
|
|
88
|
+
role="menuitem"
|
|
89
|
+
href={href}
|
|
90
|
+
tabindex={-1}
|
|
91
|
+
onClick={() => activate(item, false)}
|
|
92
|
+
>
|
|
93
|
+
{item.label}
|
|
94
|
+
</a>
|
|
95
|
+
)
|
|
96
|
+
: (
|
|
97
|
+
<button
|
|
98
|
+
key={item.id}
|
|
99
|
+
id={itemId(idx)}
|
|
100
|
+
data-part="item"
|
|
101
|
+
data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
|
|
102
|
+
role="menuitem"
|
|
103
|
+
type="button"
|
|
104
|
+
tabindex={-1}
|
|
105
|
+
disabled={item.disabled}
|
|
106
|
+
aria-disabled={item.disabled ? 'true' : undefined}
|
|
107
|
+
onClick={() => activate(item, false)}
|
|
108
|
+
>
|
|
109
|
+
{item.label}
|
|
110
|
+
</button>
|
|
111
|
+
)
|
|
112
|
+
})}
|
|
113
|
+
</div>
|
|
114
|
+
)}
|
|
115
|
+
</div>
|
|
116
|
+
)
|
|
117
|
+
}
|
package/src/Select.tsx
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Accessible custom Select (listbox) primitive with aria-activedescendant navigation + hidden input for native <form> submission.
|
|
3
|
+
|
|
4
|
+
import type { Signal } from 'prunelle'
|
|
5
|
+
import { onMount, signal } from 'prunelle'
|
|
6
|
+
import { createListPopup, nextUiId, useFieldContext } from './behaviors'
|
|
7
|
+
|
|
8
|
+
export interface SelectOption {
|
|
9
|
+
value: string
|
|
10
|
+
label: string
|
|
11
|
+
disabled?: boolean | undefined
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SelectProps {
|
|
15
|
+
id?: string | undefined
|
|
16
|
+
options: SelectOption[]
|
|
17
|
+
value?: Signal<string> | undefined
|
|
18
|
+
defaultValue?: string | undefined
|
|
19
|
+
name?: string | undefined
|
|
20
|
+
placeholder?: string | undefined
|
|
21
|
+
ariaLabel?: string | undefined
|
|
22
|
+
onChange?: ((value: string) => void) | undefined
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function Select(props: SelectProps) {
|
|
26
|
+
const field = useFieldContext()
|
|
27
|
+
const baseId = props.id ?? field?.id ?? nextUiId('kui-select')
|
|
28
|
+
const triggerId = signal(props.id ?? field?.id ?? `${baseId}-trigger`)
|
|
29
|
+
const listboxId = `${baseId}-listbox`
|
|
30
|
+
const optionId = (idx: number) => `${baseId}-opt-${idx}`
|
|
31
|
+
const name = props.name
|
|
32
|
+
const placeholder = props.placeholder ?? 'Select…'
|
|
33
|
+
const ariaLabel = props.ariaLabel
|
|
34
|
+
|
|
35
|
+
const selected = props.value ?? signal(props.defaultValue ?? props.options[0]?.value ?? '')
|
|
36
|
+
const currentLabel = () => props.options.find(o => o.value === selected.value)?.label ?? placeholder
|
|
37
|
+
|
|
38
|
+
const pick = (opt: SelectOption) => {
|
|
39
|
+
if (opt.disabled)
|
|
40
|
+
return
|
|
41
|
+
selected.value = opt.value
|
|
42
|
+
props.onChange?.(opt.value)
|
|
43
|
+
popup.close(true)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const popup = createListPopup<SelectOption>({
|
|
47
|
+
items: () => props.options,
|
|
48
|
+
itemId: optionId,
|
|
49
|
+
isDisabled: opt => Boolean(opt.disabled),
|
|
50
|
+
onSelect: pick,
|
|
51
|
+
initialIndex: () => props.options.findIndex(o => o.value === selected.value && !o.disabled),
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
let rootEl: HTMLElement | undefined
|
|
55
|
+
const syncFieldLabel = (el: HTMLElement | undefined) => {
|
|
56
|
+
if (!el || props.id || field)
|
|
57
|
+
return
|
|
58
|
+
const labelFor = el.closest('[data-ui="field"]')?.querySelector('label')?.htmlFor
|
|
59
|
+
if (labelFor)
|
|
60
|
+
triggerId.value = labelFor
|
|
61
|
+
}
|
|
62
|
+
onMount(() => { syncFieldLabel(rootEl) })
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<div
|
|
66
|
+
data-ui="select"
|
|
67
|
+
data-state={popup.open.value ? 'open' : 'closed'}
|
|
68
|
+
ref={(el) => {
|
|
69
|
+
rootEl = el
|
|
70
|
+
popup.setRoot(el)
|
|
71
|
+
syncFieldLabel(el)
|
|
72
|
+
}}
|
|
73
|
+
>
|
|
74
|
+
{name && <input type="hidden" name={name} value={selected.value} />}
|
|
75
|
+
<button
|
|
76
|
+
id={triggerId.value}
|
|
77
|
+
data-part="trigger"
|
|
78
|
+
type="button"
|
|
79
|
+
role="combobox"
|
|
80
|
+
aria-label={ariaLabel}
|
|
81
|
+
aria-haspopup="listbox"
|
|
82
|
+
aria-expanded={popup.open.value ? 'true' : 'false'}
|
|
83
|
+
aria-controls={popup.open.value ? listboxId : undefined}
|
|
84
|
+
aria-activedescendant={popup.activeDescendantId()}
|
|
85
|
+
onClick={e => popup.toggle(e)}
|
|
86
|
+
onKeyDown={e => popup.onTriggerKeyDown(e)}
|
|
87
|
+
>
|
|
88
|
+
<span data-part="value">{currentLabel()}</span>
|
|
89
|
+
<span aria-hidden="true">▾</span>
|
|
90
|
+
</button>
|
|
91
|
+
{popup.open.value && (
|
|
92
|
+
<div
|
|
93
|
+
id={listboxId}
|
|
94
|
+
data-part="listbox"
|
|
95
|
+
role="listbox"
|
|
96
|
+
aria-label={ariaLabel}
|
|
97
|
+
aria-labelledby={ariaLabel ? undefined : triggerId.value}
|
|
98
|
+
popover="auto"
|
|
99
|
+
ref={popup.mountPopup}
|
|
100
|
+
>
|
|
101
|
+
{props.options.map((opt, idx) => (
|
|
102
|
+
<button
|
|
103
|
+
key={opt.value}
|
|
104
|
+
id={optionId(idx)}
|
|
105
|
+
data-part="option"
|
|
106
|
+
data-selected={selected.value === opt.value ? 'true' : undefined}
|
|
107
|
+
data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
|
|
108
|
+
role="option"
|
|
109
|
+
type="button"
|
|
110
|
+
tabindex={-1}
|
|
111
|
+
disabled={opt.disabled}
|
|
112
|
+
aria-disabled={opt.disabled ? 'true' : undefined}
|
|
113
|
+
aria-selected={selected.value === opt.value ? 'true' : 'false'}
|
|
114
|
+
onClick={() => pick(opt)}
|
|
115
|
+
>
|
|
116
|
+
<span>{opt.label}</span>
|
|
117
|
+
<span aria-hidden="true">{selected.value === opt.value ? '✓' : ''}</span>
|
|
118
|
+
</button>
|
|
119
|
+
))}
|
|
120
|
+
</div>
|
|
121
|
+
)}
|
|
122
|
+
</div>
|
|
123
|
+
)
|
|
124
|
+
}
|
package/src/Tabs.tsx
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Accessible Tabs primitive with roving arrow-key + Home/End keyboard navigation.
|
|
3
|
+
|
|
4
|
+
import type { PrunelleChild } from 'prunelle'
|
|
5
|
+
import { nextUiId } from './behaviors'
|
|
6
|
+
import { createTabs } from './tabs-state'
|
|
7
|
+
|
|
8
|
+
export interface TabDef {
|
|
9
|
+
id: string
|
|
10
|
+
label: string
|
|
11
|
+
content: PrunelleChild
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface TabsProps {
|
|
15
|
+
id?: string | undefined
|
|
16
|
+
items: TabDef[]
|
|
17
|
+
defaultValue?: string | undefined
|
|
18
|
+
label?: string | undefined
|
|
19
|
+
class?: string | undefined
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function Tabs({ id, items, defaultValue, label = 'Tabs', class: cls }: TabsProps) {
|
|
23
|
+
const baseId = id ?? nextUiId('kui-tabs')
|
|
24
|
+
const ids = items.map(i => i.id)
|
|
25
|
+
const tabs = createTabs(defaultValue ?? items[0]?.id ?? '')
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<div data-ui="tabs" class={cls}>
|
|
29
|
+
<div data-part="list" role="tablist" aria-label={label}>
|
|
30
|
+
{items.map(item => (
|
|
31
|
+
<button
|
|
32
|
+
key={item.id}
|
|
33
|
+
id={`${baseId}-tab-${item.id}`}
|
|
34
|
+
data-part="trigger"
|
|
35
|
+
data-state={tabs.isActive(item.id) ? 'active' : 'inactive'}
|
|
36
|
+
role="tab"
|
|
37
|
+
type="button"
|
|
38
|
+
aria-selected={tabs.isActive(item.id) ? 'true' : 'false'}
|
|
39
|
+
aria-controls={`${baseId}-panel-${item.id}`}
|
|
40
|
+
tabIndex={tabs.isActive(item.id) ? 0 : -1}
|
|
41
|
+
onClick={() => tabs.select(item.id)}
|
|
42
|
+
onKeyDown={e => tabs.onKeyDown(e, ids)}
|
|
43
|
+
>
|
|
44
|
+
{item.label}
|
|
45
|
+
</button>
|
|
46
|
+
))}
|
|
47
|
+
</div>
|
|
48
|
+
{items.map(item => (
|
|
49
|
+
<div
|
|
50
|
+
key={item.id}
|
|
51
|
+
id={`${baseId}-panel-${item.id}`}
|
|
52
|
+
data-part="panel"
|
|
53
|
+
data-state={tabs.isActive(item.id) ? 'active' : 'inactive'}
|
|
54
|
+
role="tabpanel"
|
|
55
|
+
aria-labelledby={`${baseId}-tab-${item.id}`}
|
|
56
|
+
hidden={!tabs.isActive(item.id)}
|
|
57
|
+
>
|
|
58
|
+
{item.content}
|
|
59
|
+
</div>
|
|
60
|
+
))}
|
|
61
|
+
</div>
|
|
62
|
+
)
|
|
63
|
+
}
|
package/src/Toaster.tsx
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Live-region Toast stack driven by the reactive `toast` store.
|
|
3
|
+
|
|
4
|
+
import { effect, onCleanup } from 'prunelle'
|
|
5
|
+
import { toast } from './toast'
|
|
6
|
+
|
|
7
|
+
export interface ToasterProps {
|
|
8
|
+
dismissLabel?: string | undefined
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const TOAST_STORE_KEY = Symbol.for('pru.ui.toasts')
|
|
12
|
+
const RESUME_DURATION_MS = 4500
|
|
13
|
+
|
|
14
|
+
interface SharedToastTimers {
|
|
15
|
+
t: Map<string, ReturnType<typeof setTimeout>>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getTimerMap(): Map<string, ReturnType<typeof setTimeout>> | undefined {
|
|
19
|
+
if (typeof document === 'undefined')
|
|
20
|
+
return undefined
|
|
21
|
+
return (document as unknown as Record<symbol, SharedToastTimers | undefined>)[TOAST_STORE_KEY]?.t
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function Toaster({ dismissLabel = 'Dismiss' }: ToasterProps = {}) {
|
|
25
|
+
let hoveredToaster = false
|
|
26
|
+
let focusedToaster = false
|
|
27
|
+
const hoveredToasts = new Set<string>()
|
|
28
|
+
const pausedIds = new Set<string>()
|
|
29
|
+
|
|
30
|
+
const isPaused = () => hoveredToaster || focusedToaster || hoveredToasts.size > 0
|
|
31
|
+
|
|
32
|
+
const pauseTimers = () => {
|
|
33
|
+
const timers = getTimerMap()
|
|
34
|
+
if (timers === undefined)
|
|
35
|
+
return
|
|
36
|
+
for (const item of toast.items.peek()) {
|
|
37
|
+
const handle = timers.get(item.id)
|
|
38
|
+
if (handle !== undefined) {
|
|
39
|
+
clearTimeout(handle)
|
|
40
|
+
timers.delete(item.id)
|
|
41
|
+
pausedIds.add(item.id)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const resumeTimers = () => {
|
|
47
|
+
if (isPaused())
|
|
48
|
+
return
|
|
49
|
+
const timers = getTimerMap()
|
|
50
|
+
if (timers === undefined) {
|
|
51
|
+
pausedIds.clear()
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
const active = new Set(toast.items.peek().map(x => x.id))
|
|
55
|
+
for (const id of pausedIds) {
|
|
56
|
+
if (active.has(id) && !timers.has(id))
|
|
57
|
+
timers.set(id, setTimeout(() => toast.dismiss(id), RESUME_DURATION_MS))
|
|
58
|
+
}
|
|
59
|
+
pausedIds.clear()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
effect(() => {
|
|
63
|
+
const items = toast.items.value
|
|
64
|
+
if (items.length === 0)
|
|
65
|
+
return
|
|
66
|
+
if (isPaused())
|
|
67
|
+
pauseTimers()
|
|
68
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
69
|
+
if (e.key !== 'Escape' || e.defaultPrevented)
|
|
70
|
+
return
|
|
71
|
+
const current = toast.items.peek()
|
|
72
|
+
if (current.length === 0)
|
|
73
|
+
return
|
|
74
|
+
const activeEl = typeof document !== 'undefined' ? document.activeElement : null
|
|
75
|
+
const focusedId = activeEl?.closest?.('[data-ui="toast"]')?.getAttribute('data-toast-id')
|
|
76
|
+
const targetId = focusedId && current.some(x => x.id === focusedId)
|
|
77
|
+
? focusedId
|
|
78
|
+
: current[current.length - 1]!.id
|
|
79
|
+
toast.dismiss(targetId)
|
|
80
|
+
}
|
|
81
|
+
document.addEventListener('keydown', onKeyDown)
|
|
82
|
+
onCleanup(() => document.removeEventListener('keydown', onKeyDown))
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div
|
|
87
|
+
data-ui="toaster"
|
|
88
|
+
onMouseEnter={() => {
|
|
89
|
+
hoveredToaster = true
|
|
90
|
+
pauseTimers()
|
|
91
|
+
}}
|
|
92
|
+
onMouseLeave={() => {
|
|
93
|
+
hoveredToaster = false
|
|
94
|
+
resumeTimers()
|
|
95
|
+
}}
|
|
96
|
+
onFocusIn={() => {
|
|
97
|
+
focusedToaster = true
|
|
98
|
+
pauseTimers()
|
|
99
|
+
}}
|
|
100
|
+
onFocusOut={(e) => {
|
|
101
|
+
const next = e.relatedTarget as Node | null
|
|
102
|
+
if (next === null || !e.currentTarget.contains(next)) {
|
|
103
|
+
focusedToaster = false
|
|
104
|
+
resumeTimers()
|
|
105
|
+
}
|
|
106
|
+
}}
|
|
107
|
+
>
|
|
108
|
+
{toast.items.value.map(item => (
|
|
109
|
+
<div
|
|
110
|
+
key={item.id}
|
|
111
|
+
data-ui="toast"
|
|
112
|
+
data-toast-id={item.id}
|
|
113
|
+
data-tone={item.tone}
|
|
114
|
+
role={item.tone === 'danger' ? 'alert' : 'status'}
|
|
115
|
+
aria-atomic="true"
|
|
116
|
+
onMouseEnter={() => {
|
|
117
|
+
hoveredToasts.add(item.id)
|
|
118
|
+
pauseTimers()
|
|
119
|
+
}}
|
|
120
|
+
onMouseLeave={() => {
|
|
121
|
+
hoveredToasts.delete(item.id)
|
|
122
|
+
resumeTimers()
|
|
123
|
+
}}
|
|
124
|
+
>
|
|
125
|
+
<div>
|
|
126
|
+
<strong data-part="title">{item.title}</strong>
|
|
127
|
+
{item.description && <p data-part="description">{item.description}</p>}
|
|
128
|
+
</div>
|
|
129
|
+
<button
|
|
130
|
+
data-part="close"
|
|
131
|
+
type="button"
|
|
132
|
+
aria-label={dismissLabel}
|
|
133
|
+
onClick={() => toast.dismiss(item.id)}
|
|
134
|
+
>
|
|
135
|
+
✕
|
|
136
|
+
</button>
|
|
137
|
+
</div>
|
|
138
|
+
))}
|
|
139
|
+
</div>
|
|
140
|
+
)
|
|
141
|
+
}
|