@prunellejs/ui 0.0.0-stage → 0.5.1
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 +257 -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 +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
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Zero-JS static & native-platform primitives (`0 KB` client JS!) split from static.tsx so islands using only Field/Button/Notice don't bundle unused templates.
|
|
3
|
+
|
|
4
|
+
import type { PrunelleChild } from 'prunelle'
|
|
5
|
+
import type { ButtonVariant, SpaceValue, Tone, TrackValue } from './static'
|
|
6
|
+
|
|
7
|
+
const SPACE_TOKENS = new Set<string>(['none', '2xs', 'xs', 'sm', 'md', 'lg', 'xl', '2xl'])
|
|
8
|
+
const TRACK_TOKENS = new Set<string>(['xs', 'sm', 'md', 'lg', 'xl'])
|
|
9
|
+
const isSpaceToken = (v: string | undefined): boolean => v !== undefined && SPACE_TOKENS.has(v)
|
|
10
|
+
const isTrackToken = (v: string | undefined): boolean => v !== undefined && TRACK_TOKENS.has(v)
|
|
11
|
+
|
|
12
|
+
export function Badge(props: BoxProps & { tone?: Tone | undefined }) {
|
|
13
|
+
const rest = restProps(props, ['tone', 'class'])
|
|
14
|
+
return <span data-ui="badge" data-tone={props.tone} class={props.class} {...rest}>{props.children}</span>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function Grid(props: BoxProps & { min?: TrackValue | undefined, gap?: SpaceValue | undefined }) {
|
|
18
|
+
const rest = restProps(props, ['min', 'gap', 'class'])
|
|
19
|
+
return (
|
|
20
|
+
<div
|
|
21
|
+
data-layout="grid"
|
|
22
|
+
data-min={isTrackToken(props.min) ? props.min : undefined}
|
|
23
|
+
data-min-value={props.min && !isTrackToken(props.min) ? props.min : undefined}
|
|
24
|
+
data-gap={isSpaceToken(props.gap) ? props.gap : undefined}
|
|
25
|
+
data-gap-value={props.gap && !isSpaceToken(props.gap) ? props.gap : undefined}
|
|
26
|
+
class={props.class}
|
|
27
|
+
{...rest}
|
|
28
|
+
>
|
|
29
|
+
{props.children}
|
|
30
|
+
</div>
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function Sidebar(props: BoxProps & { side?: TrackValue | undefined, gap?: SpaceValue | undefined }) {
|
|
35
|
+
const rest = restProps(props, ['side', 'gap', 'class'])
|
|
36
|
+
return (
|
|
37
|
+
<div
|
|
38
|
+
data-layout="sidebar"
|
|
39
|
+
data-side={isTrackToken(props.side) ? props.side : undefined}
|
|
40
|
+
data-side-value={props.side && !isTrackToken(props.side) ? props.side : undefined}
|
|
41
|
+
data-gap={isSpaceToken(props.gap) ? props.gap : undefined}
|
|
42
|
+
data-gap-value={props.gap && !isSpaceToken(props.gap) ? props.gap : undefined}
|
|
43
|
+
class={props.class}
|
|
44
|
+
{...rest}
|
|
45
|
+
>
|
|
46
|
+
{props.children}
|
|
47
|
+
</div>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function Split(props: BoxProps & { gap?: SpaceValue | undefined }) {
|
|
52
|
+
const rest = restProps(props, ['gap', 'class'])
|
|
53
|
+
return (
|
|
54
|
+
<div
|
|
55
|
+
data-layout="split"
|
|
56
|
+
data-gap={isSpaceToken(props.gap) ? props.gap : undefined}
|
|
57
|
+
data-gap-value={props.gap && !isSpaceToken(props.gap) ? props.gap : undefined}
|
|
58
|
+
class={props.class}
|
|
59
|
+
{...rest}
|
|
60
|
+
>
|
|
61
|
+
{props.children}
|
|
62
|
+
</div>
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface BoxProps {
|
|
67
|
+
class?: string | undefined
|
|
68
|
+
children?: PrunelleChild | undefined
|
|
69
|
+
[attr: string]: unknown
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function restProps(props: Record<string, unknown>, skip: readonly string[]): Record<string, unknown> {
|
|
73
|
+
const out: Record<string, unknown> = {}
|
|
74
|
+
for (const k of Object.keys(props)) {
|
|
75
|
+
if (k !== 'children' && !skip.includes(k))
|
|
76
|
+
out[k] = props[k]
|
|
77
|
+
}
|
|
78
|
+
return out
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function Card(props: BoxProps) {
|
|
82
|
+
const rest = restProps(props, ['class'])
|
|
83
|
+
return <section data-ui="card" class={props.class} {...rest}>{props.children}</section>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function CardHeader(props: BoxProps) {
|
|
87
|
+
const rest = restProps(props, ['class'])
|
|
88
|
+
return <header data-part="header" class={props.class} {...rest}>{props.children}</header>
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function CardBody(props: BoxProps) {
|
|
92
|
+
const rest = restProps(props, ['class'])
|
|
93
|
+
return <div data-part="body" class={props.class} {...rest}>{props.children}</div>
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function CardFooter(props: BoxProps) {
|
|
97
|
+
const rest = restProps(props, ['class'])
|
|
98
|
+
return <footer data-part="footer" class={props.class} {...rest}>{props.children}</footer>
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Zero-JS native `<details>` accordion (`name` enables exclusive mode natively in the browser). */
|
|
102
|
+
export function Accordion(props: BoxProps) {
|
|
103
|
+
const rest = restProps(props, ['class'])
|
|
104
|
+
return <div data-ui="accordion" class={props.class} {...rest}>{props.children}</div>
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function AccordionItem(props: { title: string, group?: string | undefined, open?: boolean | undefined, children?: PrunelleChild | undefined, [attr: string]: unknown }) {
|
|
108
|
+
const rest = restProps(props, ['title', 'group', 'open'])
|
|
109
|
+
return (
|
|
110
|
+
<details name={props.group} open={props.open} {...rest}>
|
|
111
|
+
<summary data-part="trigger">{props.title}</summary>
|
|
112
|
+
<div data-part="content">{props.children}</div>
|
|
113
|
+
</details>
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Zero-JS native Popover API trigger + anchored popover panel (`0 KB` client JS!). */
|
|
118
|
+
export interface PopoverProps {
|
|
119
|
+
id: string
|
|
120
|
+
triggerLabel: string
|
|
121
|
+
triggerVariant?: ButtonVariant | undefined
|
|
122
|
+
children?: PrunelleChild | undefined
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function Popover({ id, triggerLabel, triggerVariant = 'outline', children }: PopoverProps) {
|
|
126
|
+
return (
|
|
127
|
+
<>
|
|
128
|
+
<button
|
|
129
|
+
data-ui="button"
|
|
130
|
+
data-variant={triggerVariant}
|
|
131
|
+
type="button"
|
|
132
|
+
popovertarget={id}
|
|
133
|
+
>
|
|
134
|
+
{triggerLabel}
|
|
135
|
+
</button>
|
|
136
|
+
<div
|
|
137
|
+
id={id}
|
|
138
|
+
data-ui="popover"
|
|
139
|
+
popover="auto"
|
|
140
|
+
>
|
|
141
|
+
{children}
|
|
142
|
+
</div>
|
|
143
|
+
</>
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ---- Zero-JS native form controls (`0 KB` client JS!) ----
|
|
148
|
+
|
|
149
|
+
export interface CheckControlProps {
|
|
150
|
+
id?: string | undefined
|
|
151
|
+
name?: string | undefined
|
|
152
|
+
value?: string | undefined
|
|
153
|
+
label: string
|
|
154
|
+
checked?: boolean | undefined
|
|
155
|
+
disabled?: boolean | undefined
|
|
156
|
+
class?: string | undefined
|
|
157
|
+
[attr: string]: unknown
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function Checkbox({ id, name, value, label, checked, disabled, class: cls, ...rest }: CheckControlProps) {
|
|
161
|
+
return (
|
|
162
|
+
<label data-ui="checkbox" data-disabled={disabled ? 'true' : undefined} class={cls}>
|
|
163
|
+
<input id={id} type="checkbox" name={name} value={value} checked={checked} disabled={disabled} {...rest} />
|
|
164
|
+
<span data-part="label">{label}</span>
|
|
165
|
+
</label>
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function Switch({ id, name, value, label, checked, disabled, class: cls, ...rest }: CheckControlProps) {
|
|
170
|
+
return (
|
|
171
|
+
<label data-ui="switch" data-disabled={disabled ? 'true' : undefined} class={cls}>
|
|
172
|
+
<input id={id} type="checkbox" role="switch" name={name} value={value} checked={checked} disabled={disabled} {...rest} />
|
|
173
|
+
<span data-part="track" aria-hidden="true" />
|
|
174
|
+
<span data-part="label">{label}</span>
|
|
175
|
+
</label>
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface RadioOption {
|
|
180
|
+
value: string
|
|
181
|
+
label: string
|
|
182
|
+
hint?: string | undefined
|
|
183
|
+
disabled?: boolean | undefined
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export interface RadioGroupProps {
|
|
187
|
+
name: string
|
|
188
|
+
legend?: string | undefined
|
|
189
|
+
options: RadioOption[]
|
|
190
|
+
defaultValue?: string | undefined
|
|
191
|
+
class?: string | undefined
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function RadioGroup({ name, legend, options, defaultValue, class: cls }: RadioGroupProps) {
|
|
195
|
+
return (
|
|
196
|
+
<fieldset data-ui="radio-group" class={cls}>
|
|
197
|
+
{legend && <legend data-part="legend">{legend}</legend>}
|
|
198
|
+
<div data-part="options">
|
|
199
|
+
{options.map(opt => (
|
|
200
|
+
<label key={opt.value} data-part="option" data-disabled={opt.disabled ? 'true' : undefined}>
|
|
201
|
+
<input
|
|
202
|
+
type="radio"
|
|
203
|
+
name={name}
|
|
204
|
+
value={opt.value}
|
|
205
|
+
checked={defaultValue === opt.value ? true : undefined}
|
|
206
|
+
disabled={opt.disabled}
|
|
207
|
+
/>
|
|
208
|
+
<span data-part="label">{opt.label}</span>
|
|
209
|
+
{opt.hint && <span data-part="hint">{opt.hint}</span>}
|
|
210
|
+
</label>
|
|
211
|
+
))}
|
|
212
|
+
</div>
|
|
213
|
+
</fieldset>
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface ProgressProps {
|
|
218
|
+
label: string
|
|
219
|
+
/** Without one the bar is indeterminate: a loader. */
|
|
220
|
+
value?: number | undefined
|
|
221
|
+
max?: number | undefined
|
|
222
|
+
class?: string | undefined
|
|
223
|
+
[attr: string]: unknown
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// The share done, rounded to the 5% step a grammar styles: a style attribute would break a CSP without 'unsafe-inline'.
|
|
227
|
+
const progressStep = (value: number, max: number): string => String(Math.round(Math.min(Math.max(value / max, 0), 1) * 20) * 5)
|
|
228
|
+
|
|
229
|
+
export function Progress({ label, value, max = 100, class: cls, ...rest }: ProgressProps) {
|
|
230
|
+
return (
|
|
231
|
+
<span data-ui="progress" data-value={value === undefined ? undefined : progressStep(value, max)} class={cls} {...rest}>
|
|
232
|
+
<span data-part="eye" aria-hidden="true" />
|
|
233
|
+
<progress value={value} max={value === undefined ? undefined : max} aria-label={label} />
|
|
234
|
+
<span data-part="label" aria-hidden="true">{label}</span>
|
|
235
|
+
</span>
|
|
236
|
+
)
|
|
237
|
+
}
|
package/src/static.tsx
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/** @jsxImportSource prunelle */
|
|
2
|
+
// Zero-JS static & native-platform primitives (`0 KB` client JS!).
|
|
3
|
+
// Emit only semantic `data-ui`, `data-part`, `data-variant`, `data-tone`, `data-layout` attributes.
|
|
4
|
+
|
|
5
|
+
import type { Context, Html, PrunelleChild } from 'prunelle'
|
|
6
|
+
import type { FieldControlWiring } from './field-context'
|
|
7
|
+
import { provideContext } from 'prunelle'
|
|
8
|
+
import { FIELD_CONTEXT_ID } from './field-context'
|
|
9
|
+
|
|
10
|
+
export type { FieldControlWiring } from './field-context'
|
|
11
|
+
export { useFieldContext } from './field-context'
|
|
12
|
+
|
|
13
|
+
export const FieldContext: Context<FieldControlWiring | undefined> = {
|
|
14
|
+
id: FIELD_CONTEXT_ID,
|
|
15
|
+
defaultValue: undefined,
|
|
16
|
+
Provider(props) {
|
|
17
|
+
return provideContext(FieldContext, props.value, () => {
|
|
18
|
+
const c = props.children
|
|
19
|
+
return typeof c === 'function' ? (c as () => PrunelleChild)() : c
|
|
20
|
+
})
|
|
21
|
+
},
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function escAttr(value: string): string {
|
|
25
|
+
return value.replaceAll('&', '&').replaceAll('"', '"').replaceAll('<', '<').replaceAll('>', '>')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type Tone = 'neutral' | 'accent' | 'success' | 'warning' | 'danger'
|
|
29
|
+
export type ButtonVariant = 'solid' | 'outline' | 'ghost'
|
|
30
|
+
export type Size = 'sm' | 'md' | 'lg'
|
|
31
|
+
|
|
32
|
+
interface BoxProps {
|
|
33
|
+
class?: string | undefined
|
|
34
|
+
children?: PrunelleChild | undefined
|
|
35
|
+
[attr: string]: unknown
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ButtonProps {
|
|
39
|
+
variant?: ButtonVariant | undefined
|
|
40
|
+
tone?: Tone | undefined
|
|
41
|
+
size?: Size | undefined
|
|
42
|
+
href?: string | undefined
|
|
43
|
+
type?: 'button' | 'submit' | 'reset' | undefined
|
|
44
|
+
disabled?: boolean | undefined
|
|
45
|
+
class?: string | undefined
|
|
46
|
+
children?: PrunelleChild | undefined
|
|
47
|
+
[attr: string]: unknown
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function restProps(props: Record<string, unknown>, skip: readonly string[]): Record<string, unknown> {
|
|
51
|
+
const out: Record<string, unknown> = {}
|
|
52
|
+
for (const k of Object.keys(props)) {
|
|
53
|
+
if (k !== 'children' && !skip.includes(k))
|
|
54
|
+
out[k] = props[k]
|
|
55
|
+
}
|
|
56
|
+
return out
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function Button(props: ButtonProps) {
|
|
60
|
+
const rest = restProps(props as unknown as Record<string, unknown>, ['variant', 'tone', 'size', 'href', 'type', 'disabled', 'class'])
|
|
61
|
+
if (props.href !== undefined) {
|
|
62
|
+
return (
|
|
63
|
+
<a data-ui="button" data-variant={props.variant ?? 'solid'} data-tone={props.tone} data-size={props.size ?? 'md'} href={props.href} class={props.class} {...rest}>
|
|
64
|
+
{props.children}
|
|
65
|
+
</a>
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
return (
|
|
69
|
+
<button data-ui="button" data-variant={props.variant ?? 'solid'} data-tone={props.tone} data-size={props.size ?? 'md'} type={props.type ?? 'button'} disabled={props.disabled} class={props.class} {...rest}>
|
|
70
|
+
{props.children}
|
|
71
|
+
</button>
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function Notice(props: BoxProps & { tone?: Tone | undefined, role?: 'status' | 'alert' | 'note' | undefined }) {
|
|
76
|
+
const rest = restProps(props, ['tone', 'role', 'class'])
|
|
77
|
+
return <div data-ui="notice" data-tone={props.tone} role={props.role ?? 'status'} class={props.class} {...rest}>{props.children}</div>
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Zero-JS form field wrapper that auto-wires `for`, `aria-describedby`, `aria-invalid`, hints, and action errors. */
|
|
81
|
+
export interface FieldProps {
|
|
82
|
+
id: string
|
|
83
|
+
label: string
|
|
84
|
+
hint?: string | undefined
|
|
85
|
+
error?: string | undefined
|
|
86
|
+
required?: boolean | undefined
|
|
87
|
+
class?: string | undefined
|
|
88
|
+
children?: PrunelleChild | undefined
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function mergeTokenAttr(existing: string | null | undefined, addition: string): string {
|
|
92
|
+
const tokens = (existing ?? '').split(/\s+/).filter(Boolean)
|
|
93
|
+
for (const tok of addition.split(/\s+/).filter(Boolean)) {
|
|
94
|
+
if (!tokens.includes(tok))
|
|
95
|
+
tokens.push(tok)
|
|
96
|
+
}
|
|
97
|
+
return tokens.join(' ')
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface ScannedTag {
|
|
101
|
+
start: number
|
|
102
|
+
end: number
|
|
103
|
+
tag: string
|
|
104
|
+
rawAttrs: string
|
|
105
|
+
selfClose: string
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const OPEN_TAG_RE = /<([a-zA-Z][\w-]*)(?=[\s/>])((?:"[^"]*"|'[^']*'|[^>"'])*)>/g
|
|
109
|
+
|
|
110
|
+
/** Quote-aware HTML opening tag scanner so `>` inside attribute values (e.g. `placeholder="x > 0"`) never truncates a tag. */
|
|
111
|
+
function scanOpeningTags(html: string): ScannedTag[] {
|
|
112
|
+
const tags: ScannedTag[] = []
|
|
113
|
+
OPEN_TAG_RE.lastIndex = 0
|
|
114
|
+
let m: RegExpExecArray | null
|
|
115
|
+
while ((m = OPEN_TAG_RE.exec(html)) !== null) {
|
|
116
|
+
const body = m[2]!
|
|
117
|
+
const selfClose = body.endsWith('/') ? '/' : ''
|
|
118
|
+
tags.push({
|
|
119
|
+
start: m.index,
|
|
120
|
+
end: m.index + m[0].length,
|
|
121
|
+
tag: m[1]!,
|
|
122
|
+
rawAttrs: selfClose ? body.slice(0, -1) : body,
|
|
123
|
+
selfClose,
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
return tags
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const NATIVE_CTRL_RE = /^(?:input|textarea|select)$/i
|
|
130
|
+
|
|
131
|
+
function wireFieldHtml(html: string, wiring: FieldControlWiring): string {
|
|
132
|
+
const tags = scanOpeningTags(html)
|
|
133
|
+
const target = tags.find(t => NATIVE_CTRL_RE.test(t.tag) && !(/\btype\s*=\s*(?:"hidden"|'hidden'|hidden\b)/i.test(t.rawAttrs) && /^input$/i.test(t.tag)))
|
|
134
|
+
?? tags.find(t => /\brole\s*=\s*["'](?:combobox|textbox|listbox|spinbutton)["']/i.test(t.rawAttrs))
|
|
135
|
+
if (target === undefined)
|
|
136
|
+
return html
|
|
137
|
+
|
|
138
|
+
let attrs = target.rawAttrs
|
|
139
|
+
if (!/\bid\s*=/i.test(attrs))
|
|
140
|
+
attrs += ` id="${escAttr(wiring.id)}"`
|
|
141
|
+
else if (/\bid\s*=\s*"kui-[^"]*"/i.test(attrs))
|
|
142
|
+
attrs = attrs.replace(/\bid\s*=\s*"kui-[^"]*"/i, `id="${escAttr(wiring.id)}"`)
|
|
143
|
+
|
|
144
|
+
if (wiring.describedBy) {
|
|
145
|
+
const descRe = /(\baria-describedby\s*=\s*")([^"]*)(")/i
|
|
146
|
+
attrs = descRe.test(attrs)
|
|
147
|
+
? attrs.replace(descRe, (_, p1: string, cur: string, p3: string) => `${p1}${escAttr(mergeTokenAttr(cur, wiring.describedBy!))}${p3}`)
|
|
148
|
+
: `${attrs} aria-describedby="${escAttr(wiring.describedBy)}"`
|
|
149
|
+
}
|
|
150
|
+
if (wiring.invalid && !/\baria-invalid\s*=/i.test(attrs))
|
|
151
|
+
attrs += ' aria-invalid="true"'
|
|
152
|
+
if (wiring.required) {
|
|
153
|
+
if (NATIVE_CTRL_RE.test(target.tag) && !/\brequired(?:\s|=|$)/i.test(attrs))
|
|
154
|
+
attrs += ' required'
|
|
155
|
+
if (!/\baria-required\s*=/i.test(attrs))
|
|
156
|
+
attrs += ' aria-required="true"'
|
|
157
|
+
}
|
|
158
|
+
return `${html.slice(0, target.start)}<${target.tag}${attrs}${target.selfClose}>${html.slice(target.end)}`
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const CTRL_SEL = 'input:not([type="hidden"]),textarea,select,[role="combobox"],[role="textbox"],[role="listbox"],[role="spinbutton"]'
|
|
162
|
+
|
|
163
|
+
function syncFieldAria(el: Element, id: string, err: string | undefined, hint: string | undefined, clearInvalid: boolean): void {
|
|
164
|
+
const errId = `${id}-error`
|
|
165
|
+
const hintId = `${id}-hint`
|
|
166
|
+
const tokens = (el.getAttribute('aria-describedby') ?? '').split(/\s+/).filter(t => t && t !== errId && t !== hintId)
|
|
167
|
+
if (err)
|
|
168
|
+
tokens.push(errId)
|
|
169
|
+
else if (hint)
|
|
170
|
+
tokens.push(hintId)
|
|
171
|
+
if (tokens.length > 0)
|
|
172
|
+
el.setAttribute('aria-describedby', tokens.join(' '))
|
|
173
|
+
else
|
|
174
|
+
el.removeAttribute('aria-describedby')
|
|
175
|
+
if (err)
|
|
176
|
+
el.setAttribute('aria-invalid', 'true')
|
|
177
|
+
else if (clearInvalid)
|
|
178
|
+
el.removeAttribute('aria-invalid')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function wireFieldElement(el: Element, wiring: FieldControlWiring, err: string | undefined, hint: string | undefined): void {
|
|
182
|
+
const curId = el.getAttribute('id')
|
|
183
|
+
if (!curId || curId.startsWith('kui-'))
|
|
184
|
+
el.setAttribute('id', wiring.id)
|
|
185
|
+
syncFieldAria(el, wiring.id, err, hint, false)
|
|
186
|
+
if (wiring.required) {
|
|
187
|
+
if (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.tagName === 'SELECT')
|
|
188
|
+
el.setAttribute('required', '')
|
|
189
|
+
if (!el.hasAttribute('aria-required'))
|
|
190
|
+
el.setAttribute('aria-required', 'true')
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function findFieldControlInNode(node: Node): Element | null {
|
|
195
|
+
if (node.nodeType === 1) {
|
|
196
|
+
const el = node as Element
|
|
197
|
+
return el.matches(CTRL_SEL) ? el : el.querySelector(CTRL_SEL)
|
|
198
|
+
}
|
|
199
|
+
return node.nodeType === 11 ? (node as DocumentFragment).querySelector(CTRL_SEL) : null
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const RAW_HTML_BRAND = Symbol.for('pru.html')
|
|
203
|
+
|
|
204
|
+
function wireFieldChildren(
|
|
205
|
+
children: PrunelleChild,
|
|
206
|
+
wiring: FieldControlWiring,
|
|
207
|
+
err: string | undefined,
|
|
208
|
+
hint: string | undefined,
|
|
209
|
+
onControl: (el: Element) => void,
|
|
210
|
+
state: { done: boolean } = { done: false },
|
|
211
|
+
): PrunelleChild {
|
|
212
|
+
if (state.done || children == null || typeof children !== 'object')
|
|
213
|
+
return children
|
|
214
|
+
if (Array.isArray(children))
|
|
215
|
+
return children.map(child => (state.done ? child : wireFieldChildren(child, wiring, err, hint, onControl, state)))
|
|
216
|
+
if ('__html' in children && (children as unknown as Record<symbol, unknown>)[RAW_HTML_BRAND] === true) {
|
|
217
|
+
const src = (children as Html).__html
|
|
218
|
+
const patched = wireFieldHtml(src, wiring)
|
|
219
|
+
if (patched !== src) {
|
|
220
|
+
state.done = true
|
|
221
|
+
return { [RAW_HTML_BRAND]: true, __html: patched } as unknown as Html
|
|
222
|
+
}
|
|
223
|
+
return children
|
|
224
|
+
}
|
|
225
|
+
if (typeof Node !== 'undefined' && children instanceof Node) {
|
|
226
|
+
const ctrl = findFieldControlInNode(children)
|
|
227
|
+
if (ctrl !== null) {
|
|
228
|
+
wireFieldElement(ctrl, wiring, err, hint)
|
|
229
|
+
onControl(ctrl)
|
|
230
|
+
state.done = true
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return children
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function Field(props: FieldProps) {
|
|
237
|
+
let ctrlEl: Element | null = null
|
|
238
|
+
let lastError: string | undefined
|
|
239
|
+
let ownedInvalid = false
|
|
240
|
+
|
|
241
|
+
const syncInvalid = (): 'true' | undefined => {
|
|
242
|
+
const err = props.error
|
|
243
|
+
lastError = err
|
|
244
|
+
if (typeof document !== 'undefined') {
|
|
245
|
+
const target = ctrlEl ?? document.getElementById(props.id)
|
|
246
|
+
if (target !== null && (target === ctrlEl || target.closest('[data-ui="field"]') !== null)) {
|
|
247
|
+
ctrlEl = target
|
|
248
|
+
syncFieldAria(target, props.id, err, props.hint, ownedInvalid)
|
|
249
|
+
ownedInvalid = Boolean(err)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return err ? 'true' : undefined
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return (
|
|
256
|
+
<div data-ui="field" data-invalid={syncInvalid()} class={props.class}>
|
|
257
|
+
<label data-part="label" for={props.id}>
|
|
258
|
+
{props.label}
|
|
259
|
+
{props.required && <span aria-hidden="true"> *</span>}
|
|
260
|
+
</label>
|
|
261
|
+
{(() => {
|
|
262
|
+
const wiring: FieldControlWiring = {
|
|
263
|
+
id: props.id,
|
|
264
|
+
describedBy: lastError ? `${props.id}-error` : props.hint ? `${props.id}-hint` : undefined,
|
|
265
|
+
invalid: Boolean(lastError),
|
|
266
|
+
required: Boolean(props.required),
|
|
267
|
+
}
|
|
268
|
+
return provideContext(FieldContext, wiring, () => wireFieldChildren(props.children, wiring, lastError, props.hint, (el) => {
|
|
269
|
+
ctrlEl = el
|
|
270
|
+
if (wiring.invalid)
|
|
271
|
+
ownedInvalid = true
|
|
272
|
+
}))
|
|
273
|
+
})()}
|
|
274
|
+
{props.hint && !props.error && <p data-part="hint" id={`${props.id}-hint`}>{props.hint}</p>}
|
|
275
|
+
{props.error && <p data-part="error" id={`${props.id}-error`} role="alert">{props.error}</p>}
|
|
276
|
+
</div>
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ---- Every Layout primitives (pure CSS, 0 KB JS, strict-CSP safe) ----
|
|
281
|
+
|
|
282
|
+
export type SpaceToken = 'none' | '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl'
|
|
283
|
+
export type TrackSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl'
|
|
284
|
+
export type SpaceValue = SpaceToken | (string & {})
|
|
285
|
+
export type TrackValue = TrackSize | (string & {})
|
|
286
|
+
|
|
287
|
+
const SPACE_TOKENS = new Set<string>(['none', '2xs', 'xs', 'sm', 'md', 'lg', 'xl', '2xl'])
|
|
288
|
+
|
|
289
|
+
const isSpaceToken = (v: string | undefined): boolean => v !== undefined && SPACE_TOKENS.has(v)
|
|
290
|
+
|
|
291
|
+
export function Stack(props: BoxProps & { gap?: SpaceValue | undefined }) {
|
|
292
|
+
const rest = restProps(props, ['gap', 'class'])
|
|
293
|
+
return (
|
|
294
|
+
<div
|
|
295
|
+
data-layout="stack"
|
|
296
|
+
data-gap={isSpaceToken(props.gap) ? props.gap : undefined}
|
|
297
|
+
data-gap-value={props.gap && !isSpaceToken(props.gap) ? props.gap : undefined}
|
|
298
|
+
class={props.class}
|
|
299
|
+
{...rest}
|
|
300
|
+
>
|
|
301
|
+
{props.children}
|
|
302
|
+
</div>
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function Cluster(props: BoxProps & { gap?: SpaceValue | undefined }) {
|
|
307
|
+
const rest = restProps(props, ['gap', 'class'])
|
|
308
|
+
return (
|
|
309
|
+
<div
|
|
310
|
+
data-layout="cluster"
|
|
311
|
+
data-gap={isSpaceToken(props.gap) ? props.gap : undefined}
|
|
312
|
+
data-gap-value={props.gap && !isSpaceToken(props.gap) ? props.gap : undefined}
|
|
313
|
+
class={props.class}
|
|
314
|
+
{...rest}
|
|
315
|
+
>
|
|
316
|
+
{props.children}
|
|
317
|
+
</div>
|
|
318
|
+
)
|
|
319
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* @prunellejs/ui bare floor: behavior, layout primitives, accessibility & top-layer positioning only.
|
|
2
|
+
* Everything sits inside @layer kui so Tailwind utilities and user CSS always win without !important. */
|
|
3
|
+
|
|
4
|
+
@import './base.css';
|
|
5
|
+
@import './components/layout.css';
|
|
6
|
+
@import './components/button.css';
|
|
7
|
+
@import './components/card.css';
|
|
8
|
+
@import './components/badge.css';
|
|
9
|
+
@import './components/notice.css';
|
|
10
|
+
@import './components/field.css';
|
|
11
|
+
@import './components/checkbox.css';
|
|
12
|
+
@import './components/switch.css';
|
|
13
|
+
@import './components/radio-group.css';
|
|
14
|
+
@import './components/progress.css';
|
|
15
|
+
@import './components/accordion.css';
|
|
16
|
+
@import './components/tabs.css';
|
|
17
|
+
@import './components/dialog.css';
|
|
18
|
+
@import './components/drawer.css';
|
|
19
|
+
@import './components/command.css';
|
|
20
|
+
@import './components/popover.css';
|
|
21
|
+
@import './components/menu.css';
|
|
22
|
+
@import './components/select.css';
|
|
23
|
+
@import './components/toast.css';
|
|
24
|
+
@import './components/datatable.css';
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/* @prunellejs/ui base floor: box-sizing, screen-reader utility, single focus indicator, reduced motion & [hidden].
|
|
2
|
+
* Everything sits inside @layer kui so Tailwind utilities and user CSS always win without !important. */
|
|
3
|
+
|
|
4
|
+
@layer kui {
|
|
5
|
+
[data-ui] *,
|
|
6
|
+
[data-ui] *::before,
|
|
7
|
+
[data-ui] *::after {
|
|
8
|
+
box-sizing: border-box;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.kui-sr-only {
|
|
12
|
+
position: absolute;
|
|
13
|
+
width: 1px;
|
|
14
|
+
height: 1px;
|
|
15
|
+
padding: 0;
|
|
16
|
+
margin: -1px;
|
|
17
|
+
overflow: hidden;
|
|
18
|
+
clip: rect(0, 0, 0, 0);
|
|
19
|
+
white-space: nowrap;
|
|
20
|
+
border: 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
[data-ui] :focus-visible,
|
|
24
|
+
[data-ui]:focus-visible {
|
|
25
|
+
outline: 2px solid var(--ui-focus-color, var(--ui-accent, currentColor));
|
|
26
|
+
outline-offset: 2px;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
@media (prefers-reduced-motion: reduce) {
|
|
30
|
+
:root {
|
|
31
|
+
--ui-motion: 0.01ms;
|
|
32
|
+
}
|
|
33
|
+
[data-ui],
|
|
34
|
+
[data-ui] *,
|
|
35
|
+
[data-ui] *::before,
|
|
36
|
+
[data-ui] *::after {
|
|
37
|
+
animation-duration: 0.01ms !important;
|
|
38
|
+
animation-iteration-count: 1 !important;
|
|
39
|
+
transition-duration: 0.01ms !important;
|
|
40
|
+
scroll-behavior: auto !important;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/* Touch-friendly 44px targets & iOS 16px input floor on phones */
|
|
45
|
+
@media (max-width: 600px) {
|
|
46
|
+
:root {
|
|
47
|
+
--ui-touch-min: 44px;
|
|
48
|
+
--ui-input-floor: max(1rem, 16px);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
[hidden] {
|
|
53
|
+
display: none !important;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
@layer kui {
|
|
2
|
+
[data-ui="checkbox"] {
|
|
3
|
+
display: inline-flex;
|
|
4
|
+
align-items: center;
|
|
5
|
+
gap: var(--ui-check-gap, 0.55rem);
|
|
6
|
+
min-block-size: var(--ui-touch-min, auto);
|
|
7
|
+
cursor: pointer;
|
|
8
|
+
user-select: none;
|
|
9
|
+
&[data-disabled="true"] { opacity: 0.45; cursor: not-allowed; }
|
|
10
|
+
}
|
|
11
|
+
}
|