@prunellejs/ui 0.1.1 → 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 +9 -3
- package/package.json +2 -2
- package/src/CommandPalette.tsx +88 -38
- package/src/index.ts +1 -1
- package/src/plugin.ts +96 -32
- package/src/styles/bare.css +21 -312
- 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 +113 -256
- package/src/styles/grammars/editorial.css +99 -270
- package/src/styles/grammars/orbit.css +118 -388
- package/src/styles/grammars/prunelle.css +157 -360
- package/src/styles/grammars/soft.css +103 -237
- package/src/styles/grammars/workshop.css +104 -246
- 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/llms.txt
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
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
10
|
- Interactive island primitives: `ControlledDialog`, `ControlledDrawer`, `Tabs`, `Menu`, `Select`, `CommandPalette`, `DataTable`, `Toaster`
|
|
11
11
|
- Headless signal behaviors & helpers: `createDialog`, `createTabs`, `createDataTable`, `toast`, `handleListKeyDown`, `findFirstEnabledIndex`, `findLastEnabledIndex`, `findNextEnabledIndex`, `isDialogBackdropClick`, `syncDialogOpen`, `nextUiId`
|
|
12
|
-
- `@prunellejs/ui/plugin`: `ui(options?)`, `GRAMMAR_NAMES`, `grammarCssPath`, `renderGrammarCss`, `ejectGrammar`
|
|
12
|
+
- `@prunellejs/ui/plugin`: `ui(options?)`, `GRAMMAR_NAMES`, `UI_COMPONENT_PARTS`, `UI_EXPORT_TO_PARTS`, `grammarCssPath`, `renderGrammarCss`, `renderGrammarBaseCss`, `renderGrammarPartsCss`, `ejectGrammar`
|
|
13
13
|
- CSS subpaths: `@prunellejs/ui/bare.css`, `@prunellejs/ui/grammars/{editorial,workshop,console,soft,orbit,prunelle}.css`
|
|
14
14
|
|
|
15
15
|
## Plugin Setup (`prunelle.config.ts`)
|
|
@@ -25,7 +25,13 @@ export default defineConfig({
|
|
|
25
25
|
|
|
26
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
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 `
|
|
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).
|
|
29
35
|
|
|
30
36
|
## Key Props & Types
|
|
31
37
|
|
|
@@ -41,7 +47,7 @@ export default defineConfig({
|
|
|
41
47
|
- `Tabs`: `{ id?, items: Array<{ id, label, content }>, defaultValue?, label? }`.
|
|
42
48
|
- `Menu`: `{ id?, label, items: Array<{ id, label, href?, disabled?, onSelect? }>, variant?, onSelect? }`.
|
|
43
49
|
- `Select`: `{ id?, options: Array<{ value, label, disabled? }>, value?: Signal<string>, defaultValue?, name?, placeholder?, ariaLabel?, onChange? }`.
|
|
44
|
-
- `CommandPalette`: `{ id?, items
|
|
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).
|
|
45
51
|
- `DataTable`: `{ rows, columns: Array<{ key, label, sortable? }>, rowKey? ('id'), pageSize? (10), searchable? (true), selectable? (false), ...labels }`.
|
|
46
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()`.
|
|
47
53
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Platform-first headless UI primitives and pure CSS @layer kui Style Grammars for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -29,6 +29,6 @@
|
|
|
29
29
|
"directory": "packages/ui"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
|
-
"prunelle": "^0.
|
|
32
|
+
"prunelle": "^0.5.1"
|
|
33
33
|
}
|
|
34
34
|
}
|
package/src/CommandPalette.tsx
CHANGED
|
@@ -8,6 +8,7 @@ import { findFirstEnabledIndex, handleListKeyDown, isDialogBackdropClick, nextUi
|
|
|
8
8
|
export interface CommandItem {
|
|
9
9
|
id: string
|
|
10
10
|
label: string
|
|
11
|
+
description?: string | undefined
|
|
11
12
|
group?: string | undefined
|
|
12
13
|
shortcut?: string | undefined
|
|
13
14
|
href?: string | undefined
|
|
@@ -15,9 +16,15 @@ export interface CommandItem {
|
|
|
15
16
|
onSelect?: (() => void) | undefined
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
export type CommandSource =
|
|
20
|
+
| ((query: string) => CommandItem[] | Promise<CommandItem[]>)
|
|
21
|
+
| { search: (query: string) => CommandItem[] | Promise<CommandItem[]> }
|
|
22
|
+
|
|
18
23
|
export interface CommandPaletteProps {
|
|
19
24
|
id?: string | undefined
|
|
20
|
-
items
|
|
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
|
|
21
28
|
placeholder?: string | undefined
|
|
22
29
|
inputLabel?: string | undefined
|
|
23
30
|
ariaLabel?: string | undefined
|
|
@@ -33,6 +40,66 @@ export interface CommandPaletteProps {
|
|
|
33
40
|
|
|
34
41
|
const isItemDisabled = (it: CommandItem) => Boolean(it.disabled)
|
|
35
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
|
+
window.location.assign(item.href)
|
|
101
|
+
}
|
|
102
|
+
|
|
36
103
|
export function CommandPalette(props: CommandPaletteProps) {
|
|
37
104
|
const baseId = props.id ?? nextUiId('kui-cmd')
|
|
38
105
|
const listId = `${baseId}-list`
|
|
@@ -45,22 +112,23 @@ export function CommandPalette(props: CommandPaletteProps) {
|
|
|
45
112
|
const noResultsLabel = props.noResultsLabel ?? 'No results'
|
|
46
113
|
const resultLabel = props.resultLabel ?? 'result'
|
|
47
114
|
const resultsLabel = props.resultsLabel ?? 'results'
|
|
48
|
-
const
|
|
115
|
+
const staticItems = props.items ?? []
|
|
49
116
|
|
|
50
117
|
const isOpen = props.open ?? signal(false)
|
|
51
118
|
const query = signal('')
|
|
52
|
-
const
|
|
119
|
+
const sourceItems = signal<CommandItem[]>([])
|
|
120
|
+
const activeIdx = signal(findFirstEnabledIndex(staticItems, isItemDisabled))
|
|
53
121
|
let dialogEl: HTMLDialogElement | undefined
|
|
54
122
|
let inputEl: HTMLInputElement | undefined
|
|
55
123
|
|
|
56
124
|
const filtered = computed(() => {
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
)
|
|
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)
|
|
64
132
|
})
|
|
65
133
|
|
|
66
134
|
const activeOptionId = () => {
|
|
@@ -73,41 +141,20 @@ export function CommandPalette(props: CommandPaletteProps) {
|
|
|
73
141
|
if (!isOpen.value)
|
|
74
142
|
return ''
|
|
75
143
|
const n = filtered.value.length
|
|
76
|
-
|
|
77
|
-
return noResultsLabel
|
|
78
|
-
return n === 1 ? `1 ${resultLabel}` : `${n} ${resultsLabel}`
|
|
144
|
+
return n === 0 ? noResultsLabel : n === 1 ? `1 ${resultLabel}` : `${n} ${resultsLabel}`
|
|
79
145
|
}
|
|
80
146
|
|
|
81
147
|
const onOpened = () => {
|
|
82
148
|
query.value = ''
|
|
83
|
-
activeIdx.value = findFirstEnabledIndex(
|
|
149
|
+
activeIdx.value = findFirstEnabledIndex(staticItems, isItemDisabled)
|
|
150
|
+
refreshSource('')
|
|
84
151
|
queueMicrotask(() => inputEl?.focus())
|
|
85
152
|
}
|
|
86
153
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === shortcutKey) {
|
|
90
|
-
e.preventDefault()
|
|
91
|
-
isOpen.value = !isOpen.value
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
window.addEventListener('keydown', onKeyDown)
|
|
95
|
-
return () => window.removeEventListener('keydown', onKeyDown)
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
effect(() => {
|
|
99
|
-
syncDialogOpen(dialogEl, isOpen, isOpen.value, onOpened)
|
|
100
|
-
})
|
|
154
|
+
bindPaletteShortcut((props.shortcutKey ?? 'k').toLowerCase(), isOpen)
|
|
155
|
+
effect(() => { syncDialogOpen(dialogEl, isOpen, isOpen.value, onOpened) })
|
|
101
156
|
|
|
102
|
-
const runItem = (item: CommandItem) =>
|
|
103
|
-
if (item.disabled)
|
|
104
|
-
return
|
|
105
|
-
isOpen.value = false
|
|
106
|
-
item.onSelect?.()
|
|
107
|
-
props.onSelect?.(item.id)
|
|
108
|
-
if (item.href)
|
|
109
|
-
window.location.assign(item.href)
|
|
110
|
-
}
|
|
157
|
+
const runItem = (item: CommandItem) => selectCommandItem(item, isOpen, props.onSelect)
|
|
111
158
|
|
|
112
159
|
return (
|
|
113
160
|
<div data-ui="command-root" data-state={isOpen.value ? 'open' : 'closed'}>
|
|
@@ -153,8 +200,10 @@ export function CommandPalette(props: CommandPaletteProps) {
|
|
|
153
200
|
placeholder={placeholder}
|
|
154
201
|
value={query.value}
|
|
155
202
|
onInput={(e) => {
|
|
156
|
-
|
|
203
|
+
const val = e.currentTarget.value
|
|
204
|
+
query.value = val
|
|
157
205
|
activeIdx.value = findFirstEnabledIndex(filtered.value, isItemDisabled)
|
|
206
|
+
refreshSource(val)
|
|
158
207
|
}}
|
|
159
208
|
onKeyDown={(e) => {
|
|
160
209
|
handleListKeyDown(e, {
|
|
@@ -190,6 +239,7 @@ export function CommandPalette(props: CommandPaletteProps) {
|
|
|
190
239
|
onClick={() => runItem(item)}
|
|
191
240
|
>
|
|
192
241
|
<span data-part="item-label">{item.label}</span>
|
|
242
|
+
{item.description && <span data-part="item-description">{item.description}</span>}
|
|
193
243
|
{item.shortcut && <kbd data-part="kbd">{item.shortcut}</kbd>}
|
|
194
244
|
</button>
|
|
195
245
|
))}
|
package/src/index.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type { DataTableOptions, DataTableState, DialogBehavior, ListNavigationOp
|
|
|
17
17
|
export { toast } from './toast'
|
|
18
18
|
export type { ToastItem } from './toast'
|
|
19
19
|
export { CommandPalette } from './CommandPalette'
|
|
20
|
-
export type { CommandItem, CommandPaletteProps } from './CommandPalette'
|
|
20
|
+
export type { CommandItem, CommandPaletteProps, CommandSource } from './CommandPalette'
|
|
21
21
|
export { DataTable } from './DataTable'
|
|
22
22
|
export type { DataTableColumn, DataTableProps } from './DataTable'
|
|
23
23
|
export { ControlledDialog, Dialog } from './Dialog'
|
package/src/plugin.ts
CHANGED
|
@@ -1,38 +1,42 @@
|
|
|
1
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
|
|
2
|
+
// and adds `prunelle ui-eject <grammar> [dest]` so apps can copy & own the entire look in ONE CSS file.
|
|
3
3
|
|
|
4
|
-
import type { PrunellePlugin } from 'prunelle/config'
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
4
|
+
import type { BuildPluginContext, PrunellePlugin } from 'prunelle/config'
|
|
5
|
+
import type { GrammarName, UiComponentPart } from './styles/split'
|
|
6
|
+
import { createHash } from 'node:crypto'
|
|
7
|
+
import { mkdirSync, writeFileSync } from 'node:fs'
|
|
8
|
+
import { dirname, resolve } from 'node:path'
|
|
9
|
+
import { definePlugin, minifyCss, serviceKey } from 'prunelle/config'
|
|
8
10
|
import { FALLBACK_FILE, FALLBACK_GUARD, FALLBACK_JS, withPlatformFallback } from './platform-fallback'
|
|
11
|
+
import { createUiPartScanner } from './styles/scan'
|
|
12
|
+
import {
|
|
13
|
+
GRAMMAR_NAMES,
|
|
14
|
+
grammarCssPath,
|
|
15
|
+
renderGrammarBaseCss,
|
|
16
|
+
renderGrammarCss as renderFullGrammarCss,
|
|
17
|
+
renderGrammarPartsCss,
|
|
18
|
+
UI_COMPONENT_PARTS,
|
|
19
|
+
UI_EXPORT_TO_PARTS,
|
|
20
|
+
} from './styles/split'
|
|
9
21
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const BARE_IMPORT_RE = /^@import\s+['"]\.\.\/bare\.css['"];?\s*/gm
|
|
18
|
-
|
|
19
|
-
export function grammarCssPath(grammar: GrammarName): string {
|
|
20
|
-
return grammar === 'bare'
|
|
21
|
-
? join(STYLES_DIR, 'bare.css')
|
|
22
|
-
: join(STYLES_DIR, 'grammars', `${grammar}.css`)
|
|
22
|
+
export {
|
|
23
|
+
GRAMMAR_NAMES,
|
|
24
|
+
grammarCssPath,
|
|
25
|
+
renderGrammarBaseCss,
|
|
26
|
+
renderGrammarPartsCss,
|
|
27
|
+
UI_COMPONENT_PARTS,
|
|
28
|
+
UI_EXPORT_TO_PARTS,
|
|
23
29
|
}
|
|
30
|
+
export type { GrammarName, UiComponentPart }
|
|
24
31
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const rawGrammar = readFileSync(grammarCssPath(grammar), 'utf8')
|
|
31
|
-
const withoutImport = rawGrammar.replace(BARE_IMPORT_RE, '')
|
|
32
|
-
return `${bare}\n${withoutImport}`
|
|
32
|
+
const UI_KUI_LAYER_SERVICE = serviceKey<boolean>('prunelle.ui.kuiLayer')
|
|
33
|
+
|
|
34
|
+
/** Returns the self-contained CSS source for a grammar (with `base.css` and component sheets inlined). */
|
|
35
|
+
export function renderGrammarCss(grammar: GrammarName, _bareCss?: string): string {
|
|
36
|
+
return renderFullGrammarCss(grammar)
|
|
33
37
|
}
|
|
34
38
|
|
|
35
|
-
/** Ejects a complete, self-contained CSS Grammar file
|
|
39
|
+
/** Ejects a complete, self-contained CSS Grammar file into the app. */
|
|
36
40
|
export function ejectGrammar(root: string, grammar: GrammarName = 'editorial', destRel = 'src/styles/ui.css'): string {
|
|
37
41
|
const outPath = resolve(root, destRel)
|
|
38
42
|
mkdirSync(dirname(outPath), { recursive: true })
|
|
@@ -66,23 +70,83 @@ function resolveEmitGrammars(opt: UiPluginOptions['emitGrammars']): { dir: strin
|
|
|
66
70
|
return { dir, grammars: opts.grammars ?? GRAMMAR_NAMES }
|
|
67
71
|
}
|
|
68
72
|
|
|
73
|
+
function cssHash(css: string): string {
|
|
74
|
+
return createHash('sha256').update(css).digest('hex').slice(0, 8)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function emitGrammarStyles(ctx: BuildPluginContext, grammar: GrammarName): void {
|
|
78
|
+
const prepGlobal = ctx.prependStyle ?? ctx.addStyle
|
|
79
|
+
const prepRoute = ctx.prependRouteStyle ?? ctx.addRouteStyle
|
|
80
|
+
if (prepGlobal === undefined)
|
|
81
|
+
return
|
|
82
|
+
const format = (css: string): string => ctx.minify ? minifyCss(css) : css
|
|
83
|
+
|
|
84
|
+
if (ctx.routeSourceFiles === undefined) {
|
|
85
|
+
const full = format(renderGrammarCss(grammar))
|
|
86
|
+
prepGlobal(ctx.emitAsset(`${grammar}-${cssHash(full)}.css`, full))
|
|
87
|
+
return
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const scan = createUiPartScanner()
|
|
91
|
+
const globalParts = scan(ctx.globalSourceFiles ?? [])
|
|
92
|
+
const routes = ctx.routes ?? []
|
|
93
|
+
const perRoute = new Map<string, Set<UiComponentPart>>()
|
|
94
|
+
for (const route of routes)
|
|
95
|
+
perRoute.set(route.path, scan(ctx.routeSourceFiles.get(route.path) ?? []))
|
|
96
|
+
|
|
97
|
+
// Any UI component part rendered on every route belongs in the site-wide sheet so it is cached across navigations.
|
|
98
|
+
if (routes.length > 0) {
|
|
99
|
+
const firstParts = perRoute.get(routes[0]!.path)!
|
|
100
|
+
for (const part of firstParts) {
|
|
101
|
+
if (routes.every(r => perRoute.get(r.path)!.has(part)))
|
|
102
|
+
globalParts.add(part)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const baseSource = globalParts.size > 0
|
|
107
|
+
? `${renderGrammarBaseCss(grammar)}\n${renderGrammarPartsCss(grammar, globalParts)}`
|
|
108
|
+
: renderGrammarBaseCss(grammar)
|
|
109
|
+
const baseCss = format(baseSource)
|
|
110
|
+
prepGlobal(ctx.emitAsset(`${grammar}-${cssHash(baseCss)}.css`, baseCss))
|
|
111
|
+
|
|
112
|
+
if (prepRoute === undefined)
|
|
113
|
+
return
|
|
114
|
+
const routeCache = new Map<string, string>()
|
|
115
|
+
for (const route of routes) {
|
|
116
|
+
const routeParts = perRoute.get(route.path)!
|
|
117
|
+
const extra = UI_COMPONENT_PARTS.filter(p => routeParts.has(p) && !globalParts.has(p))
|
|
118
|
+
if (extra.length === 0)
|
|
119
|
+
continue
|
|
120
|
+
const key = extra.join(',')
|
|
121
|
+
let href = routeCache.get(key)
|
|
122
|
+
if (href === undefined) {
|
|
123
|
+
const partsCss = format(renderGrammarPartsCss(grammar, extra))
|
|
124
|
+
href = ctx.emitAsset(`${grammar}-parts-${cssHash(partsCss)}.css`, partsCss)
|
|
125
|
+
routeCache.set(key, href)
|
|
126
|
+
}
|
|
127
|
+
prepRoute(route.path, href)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
69
131
|
export function ui(options: UiPluginOptions = {}): PrunellePlugin {
|
|
70
132
|
const grammar = options.grammar === undefined ? 'editorial' : options.grammar
|
|
71
133
|
const emit = resolveEmitGrammars(options.emitGrammars)
|
|
72
134
|
let assetPrefix = '/_pru'
|
|
73
135
|
return definePlugin({
|
|
74
136
|
name: '@prunellejs/ui',
|
|
75
|
-
css: grammar === false ? undefined : [grammarCssPath(grammar)],
|
|
76
137
|
inlineScripts: [FALLBACK_GUARD],
|
|
77
138
|
build(ctx) {
|
|
78
139
|
ctx.emitAsset(FALLBACK_FILE, FALLBACK_JS)
|
|
79
|
-
if (grammar !== false)
|
|
140
|
+
if (grammar !== false) {
|
|
80
141
|
ctx.provide?.(UI_KUI_LAYER_SERVICE, true)
|
|
142
|
+
emitGrammarStyles(ctx, grammar)
|
|
143
|
+
}
|
|
81
144
|
if (emit !== undefined) {
|
|
82
|
-
const bare = readFileSync(join(STYLES_DIR, 'bare.css'), 'utf8')
|
|
83
145
|
const prefix = emit.dir ? `${emit.dir}/` : ''
|
|
84
|
-
for (const g of emit.grammars)
|
|
85
|
-
|
|
146
|
+
for (const g of emit.grammars) {
|
|
147
|
+
const css = renderGrammarCss(g)
|
|
148
|
+
ctx.emitPublic(`${prefix}${g}.css`, ctx.minify ? minifyCss(css) : css)
|
|
149
|
+
}
|
|
86
150
|
}
|
|
87
151
|
},
|
|
88
152
|
server(api) {
|