@prunellejs/ui 0.0.0-stage → 0.5.3
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/llms.txt
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
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`, `UI_COMPONENT_PARTS`, `UI_EXPORT_TO_PARTS`, `grammarCssPath`, `renderGrammarCss`, `renderGrammarBaseCss`, `renderGrammarPartsCss`, `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 `base.css` and component sheets inlined) into your app (default `editorial` → `src/styles/ui.css`).
|
|
29
|
+
|
|
30
|
+
## Tokenized Grammar Architecture & Per-Component CSS
|
|
31
|
+
|
|
32
|
+
- All styles live inside `@layer kui`, layered as `base.css` (box-sizing, `.kui-sr-only`, single `:focus-visible` indicator, reduced motion, `[hidden]`) → per-component structural sheets (`components/<part>.css`) → shared tokenized sheets (`themed/<part>.css`) → thin grammar token files (`grammars/<name>.css`).
|
|
33
|
+
- Focus visibility (RGAA 10.7) is defined once in `base.css` via `--ui-focus-color` (falling back to `--ui-accent`); grammars never suppress focus outlines with `outline: none`.
|
|
34
|
+
- Per-component assembly: `renderGrammarBaseCss(grammar)` returns the base floor + grammar tokens; `renderGrammarPartsCss(grammar, parts)` returns only the requested `UiComponentPart` stylesheets in deterministic cascade order; `UI_EXPORT_TO_PARTS` maps every component export to its required parts (including transitive dependencies).
|
|
35
|
+
|
|
36
|
+
## Key Props & Types
|
|
37
|
+
|
|
38
|
+
- 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'`.
|
|
39
|
+
- Layout: `<Stack gap="md">`, `<Cluster gap="sm">`, `<Grid min="md" gap="md">`, `<Sidebar side="sm" gap="lg">`, `<Split gap="md">`.
|
|
40
|
+
- `Button`: `{ variant?, tone?, size?, href?, type?, disabled?, class?, children? }` (renders `<a>` when `href` is set, else `<button>`).
|
|
41
|
+
- `Field`: `{ id, label, hint?, error?, required?, class?, children? }` — auto-wires `for`, `id`, `aria-describedby`, `aria-invalid`, `required` onto its child control (and provides `FieldContext`).
|
|
42
|
+
- `Checkbox` / `Switch`: `{ id?, name?, value?, label, checked?, disabled? }`; `RadioGroup`: `{ name, legend?, options: RadioOption[], defaultValue? }`.
|
|
43
|
+
- `Progress`: `{ label, value?, max? (100) }`, a native `<progress>`: indeterminate (a loader) without `value`.
|
|
44
|
+
- `Accordion` + `AccordionItem`: `{ title, group?, open?, children? }` (native `<details name={group}>`).
|
|
45
|
+
- `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.
|
|
46
|
+
- `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`.
|
|
47
|
+
- `Tabs`: `{ id?, items: Array<{ id, label, content }>, defaultValue?, label? }`.
|
|
48
|
+
- `Menu`: `{ id?, label, items: Array<{ id, label, href?, disabled?, onSelect? }>, variant?, onSelect? }`.
|
|
49
|
+
- `Select`: `{ id?, options: Array<{ value, label, disabled? }>, value?: Signal<string>, defaultValue?, name?, placeholder?, ariaLabel?, onChange? }`.
|
|
50
|
+
- `CommandPalette`: `{ id?, items?: CommandItem[], source?: CommandSource, shortcutKey? ('k'), open?: Signal<boolean>, onSelect?, ...labels }` (`source` is a lazy search function or `{ search(query) }`, e.g. `createSearchSource()` from `@prunellejs/search/client`, invoked only once opened).
|
|
51
|
+
- `DataTable`: `{ rows, columns: Array<{ key, label, sortable? }>, rowKey? ('id'), pageSize? (10), searchable? (true), selectable? (false), ...labels }`.
|
|
52
|
+
- `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()`.
|
|
53
|
+
|
|
54
|
+
## Example
|
|
55
|
+
|
|
56
|
+
```tsx
|
|
57
|
+
import { Button, Card, CardBody, Field, Stack } from '@prunellejs/ui'
|
|
58
|
+
import { createPost } from '../actions/posts'
|
|
59
|
+
|
|
60
|
+
export default function NewPostForm({ error }: { error?: string }) {
|
|
61
|
+
return (
|
|
62
|
+
<Card>
|
|
63
|
+
<CardBody>
|
|
64
|
+
<form action={createPost} method="post">
|
|
65
|
+
<Stack gap="md">
|
|
66
|
+
<Field id="title" label="Title" error={error} required>
|
|
67
|
+
<input name="title" type="text" />
|
|
68
|
+
</Field>
|
|
69
|
+
<Button type="submit" tone="accent">Publish</Button>
|
|
70
|
+
</Stack>
|
|
71
|
+
</form>
|
|
72
|
+
</CardBody>
|
|
73
|
+
</Card>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/ui",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.3",
|
|
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.5.3"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
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, navigateSafeHref, nextUiId, syncDialogOpen } from './behaviors'
|
|
7
|
+
|
|
8
|
+
export interface CommandItem {
|
|
9
|
+
id: string
|
|
10
|
+
label: string
|
|
11
|
+
description?: string | undefined
|
|
12
|
+
group?: string | undefined
|
|
13
|
+
shortcut?: string | undefined
|
|
14
|
+
href?: string | undefined
|
|
15
|
+
disabled?: boolean | undefined
|
|
16
|
+
onSelect?: (() => void) | undefined
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type CommandSource =
|
|
20
|
+
| ((query: string) => CommandItem[] | Promise<CommandItem[]>)
|
|
21
|
+
| { search: (query: string) => CommandItem[] | Promise<CommandItem[]> }
|
|
22
|
+
|
|
23
|
+
export interface CommandPaletteProps {
|
|
24
|
+
id?: string | undefined
|
|
25
|
+
items?: CommandItem[] | undefined
|
|
26
|
+
/** Dynamic or lazy search source (invoked only once the palette opens, e.g. `createSearchSource()` from `@prunellejs/search/client`). */
|
|
27
|
+
source?: CommandSource | undefined
|
|
28
|
+
placeholder?: string | undefined
|
|
29
|
+
inputLabel?: string | undefined
|
|
30
|
+
ariaLabel?: string | undefined
|
|
31
|
+
triggerLabel?: string | undefined
|
|
32
|
+
emptyLabel?: string | undefined
|
|
33
|
+
noResultsLabel?: string | undefined
|
|
34
|
+
resultLabel?: string | undefined
|
|
35
|
+
resultsLabel?: string | undefined
|
|
36
|
+
shortcutKey?: string | undefined
|
|
37
|
+
open?: Signal<boolean> | undefined
|
|
38
|
+
onSelect?: ((id: string) => void) | undefined
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isItemDisabled = (it: CommandItem) => Boolean(it.disabled)
|
|
42
|
+
|
|
43
|
+
function filterStaticItems(items: CommandItem[], rawQuery: string): CommandItem[] {
|
|
44
|
+
const q = rawQuery.trim().toLowerCase()
|
|
45
|
+
if (!q)
|
|
46
|
+
return items
|
|
47
|
+
return items.filter(it =>
|
|
48
|
+
it.label.toLowerCase().includes(q)
|
|
49
|
+
|| (it.group?.toLowerCase().includes(q) ?? false)
|
|
50
|
+
|| (it.description?.toLowerCase().includes(q) ?? false),
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function createSourceLoader(
|
|
55
|
+
source: CommandSource | undefined,
|
|
56
|
+
sourceItems: Signal<CommandItem[]>,
|
|
57
|
+
onLoaded: () => void,
|
|
58
|
+
): (q: string) => void {
|
|
59
|
+
if (!source)
|
|
60
|
+
return () => {}
|
|
61
|
+
let seq = 0
|
|
62
|
+
return (q: string) => {
|
|
63
|
+
const id = ++seq
|
|
64
|
+
const out = typeof source === 'function' ? source(q) : source.search(q)
|
|
65
|
+
Promise.resolve(out).then(
|
|
66
|
+
(results) => {
|
|
67
|
+
if (id === seq) {
|
|
68
|
+
sourceItems.value = results
|
|
69
|
+
onLoaded()
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
() => {
|
|
73
|
+
if (id === seq)
|
|
74
|
+
sourceItems.value = []
|
|
75
|
+
},
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function bindPaletteShortcut(shortcutKey: string, isOpen: Signal<boolean>): void {
|
|
81
|
+
onMount(() => {
|
|
82
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
83
|
+
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === shortcutKey) {
|
|
84
|
+
e.preventDefault()
|
|
85
|
+
isOpen.value = !isOpen.value
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
window.addEventListener('keydown', onKeyDown)
|
|
89
|
+
return () => window.removeEventListener('keydown', onKeyDown)
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function selectCommandItem(item: CommandItem, isOpen: Signal<boolean>, onSelect?: (id: string) => void): void {
|
|
94
|
+
if (item.disabled)
|
|
95
|
+
return
|
|
96
|
+
isOpen.value = false
|
|
97
|
+
item.onSelect?.()
|
|
98
|
+
onSelect?.(item.id)
|
|
99
|
+
if (item.href)
|
|
100
|
+
navigateSafeHref(item.href)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function CommandPalette(props: CommandPaletteProps) {
|
|
104
|
+
const baseId = props.id ?? nextUiId('kui-cmd')
|
|
105
|
+
const listId = `${baseId}-list`
|
|
106
|
+
const optionId = (idx: number) => `${baseId}-opt-${idx}`
|
|
107
|
+
const placeholder = props.placeholder ?? 'Type a command or search...'
|
|
108
|
+
const inputLabel = props.inputLabel ?? 'Search commands'
|
|
109
|
+
const ariaLabel = props.ariaLabel ?? 'Command palette'
|
|
110
|
+
const triggerLabel = props.triggerLabel ?? 'Search (⌘K)'
|
|
111
|
+
const emptyLabel = props.emptyLabel ?? 'No matching commands.'
|
|
112
|
+
const noResultsLabel = props.noResultsLabel ?? 'No results'
|
|
113
|
+
const resultLabel = props.resultLabel ?? 'result'
|
|
114
|
+
const resultsLabel = props.resultsLabel ?? 'results'
|
|
115
|
+
const staticItems = props.items ?? []
|
|
116
|
+
|
|
117
|
+
const isOpen = props.open ?? signal(false)
|
|
118
|
+
const query = signal('')
|
|
119
|
+
const sourceItems = signal<CommandItem[]>([])
|
|
120
|
+
const activeIdx = signal(findFirstEnabledIndex(staticItems, isItemDisabled))
|
|
121
|
+
let dialogEl: HTMLDialogElement | undefined
|
|
122
|
+
let inputEl: HTMLInputElement | undefined
|
|
123
|
+
|
|
124
|
+
const filtered = computed(() => {
|
|
125
|
+
const base = filterStaticItems(staticItems, query.value)
|
|
126
|
+
const remote = sourceItems.value
|
|
127
|
+
return remote.length ? (base.length ? [...base, ...remote] : remote) : base
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
const refreshSource = createSourceLoader(props.source, sourceItems, () => {
|
|
131
|
+
activeIdx.value = findFirstEnabledIndex(filtered.value, isItemDisabled)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
const activeOptionId = () => {
|
|
135
|
+
const list = filtered.value
|
|
136
|
+
const idx = activeIdx.value
|
|
137
|
+
return !isOpen.value || idx < 0 || idx >= list.length || list[idx]?.disabled ? undefined : optionId(idx)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const statusText = () => {
|
|
141
|
+
if (!isOpen.value)
|
|
142
|
+
return ''
|
|
143
|
+
const n = filtered.value.length
|
|
144
|
+
return n === 0 ? noResultsLabel : n === 1 ? `1 ${resultLabel}` : `${n} ${resultsLabel}`
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const onOpened = () => {
|
|
148
|
+
query.value = ''
|
|
149
|
+
activeIdx.value = findFirstEnabledIndex(staticItems, isItemDisabled)
|
|
150
|
+
refreshSource('')
|
|
151
|
+
queueMicrotask(() => inputEl?.focus())
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
bindPaletteShortcut((props.shortcutKey ?? 'k').toLowerCase(), isOpen)
|
|
155
|
+
effect(() => { syncDialogOpen(dialogEl, isOpen, isOpen.value, onOpened) })
|
|
156
|
+
|
|
157
|
+
const runItem = (item: CommandItem) => selectCommandItem(item, isOpen, props.onSelect)
|
|
158
|
+
|
|
159
|
+
return (
|
|
160
|
+
<div data-ui="command-root" data-state={isOpen.value ? 'open' : 'closed'}>
|
|
161
|
+
{triggerLabel && (
|
|
162
|
+
<button
|
|
163
|
+
data-ui="button"
|
|
164
|
+
data-variant="outline"
|
|
165
|
+
data-part="trigger"
|
|
166
|
+
type="button"
|
|
167
|
+
aria-haspopup="dialog"
|
|
168
|
+
aria-expanded={isOpen.value ? 'true' : 'false'}
|
|
169
|
+
onClick={() => { isOpen.value = true }}
|
|
170
|
+
>
|
|
171
|
+
{triggerLabel}
|
|
172
|
+
</button>
|
|
173
|
+
)}
|
|
174
|
+
<dialog
|
|
175
|
+
data-ui="command"
|
|
176
|
+
data-part="content"
|
|
177
|
+
aria-label={ariaLabel}
|
|
178
|
+
ref={(el) => {
|
|
179
|
+
dialogEl = el
|
|
180
|
+
syncDialogOpen(dialogEl, isOpen, isOpen.peek(), onOpened)
|
|
181
|
+
}}
|
|
182
|
+
onClose={() => { isOpen.value = false }}
|
|
183
|
+
onClick={(e) => {
|
|
184
|
+
if (isDialogBackdropClick(e, dialogEl))
|
|
185
|
+
isOpen.value = false
|
|
186
|
+
}}
|
|
187
|
+
>
|
|
188
|
+
<div data-part="panel">
|
|
189
|
+
<div data-part="input-wrap">
|
|
190
|
+
<input
|
|
191
|
+
ref={(el) => { inputEl = el }}
|
|
192
|
+
data-part="input"
|
|
193
|
+
type="search"
|
|
194
|
+
role="combobox"
|
|
195
|
+
aria-label={inputLabel}
|
|
196
|
+
aria-expanded={isOpen.value ? 'true' : 'false'}
|
|
197
|
+
aria-autocomplete="list"
|
|
198
|
+
aria-controls={filtered.value.length > 0 ? listId : undefined}
|
|
199
|
+
aria-activedescendant={activeOptionId()}
|
|
200
|
+
placeholder={placeholder}
|
|
201
|
+
value={query.value}
|
|
202
|
+
onInput={(e) => {
|
|
203
|
+
const val = e.currentTarget.value
|
|
204
|
+
query.value = val
|
|
205
|
+
activeIdx.value = findFirstEnabledIndex(filtered.value, isItemDisabled)
|
|
206
|
+
refreshSource(val)
|
|
207
|
+
}}
|
|
208
|
+
onKeyDown={(e) => {
|
|
209
|
+
handleListKeyDown(e, {
|
|
210
|
+
items: () => filtered.value,
|
|
211
|
+
activeIndex: activeIdx,
|
|
212
|
+
isDisabled: isItemDisabled,
|
|
213
|
+
onSelect: runItem,
|
|
214
|
+
onClose: () => { isOpen.value = false },
|
|
215
|
+
selectOnSpace: false,
|
|
216
|
+
homeEnd: false,
|
|
217
|
+
closeOnTab: false,
|
|
218
|
+
})
|
|
219
|
+
}}
|
|
220
|
+
/>
|
|
221
|
+
</div>
|
|
222
|
+
{filtered.value.length > 0
|
|
223
|
+
? (
|
|
224
|
+
<div id={listId} data-part="list" role="listbox" aria-label={inputLabel}>
|
|
225
|
+
{filtered.value.map((item, idx) => (
|
|
226
|
+
<button
|
|
227
|
+
key={item.id}
|
|
228
|
+
id={optionId(idx)}
|
|
229
|
+
type="button"
|
|
230
|
+
role="option"
|
|
231
|
+
tabindex={-1}
|
|
232
|
+
disabled={item.disabled}
|
|
233
|
+
aria-disabled={item.disabled ? 'true' : undefined}
|
|
234
|
+
data-part="item"
|
|
235
|
+
data-highlighted={activeIdx.value === idx ? 'true' : undefined}
|
|
236
|
+
aria-selected={activeIdx.value === idx ? 'true' : 'false'}
|
|
237
|
+
onMouseEnter={() => {
|
|
238
|
+
if (!item.disabled)
|
|
239
|
+
activeIdx.value = idx
|
|
240
|
+
}}
|
|
241
|
+
onClick={() => runItem(item)}
|
|
242
|
+
>
|
|
243
|
+
<span data-part="item-label">{item.label}</span>
|
|
244
|
+
{item.description && <span data-part="item-description">{item.description}</span>}
|
|
245
|
+
{item.shortcut && <kbd data-part="kbd">{item.shortcut}</kbd>}
|
|
246
|
+
</button>
|
|
247
|
+
))}
|
|
248
|
+
</div>
|
|
249
|
+
)
|
|
250
|
+
: (
|
|
251
|
+
<p data-part="empty">{emptyLabel}</p>
|
|
252
|
+
)}
|
|
253
|
+
<div class="kui-sr-only" role="status" aria-live="polite" aria-atomic="true">
|
|
254
|
+
{statusText()}
|
|
255
|
+
</div>
|
|
256
|
+
</div>
|
|
257
|
+
</dialog>
|
|
258
|
+
</div>
|
|
259
|
+
)
|
|
260
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
emptyLabel?: string | undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function DataTable(props: DataTableProps) {
|
|
34
|
+
const rowKey = props.rowKey ?? 'id'
|
|
35
|
+
const searchable = props.searchable ?? true
|
|
36
|
+
const selectable = props.selectable ?? false
|
|
37
|
+
const caption = props.caption
|
|
38
|
+
const ariaLabel = props.ariaLabel
|
|
39
|
+
const placeholder = props.placeholder ?? 'Filter rows...'
|
|
40
|
+
const searchLabel = props.searchLabel ?? 'Search'
|
|
41
|
+
const selectColumnLabel = props.selectColumnLabel ?? 'Select'
|
|
42
|
+
const selectRowLabel = props.selectRowLabel ?? 'Select row'
|
|
43
|
+
const rowsLabel = props.rowsLabel ?? 'rows'
|
|
44
|
+
const selectedLabel = props.selectedLabel ?? 'selected'
|
|
45
|
+
const pageLabel = props.pageLabel ?? 'Page'
|
|
46
|
+
const prevLabel = props.prevLabel ?? 'Prev'
|
|
47
|
+
const nextLabel = props.nextLabel ?? 'Next'
|
|
48
|
+
const emptyLabel = props.emptyLabel ?? 'No matching rows.'
|
|
49
|
+
const columns = props.columns
|
|
50
|
+
const keyOf = (r: Record<string, string | number>) => String(r[rowKey] ?? '')
|
|
51
|
+
const sortOf = (k: string) => (table.sortBy.value === k ? table.sortDir.value : undefined)
|
|
52
|
+
|
|
53
|
+
const table = createDataTable({
|
|
54
|
+
rows: () => props.rows,
|
|
55
|
+
getKey: keyOf,
|
|
56
|
+
pageSize: props.pageSize ?? 10,
|
|
57
|
+
searchText: r => columns.map(c => String(r[c.key] ?? '')).join(' '),
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div data-ui="datatable">
|
|
62
|
+
{searchable && (
|
|
63
|
+
<div data-part="toolbar" data-layout="split">
|
|
64
|
+
<input
|
|
65
|
+
data-part="search"
|
|
66
|
+
type="search"
|
|
67
|
+
aria-label={searchLabel}
|
|
68
|
+
placeholder={placeholder}
|
|
69
|
+
value={table.query.value}
|
|
70
|
+
onInput={(e) => {
|
|
71
|
+
table.query.value = e.currentTarget.value
|
|
72
|
+
table.page.value = 1
|
|
73
|
+
}}
|
|
74
|
+
/>
|
|
75
|
+
<span data-part="meta" role="status" aria-live="polite">
|
|
76
|
+
{`${table.filtered.value.length} ${rowsLabel}${selectable && table.selected.value.size > 0 ? ` · ${table.selected.value.size} ${selectedLabel}` : ''}`}
|
|
77
|
+
</span>
|
|
78
|
+
</div>
|
|
79
|
+
)}
|
|
80
|
+
|
|
81
|
+
<div data-part="scroll">
|
|
82
|
+
<table data-ui="table" aria-label={caption ? undefined : ariaLabel}>
|
|
83
|
+
{caption && <caption data-part="caption">{caption}</caption>}
|
|
84
|
+
<thead>
|
|
85
|
+
<tr>
|
|
86
|
+
{selectable && (
|
|
87
|
+
<th scope="col" data-part="select-col">
|
|
88
|
+
<span class="kui-sr-only">{selectColumnLabel}</span>
|
|
89
|
+
</th>
|
|
90
|
+
)}
|
|
91
|
+
{columns.map(col => (
|
|
92
|
+
<th
|
|
93
|
+
key={col.key}
|
|
94
|
+
scope="col"
|
|
95
|
+
aria-sort={col.sortable === false ? undefined : sortOf(col.key) === 'asc' ? 'ascending' : sortOf(col.key) === 'desc' ? 'descending' : 'none'}
|
|
96
|
+
>
|
|
97
|
+
{col.sortable !== false
|
|
98
|
+
? (
|
|
99
|
+
<button
|
|
100
|
+
type="button"
|
|
101
|
+
data-part="sort"
|
|
102
|
+
data-active={sortOf(col.key) ? 'true' : undefined}
|
|
103
|
+
onClick={() => table.toggleSort(col.key)}
|
|
104
|
+
>
|
|
105
|
+
{col.label}
|
|
106
|
+
<span aria-hidden="true">{sortOf(col.key) === 'asc' ? ' ↑' : sortOf(col.key) === 'desc' ? ' ↓' : ''}</span>
|
|
107
|
+
</button>
|
|
108
|
+
)
|
|
109
|
+
: col.label}
|
|
110
|
+
</th>
|
|
111
|
+
))}
|
|
112
|
+
</tr>
|
|
113
|
+
</thead>
|
|
114
|
+
<tbody>
|
|
115
|
+
{table.visible.value.length === 0
|
|
116
|
+
? (
|
|
117
|
+
<tr data-part="empty">
|
|
118
|
+
<td colspan={Math.max(1, columns.length + (selectable ? 1 : 0))}>
|
|
119
|
+
{emptyLabel}
|
|
120
|
+
</td>
|
|
121
|
+
</tr>
|
|
122
|
+
)
|
|
123
|
+
: table.visible.value.map(row => (
|
|
124
|
+
<tr key={keyOf(row)} data-selected={table.isSelected(keyOf(row)) ? 'true' : undefined}>
|
|
125
|
+
{selectable && (
|
|
126
|
+
<td data-part="select-col">
|
|
127
|
+
<input
|
|
128
|
+
type="checkbox"
|
|
129
|
+
checked={table.isSelected(keyOf(row))}
|
|
130
|
+
aria-label={`${selectRowLabel} ${keyOf(row)}`}
|
|
131
|
+
onChange={() => table.toggleSelect(keyOf(row))}
|
|
132
|
+
/>
|
|
133
|
+
</td>
|
|
134
|
+
)}
|
|
135
|
+
{columns.map(col => (
|
|
136
|
+
<td key={col.key}>{String(row[col.key] ?? '')}</td>
|
|
137
|
+
))}
|
|
138
|
+
</tr>
|
|
139
|
+
))}
|
|
140
|
+
</tbody>
|
|
141
|
+
</table>
|
|
142
|
+
</div>
|
|
143
|
+
|
|
144
|
+
{table.totalPages.value > 1 && (
|
|
145
|
+
<div data-part="pagination" data-layout="split">
|
|
146
|
+
<span data-part="meta">
|
|
147
|
+
{`${pageLabel} ${table.page.value} / ${table.totalPages.value}`}
|
|
148
|
+
</span>
|
|
149
|
+
<div data-layout="cluster">
|
|
150
|
+
<button
|
|
151
|
+
data-ui="button"
|
|
152
|
+
data-variant="outline"
|
|
153
|
+
data-size="sm"
|
|
154
|
+
type="button"
|
|
155
|
+
disabled={table.page.value <= 1}
|
|
156
|
+
onClick={() => { table.page.value-- }}
|
|
157
|
+
>
|
|
158
|
+
{prevLabel}
|
|
159
|
+
</button>
|
|
160
|
+
<button
|
|
161
|
+
data-ui="button"
|
|
162
|
+
data-variant="outline"
|
|
163
|
+
data-size="sm"
|
|
164
|
+
type="button"
|
|
165
|
+
disabled={table.page.value >= table.totalPages.value}
|
|
166
|
+
onClick={() => { table.page.value++ }}
|
|
167
|
+
>
|
|
168
|
+
{nextLabel}
|
|
169
|
+
</button>
|
|
170
|
+
</div>
|
|
171
|
+
</div>
|
|
172
|
+
)}
|
|
173
|
+
</div>
|
|
174
|
+
)
|
|
175
|
+
}
|