@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.
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
@@ -0,0 +1,171 @@
1
+ /* Grammar: Workshop — tactile 2px ink frames, hard unblurred offset shadows, physical press travel.
2
+ * Inspired by print & craft studios (like jb-peinture). Eject via `prunelle ui-eject workshop`. */
3
+
4
+ @import '../theme.css';
5
+
6
+ @layer kui {
7
+ :root {
8
+ color-scheme: light dark;
9
+ --ui-paper: light-dark(#f1eadc, #181512);
10
+ --ui-surface: light-dark(#fbf7ef, #231f1a);
11
+ --ui-ink: light-dark(#2a231b, #f3ece0);
12
+ --ui-muted: light-dark(#6c6152, #a89d8c);
13
+ --ui-line: light-dark(#2a231b, #e8dfd0);
14
+ --ui-shadow-color: light-dark(#ded4c1, #0b0907);
15
+ --ui-accent: light-dark(#b2492c, #e37455);
16
+ --ui-on-accent: light-dark(#ffffff, #181512);
17
+ --ui-success: light-dark(#31692d, #7ac074);
18
+ --ui-warning: light-dark(#855700, #eab354);
19
+ --ui-danger: light-dark(#b0322a, #f0756b);
20
+ --ui-font-body: ui-sans-serif, system-ui, -apple-system, sans-serif;
21
+ --ui-font-display: ui-sans-serif, system-ui, -apple-system, sans-serif;
22
+ --ui-radius: 2px;
23
+ --ui-border-w: 2px;
24
+ --ui-shadow: 5px 5px 0 var(--ui-shadow-color);
25
+ --ui-press-shadow: 3px 3px 0 var(--ui-line);
26
+ --ui-motion: 120ms;
27
+ }
28
+
29
+ [data-ui="button"] {
30
+ --ui-btn-f: 700 0.88rem/1 var(--ui-font-body);
31
+ --ui-btn-pad: 0.7rem 1.25rem;
32
+ --ui-btn-bd: var(--ui-border-w) solid var(--ui-line);
33
+ letter-spacing: 0.04em;
34
+ text-transform: uppercase;
35
+ box-shadow: var(--ui-press-shadow);
36
+ &:hover:not(:disabled) { transform: translate(-1px, -1px); box-shadow: 4px 4px 0 var(--ui-line); }
37
+ &:active:not(:disabled) { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ui-line); }
38
+ &[data-tone="accent"], &[data-tone="danger"], &[data-variant="outline"] { border-color: var(--ui-line); }
39
+ &[data-variant="ghost"]:hover:not(:disabled) { border-color: var(--ui-line); transform: none; box-shadow: none; }
40
+ }
41
+
42
+ [data-ui="card"] {
43
+ --ui-card-bd: var(--ui-border-w) solid var(--ui-line);
44
+ --ui-card-pad: 1.25rem;
45
+ --ui-card-hf: 800 1.1rem/1.3 var(--ui-font-display);
46
+ --ui-card-hpb: 0.75rem;
47
+ --ui-card-hmb: 0.75rem;
48
+ --ui-card-hbd: var(--ui-border-w) solid var(--ui-line);
49
+ --ui-card-fpt: 0.75rem;
50
+ --ui-card-fbd: var(--ui-border-w) solid var(--ui-line);
51
+ box-shadow: var(--ui-shadow);
52
+ }
53
+
54
+ [data-ui="badge"] {
55
+ --ui-badge-f: 700 0.74rem/1 var(--ui-font-body);
56
+ --ui-badge-pad: 0.3rem 0.65rem;
57
+ --ui-badge-bd: var(--ui-border-w) solid var(--ui-line);
58
+ --ui-badge-bg: var(--ui-surface);
59
+ --ui-badge-fg: var(--ui-ink);
60
+ --ui-badge-tbg: 100%;
61
+ --ui-badge-tfg: var(--ui-on-accent);
62
+ --ui-badge-tbd: 0%;
63
+ --ui-badge-tbd0: var(--ui-line);
64
+ letter-spacing: 0.08em;
65
+ text-transform: uppercase;
66
+ }
67
+
68
+ [data-ui="notice"] {
69
+ --ui-notice-bw: var(--ui-border-w);
70
+ --ui-notice-pad: 0.9rem 1.1rem;
71
+ box-shadow: 4px 4px 0 var(--ui-shadow-color);
72
+ &[data-tone] { border-color: var(--ui-tone); }
73
+ }
74
+
75
+ [data-ui="field"] {
76
+ --ui-fld-lf: 700 0.82rem/1.2 var(--ui-font-body);
77
+ --ui-in-f: 500 0.95rem/1.4 var(--ui-font-body);
78
+ --ui-in-bd: var(--ui-border-w) solid var(--ui-line);
79
+ --ui-in-pad: 0.6rem 0.85rem;
80
+ & [data-part="label"] { text-transform: uppercase; letter-spacing: 0.06em; }
81
+ & [data-part="error"] { font-weight: 600; }
82
+ }
83
+
84
+ [data-ui="accordion"] {
85
+ --ui-acc-bd: var(--ui-border-w) solid var(--ui-line);
86
+ --ui-acc-bg: var(--ui-surface);
87
+ --ui-acc-gap: calc(-1 * var(--ui-border-w));
88
+ --ui-acc-tpad: 0.85rem 1rem;
89
+ --ui-acc-tf: 700 1rem/1.3 var(--ui-font-body);
90
+ --ui-acc-m: '▸';
91
+ --ui-acc-mo: '▾';
92
+ --ui-acc-cpad: 0 1rem 1rem;
93
+ }
94
+
95
+ [data-ui="tabs"] {
96
+ --ui-tab-disp: inline-flex;
97
+ --ui-tab-gap: 0;
98
+ --ui-tab-pad: 0.55rem 1rem;
99
+ --ui-tab-f: 700 0.82rem/1 var(--ui-font-body);
100
+ --ui-tab-fg: var(--ui-ink);
101
+ --ui-tab-abg: var(--ui-accent);
102
+ --ui-tab-afg: var(--ui-on-accent);
103
+ & [data-part="list"] { border: var(--ui-border-w) solid var(--ui-line); background: var(--ui-paper); }
104
+ & [data-part="trigger"] { border-right: var(--ui-border-w) solid var(--ui-line); text-transform: uppercase; &:last-child { border-right: 0; } }
105
+ }
106
+
107
+ dialog[data-ui="dialog"],
108
+ dialog[data-ui="drawer"],
109
+ dialog[data-ui="command"] {
110
+ --ui-dlg-bd: var(--ui-border-w) solid var(--ui-line);
111
+ --ui-dlg-sh: 8px 8px 0 var(--ui-line);
112
+ --ui-dlg-tf: 800 1.25rem/1.25 var(--ui-font-display);
113
+ }
114
+
115
+ [data-ui="popover"],
116
+ [data-ui="menu"],
117
+ [data-ui="select"] {
118
+ --ui-pop-bd: var(--ui-border-w) solid var(--ui-line);
119
+ --ui-pop-sh: var(--ui-shadow);
120
+ --ui-it-f: 600 0.88rem/1.3 var(--ui-font-body);
121
+ --ui-it-hbg: var(--ui-paper);
122
+ }
123
+
124
+ [data-ui="select"] {
125
+ --ui-sel-bd: var(--ui-border-w) solid var(--ui-line);
126
+ --ui-sel-pad: 0.55rem 0.85rem;
127
+ --ui-sel-f: 600 0.9rem/1.3 var(--ui-font-body);
128
+ & [data-part="trigger"] { box-shadow: var(--ui-press-shadow); }
129
+ & [data-part="option"][data-selected="true"] { background: var(--ui-accent); color: var(--ui-on-accent); }
130
+ }
131
+
132
+ [data-ui="toast"] {
133
+ --ui-tst-bw: var(--ui-border-w);
134
+ --ui-tst-sh: var(--ui-shadow);
135
+ --ui-tst-pad: 0.8rem 1rem;
136
+ &[data-tone] { border-color: var(--ui-tone); }
137
+ }
138
+
139
+ [data-ui="switch"] {
140
+ --ui-sw-bd: var(--ui-border-w) solid var(--ui-line);
141
+ --ui-sw-bg: var(--ui-surface);
142
+ --ui-sw-th: var(--ui-line);
143
+ --ui-sw-onbd: var(--ui-line);
144
+ }
145
+
146
+ [data-ui="radio-group"] {
147
+ --ui-rg-lf: 800 0.82rem/1.2 var(--ui-font-body);
148
+ --ui-rg-lc: uppercase;
149
+ --ui-rg-lfg: var(--ui-ink);
150
+ }
151
+
152
+ dialog[data-ui="command"] {
153
+ --ui-cmd-ibb: var(--ui-border-w) solid var(--ui-line);
154
+ --ui-cmd-ibg: var(--ui-paper);
155
+ --ui-cmd-if: 600 0.95rem/1.3 var(--ui-font-body);
156
+ --ui-cmd-mf: 600 0.9rem/1.3 var(--ui-font-body);
157
+ --ui-cmd-hbg: var(--ui-accent);
158
+ --ui-cmd-hfg: var(--ui-on-accent);
159
+ & [data-part="kbd"] { color: currentColor; border: var(--ui-border-w) solid currentColor; font-weight: 700; }
160
+ }
161
+
162
+ [data-ui="datatable"] {
163
+ --ui-srch-bd: var(--ui-border-w) solid var(--ui-line);
164
+ --ui-tbl-bd: var(--ui-border-w) solid var(--ui-line);
165
+ --ui-tbl-bg: var(--ui-surface);
166
+ --ui-th-bb: var(--ui-border-w) solid var(--ui-line);
167
+ --ui-tbl-sel: color-mix(in srgb, var(--ui-accent) 16%, var(--ui-surface));
168
+ & table { box-shadow: var(--ui-shadow); }
169
+ & th { background: var(--ui-paper); text-transform: uppercase; font-weight: 800; }
170
+ }
171
+ }
@@ -0,0 +1,137 @@
1
+ // Static scanner detecting which `@prunellejs/ui` component parts a set of source files renders.
2
+ // Checks both named imports from `@prunellejs/ui` and literal `data-ui="..."` / `data-layout="..."` attributes.
3
+
4
+ import type { UiComponentPart } from './split'
5
+ import { existsSync, readFileSync } from 'node:fs'
6
+ import { resolve } from 'node:path'
7
+ import { UI_COMPONENT_PARTS, UI_EXPORT_TO_PARTS } from './split'
8
+
9
+ const PART_SET = new Set<string>(UI_COMPONENT_PARTS)
10
+ const UI_SRC_DIR = resolve(import.meta.dir, '..')
11
+
12
+ const IMPORT_STMT_RE = /import\s+(?!type\b)([\s\S]*?)\s+from\s+['"]([^'"]+)['"]/g
13
+ const NAMED_LIST_RE = /\{([\s\S]*?)\}/
14
+ const NAMESPACE_RE = /\*\s+as\s+[A-Za-z_$]/
15
+ const DATA_UI_ATTR_RE = /\bdata-ui\s*=\s*(?:["']([^"']+)["']|\{([^}]*)\})/g
16
+ const DATA_LAYOUT_ATTR_RE = /\bdata-layout\s*=/
17
+ const STATIC_QUOTED_RE = /^\s*["']([^"']+)["']\s*$/
18
+
19
+ const DATA_UI_ALIASES: Readonly<Record<string, readonly UiComponentPart[]>> = {
20
+ 'table': ['datatable'],
21
+ 'toaster': ['toast'],
22
+ 'dialog-root': ['dialog', 'button', 'layout'],
23
+ 'drawer-root': ['drawer', 'button', 'layout'],
24
+ 'command-root': ['command', 'button'],
25
+ }
26
+
27
+ function isUiImportSource(specifier: string): boolean {
28
+ if (specifier === '@prunellejs/ui')
29
+ return true
30
+ // Tests and monorepo apps may import the workspace package entry directly.
31
+ return specifier.endsWith('/packages/ui/src/index.ts') || specifier.endsWith('/packages/ui/src/index')
32
+ }
33
+
34
+ function isInsideUiPackage(file: string): boolean {
35
+ const norm = file.replace(/\\/g, '/')
36
+ return norm.startsWith(`${UI_SRC_DIR.replace(/\\/g, '/')}/`)
37
+ || norm.includes('/node_modules/@prunellejs/ui/')
38
+ }
39
+
40
+ function addPartForAttr(value: string, out: Set<UiComponentPart>): void {
41
+ const aliased = DATA_UI_ALIASES[value]
42
+ if (aliased !== undefined) {
43
+ for (const p of aliased)
44
+ out.add(p)
45
+ return
46
+ }
47
+ if (PART_SET.has(value))
48
+ out.add(value as UiComponentPart)
49
+ }
50
+
51
+ export interface FileUiScan {
52
+ parts: readonly UiComponentPart[]
53
+ all: boolean
54
+ }
55
+
56
+ function scanSingleFile(file: string): FileUiScan {
57
+ if (isInsideUiPackage(file) || !existsSync(file))
58
+ return { parts: [], all: false }
59
+
60
+ const code = readFileSync(file, 'utf8')
61
+ const parts = new Set<UiComponentPart>()
62
+ let all = false
63
+
64
+ for (const match of code.matchAll(IMPORT_STMT_RE)) {
65
+ const clause = match[1]!
66
+ const source = match[2]!
67
+ if (!isUiImportSource(source))
68
+ continue
69
+ if (NAMESPACE_RE.test(clause)) {
70
+ all = true
71
+ break
72
+ }
73
+ const named = NAMED_LIST_RE.exec(clause)
74
+ if (named !== null) {
75
+ for (const rawSpec of named[1]!.split(',')) {
76
+ const spec = rawSpec.trim()
77
+ if (spec === '' || spec.startsWith('type '))
78
+ continue
79
+ const imported = spec.split(/\s+as\s+/)[0]!.trim()
80
+ const mapped = UI_EXPORT_TO_PARTS[imported]
81
+ if (mapped !== undefined) {
82
+ for (const p of mapped)
83
+ parts.add(p)
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ if (!all) {
90
+ if (DATA_LAYOUT_ATTR_RE.test(code))
91
+ parts.add('layout')
92
+ for (const match of code.matchAll(DATA_UI_ATTR_RE)) {
93
+ const literal = match[1]
94
+ if (literal !== undefined) {
95
+ addPartForAttr(literal, parts)
96
+ continue
97
+ }
98
+ const expr = match[2] ?? ''
99
+ const staticMatch = STATIC_QUOTED_RE.exec(expr)
100
+ if (staticMatch !== null) {
101
+ addPartForAttr(staticMatch[1]!, parts)
102
+ }
103
+ else {
104
+ all = true
105
+ break
106
+ }
107
+ }
108
+ }
109
+
110
+ return all
111
+ ? { parts: UI_COMPONENT_PARTS, all: true }
112
+ : { parts: [...parts], all: false }
113
+ }
114
+
115
+ /** Creates a per-build cached scanner resolving the set of `@prunellejs/ui` component parts used by a list of source files. */
116
+ export function createUiPartScanner(): (files: readonly string[]) => Set<UiComponentPart> {
117
+ const cache = new Map<string, FileUiScan>()
118
+ const scan = (file: string): FileUiScan => {
119
+ let entry = cache.get(file)
120
+ if (entry === undefined) {
121
+ entry = scanSingleFile(file)
122
+ cache.set(file, entry)
123
+ }
124
+ return entry
125
+ }
126
+ return (files: readonly string[]): Set<UiComponentPart> => {
127
+ const out = new Set<UiComponentPart>()
128
+ for (const file of files) {
129
+ const res = scan(file)
130
+ if (res.all)
131
+ return new Set(UI_COMPONENT_PARTS)
132
+ for (const p of res.parts)
133
+ out.add(p)
134
+ }
135
+ return out
136
+ }
137
+ }
@@ -0,0 +1,381 @@
1
+ // Per-component CSS assembly and grammar splitting for @prunellejs/ui.
2
+ // Keeps `base.css`, structural `components/*.css`, tokenized `themed/*.css`, and thin `grammars/*.css`
3
+ // composable both as full self-contained sheets and as minimal per-route component subsets.
4
+
5
+ import { existsSync, readFileSync } from 'node:fs'
6
+ import { join } from 'node:path'
7
+
8
+ export type GrammarName = 'bare' | 'editorial' | 'workshop' | 'console' | 'soft' | 'orbit' | 'prunelle'
9
+
10
+ export const GRAMMAR_NAMES: readonly GrammarName[] = ['bare', 'editorial', 'workshop', 'console', 'soft', 'orbit', 'prunelle']
11
+
12
+ export const UI_COMPONENT_PARTS = [
13
+ 'layout',
14
+ 'button',
15
+ 'card',
16
+ 'badge',
17
+ 'notice',
18
+ 'field',
19
+ 'checkbox',
20
+ 'switch',
21
+ 'radio-group',
22
+ 'progress',
23
+ 'accordion',
24
+ 'tabs',
25
+ 'dialog',
26
+ 'drawer',
27
+ 'command',
28
+ 'popover',
29
+ 'menu',
30
+ 'select',
31
+ 'toast',
32
+ 'datatable',
33
+ ] as const
34
+
35
+ export type UiComponentPart = (typeof UI_COMPONENT_PARTS)[number]
36
+
37
+ const PART_SET = new Set<string>(UI_COMPONENT_PARTS)
38
+
39
+ /** Maps every `@prunellejs/ui` TSX/runtime export to the CSS component parts it renders (including transitive parts). */
40
+ export const UI_EXPORT_TO_PARTS: Readonly<Record<string, readonly UiComponentPart[]>> = {
41
+ Stack: ['layout'],
42
+ Cluster: ['layout'],
43
+ Grid: ['layout'],
44
+ Sidebar: ['layout'],
45
+ Split: ['layout'],
46
+ Button: ['button'],
47
+ Card: ['card'],
48
+ CardHeader: ['card'],
49
+ CardBody: ['card'],
50
+ CardFooter: ['card'],
51
+ Badge: ['badge'],
52
+ Notice: ['notice'],
53
+ Field: ['field'],
54
+ Checkbox: ['checkbox'],
55
+ Switch: ['switch'],
56
+ RadioGroup: ['radio-group'],
57
+ Progress: ['progress'],
58
+ Accordion: ['accordion'],
59
+ AccordionItem: ['accordion'],
60
+ Popover: ['popover', 'button'],
61
+ Dialog: ['dialog', 'button', 'layout'],
62
+ ControlledDialog: ['dialog', 'button', 'layout'],
63
+ Drawer: ['drawer', 'button', 'layout'],
64
+ ControlledDrawer: ['drawer', 'button', 'layout'],
65
+ Tabs: ['tabs'],
66
+ Menu: ['menu', 'button'],
67
+ Select: ['select'],
68
+ CommandPalette: ['command', 'button'],
69
+ DataTable: ['datatable', 'layout', 'button'],
70
+ Toaster: ['toast'],
71
+ toast: ['toast'],
72
+ }
73
+
74
+ const STYLES_DIR = import.meta.dir
75
+
76
+ export function grammarCssPath(grammar: GrammarName): string {
77
+ return grammar === 'bare'
78
+ ? join(STYLES_DIR, 'bare.css')
79
+ : join(STYLES_DIR, 'grammars', `${grammar}.css`)
80
+ }
81
+
82
+ interface TaggedBlock {
83
+ css: string
84
+ parts: readonly UiComponentPart[] | null
85
+ }
86
+
87
+ interface ParsedGrammar {
88
+ header: string
89
+ topRules: TaggedBlock[]
90
+ baseBlocks: string[]
91
+ partBlocks: TaggedBlock[]
92
+ }
93
+
94
+ const DATA_UI_RE = /\[data-ui="([^"]+)"\]/g
95
+ const DATA_LAYOUT_RE = /\[data-layout(?:="[^"]*")?\]/
96
+ const PART_TAG_RE = /\/\*\s*@part:\s*([^*]+?)\s*\*\//
97
+ const LEADING_COMMENT_RE = /^\s*(\/\*[\s\S]*?\*\/)/
98
+
99
+ function resolveDataUiPart(name: string): UiComponentPart | undefined {
100
+ if (name === 'table')
101
+ return 'datatable'
102
+ if (name === 'toaster')
103
+ return 'toast'
104
+ return PART_SET.has(name) ? (name as UiComponentPart) : undefined
105
+ }
106
+
107
+ function detectBlockParts(prelude: string, partComment: string | undefined): readonly UiComponentPart[] | null {
108
+ if (partComment !== undefined) {
109
+ const tagged = partComment
110
+ .split(',')
111
+ .map(s => s.trim())
112
+ .filter((s): s is UiComponentPart => PART_SET.has(s))
113
+ return tagged.length > 0 ? tagged : null
114
+ }
115
+ const found = new Set<UiComponentPart>()
116
+ if (DATA_LAYOUT_RE.test(prelude))
117
+ found.add('layout')
118
+ for (const match of prelude.matchAll(DATA_UI_RE)) {
119
+ const part = resolveDataUiPart(match[1]!)
120
+ if (part !== undefined)
121
+ found.add(part)
122
+ }
123
+ return found.size > 0 ? [...found] : null
124
+ }
125
+
126
+ interface RawBlock {
127
+ prelude: string
128
+ full: string
129
+ partComment: string | undefined
130
+ }
131
+
132
+ function scanTopBlocks(css: string): RawBlock[] {
133
+ const blocks: RawBlock[] = []
134
+ let i = 0
135
+ const n = css.length
136
+ let pendingPart: string | undefined
137
+
138
+ while (i < n) {
139
+ while (i < n && /\s/.test(css[i]!))
140
+ i++
141
+ if (i >= n)
142
+ break
143
+ if (css[i] === '/' && css[i + 1] === '*') {
144
+ const end = css.indexOf('*/', i + 2)
145
+ const commentEnd = end === -1 ? n : end + 2
146
+ const comment = css.slice(i, commentEnd)
147
+ const tag = comment.match(PART_TAG_RE)
148
+ if (tag !== null)
149
+ pendingPart = tag[1]
150
+ i = commentEnd
151
+ continue
152
+ }
153
+
154
+ const start = i
155
+ let depth = 0
156
+ let braceStart = -1
157
+ let quote: string | null = null
158
+
159
+ while (i < n) {
160
+ const ch = css[i]!
161
+ if (quote !== null) {
162
+ if (ch === '\\')
163
+ i++
164
+ else if (ch === quote)
165
+ quote = null
166
+ }
167
+ else if (ch === '"' || ch === '\'') {
168
+ quote = ch
169
+ }
170
+ else if (ch === '/' && css[i + 1] === '*') {
171
+ const end = css.indexOf('*/', i + 2)
172
+ i = end === -1 ? n - 1 : end + 1
173
+ }
174
+ else if (ch === '{') {
175
+ if (depth === 0)
176
+ braceStart = i
177
+ depth++
178
+ }
179
+ else if (ch === '}') {
180
+ depth--
181
+ if (depth === 0) {
182
+ i++
183
+ break
184
+ }
185
+ }
186
+ else if (ch === ';' && depth === 0) {
187
+ i++
188
+ break
189
+ }
190
+ i++
191
+ }
192
+
193
+ const full = css.slice(start, i).trim()
194
+ if (full.length > 0 && !full.startsWith('@import')) {
195
+ const prelude = braceStart === -1 ? full : css.slice(start, braceStart).trim()
196
+ blocks.push({ prelude, full, partComment: pendingPart })
197
+ }
198
+ pendingPart = undefined
199
+ }
200
+ return blocks
201
+ }
202
+
203
+ function unwrapLayerKui(css: string): string {
204
+ const blocks = scanTopBlocks(css)
205
+ const inner: string[] = []
206
+ for (const b of blocks) {
207
+ if (/^@layer\s+kui\b/.test(b.prelude)) {
208
+ const open = b.full.indexOf('{')
209
+ const close = b.full.lastIndexOf('}')
210
+ if (open !== -1 && close > open)
211
+ inner.push(b.full.slice(open + 1, close).trim())
212
+ }
213
+ else {
214
+ inner.push(b.full)
215
+ }
216
+ }
217
+ return inner.join('\n')
218
+ }
219
+
220
+ let cachedBaseBody: string | undefined
221
+ const cachedComponentBodies = new Map<UiComponentPart, string>()
222
+ const cachedThemedBodies = new Map<UiComponentPart, string | null>()
223
+ const cachedGrammars = new Map<GrammarName, ParsedGrammar>()
224
+
225
+ function readBaseBody(): string {
226
+ if (cachedBaseBody === undefined)
227
+ cachedBaseBody = unwrapLayerKui(readFileSync(join(STYLES_DIR, 'base.css'), 'utf8'))
228
+ return cachedBaseBody
229
+ }
230
+
231
+ function readComponentBody(part: UiComponentPart): string {
232
+ let body = cachedComponentBodies.get(part)
233
+ if (body === undefined) {
234
+ body = unwrapLayerKui(readFileSync(join(STYLES_DIR, 'components', `${part}.css`), 'utf8'))
235
+ cachedComponentBodies.set(part, body)
236
+ }
237
+ return body
238
+ }
239
+
240
+ function readThemedBody(part: UiComponentPart): string | null {
241
+ let body = cachedThemedBodies.get(part)
242
+ if (body === undefined) {
243
+ const file = join(STYLES_DIR, 'themed', `${part}.css`)
244
+ body = existsSync(file) ? unwrapLayerKui(readFileSync(file, 'utf8')) : null
245
+ cachedThemedBodies.set(part, body)
246
+ }
247
+ return body
248
+ }
249
+
250
+ function parseGrammar(grammar: GrammarName): ParsedGrammar {
251
+ const existing = cachedGrammars.get(grammar)
252
+ if (existing !== undefined)
253
+ return existing
254
+
255
+ const raw = readFileSync(grammarCssPath(grammar), 'utf8')
256
+ const header = raw.match(LEADING_COMMENT_RE)?.[1] ?? ''
257
+ if (grammar === 'bare') {
258
+ const parsed: ParsedGrammar = { header, topRules: [], baseBlocks: [], partBlocks: [] }
259
+ cachedGrammars.set(grammar, parsed)
260
+ return parsed
261
+ }
262
+
263
+ const topRules: TaggedBlock[] = []
264
+ const baseBlocks: string[] = []
265
+ const partBlocks: TaggedBlock[] = []
266
+
267
+ for (const top of scanTopBlocks(raw)) {
268
+ if (/^@layer\s+kui\b/.test(top.prelude)) {
269
+ const open = top.full.indexOf('{')
270
+ const close = top.full.lastIndexOf('}')
271
+ if (open === -1 || close <= open)
272
+ continue
273
+ const inner = top.full.slice(open + 1, close)
274
+ for (const blk of scanTopBlocks(inner)) {
275
+ const parts = detectBlockParts(blk.prelude, blk.partComment)
276
+ if (parts === null)
277
+ baseBlocks.push(blk.full)
278
+ else
279
+ partBlocks.push({ css: blk.full, parts })
280
+ }
281
+ }
282
+ else {
283
+ topRules.push({ css: top.full, parts: detectBlockParts(top.prelude, top.partComment) })
284
+ }
285
+ }
286
+
287
+ const parsed: ParsedGrammar = { header, topRules, baseBlocks, partBlocks }
288
+ cachedGrammars.set(grammar, parsed)
289
+ return parsed
290
+ }
291
+
292
+ function matchesAnyPart(blockParts: readonly UiComponentPart[] | null, wanted: ReadonlySet<UiComponentPart>): boolean {
293
+ if (blockParts === null)
294
+ return false
295
+ for (const p of blockParts) {
296
+ if (wanted.has(p))
297
+ return true
298
+ }
299
+ return false
300
+ }
301
+
302
+ /** Returns `base.css` plus the grammar's global tokens & root/body rules (excluding component-specific blocks). */
303
+ export function renderGrammarBaseCss(grammar: GrammarName): string {
304
+ const parsed = parseGrammar(grammar)
305
+ const out: string[] = []
306
+ if (parsed.header)
307
+ out.push(parsed.header)
308
+ for (const rule of parsed.topRules) {
309
+ if (rule.parts === null)
310
+ out.push(rule.css)
311
+ }
312
+ const layerBody = [readBaseBody(), ...parsed.baseBlocks].filter(Boolean).join('\n\n')
313
+ out.push(`@layer kui {\n${layerBody}\n}`)
314
+ return `${out.join('\n\n')}\n`
315
+ }
316
+
317
+ /** Returns ONLY the component CSS (structural + tokenized + grammar overrides) for `parts`, in deterministic cascade order inside `@layer kui`. */
318
+ export function renderGrammarPartsCss(grammar: GrammarName, parts: Iterable<UiComponentPart>): string {
319
+ const wanted = new Set<UiComponentPart>()
320
+ for (const p of parts) {
321
+ if (PART_SET.has(p))
322
+ wanted.add(p)
323
+ }
324
+ if (wanted.size === 0)
325
+ return ''
326
+
327
+ const ordered = UI_COMPONENT_PARTS.filter(p => wanted.has(p))
328
+ const parsed = parseGrammar(grammar)
329
+ const out: string[] = []
330
+
331
+ for (const rule of parsed.topRules) {
332
+ if (matchesAnyPart(rule.parts, wanted))
333
+ out.push(rule.css)
334
+ }
335
+
336
+ const layerChunks: string[] = []
337
+ for (const part of ordered)
338
+ layerChunks.push(readComponentBody(part))
339
+
340
+ if (grammar !== 'bare') {
341
+ for (const part of ordered) {
342
+ const themed = readThemedBody(part)
343
+ if (themed !== null)
344
+ layerChunks.push(themed)
345
+ }
346
+ for (const blk of parsed.partBlocks) {
347
+ if (matchesAnyPart(blk.parts, wanted))
348
+ layerChunks.push(blk.css)
349
+ }
350
+ }
351
+
352
+ out.push(`@layer kui {\n${layerChunks.filter(Boolean).join('\n\n')}\n}`)
353
+ return `${out.join('\n\n')}\n`
354
+ }
355
+
356
+ /** Returns the complete self-contained CSS source for a grammar (with all `@import`s resolved). */
357
+ export function renderGrammarCss(grammar: GrammarName): string {
358
+ const parsed = parseGrammar(grammar)
359
+ const out: string[] = []
360
+ if (parsed.header)
361
+ out.push(parsed.header)
362
+ for (const rule of parsed.topRules)
363
+ out.push(rule.css)
364
+
365
+ const layerChunks: string[] = [readBaseBody(), ...parsed.baseBlocks]
366
+ for (const part of UI_COMPONENT_PARTS)
367
+ layerChunks.push(readComponentBody(part))
368
+
369
+ if (grammar !== 'bare') {
370
+ for (const part of UI_COMPONENT_PARTS) {
371
+ const themed = readThemedBody(part)
372
+ if (themed !== null)
373
+ layerChunks.push(themed)
374
+ }
375
+ for (const blk of parsed.partBlocks)
376
+ layerChunks.push(blk.css)
377
+ }
378
+
379
+ out.push(`@layer kui {\n${layerChunks.filter(Boolean).join('\n\n')}\n}`)
380
+ return `${out.join('\n\n')}\n`
381
+ }