@prunellejs/ui 0.0.0-stage → 0.1.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/llms.txt +70 -0
- package/package.json +32 -4
- package/src/CommandPalette.tsx +207 -0
- package/src/DataTable.tsx +165 -0
- package/src/Dialog.tsx +165 -0
- package/src/Drawer.tsx +4 -0
- package/src/Menu.tsx +115 -0
- package/src/Select.tsx +114 -0
- package/src/Tabs.tsx +63 -0
- package/src/Toaster.tsx +31 -0
- package/src/behaviors.ts +287 -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 +105 -0
- package/src/primitives.tsx +237 -0
- package/src/static.tsx +319 -0
- package/src/styles/bare.css +315 -0
- package/src/styles/grammars/console.css +321 -0
- package/src/styles/grammars/editorial.css +334 -0
- package/src/styles/grammars/orbit.css +538 -0
- package/src/styles/grammars/prunelle.css +652 -0
- package/src/styles/grammars/soft.css +294 -0
- package/src/styles/grammars/workshop.css +313 -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/llms.txt
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @prunellejs/ui Reference
|
|
2
|
+
|
|
3
|
+
> Platform-first headless UI primitives and pure CSS `@layer kui` Style Grammars (`0 KB` JS for static primitives; islands only for interactive widgets).
|
|
4
|
+
|
|
5
|
+
## Imports
|
|
6
|
+
|
|
7
|
+
- `@prunellejs/ui`:
|
|
8
|
+
- Static layout primitives (`0 KB` JS): `Stack`, `Cluster`, `Grid`, `Sidebar`, `Split`
|
|
9
|
+
- Static & native-platform primitives (`0 KB` JS): `Button`, `Card`, `CardHeader`, `CardBody`, `CardFooter`, `Badge`, `Notice`, `Field`, `FieldContext`, `useFieldContext`, `Checkbox`, `Switch`, `RadioGroup`, `Progress`, `Accordion`, `AccordionItem`, `Popover`, `Dialog`, `Drawer` (opened by `commandfor`/`command` buttons and closed by Esc or a backdrop click, with no JS; `ui()` adds a fallback that only a browser without invoker commands, `closedby` or anchor positioning loads)
|
|
10
|
+
- Interactive island primitives: `ControlledDialog`, `ControlledDrawer`, `Tabs`, `Menu`, `Select`, `CommandPalette`, `DataTable`, `Toaster`
|
|
11
|
+
- Headless signal behaviors & helpers: `createDialog`, `createTabs`, `createDataTable`, `toast`, `handleListKeyDown`, `findFirstEnabledIndex`, `findLastEnabledIndex`, `findNextEnabledIndex`, `isDialogBackdropClick`, `syncDialogOpen`, `nextUiId`
|
|
12
|
+
- `@prunellejs/ui/plugin`: `ui(options?)`, `GRAMMAR_NAMES`, `grammarCssPath`, `renderGrammarCss`, `ejectGrammar`
|
|
13
|
+
- CSS subpaths: `@prunellejs/ui/bare.css`, `@prunellejs/ui/grammars/{editorial,workshop,console,soft,orbit,prunelle}.css`
|
|
14
|
+
|
|
15
|
+
## Plugin Setup (`prunelle.config.ts`)
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { defineConfig } from 'prunelle/config'
|
|
19
|
+
import { ui } from '@prunellejs/ui/plugin'
|
|
20
|
+
|
|
21
|
+
export default defineConfig({
|
|
22
|
+
plugins: [ui({ grammar: 'editorial' })],
|
|
23
|
+
})
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
- `grammar`: `'editorial'` (default) | `'workshop'` | `'console'` | `'soft'` | `'orbit'` | `'prunelle'` (prunelle's own: eye radios, switches and progress; titles in Fraunces, which the app loads) | `'bare'` | `false` (skip injecting CSS when importing your own).
|
|
27
|
+
- `emitGrammars`: `boolean | GrammarName[] | { dir?: string, grammars?: GrammarName[] }` — emits standalone CSS files to `/<dir>/<name>.css` (default `grammars/<name>.css`) for runtime theme switching.
|
|
28
|
+
- CLI: `prunelle ui-eject <grammar> [dest]` copies a self-contained CSS grammar (with `bare.css` inlined) into your app (default `editorial` → `src/styles/ui.css`).
|
|
29
|
+
|
|
30
|
+
## Key Props & Types
|
|
31
|
+
|
|
32
|
+
- Shared tokens: `Tone = 'neutral' | 'accent' | 'success' | 'warning' | 'danger'`, `ButtonVariant = 'solid' | 'outline' | 'ghost'`, `Size = 'sm' | 'md' | 'lg'`, `SpaceToken = 'none' | '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'`, `TrackSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl'`.
|
|
33
|
+
- Layout: `<Stack gap="md">`, `<Cluster gap="sm">`, `<Grid min="md" gap="md">`, `<Sidebar side="sm" gap="lg">`, `<Split gap="md">`.
|
|
34
|
+
- `Button`: `{ variant?, tone?, size?, href?, type?, disabled?, class?, children? }` (renders `<a>` when `href` is set, else `<button>`).
|
|
35
|
+
- `Field`: `{ id, label, hint?, error?, required?, class?, children? }` — auto-wires `for`, `id`, `aria-describedby`, `aria-invalid`, `required` onto its child control (and provides `FieldContext`).
|
|
36
|
+
- `Checkbox` / `Switch`: `{ id?, name?, value?, label, checked?, disabled? }`; `RadioGroup`: `{ name, legend?, options: RadioOption[], defaultValue? }`.
|
|
37
|
+
- `Progress`: `{ label, value?, max? (100) }`, a native `<progress>`: indeterminate (a loader) without `value`.
|
|
38
|
+
- `Accordion` + `AccordionItem`: `{ title, group?, open?, children? }` (native `<details name={group}>`).
|
|
39
|
+
- `Popover`: `{ id, triggerLabel, triggerVariant?, children? }` (native Popover API). `Popover`, `Menu` and `Select` open in the top layer (never clipped by an `overflow` parent), anchored below their trigger and flipped when the viewport has no room.
|
|
40
|
+
- `Dialog` / `Drawer`: `{ id?, title?, ariaLabel?, description?, triggerLabel?, triggerVariant?, closeLabel?, side?: 'right' | 'left' | 'bottom', children? }`; another button opens one with `commandfor={id} command="show-modal"`. `ControlledDialog` / `ControlledDrawer` take the same props plus `open: Signal<boolean>`, which the browser closing it sets back to `false`.
|
|
41
|
+
- `Tabs`: `{ id?, items: Array<{ id, label, content }>, defaultValue?, label? }`.
|
|
42
|
+
- `Menu`: `{ id?, label, items: Array<{ id, label, href?, disabled?, onSelect? }>, variant?, onSelect? }`.
|
|
43
|
+
- `Select`: `{ id?, options: Array<{ value, label, disabled? }>, value?: Signal<string>, defaultValue?, name?, placeholder?, ariaLabel?, onChange? }`.
|
|
44
|
+
- `CommandPalette`: `{ id?, items: CommandItem[], shortcutKey? ('k'), open?: Signal<boolean>, onSelect?, ...labels }`.
|
|
45
|
+
- `DataTable`: `{ rows, columns: Array<{ key, label, sortable? }>, rowKey? ('id'), pageSize? (10), searchable? (true), selectable? (false), ...labels }`.
|
|
46
|
+
- `Toaster` + `toast`: mount `<Toaster dismissLabel? />` once in `_layout.tsx`; call `toast.show|info|success|warning|danger|error(title, description?, durationMs?)`, `toast.dismiss(id)`, `toast.clear()`.
|
|
47
|
+
|
|
48
|
+
## Example
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { Button, Card, CardBody, Field, Stack } from '@prunellejs/ui'
|
|
52
|
+
import { createPost } from '../actions/posts'
|
|
53
|
+
|
|
54
|
+
export default function NewPostForm({ error }: { error?: string }) {
|
|
55
|
+
return (
|
|
56
|
+
<Card>
|
|
57
|
+
<CardBody>
|
|
58
|
+
<form action={createPost} method="post">
|
|
59
|
+
<Stack gap="md">
|
|
60
|
+
<Field id="title" label="Title" error={error} required>
|
|
61
|
+
<input name="title" type="text" />
|
|
62
|
+
</Field>
|
|
63
|
+
<Button type="submit" tone="accent">Publish</Button>
|
|
64
|
+
</Stack>
|
|
65
|
+
</form>
|
|
66
|
+
</CardBody>
|
|
67
|
+
</Card>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/ui",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"description": "Platform-first headless UI primitives and pure CSS @layer kui Style Grammars for Prunelle",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"files": [
|
|
9
|
+
"src",
|
|
10
|
+
"llms.txt"
|
|
11
|
+
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": "./src/index.ts",
|
|
14
|
+
"./plugin": "./src/plugin.ts",
|
|
15
|
+
"./bare.css": "./src/styles/bare.css",
|
|
16
|
+
"./grammars/editorial.css": "./src/styles/grammars/editorial.css",
|
|
17
|
+
"./grammars/workshop.css": "./src/styles/grammars/workshop.css",
|
|
18
|
+
"./grammars/console.css": "./src/styles/grammars/console.css",
|
|
19
|
+
"./grammars/soft.css": "./src/styles/grammars/soft.css",
|
|
20
|
+
"./grammars/orbit.css": "./src/styles/grammars/orbit.css",
|
|
21
|
+
"./grammars/prunelle.css": "./src/styles/grammars/prunelle.css"
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"bun": ">=1.4.0"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/Kayno0o/prunelle",
|
|
29
|
+
"directory": "packages/ui"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"prunelle": "^0.1.2"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Accessible Cmd+K / Ctrl+K CommandPalette built on native `<dialog>` + fine-grained signals.
|
|
3
|
+
|
|
4
|
+
import type { Signal } from 'prunelle'
|
|
5
|
+
import { computed, effect, onMount, signal } from 'prunelle'
|
|
6
|
+
import { findFirstEnabledIndex, handleListKeyDown, isDialogBackdropClick, nextUiId, syncDialogOpen } from './behaviors'
|
|
7
|
+
|
|
8
|
+
export interface CommandItem {
|
|
9
|
+
id: string
|
|
10
|
+
label: string
|
|
11
|
+
group?: string | undefined
|
|
12
|
+
shortcut?: string | undefined
|
|
13
|
+
href?: string | undefined
|
|
14
|
+
disabled?: boolean | undefined
|
|
15
|
+
onSelect?: (() => void) | undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface CommandPaletteProps {
|
|
19
|
+
id?: string | undefined
|
|
20
|
+
items: CommandItem[]
|
|
21
|
+
placeholder?: string | undefined
|
|
22
|
+
inputLabel?: string | undefined
|
|
23
|
+
ariaLabel?: string | undefined
|
|
24
|
+
triggerLabel?: string | undefined
|
|
25
|
+
emptyLabel?: string | undefined
|
|
26
|
+
noResultsLabel?: string | undefined
|
|
27
|
+
resultLabel?: string | undefined
|
|
28
|
+
resultsLabel?: string | undefined
|
|
29
|
+
shortcutKey?: string | undefined
|
|
30
|
+
open?: Signal<boolean> | undefined
|
|
31
|
+
onSelect?: ((id: string) => void) | undefined
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const isItemDisabled = (it: CommandItem) => Boolean(it.disabled)
|
|
35
|
+
|
|
36
|
+
export function CommandPalette(props: CommandPaletteProps) {
|
|
37
|
+
const baseId = props.id ?? nextUiId('kui-cmd')
|
|
38
|
+
const listId = `${baseId}-list`
|
|
39
|
+
const optionId = (idx: number) => `${baseId}-opt-${idx}`
|
|
40
|
+
const placeholder = props.placeholder ?? 'Type a command or search...'
|
|
41
|
+
const inputLabel = props.inputLabel ?? 'Search commands'
|
|
42
|
+
const ariaLabel = props.ariaLabel ?? 'Command palette'
|
|
43
|
+
const triggerLabel = props.triggerLabel ?? 'Search (⌘K)'
|
|
44
|
+
const emptyLabel = props.emptyLabel ?? 'No matching commands.'
|
|
45
|
+
const noResultsLabel = props.noResultsLabel ?? 'No results'
|
|
46
|
+
const resultLabel = props.resultLabel ?? 'result'
|
|
47
|
+
const resultsLabel = props.resultsLabel ?? 'results'
|
|
48
|
+
const shortcutKey = (props.shortcutKey ?? 'k').toLowerCase()
|
|
49
|
+
|
|
50
|
+
const isOpen = props.open ?? signal(false)
|
|
51
|
+
const query = signal('')
|
|
52
|
+
const activeIdx = signal(findFirstEnabledIndex(props.items, isItemDisabled))
|
|
53
|
+
let dialogEl: HTMLDialogElement | undefined
|
|
54
|
+
let inputEl: HTMLInputElement | undefined
|
|
55
|
+
|
|
56
|
+
const filtered = computed(() => {
|
|
57
|
+
const q = query.value.trim().toLowerCase()
|
|
58
|
+
const items = props.items
|
|
59
|
+
if (!q)
|
|
60
|
+
return items
|
|
61
|
+
return items.filter(it =>
|
|
62
|
+
it.label.toLowerCase().includes(q) || (it.group?.toLowerCase().includes(q) ?? false),
|
|
63
|
+
)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const activeOptionId = () => {
|
|
67
|
+
const list = filtered.value
|
|
68
|
+
const idx = activeIdx.value
|
|
69
|
+
return !isOpen.value || idx < 0 || idx >= list.length || list[idx]?.disabled ? undefined : optionId(idx)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const statusText = () => {
|
|
73
|
+
if (!isOpen.value)
|
|
74
|
+
return ''
|
|
75
|
+
const n = filtered.value.length
|
|
76
|
+
if (n === 0)
|
|
77
|
+
return noResultsLabel
|
|
78
|
+
return n === 1 ? `1 ${resultLabel}` : `${n} ${resultsLabel}`
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const onOpened = () => {
|
|
82
|
+
query.value = ''
|
|
83
|
+
activeIdx.value = findFirstEnabledIndex(props.items, isItemDisabled)
|
|
84
|
+
queueMicrotask(() => inputEl?.focus())
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
onMount(() => {
|
|
88
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
89
|
+
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === shortcutKey) {
|
|
90
|
+
e.preventDefault()
|
|
91
|
+
isOpen.value = !isOpen.value
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
window.addEventListener('keydown', onKeyDown)
|
|
95
|
+
return () => window.removeEventListener('keydown', onKeyDown)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
effect(() => {
|
|
99
|
+
syncDialogOpen(dialogEl, isOpen, isOpen.value, onOpened)
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
const runItem = (item: CommandItem) => {
|
|
103
|
+
if (item.disabled)
|
|
104
|
+
return
|
|
105
|
+
isOpen.value = false
|
|
106
|
+
item.onSelect?.()
|
|
107
|
+
props.onSelect?.(item.id)
|
|
108
|
+
if (item.href)
|
|
109
|
+
window.location.assign(item.href)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return (
|
|
113
|
+
<div data-ui="command-root" data-state={isOpen.value ? 'open' : 'closed'}>
|
|
114
|
+
{triggerLabel && (
|
|
115
|
+
<button
|
|
116
|
+
data-ui="button"
|
|
117
|
+
data-variant="outline"
|
|
118
|
+
data-part="trigger"
|
|
119
|
+
type="button"
|
|
120
|
+
aria-haspopup="dialog"
|
|
121
|
+
aria-expanded={isOpen.value ? 'true' : 'false'}
|
|
122
|
+
onClick={() => { isOpen.value = true }}
|
|
123
|
+
>
|
|
124
|
+
{triggerLabel}
|
|
125
|
+
</button>
|
|
126
|
+
)}
|
|
127
|
+
<dialog
|
|
128
|
+
data-ui="command"
|
|
129
|
+
data-part="content"
|
|
130
|
+
aria-label={ariaLabel}
|
|
131
|
+
ref={(el) => {
|
|
132
|
+
dialogEl = el
|
|
133
|
+
syncDialogOpen(dialogEl, isOpen, isOpen.peek(), onOpened)
|
|
134
|
+
}}
|
|
135
|
+
onClose={() => { isOpen.value = false }}
|
|
136
|
+
onClick={(e) => {
|
|
137
|
+
if (isDialogBackdropClick(e, dialogEl))
|
|
138
|
+
isOpen.value = false
|
|
139
|
+
}}
|
|
140
|
+
>
|
|
141
|
+
<div data-part="panel">
|
|
142
|
+
<div data-part="input-wrap">
|
|
143
|
+
<input
|
|
144
|
+
ref={(el) => { inputEl = el }}
|
|
145
|
+
data-part="input"
|
|
146
|
+
type="search"
|
|
147
|
+
role="combobox"
|
|
148
|
+
aria-label={inputLabel}
|
|
149
|
+
aria-expanded={isOpen.value ? 'true' : 'false'}
|
|
150
|
+
aria-autocomplete="list"
|
|
151
|
+
aria-controls={listId}
|
|
152
|
+
aria-activedescendant={activeOptionId()}
|
|
153
|
+
placeholder={placeholder}
|
|
154
|
+
value={query.value}
|
|
155
|
+
onInput={(e) => {
|
|
156
|
+
query.value = e.currentTarget.value
|
|
157
|
+
activeIdx.value = findFirstEnabledIndex(filtered.value, isItemDisabled)
|
|
158
|
+
}}
|
|
159
|
+
onKeyDown={(e) => {
|
|
160
|
+
handleListKeyDown(e, {
|
|
161
|
+
items: () => filtered.value,
|
|
162
|
+
activeIndex: activeIdx,
|
|
163
|
+
isDisabled: isItemDisabled,
|
|
164
|
+
onSelect: runItem,
|
|
165
|
+
onClose: () => { isOpen.value = false },
|
|
166
|
+
selectOnSpace: false,
|
|
167
|
+
homeEnd: false,
|
|
168
|
+
closeOnTab: false,
|
|
169
|
+
})
|
|
170
|
+
}}
|
|
171
|
+
/>
|
|
172
|
+
</div>
|
|
173
|
+
<div id={listId} data-part="list" role="listbox" aria-label={inputLabel}>
|
|
174
|
+
{filtered.value.map((item, idx) => (
|
|
175
|
+
<button
|
|
176
|
+
key={item.id}
|
|
177
|
+
id={optionId(idx)}
|
|
178
|
+
type="button"
|
|
179
|
+
role="option"
|
|
180
|
+
tabindex={-1}
|
|
181
|
+
disabled={item.disabled}
|
|
182
|
+
aria-disabled={item.disabled ? 'true' : undefined}
|
|
183
|
+
data-part="item"
|
|
184
|
+
data-highlighted={activeIdx.value === idx ? 'true' : undefined}
|
|
185
|
+
aria-selected={activeIdx.value === idx ? 'true' : 'false'}
|
|
186
|
+
onMouseEnter={() => {
|
|
187
|
+
if (!item.disabled)
|
|
188
|
+
activeIdx.value = idx
|
|
189
|
+
}}
|
|
190
|
+
onClick={() => runItem(item)}
|
|
191
|
+
>
|
|
192
|
+
<span data-part="item-label">{item.label}</span>
|
|
193
|
+
{item.shortcut && <kbd data-part="kbd">{item.shortcut}</kbd>}
|
|
194
|
+
</button>
|
|
195
|
+
))}
|
|
196
|
+
</div>
|
|
197
|
+
{filtered.value.length === 0 && (
|
|
198
|
+
<p data-part="empty">{emptyLabel}</p>
|
|
199
|
+
)}
|
|
200
|
+
<div class="kui-sr-only" role="status" aria-live="polite" aria-atomic="true">
|
|
201
|
+
{statusText()}
|
|
202
|
+
</div>
|
|
203
|
+
</div>
|
|
204
|
+
</dialog>
|
|
205
|
+
</div>
|
|
206
|
+
)
|
|
207
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Interactive DataTable island powered by headless `createDataTable()` signals (sort, filter, select, paginate).
|
|
3
|
+
|
|
4
|
+
import { createDataTable } from './table-state'
|
|
5
|
+
|
|
6
|
+
export interface DataTableColumn {
|
|
7
|
+
key: string
|
|
8
|
+
label: string
|
|
9
|
+
sortable?: boolean | undefined
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface DataTableProps {
|
|
13
|
+
rows: Array<Record<string, string | number>>
|
|
14
|
+
columns: DataTableColumn[]
|
|
15
|
+
rowKey?: string | undefined
|
|
16
|
+
pageSize?: number | undefined
|
|
17
|
+
searchable?: boolean | undefined
|
|
18
|
+
selectable?: boolean | undefined
|
|
19
|
+
caption?: string | undefined
|
|
20
|
+
ariaLabel?: string | undefined
|
|
21
|
+
placeholder?: string | undefined
|
|
22
|
+
searchLabel?: string | undefined
|
|
23
|
+
selectColumnLabel?: string | undefined
|
|
24
|
+
selectRowLabel?: string | undefined
|
|
25
|
+
rowsLabel?: string | undefined
|
|
26
|
+
selectedLabel?: string | undefined
|
|
27
|
+
pageLabel?: string | undefined
|
|
28
|
+
prevLabel?: string | undefined
|
|
29
|
+
nextLabel?: string | undefined
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function DataTable(props: DataTableProps) {
|
|
33
|
+
const rowKey = props.rowKey ?? 'id'
|
|
34
|
+
const searchable = props.searchable ?? true
|
|
35
|
+
const selectable = props.selectable ?? false
|
|
36
|
+
const caption = props.caption
|
|
37
|
+
const ariaLabel = props.ariaLabel
|
|
38
|
+
const placeholder = props.placeholder ?? 'Filter rows...'
|
|
39
|
+
const searchLabel = props.searchLabel ?? 'Search'
|
|
40
|
+
const selectColumnLabel = props.selectColumnLabel ?? 'Select'
|
|
41
|
+
const selectRowLabel = props.selectRowLabel ?? 'Select row'
|
|
42
|
+
const rowsLabel = props.rowsLabel ?? 'rows'
|
|
43
|
+
const selectedLabel = props.selectedLabel ?? 'selected'
|
|
44
|
+
const pageLabel = props.pageLabel ?? 'Page'
|
|
45
|
+
const prevLabel = props.prevLabel ?? 'Prev'
|
|
46
|
+
const nextLabel = props.nextLabel ?? 'Next'
|
|
47
|
+
const columns = props.columns
|
|
48
|
+
const keyOf = (r: Record<string, string | number>) => String(r[rowKey] ?? '')
|
|
49
|
+
const sortOf = (k: string) => (table.sortBy.value === k ? table.sortDir.value : undefined)
|
|
50
|
+
|
|
51
|
+
const table = createDataTable({
|
|
52
|
+
rows: () => props.rows,
|
|
53
|
+
getKey: keyOf,
|
|
54
|
+
pageSize: props.pageSize ?? 10,
|
|
55
|
+
searchText: r => columns.map(c => String(r[c.key] ?? '')).join(' '),
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div data-ui="datatable">
|
|
60
|
+
{searchable && (
|
|
61
|
+
<div data-part="toolbar" data-layout="split">
|
|
62
|
+
<input
|
|
63
|
+
data-part="search"
|
|
64
|
+
type="search"
|
|
65
|
+
aria-label={searchLabel}
|
|
66
|
+
placeholder={placeholder}
|
|
67
|
+
value={table.query.value}
|
|
68
|
+
onInput={(e) => {
|
|
69
|
+
table.query.value = e.currentTarget.value
|
|
70
|
+
table.page.value = 1
|
|
71
|
+
}}
|
|
72
|
+
/>
|
|
73
|
+
<span data-part="meta">
|
|
74
|
+
{`${table.filtered.value.length} ${rowsLabel}${selectable && table.selected.value.size > 0 ? ` · ${table.selected.value.size} ${selectedLabel}` : ''}`}
|
|
75
|
+
</span>
|
|
76
|
+
</div>
|
|
77
|
+
)}
|
|
78
|
+
|
|
79
|
+
<div data-part="scroll">
|
|
80
|
+
<table data-ui="table" aria-label={caption ? undefined : ariaLabel}>
|
|
81
|
+
{caption && <caption data-part="caption">{caption}</caption>}
|
|
82
|
+
<thead>
|
|
83
|
+
<tr>
|
|
84
|
+
{selectable && (
|
|
85
|
+
<th scope="col" data-part="select-col">
|
|
86
|
+
<span class="kui-sr-only">{selectColumnLabel}</span>
|
|
87
|
+
</th>
|
|
88
|
+
)}
|
|
89
|
+
{columns.map(col => (
|
|
90
|
+
<th
|
|
91
|
+
key={col.key}
|
|
92
|
+
scope="col"
|
|
93
|
+
aria-sort={col.sortable === false ? undefined : sortOf(col.key) === 'asc' ? 'ascending' : sortOf(col.key) === 'desc' ? 'descending' : 'none'}
|
|
94
|
+
>
|
|
95
|
+
{col.sortable !== false
|
|
96
|
+
? (
|
|
97
|
+
<button
|
|
98
|
+
type="button"
|
|
99
|
+
data-part="sort"
|
|
100
|
+
data-active={sortOf(col.key) ? 'true' : undefined}
|
|
101
|
+
onClick={() => table.toggleSort(col.key)}
|
|
102
|
+
>
|
|
103
|
+
{col.label}
|
|
104
|
+
<span aria-hidden="true">{sortOf(col.key) === 'asc' ? ' ↑' : sortOf(col.key) === 'desc' ? ' ↓' : ''}</span>
|
|
105
|
+
</button>
|
|
106
|
+
)
|
|
107
|
+
: col.label}
|
|
108
|
+
</th>
|
|
109
|
+
))}
|
|
110
|
+
</tr>
|
|
111
|
+
</thead>
|
|
112
|
+
<tbody>
|
|
113
|
+
{table.visible.value.map(row => (
|
|
114
|
+
<tr key={keyOf(row)} data-selected={table.isSelected(keyOf(row)) ? 'true' : undefined}>
|
|
115
|
+
{selectable && (
|
|
116
|
+
<td data-part="select-col">
|
|
117
|
+
<input
|
|
118
|
+
type="checkbox"
|
|
119
|
+
checked={table.isSelected(keyOf(row))}
|
|
120
|
+
aria-label={`${selectRowLabel} ${keyOf(row)}`}
|
|
121
|
+
onChange={() => table.toggleSelect(keyOf(row))}
|
|
122
|
+
/>
|
|
123
|
+
</td>
|
|
124
|
+
)}
|
|
125
|
+
{columns.map(col => (
|
|
126
|
+
<td key={col.key}>{String(row[col.key] ?? '')}</td>
|
|
127
|
+
))}
|
|
128
|
+
</tr>
|
|
129
|
+
))}
|
|
130
|
+
</tbody>
|
|
131
|
+
</table>
|
|
132
|
+
</div>
|
|
133
|
+
|
|
134
|
+
{table.totalPages.value > 1 && (
|
|
135
|
+
<div data-part="pagination" data-layout="split">
|
|
136
|
+
<span data-part="meta">
|
|
137
|
+
{`${pageLabel} ${table.page.value} / ${table.totalPages.value}`}
|
|
138
|
+
</span>
|
|
139
|
+
<div data-layout="cluster">
|
|
140
|
+
<button
|
|
141
|
+
data-ui="button"
|
|
142
|
+
data-variant="outline"
|
|
143
|
+
data-size="sm"
|
|
144
|
+
type="button"
|
|
145
|
+
disabled={table.page.value <= 1}
|
|
146
|
+
onClick={() => { table.page.value-- }}
|
|
147
|
+
>
|
|
148
|
+
{prevLabel}
|
|
149
|
+
</button>
|
|
150
|
+
<button
|
|
151
|
+
data-ui="button"
|
|
152
|
+
data-variant="outline"
|
|
153
|
+
data-size="sm"
|
|
154
|
+
type="button"
|
|
155
|
+
disabled={table.page.value >= table.totalPages.value}
|
|
156
|
+
onClick={() => { table.page.value++ }}
|
|
157
|
+
>
|
|
158
|
+
{nextLabel}
|
|
159
|
+
</button>
|
|
160
|
+
</div>
|
|
161
|
+
</div>
|
|
162
|
+
)}
|
|
163
|
+
</div>
|
|
164
|
+
)
|
|
165
|
+
}
|
package/src/Dialog.tsx
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
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
|
+
// Takes `props` without spreading so lazy `get children()` getters evaluate strictly inside `<div data-part="body">`.
|
|
64
|
+
function DialogPanel(props: { dialog: Shared, ids: DialogIds }) {
|
|
65
|
+
const { dialog, ids } = props
|
|
66
|
+
return (
|
|
67
|
+
<div data-part="panel">
|
|
68
|
+
<header data-part="header" data-layout="split">
|
|
69
|
+
{dialog.title && <h2 id={ids.titleId} data-part="title">{dialog.title}</h2>}
|
|
70
|
+
<button
|
|
71
|
+
data-ui="button"
|
|
72
|
+
data-variant="ghost"
|
|
73
|
+
data-size="sm"
|
|
74
|
+
data-part="close"
|
|
75
|
+
type="button"
|
|
76
|
+
commandfor={ids.baseId}
|
|
77
|
+
command="close"
|
|
78
|
+
aria-label={dialog.closeLabel ?? 'Close'}
|
|
79
|
+
>
|
|
80
|
+
✕
|
|
81
|
+
</button>
|
|
82
|
+
</header>
|
|
83
|
+
{dialog.description && <p id={ids.descId} data-part="description">{dialog.description}</p>}
|
|
84
|
+
<div data-part="body">{dialog.children}</div>
|
|
85
|
+
</div>
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function renderStaticDialog(props: DrawerProps, kind: Kind) {
|
|
90
|
+
const ids = dialogIds(props, kind)
|
|
91
|
+
return (
|
|
92
|
+
<div data-ui={`${kind}-root`}>
|
|
93
|
+
{props.triggerLabel && (
|
|
94
|
+
<button
|
|
95
|
+
data-ui="button"
|
|
96
|
+
data-variant={props.triggerVariant ?? 'outline'}
|
|
97
|
+
data-part="trigger"
|
|
98
|
+
type="button"
|
|
99
|
+
aria-haspopup="dialog"
|
|
100
|
+
commandfor={ids.baseId}
|
|
101
|
+
command="show-modal"
|
|
102
|
+
>
|
|
103
|
+
{props.triggerLabel}
|
|
104
|
+
</button>
|
|
105
|
+
)}
|
|
106
|
+
<dialog {...dialogAttrs(props, kind, ids)}>
|
|
107
|
+
<DialogPanel dialog={props} ids={ids} />
|
|
108
|
+
</dialog>
|
|
109
|
+
</div>
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function renderControlledDialog(props: ControlledDrawerProps, kind: Kind) {
|
|
114
|
+
const ids = dialogIds(props, kind)
|
|
115
|
+
const isOpen = props.open
|
|
116
|
+
let dialogEl: HTMLDialogElement | undefined
|
|
117
|
+
|
|
118
|
+
effect(() => {
|
|
119
|
+
syncDialogOpen(dialogEl, isOpen, isOpen.value)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<div data-ui={`${kind}-root`} data-state={isOpen.value ? 'open' : 'closed'}>
|
|
124
|
+
{props.triggerLabel && (
|
|
125
|
+
<button
|
|
126
|
+
data-ui="button"
|
|
127
|
+
data-variant={props.triggerVariant ?? 'outline'}
|
|
128
|
+
data-part="trigger"
|
|
129
|
+
type="button"
|
|
130
|
+
aria-haspopup="dialog"
|
|
131
|
+
aria-expanded={isOpen.value ? 'true' : 'false'}
|
|
132
|
+
onClick={() => { isOpen.value = true }}
|
|
133
|
+
>
|
|
134
|
+
{props.triggerLabel}
|
|
135
|
+
</button>
|
|
136
|
+
)}
|
|
137
|
+
<dialog
|
|
138
|
+
{...dialogAttrs(props, kind, ids)}
|
|
139
|
+
ref={(el) => {
|
|
140
|
+
dialogEl = el
|
|
141
|
+
syncDialogOpen(dialogEl, isOpen, isOpen.peek())
|
|
142
|
+
}}
|
|
143
|
+
onClose={() => { isOpen.value = false }}
|
|
144
|
+
>
|
|
145
|
+
<DialogPanel dialog={props} ids={ids} />
|
|
146
|
+
</dialog>
|
|
147
|
+
</div>
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function Dialog(props: DialogProps) {
|
|
152
|
+
return renderStaticDialog(props, 'dialog')
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function Drawer(props: DrawerProps) {
|
|
156
|
+
return renderStaticDialog(props, 'drawer')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function ControlledDialog(props: ControlledDialogProps) {
|
|
160
|
+
return renderControlledDialog(props, 'dialog')
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function ControlledDrawer(props: ControlledDrawerProps) {
|
|
164
|
+
return renderControlledDialog(props, 'drawer')
|
|
165
|
+
}
|
package/src/Drawer.tsx
ADDED