@prunellejs/ui 0.0.0-stage → 0.5.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.
Files changed (70) hide show
  1. package/llms.txt +76 -0
  2. package/package.json +32 -4
  3. package/src/CommandPalette.tsx +257 -0
  4. package/src/DataTable.tsx +165 -0
  5. package/src/Dialog.tsx +165 -0
  6. package/src/Drawer.tsx +4 -0
  7. package/src/Menu.tsx +115 -0
  8. package/src/Select.tsx +114 -0
  9. package/src/Tabs.tsx +63 -0
  10. package/src/Toaster.tsx +31 -0
  11. package/src/behaviors.ts +287 -0
  12. package/src/field-context.ts +21 -0
  13. package/src/index.ts +72 -0
  14. package/src/platform-fallback.ts +36 -0
  15. package/src/plugin.ts +169 -0
  16. package/src/primitives.tsx +237 -0
  17. package/src/static.tsx +319 -0
  18. package/src/styles/bare.css +24 -0
  19. package/src/styles/base.css +55 -0
  20. package/src/styles/components/accordion.css +10 -0
  21. package/src/styles/components/badge.css +6 -0
  22. package/src/styles/components/button.css +11 -0
  23. package/src/styles/components/card.css +5 -0
  24. package/src/styles/components/checkbox.css +11 -0
  25. package/src/styles/components/command.css +25 -0
  26. package/src/styles/components/datatable.css +9 -0
  27. package/src/styles/components/dialog.css +16 -0
  28. package/src/styles/components/drawer.css +29 -0
  29. package/src/styles/components/field.css +8 -0
  30. package/src/styles/components/layout.css +69 -0
  31. package/src/styles/components/menu.css +21 -0
  32. package/src/styles/components/notice.css +5 -0
  33. package/src/styles/components/popover.css +8 -0
  34. package/src/styles/components/progress.css +14 -0
  35. package/src/styles/components/radio-group.css +11 -0
  36. package/src/styles/components/select.css +25 -0
  37. package/src/styles/components/switch.css +42 -0
  38. package/src/styles/components/tabs.css +4 -0
  39. package/src/styles/components/toast.css +28 -0
  40. package/src/styles/grammars/console.css +178 -0
  41. package/src/styles/grammars/editorial.css +163 -0
  42. package/src/styles/grammars/orbit.css +268 -0
  43. package/src/styles/grammars/prunelle.css +449 -0
  44. package/src/styles/grammars/soft.css +160 -0
  45. package/src/styles/grammars/workshop.css +171 -0
  46. package/src/styles/scan.ts +137 -0
  47. package/src/styles/split.ts +381 -0
  48. package/src/styles/theme.css +21 -0
  49. package/src/styles/themed/accordion.css +20 -0
  50. package/src/styles/themed/badge.css +20 -0
  51. package/src/styles/themed/button.css +21 -0
  52. package/src/styles/themed/card.css +23 -0
  53. package/src/styles/themed/checkbox.css +4 -0
  54. package/src/styles/themed/command.css +33 -0
  55. package/src/styles/themed/datatable.css +28 -0
  56. package/src/styles/themed/dialog.css +13 -0
  57. package/src/styles/themed/drawer.css +13 -0
  58. package/src/styles/themed/field.css +22 -0
  59. package/src/styles/themed/menu.css +21 -0
  60. package/src/styles/themed/notice.css +17 -0
  61. package/src/styles/themed/popover.css +11 -0
  62. package/src/styles/themed/radio-group.css +12 -0
  63. package/src/styles/themed/select.css +32 -0
  64. package/src/styles/themed/switch.css +18 -0
  65. package/src/styles/themed/tabs.css +17 -0
  66. package/src/styles/themed/toast.css +18 -0
  67. package/src/table-state.ts +126 -0
  68. package/src/tabs-state.ts +39 -0
  69. package/src/toast.ts +68 -0
  70. package/README.md +0 -3
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
@@ -0,0 +1,4 @@
1
+ // Accessible native `<dialog>` side/bottom Drawer primitive (re-exported from shared Dialog/ModalDialogSurface module).
2
+
3
+ export { ControlledDrawer, Drawer } from './Dialog'
4
+ export type { ControlledDrawerProps, DrawerProps } from './Dialog'
package/src/Menu.tsx ADDED
@@ -0,0 +1,115 @@
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, nextUiId } 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
+ popup.close(item.href === undefined)
37
+ if (navigateHref && item.href !== undefined && typeof location !== 'undefined')
38
+ location.assign(item.href)
39
+ }
40
+
41
+ const popup = createListPopup<MenuItemDef>({
42
+ items: () => props.items,
43
+ itemId,
44
+ isDisabled: it => Boolean(it.disabled),
45
+ onSelect: active => activate(active, true),
46
+ focusActive: true,
47
+ })
48
+
49
+ return (
50
+ <div
51
+ data-ui="menu"
52
+ data-state={popup.open.value ? 'open' : 'closed'}
53
+ ref={(el) => { popup.setRoot(el) }}
54
+ >
55
+ <button
56
+ id={triggerId}
57
+ data-ui="button"
58
+ data-variant={variant}
59
+ data-part="trigger"
60
+ type="button"
61
+ aria-haspopup="menu"
62
+ aria-expanded={popup.open.value ? 'true' : 'false'}
63
+ aria-controls={popup.open.value ? menuId : undefined}
64
+ onClick={e => popup.toggle(e)}
65
+ onKeyDown={e => popup.onTriggerKeyDown(e)}
66
+ >
67
+ {label}
68
+ </button>
69
+ {popup.open.value && (
70
+ <div
71
+ id={menuId}
72
+ data-part="content"
73
+ role="menu"
74
+ aria-labelledby={triggerId}
75
+ popover="auto"
76
+ ref={popup.mountPopup}
77
+ >
78
+ {props.items.map((item, idx) => (
79
+ item.href !== undefined && !item.disabled
80
+ ? (
81
+ <a
82
+ key={item.id}
83
+ id={itemId(idx)}
84
+ data-part="item"
85
+ data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
86
+ role="menuitem"
87
+ href={item.href}
88
+ tabindex={-1}
89
+ onClick={() => activate(item, false)}
90
+ >
91
+ {item.label}
92
+ </a>
93
+ )
94
+ : (
95
+ <button
96
+ key={item.id}
97
+ id={itemId(idx)}
98
+ data-part="item"
99
+ data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
100
+ role="menuitem"
101
+ type="button"
102
+ tabindex={-1}
103
+ disabled={item.disabled}
104
+ aria-disabled={item.disabled ? 'true' : undefined}
105
+ onClick={() => activate(item, false)}
106
+ >
107
+ {item.label}
108
+ </button>
109
+ )
110
+ ))}
111
+ </div>
112
+ )}
113
+ </div>
114
+ )
115
+ }
package/src/Select.tsx ADDED
@@ -0,0 +1,114 @@
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 { 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
+ let triggerId = 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
+ return (
55
+ <div
56
+ data-ui="select"
57
+ data-state={popup.open.value ? 'open' : 'closed'}
58
+ ref={(el) => {
59
+ popup.setRoot(el)
60
+ if (!props.id && !field)
61
+ triggerId = el.closest('[data-ui="field"]')?.querySelector('label')?.htmlFor || triggerId
62
+ }}
63
+ >
64
+ {name && <input type="hidden" name={name} value={selected.value} />}
65
+ <button
66
+ id={triggerId}
67
+ data-part="trigger"
68
+ type="button"
69
+ role="combobox"
70
+ aria-label={ariaLabel}
71
+ aria-haspopup="listbox"
72
+ aria-expanded={popup.open.value ? 'true' : 'false'}
73
+ aria-controls={popup.open.value ? listboxId : undefined}
74
+ aria-activedescendant={popup.activeDescendantId()}
75
+ onClick={e => popup.toggle(e)}
76
+ onKeyDown={e => popup.onTriggerKeyDown(e)}
77
+ >
78
+ <span data-part="value">{currentLabel()}</span>
79
+ <span aria-hidden="true">▾</span>
80
+ </button>
81
+ {popup.open.value && (
82
+ <div
83
+ id={listboxId}
84
+ data-part="listbox"
85
+ role="listbox"
86
+ aria-label={ariaLabel}
87
+ aria-labelledby={ariaLabel ? undefined : triggerId}
88
+ popover="auto"
89
+ ref={popup.mountPopup}
90
+ >
91
+ {props.options.map((opt, idx) => (
92
+ <button
93
+ key={opt.value}
94
+ id={optionId(idx)}
95
+ data-part="option"
96
+ data-selected={selected.value === opt.value ? 'true' : undefined}
97
+ data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
98
+ role="option"
99
+ type="button"
100
+ tabindex={-1}
101
+ disabled={opt.disabled}
102
+ aria-disabled={opt.disabled ? 'true' : undefined}
103
+ aria-selected={selected.value === opt.value ? 'true' : 'false'}
104
+ onClick={() => pick(opt)}
105
+ >
106
+ <span>{opt.label}</span>
107
+ <span aria-hidden="true">{selected.value === opt.value ? '✓' : ''}</span>
108
+ </button>
109
+ ))}
110
+ </div>
111
+ )}
112
+ </div>
113
+ )
114
+ }
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
+ }
@@ -0,0 +1,31 @@
1
+ /** @jsxImportSource prunelle */
2
+ // Live-region Toast stack driven by the reactive `toast` store.
3
+
4
+ import { toast } from './toast'
5
+
6
+ export interface ToasterProps {
7
+ dismissLabel?: string | undefined
8
+ }
9
+
10
+ export function Toaster({ dismissLabel = 'Dismiss' }: ToasterProps = {}) {
11
+ return (
12
+ <div data-ui="toaster" aria-live="polite" aria-atomic="false">
13
+ {toast.items.value.map(item => (
14
+ <div key={item.id} data-ui="toast" data-tone={item.tone} role="status">
15
+ <div>
16
+ <strong data-part="title">{item.title}</strong>
17
+ {item.description && <p data-part="description">{item.description}</p>}
18
+ </div>
19
+ <button
20
+ data-part="close"
21
+ type="button"
22
+ aria-label={dismissLabel}
23
+ onClick={() => toast.dismiss(item.id)}
24
+ >
25
+ ✕
26
+ </button>
27
+ </div>
28
+ ))}
29
+ </div>
30
+ )
31
+ }