@prunellejs/ui 0.0.0-stage → 0.7.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 (84) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +78 -0
  3. package/package.json +32 -4
  4. package/src/CommandPalette.tsx +262 -0
  5. package/src/DataTable.tsx +244 -0
  6. package/src/Dialog.tsx +181 -0
  7. package/src/Drawer.tsx +4 -0
  8. package/src/Menu.tsx +117 -0
  9. package/src/Select.tsx +125 -0
  10. package/src/Tabs.tsx +64 -0
  11. package/src/Toaster.tsx +105 -0
  12. package/src/behaviors.ts +385 -0
  13. package/src/field-context.ts +21 -0
  14. package/src/fold.ts +7 -0
  15. package/src/index.ts +72 -0
  16. package/src/labels/dialog.ts +3 -0
  17. package/src/labels/fr/dialog.ts +3 -0
  18. package/src/labels/fr/palette.ts +8 -0
  19. package/src/labels/fr/select.ts +1 -0
  20. package/src/labels/fr/table.ts +13 -0
  21. package/src/labels/fr/toaster.ts +1 -0
  22. package/src/labels/palette.ts +8 -0
  23. package/src/labels/select.ts +1 -0
  24. package/src/labels/table.ts +13 -0
  25. package/src/labels/toaster.ts +1 -0
  26. package/src/platform-fallback.ts +36 -0
  27. package/src/plugin.ts +188 -0
  28. package/src/primitives.tsx +238 -0
  29. package/src/static.tsx +324 -0
  30. package/src/styles/bare.css +25 -0
  31. package/src/styles/base.css +61 -0
  32. package/src/styles/components/accordion.css +10 -0
  33. package/src/styles/components/badge.css +6 -0
  34. package/src/styles/components/button.css +11 -0
  35. package/src/styles/components/card.css +5 -0
  36. package/src/styles/components/checkbox.css +11 -0
  37. package/src/styles/components/command.css +25 -0
  38. package/src/styles/components/consent.css +38 -0
  39. package/src/styles/components/datatable.css +9 -0
  40. package/src/styles/components/dialog.css +16 -0
  41. package/src/styles/components/drawer.css +29 -0
  42. package/src/styles/components/field.css +8 -0
  43. package/src/styles/components/layout.css +69 -0
  44. package/src/styles/components/menu.css +21 -0
  45. package/src/styles/components/notice.css +5 -0
  46. package/src/styles/components/popover.css +8 -0
  47. package/src/styles/components/progress.css +14 -0
  48. package/src/styles/components/radio-group.css +11 -0
  49. package/src/styles/components/select.css +25 -0
  50. package/src/styles/components/switch.css +42 -0
  51. package/src/styles/components/tabs.css +4 -0
  52. package/src/styles/components/toast.css +28 -0
  53. package/src/styles/grammars/console.css +179 -0
  54. package/src/styles/grammars/editorial.css +164 -0
  55. package/src/styles/grammars/orbit.css +269 -0
  56. package/src/styles/grammars/prunelle.css +504 -0
  57. package/src/styles/grammars/soft.css +160 -0
  58. package/src/styles/grammars/workshop.css +172 -0
  59. package/src/styles/scan.ts +144 -0
  60. package/src/styles/split.ts +429 -0
  61. package/src/styles/theme.css +22 -0
  62. package/src/styles/themed/accordion.css +20 -0
  63. package/src/styles/themed/badge.css +20 -0
  64. package/src/styles/themed/button.css +24 -0
  65. package/src/styles/themed/card.css +23 -0
  66. package/src/styles/themed/checkbox.css +4 -0
  67. package/src/styles/themed/command.css +33 -0
  68. package/src/styles/themed/consent.css +33 -0
  69. package/src/styles/themed/datatable.css +28 -0
  70. package/src/styles/themed/dialog.css +13 -0
  71. package/src/styles/themed/drawer.css +13 -0
  72. package/src/styles/themed/field.css +22 -0
  73. package/src/styles/themed/menu.css +21 -0
  74. package/src/styles/themed/notice.css +17 -0
  75. package/src/styles/themed/popover.css +11 -0
  76. package/src/styles/themed/radio-group.css +12 -0
  77. package/src/styles/themed/select.css +32 -0
  78. package/src/styles/themed/switch.css +23 -0
  79. package/src/styles/themed/tabs.css +20 -0
  80. package/src/styles/themed/toast.css +18 -0
  81. package/src/table-state.ts +163 -0
  82. package/src/tabs-state.ts +39 -0
  83. package/src/toast.ts +110 -0
  84. package/README.md +0 -3
package/src/plugin.ts ADDED
@@ -0,0 +1,188 @@
1
+ // @prunellejs/ui PrunellePlugin — wires a pure CSS Grammar (`editorial | workshop | console | soft | orbit | prunelle | bare`)
2
+ // and adds `prunelle ui-eject <grammar> [dest]` so apps can copy & own the entire look in ONE CSS file.
3
+
4
+ import type { BunPlugin } from 'bun'
5
+ import type { BuildPluginContext, PrunellePlugin } from 'prunelle/config'
6
+ import type { GrammarName, UiComponentPart } from './styles/split'
7
+ import { createHash } from 'node:crypto'
8
+ import { mkdirSync, writeFileSync } from 'node:fs'
9
+ import { dirname, join, resolve } from 'node:path'
10
+ import { definePlugin, minifyCss, UI_KUI_LAYER_SERVICE } from 'prunelle/config'
11
+ import { FALLBACK_FILE, FALLBACK_GUARD, FALLBACK_JS, withPlatformFallback } from './platform-fallback'
12
+ import { createUiPartScanner } from './styles/scan'
13
+ import {
14
+ GRAMMAR_NAMES,
15
+ grammarCssPath,
16
+ renderGrammarBaseCss,
17
+ renderGrammarCss as renderFullGrammarCss,
18
+ renderGrammarPartsCss,
19
+ UI_COMPONENT_PARTS,
20
+ UI_EXPORT_TO_PARTS,
21
+ } from './styles/split'
22
+
23
+ export {
24
+ GRAMMAR_NAMES,
25
+ grammarCssPath,
26
+ renderGrammarBaseCss,
27
+ renderGrammarPartsCss,
28
+ UI_COMPONENT_PARTS,
29
+ UI_EXPORT_TO_PARTS,
30
+ }
31
+ export type { GrammarName, UiComponentPart }
32
+
33
+ /** Returns the self-contained CSS source for a grammar (with `base.css` and component sheets inlined). */
34
+ export function renderGrammarCss(grammar: GrammarName): string {
35
+ return renderFullGrammarCss(grammar)
36
+ }
37
+
38
+ /** Ejects a complete, self-contained CSS Grammar file into the app. */
39
+ export function ejectGrammar(root: string, grammar: GrammarName = 'editorial', destRel = 'src/styles/ui.css'): string {
40
+ const outPath = resolve(root, destRel)
41
+ mkdirSync(dirname(outPath), { recursive: true })
42
+ writeFileSync(outPath, renderGrammarCss(grammar))
43
+ return outPath
44
+ }
45
+
46
+ export interface EmitGrammarsOptions {
47
+ /** Public directory relative to `dist/public` (default: `'grammars'`). */
48
+ dir?: string | undefined
49
+ /** Subset of grammars to emit (default: all grammars). */
50
+ grammars?: readonly GrammarName[] | undefined
51
+ }
52
+
53
+ export interface UiPluginOptions {
54
+ /** Which pure CSS Grammar to include (default: 'editorial'). Pass `false` if importing your own CSS. */
55
+ grammar?: GrammarName | false | undefined
56
+ /** Emit standalone self-contained grammar CSS files into `public/grammars/<name>.css` for runtime switching. */
57
+ emitGrammars?: boolean | readonly GrammarName[] | EmitGrammarsOptions | undefined
58
+ }
59
+
60
+ function resolveEmitGrammars(opt: UiPluginOptions['emitGrammars']): { dir: string, grammars: readonly GrammarName[] } | undefined {
61
+ if (!opt)
62
+ return undefined
63
+ if (opt === true)
64
+ return { dir: 'grammars', grammars: GRAMMAR_NAMES }
65
+ if (Array.isArray(opt))
66
+ return { dir: 'grammars', grammars: opt }
67
+ const opts = opt as EmitGrammarsOptions
68
+ const dir = (opts.dir ?? 'grammars').replace(/^\/+|\/+$/g, '')
69
+ return { dir, grammars: opts.grammars ?? GRAMMAR_NAMES }
70
+ }
71
+
72
+ function cssHash(css: string): string {
73
+ return createHash('sha256').update(css).digest('hex').slice(0, 8)
74
+ }
75
+
76
+ function emitGrammarStyles(ctx: BuildPluginContext, grammar: GrammarName): void {
77
+ const prepGlobal = ctx.prependStyle ?? ctx.addStyle
78
+ const prepRoute = ctx.prependRouteStyle ?? ctx.addRouteStyle
79
+ if (prepGlobal === undefined)
80
+ return
81
+ const format = (css: string): string => ctx.minify ? minifyCss(css) : css
82
+
83
+ if (ctx.routeSourceFiles === undefined) {
84
+ const full = format(renderGrammarCss(grammar))
85
+ prepGlobal(ctx.emitAsset(`${grammar}-${cssHash(full)}.css`, full))
86
+ return
87
+ }
88
+
89
+ const scan = createUiPartScanner()
90
+ const globalParts = scan(ctx.globalSourceFiles ?? [])
91
+ const routes = ctx.routes ?? []
92
+ const perRoute = new Map<string, Set<UiComponentPart>>()
93
+ for (const route of routes)
94
+ perRoute.set(route.path, scan(ctx.routeSourceFiles.get(route.path) ?? []))
95
+
96
+ // Any UI component part rendered on every route belongs in the site-wide sheet so it is cached across navigations.
97
+ if (routes.length > 0) {
98
+ const firstParts = perRoute.get(routes[0]!.path)!
99
+ for (const part of firstParts) {
100
+ if (routes.every(r => perRoute.get(r.path)!.has(part)))
101
+ globalParts.add(part)
102
+ }
103
+ }
104
+
105
+ const baseSource = globalParts.size > 0
106
+ ? `${renderGrammarBaseCss(grammar)}\n${renderGrammarPartsCss(grammar, globalParts)}`
107
+ : renderGrammarBaseCss(grammar)
108
+ const baseCss = format(baseSource)
109
+ prepGlobal(ctx.emitAsset(`${grammar}-${cssHash(baseCss)}.css`, baseCss))
110
+
111
+ if (prepRoute === undefined)
112
+ return
113
+ const routeCache = new Map<string, string>()
114
+ for (const route of routes) {
115
+ const routeParts = perRoute.get(route.path)!
116
+ const extra = UI_COMPONENT_PARTS.filter(p => routeParts.has(p) && !globalParts.has(p))
117
+ if (extra.length === 0)
118
+ continue
119
+ const key = extra.join(',')
120
+ let href = routeCache.get(key)
121
+ if (href === undefined) {
122
+ const partsCss = format(renderGrammarPartsCss(grammar, extra))
123
+ href = ctx.emitAsset(`${grammar}-parts-${cssHash(partsCss)}.css`, partsCss)
124
+ routeCache.set(key, href)
125
+ }
126
+ prepRoute(route.path, href)
127
+ }
128
+ }
129
+
130
+ const LABELS_DIR = join(import.meta.dir, 'labels')
131
+ const LABELS_FILTER = new RegExp(`^${LABELS_DIR.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}[\\\\/](\\w+)\\.ts$`)
132
+ const FRENCH_RE = /^fr(?:-|$)/i
133
+
134
+ // A French app loads labels/fr/<name>.ts wherever ui imports labels/<name>.ts, so each island ships its own labels in one language.
135
+ function frenchLabels(lang: string | undefined): BunPlugin[] {
136
+ if (lang === undefined || !FRENCH_RE.test(lang))
137
+ return []
138
+ return [{
139
+ name: 'prunelle-ui-labels',
140
+ setup(build) {
141
+ build.onLoad({ filter: LABELS_FILTER }, async args => ({
142
+ contents: await Bun.file(join(LABELS_DIR, 'fr', `${LABELS_FILTER.exec(args.path)![1]}.ts`)).text(),
143
+ loader: 'ts',
144
+ }))
145
+ },
146
+ }]
147
+ }
148
+
149
+ export function ui(options: UiPluginOptions = {}): PrunellePlugin {
150
+ const grammar = options.grammar === undefined ? 'editorial' : options.grammar
151
+ const emit = resolveEmitGrammars(options.emitGrammars)
152
+ let assetPrefix = '/_pru'
153
+ return definePlugin({
154
+ name: '@prunellejs/ui',
155
+ inlineScripts: [FALLBACK_GUARD],
156
+ bunPlugins: ({ lang }) => frenchLabels(lang),
157
+ build(ctx) {
158
+ ctx.emitAsset(FALLBACK_FILE, FALLBACK_JS)
159
+ if (grammar !== false) {
160
+ ctx.provide?.(UI_KUI_LAYER_SERVICE, true)
161
+ emitGrammarStyles(ctx, grammar)
162
+ }
163
+ if (emit !== undefined) {
164
+ const prefix = emit.dir ? `${emit.dir}/` : ''
165
+ for (const g of emit.grammars) {
166
+ const css = renderGrammarCss(g)
167
+ ctx.emitPublic(`${prefix}${g}.css`, ctx.minify ? minifyCss(css) : css)
168
+ }
169
+ }
170
+ },
171
+ server(api) {
172
+ assetPrefix = api.assetPrefix
173
+ },
174
+ transformHtml: html => withPlatformFallback(html, assetPrefix),
175
+ commands: {
176
+ 'ui-eject': (args, root) => {
177
+ const name = (args[0] ?? 'editorial') as GrammarName
178
+ if (!GRAMMAR_NAMES.includes(name)) {
179
+ console.error(`Unknown grammar "${name}". Choose one of: ${GRAMMAR_NAMES.join(', ')}`)
180
+ process.exit(1)
181
+ }
182
+ const dest = args[1] ?? 'src/styles/ui.css'
183
+ const outPath = ejectGrammar(root, name, dest)
184
+ console.log(`Ejected @prunellejs/ui "${name}" grammar to ${outPath}`)
185
+ },
186
+ },
187
+ })
188
+ }
@@ -0,0 +1,238 @@
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
+ /** Text or markup (a link to the privacy policy); it sits inside the wrapping `<label>`, so it names the control. */
154
+ label: PrunelleChild
155
+ checked?: boolean | undefined
156
+ disabled?: boolean | undefined
157
+ class?: string | undefined
158
+ [attr: string]: unknown
159
+ }
160
+
161
+ export function Checkbox({ id, name, value, label, checked, disabled, class: cls, ...rest }: CheckControlProps) {
162
+ return (
163
+ <label data-ui="checkbox" data-disabled={disabled ? 'true' : undefined} class={cls}>
164
+ <input id={id} type="checkbox" name={name} value={value} checked={checked} disabled={disabled} {...rest} />
165
+ <span data-part="label">{label}</span>
166
+ </label>
167
+ )
168
+ }
169
+
170
+ export function Switch({ id, name, value, label, checked, disabled, class: cls, ...rest }: CheckControlProps) {
171
+ return (
172
+ <label data-ui="switch" data-disabled={disabled ? 'true' : undefined} class={cls}>
173
+ <input id={id} type="checkbox" role="switch" name={name} value={value} checked={checked} disabled={disabled} {...rest} />
174
+ <span data-part="track" aria-hidden="true" />
175
+ <span data-part="label">{label}</span>
176
+ </label>
177
+ )
178
+ }
179
+
180
+ export interface RadioOption {
181
+ value: string
182
+ label: PrunelleChild
183
+ hint?: string | undefined
184
+ disabled?: boolean | undefined
185
+ }
186
+
187
+ export interface RadioGroupProps {
188
+ name: string
189
+ legend?: string | undefined
190
+ options: RadioOption[]
191
+ defaultValue?: string | undefined
192
+ class?: string | undefined
193
+ }
194
+
195
+ export function RadioGroup({ name, legend, options, defaultValue, class: cls }: RadioGroupProps) {
196
+ return (
197
+ <fieldset data-ui="radio-group" class={cls}>
198
+ {legend && <legend data-part="legend">{legend}</legend>}
199
+ <div data-part="options">
200
+ {options.map(opt => (
201
+ <label key={opt.value} data-part="option" data-disabled={opt.disabled ? 'true' : undefined}>
202
+ <input
203
+ type="radio"
204
+ name={name}
205
+ value={opt.value}
206
+ checked={defaultValue === opt.value ? true : undefined}
207
+ disabled={opt.disabled}
208
+ />
209
+ <span data-part="label">{opt.label}</span>
210
+ {opt.hint && <span data-part="hint">{opt.hint}</span>}
211
+ </label>
212
+ ))}
213
+ </div>
214
+ </fieldset>
215
+ )
216
+ }
217
+
218
+ export interface ProgressProps {
219
+ label: string
220
+ /** Without one the bar is indeterminate: a loader. */
221
+ value?: number | undefined
222
+ max?: number | undefined
223
+ class?: string | undefined
224
+ [attr: string]: unknown
225
+ }
226
+
227
+ // The share done, rounded to the 5% step a grammar styles: a style attribute would break a CSP without 'unsafe-inline'.
228
+ const progressStep = (value: number, max: number): string => String(Math.round(Math.min(Math.max(value / max, 0), 1) * 20) * 5)
229
+
230
+ export function Progress({ label, value, max = 100, class: cls, ...rest }: ProgressProps) {
231
+ return (
232
+ <span data-ui="progress" data-value={value === undefined ? undefined : progressStep(value, max)} class={cls} {...rest}>
233
+ <span data-part="eye" aria-hidden="true" />
234
+ <progress value={value} max={value === undefined ? undefined : max} aria-label={label} />
235
+ <span data-part="label" aria-hidden="true">{label}</span>
236
+ </span>
237
+ )
238
+ }