nitrowind 0.0.0-stage → 0.1.0
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/CHANGELOG.md +23 -0
- package/LICENSE +21 -0
- package/README.md +44 -2
- package/css.d.ts +6 -0
- package/dist/color.d.ts +24 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +158 -0
- package/dist/color.js.map +1 -0
- package/dist/core.d.ts +121 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +1026 -0
- package/dist/core.js.map +1 -0
- package/dist/css/compile.d.ts +18 -0
- package/dist/css/compile.d.ts.map +1 -0
- package/dist/css/compile.js +1018 -0
- package/dist/css/compile.js.map +1 -0
- package/dist/css/declarations.d.ts +18 -0
- package/dist/css/declarations.d.ts.map +1 -0
- package/dist/css/declarations.js +1173 -0
- package/dist/css/declarations.js.map +1 -0
- package/dist/css/index.d.ts +7 -0
- package/dist/css/index.d.ts.map +1 -0
- package/dist/css/index.js +7 -0
- package/dist/css/index.js.map +1 -0
- package/dist/css/parser.d.ts +9 -0
- package/dist/css/parser.d.ts.map +1 -0
- package/dist/css/parser.js +188 -0
- package/dist/css/parser.js.map +1 -0
- package/dist/css/tokenizer.d.ts +14 -0
- package/dist/css/tokenizer.d.ts.map +1 -0
- package/dist/css/tokenizer.js +200 -0
- package/dist/css/tokenizer.js.map +1 -0
- package/dist/css/types.d.ts +128 -0
- package/dist/css/types.d.ts.map +1 -0
- package/dist/css/types.js +19 -0
- package/dist/css/types.js.map +1 -0
- package/dist/css/values.d.ts +37 -0
- package/dist/css/values.d.ts.map +1 -0
- package/dist/css/values.js +343 -0
- package/dist/css/values.js.map +1 -0
- package/dist/environment.d.ts +5 -0
- package/dist/environment.d.ts.map +1 -0
- package/dist/environment.js +99 -0
- package/dist/environment.js.map +1 -0
- package/dist/hooks.d.ts +22 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +140 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +17 -0
- package/dist/jsx-dev-runtime.d.ts.map +1 -0
- package/dist/jsx-dev-runtime.js +12 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-namespace.d.ts +29 -0
- package/dist/jsx-namespace.d.ts.map +1 -0
- package/dist/jsx-namespace.js +2 -0
- package/dist/jsx-namespace.js.map +1 -0
- package/dist/jsx-runtime.d.ts +22 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +21 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/math.d.ts +50 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.js +442 -0
- package/dist/math.js.map +1 -0
- package/dist/plugin.d.ts +45 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/plugin.js +172 -0
- package/dist/plugin.js.map +1 -0
- package/dist/props.d.ts +29 -0
- package/dist/props.d.ts.map +1 -0
- package/dist/props.js +163 -0
- package/dist/props.js.map +1 -0
- package/dist/scope.d.ts +99 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +228 -0
- package/dist/scope.js.map +1 -0
- package/dist/store.d.ts +18 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +244 -0
- package/dist/store.js.map +1 -0
- package/dist/tokens/index.d.ts +12 -0
- package/dist/tokens/index.d.ts.map +1 -0
- package/dist/tokens/index.js +237 -0
- package/dist/tokens/index.js.map +1 -0
- package/dist/tokens/tailwind.generated.d.ts +3 -0
- package/dist/tokens/tailwind.generated.d.ts.map +1 -0
- package/dist/tokens/tailwind.generated.js +610 -0
- package/dist/tokens/tailwind.generated.js.map +1 -0
- package/dist/tokens/types.d.ts +95 -0
- package/dist/tokens/types.d.ts.map +1 -0
- package/dist/tokens/types.js +2 -0
- package/dist/tokens/types.js.map +1 -0
- package/dist/transform.d.ts +22 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +79 -0
- package/dist/transform.js.map +1 -0
- package/dist/transition.d.ts +7 -0
- package/dist/transition.d.ts.map +1 -0
- package/dist/transition.js +355 -0
- package/dist/transition.js.map +1 -0
- package/dist/types.d.ts +214 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utilities.d.ts +109 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +2000 -0
- package/dist/utilities.js.map +1 -0
- package/dist/variables.d.ts +65 -0
- package/dist/variables.d.ts.map +1 -0
- package/dist/variables.js +408 -0
- package/dist/variables.js.map +1 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +3 -0
- package/dist/version.js.map +1 -0
- package/dist/withNitroWind.d.ts +11 -0
- package/dist/withNitroWind.d.ts.map +1 -0
- package/dist/withNitroWind.js +27 -0
- package/dist/withNitroWind.js.map +1 -0
- package/package.json +89 -4
- package/src/color.ts +164 -0
- package/src/core.ts +1248 -0
- package/src/css/compile.ts +1198 -0
- package/src/css/declarations.ts +1141 -0
- package/src/css/index.ts +22 -0
- package/src/css/parser.ts +196 -0
- package/src/css/tokenizer.ts +227 -0
- package/src/css/types.ts +178 -0
- package/src/css/values.ts +330 -0
- package/src/environment.ts +112 -0
- package/src/hooks.ts +190 -0
- package/src/index.ts +61 -0
- package/src/jsx-dev-runtime.ts +43 -0
- package/src/jsx-namespace.ts +23 -0
- package/src/jsx-runtime.ts +39 -0
- package/src/math.ts +488 -0
- package/src/plugin.ts +230 -0
- package/src/props.ts +173 -0
- package/src/scope.ts +331 -0
- package/src/store.ts +288 -0
- package/src/tokens/index.ts +252 -0
- package/src/tokens/tailwind.generated.ts +613 -0
- package/src/tokens/types.ts +99 -0
- package/src/transform.ts +115 -0
- package/src/transition.ts +414 -0
- package/src/types.ts +248 -0
- package/src/utilities.ts +1992 -0
- package/src/variables.ts +509 -0
- package/src/version.ts +2 -0
- package/src/withNitroWind.ts +36 -0
package/src/variables.ts
ADDED
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS variable storage, layering, `var()` expansion with cycle detection,
|
|
3
|
+
* and class-level variable syntax parsing.
|
|
4
|
+
*/
|
|
5
|
+
import type { CSSVariables, CSSVariableValue, ThemeDefinition } from './types'
|
|
6
|
+
|
|
7
|
+
export type TypeHint = 'color' | 'length'
|
|
8
|
+
|
|
9
|
+
export interface ParsedDynamicValue {
|
|
10
|
+
/** Expression ready for `var()` expansion (e.g. `var(--brand)` or `#ff0` or `rgb(var(--r) 0 0)`). */
|
|
11
|
+
readonly expr: string
|
|
12
|
+
/** Explicit `(color:...)` / `(length:...)` / `[color:...]` / `[length:...]` hint, if any. */
|
|
13
|
+
readonly hint: TypeHint | null
|
|
14
|
+
/** True when the expression comes from `(--x)` shorthand or contains `var(...)`. */
|
|
15
|
+
readonly isVar: boolean
|
|
16
|
+
/** True when written with `(...)` shorthand rather than `[...]` arbitrary syntax. */
|
|
17
|
+
readonly isShorthand: boolean
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type ExpandResult =
|
|
21
|
+
| { readonly ok: true; readonly value: string; readonly variables: readonly string[] }
|
|
22
|
+
| { readonly ok: false; readonly kind: 'missing' | 'cycle'; readonly variable?: string }
|
|
23
|
+
|
|
24
|
+
export interface VariableRegistry {
|
|
25
|
+
lookupRaw(
|
|
26
|
+
name: string,
|
|
27
|
+
theme: string,
|
|
28
|
+
scope?: Readonly<CSSVariables>,
|
|
29
|
+
outerScope?: Readonly<CSSVariables>,
|
|
30
|
+
): CSSVariableValue | undefined
|
|
31
|
+
expandExpression(
|
|
32
|
+
expr: string,
|
|
33
|
+
theme: string,
|
|
34
|
+
scope?: Readonly<CSSVariables>,
|
|
35
|
+
options?: { outerScope?: Readonly<CSSVariables> },
|
|
36
|
+
): ExpandResult
|
|
37
|
+
expandVariable(
|
|
38
|
+
name: string,
|
|
39
|
+
theme: string,
|
|
40
|
+
scope?: Readonly<CSSVariables>,
|
|
41
|
+
options?: { outerScope?: Readonly<CSSVariables> },
|
|
42
|
+
): ExpandResult
|
|
43
|
+
getCSSVariable(
|
|
44
|
+
name: string,
|
|
45
|
+
theme: string,
|
|
46
|
+
scope?: Readonly<CSSVariables>,
|
|
47
|
+
outerScope?: Readonly<CSSVariables>,
|
|
48
|
+
): string | undefined
|
|
49
|
+
/**
|
|
50
|
+
* Merges runtime variable updates into the shared layer (when `targetTheme` is omitted)
|
|
51
|
+
* or a specific theme's layer. Returns affected variable names (including transitive `var()`
|
|
52
|
+
* chains) per theme across `activeTheme` and any `observedThemes`.
|
|
53
|
+
*/
|
|
54
|
+
update(
|
|
55
|
+
updates: Readonly<CSSVariables>,
|
|
56
|
+
activeTheme: string,
|
|
57
|
+
targetTheme?: string,
|
|
58
|
+
observedThemes?: Iterable<string>,
|
|
59
|
+
): ReadonlyMap<string, ReadonlySet<string>>
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const VAR_RE = /(?:^|[^A-Za-z0-9_-])var\s*\(/i
|
|
63
|
+
const HINT_RE = /^(color|length):(.*)$/
|
|
64
|
+
const VAR_NAME_RE = /^--[^\s,()[\]/]+$/
|
|
65
|
+
|
|
66
|
+
export function hasVarCall(value: string): boolean {
|
|
67
|
+
return VAR_RE.test(value)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function isValidVariableName(name: string): boolean {
|
|
71
|
+
return name.startsWith('--') && name.length >= 3 && !/\s/.test(name)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function validateVariableName(name: string): void {
|
|
75
|
+
if (!isValidVariableName(name)) {
|
|
76
|
+
throw new Error(`[nitrowind] CSS variable name must start with "--": "${name}"`)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function copyVariables(source?: Readonly<CSSVariables>): Record<string, CSSVariableValue> {
|
|
81
|
+
const out: Record<string, CSSVariableValue> = {}
|
|
82
|
+
if (!source) return out
|
|
83
|
+
for (const [name, value] of Object.entries(source)) {
|
|
84
|
+
if (value === undefined) continue
|
|
85
|
+
validateVariableName(name)
|
|
86
|
+
out[name] = value
|
|
87
|
+
}
|
|
88
|
+
return out
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function findMatchingClose(input: string, openIndex: number, openChar: string, closeChar: string): number {
|
|
92
|
+
let depth = 0
|
|
93
|
+
for (let i = openIndex; i < input.length; i++) {
|
|
94
|
+
const ch = input[i]
|
|
95
|
+
if (ch === openChar) depth++
|
|
96
|
+
else if (ch === closeChar) {
|
|
97
|
+
depth--
|
|
98
|
+
if (depth === 0) return i
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return -1
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Splits `var(...)` inner arguments on the first top-level comma. */
|
|
105
|
+
function splitVarArgs(inner: string): { name: string; fallback?: string } | null {
|
|
106
|
+
let depth = 0
|
|
107
|
+
let commaIndex = -1
|
|
108
|
+
for (let i = 0; i < inner.length; i++) {
|
|
109
|
+
const ch = inner[i]
|
|
110
|
+
if (ch === '(') depth++
|
|
111
|
+
else if (ch === ')') depth = Math.max(0, depth - 1)
|
|
112
|
+
else if (ch === ',' && depth === 0) {
|
|
113
|
+
commaIndex = i
|
|
114
|
+
break
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const name = (commaIndex === -1 ? inner : inner.slice(0, commaIndex)).trim()
|
|
118
|
+
if (!VAR_NAME_RE.test(name)) return null
|
|
119
|
+
if (commaIndex === -1) return { name }
|
|
120
|
+
return { name, fallback: inner.slice(commaIndex + 1).trim() }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Parses `(--x)`, `(color:--x)`, `(length:--x)`, `(--x,#fff)`, `[var(--x)]`,
|
|
125
|
+
* `[color:var(--x)]`, `[length:16px]`, `[#ff0]`, etc.
|
|
126
|
+
* Returns `null` for plain token values.
|
|
127
|
+
*/
|
|
128
|
+
export function parseDynamicValue(value: string): ParsedDynamicValue | null {
|
|
129
|
+
if (value.startsWith('(') && value.endsWith(')')) {
|
|
130
|
+
if (findMatchingClose(value, 0, '(', ')') !== value.length - 1) return null
|
|
131
|
+
const inner = value.slice(1, -1).replace(/_/g, ' ').trim()
|
|
132
|
+
const hintMatch = HINT_RE.exec(inner)
|
|
133
|
+
const hint = (hintMatch ? hintMatch[1] : null) as TypeHint | null
|
|
134
|
+
const body = (hintMatch ? hintMatch[2]! : inner).trim()
|
|
135
|
+
if (!body.startsWith('--')) return null
|
|
136
|
+
return { expr: `var(${body})`, hint, isVar: true, isShorthand: true }
|
|
137
|
+
}
|
|
138
|
+
if (value.startsWith('[') && value.endsWith(']')) {
|
|
139
|
+
if (findMatchingClose(value, 0, '[', ']') !== value.length - 1) return null
|
|
140
|
+
const inner = value.slice(1, -1).replace(/_/g, ' ').trim()
|
|
141
|
+
const hintMatch = HINT_RE.exec(inner)
|
|
142
|
+
const hint = (hintMatch ? hintMatch[1] : null) as TypeHint | null
|
|
143
|
+
const expr = (hintMatch ? hintMatch[2]! : inner).trim()
|
|
144
|
+
return { expr, hint, isVar: hasVarCall(expr), isShorthand: false }
|
|
145
|
+
}
|
|
146
|
+
return null
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Extracts all CSS variable names (`--...`) referenced by `var(...)` calls in a raw CSS value. */
|
|
150
|
+
export function extractCSSVariableNames(cssValue: string): string[] {
|
|
151
|
+
if (!hasVarCall(cssValue)) return []
|
|
152
|
+
const names = new Set<string>()
|
|
153
|
+
const scan = (text: string): void => {
|
|
154
|
+
for (let i = 0; i < text.length; i++) {
|
|
155
|
+
if (
|
|
156
|
+
(text[i] === 'v' || text[i] === 'V') &&
|
|
157
|
+
text.slice(i, i + 4).toLowerCase() === 'var(' &&
|
|
158
|
+
(i === 0 || !/[A-Za-z0-9_-]/.test(text[i - 1]!))
|
|
159
|
+
) {
|
|
160
|
+
const close = findMatchingClose(text, i + 3, '(', ')')
|
|
161
|
+
if (close === -1) continue
|
|
162
|
+
const inner = text.slice(i + 4, close)
|
|
163
|
+
const parsed = splitVarArgs(inner)
|
|
164
|
+
if (parsed) {
|
|
165
|
+
names.add(parsed.name)
|
|
166
|
+
if (parsed.fallback !== undefined) scan(parsed.fallback)
|
|
167
|
+
}
|
|
168
|
+
i = close
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
scan(cssValue)
|
|
173
|
+
return [...names]
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Extracts all CSS variable names (`--...`) referenced by a utility token. */
|
|
177
|
+
export function extractVariableNames(utility: string): string[] {
|
|
178
|
+
const names = new Set<string>()
|
|
179
|
+
const scan = (text: string): void => {
|
|
180
|
+
for (let i = 0; i < text.length; i++) {
|
|
181
|
+
// Match `var(--name...)`
|
|
182
|
+
if (
|
|
183
|
+
(text[i] === 'v' || text[i] === 'V') &&
|
|
184
|
+
text.slice(i, i + 4).toLowerCase() === 'var(' &&
|
|
185
|
+
(i === 0 || !/[A-Za-z0-9_-]/.test(text[i - 1]!))
|
|
186
|
+
) {
|
|
187
|
+
const close = findMatchingClose(text, i + 3, '(', ')')
|
|
188
|
+
if (close === -1) continue
|
|
189
|
+
const inner = text.slice(i + 4, close).replace(/_/g, ' ')
|
|
190
|
+
const parsed = splitVarArgs(inner)
|
|
191
|
+
if (parsed) {
|
|
192
|
+
names.add(parsed.name)
|
|
193
|
+
if (parsed.fallback !== undefined) scan(parsed.fallback)
|
|
194
|
+
}
|
|
195
|
+
i = close
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
198
|
+
// Match shorthand `(--name...)`, `(color:--name...)`, `(length:--name...)`
|
|
199
|
+
if (text[i] === '(' && (i === 0 || text[i - 1] === '-' || text[i - 1] === '/')) {
|
|
200
|
+
const close = findMatchingClose(text, i, '(', ')')
|
|
201
|
+
if (close === -1) continue
|
|
202
|
+
const inner = text
|
|
203
|
+
.slice(i + 1, close)
|
|
204
|
+
.replace(/_/g, ' ')
|
|
205
|
+
.trim()
|
|
206
|
+
const hintMatch = HINT_RE.exec(inner)
|
|
207
|
+
const body = (hintMatch ? hintMatch[2]! : inner).trim()
|
|
208
|
+
if (body.startsWith('--')) {
|
|
209
|
+
const parsed = splitVarArgs(body)
|
|
210
|
+
if (parsed) {
|
|
211
|
+
names.add(parsed.name)
|
|
212
|
+
if (parsed.fallback !== undefined) scan(parsed.fallback)
|
|
213
|
+
}
|
|
214
|
+
i = close
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
scan(utility)
|
|
220
|
+
return [...names]
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const EMPTY_SCOPE: Readonly<CSSVariables> = Object.freeze({})
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Merges a variable scope's own values over its parent scope's merged values, so nested
|
|
227
|
+
* scopes merge and the innermost wins. Names without `--` warn and are skipped. Returns
|
|
228
|
+
* frozen plain data, reusing `parent` when the child sets nothing.
|
|
229
|
+
*/
|
|
230
|
+
export function mergeScopedVariables(
|
|
231
|
+
parent: Readonly<CSSVariables> | undefined,
|
|
232
|
+
own: Readonly<CSSVariables>,
|
|
233
|
+
): Readonly<CSSVariables> {
|
|
234
|
+
let merged: Record<string, CSSVariableValue> | undefined
|
|
235
|
+
for (const [name, value] of Object.entries(own)) {
|
|
236
|
+
if (value === undefined) continue
|
|
237
|
+
if (!isValidVariableName(name)) {
|
|
238
|
+
console.warn(`[nitrowind] CSS variable name must start with "--": "${name}"`)
|
|
239
|
+
continue
|
|
240
|
+
}
|
|
241
|
+
if (merged === undefined) merged = { ...parent } as Record<string, CSSVariableValue>
|
|
242
|
+
merged[name] = value
|
|
243
|
+
}
|
|
244
|
+
if (merged === undefined) return parent ?? EMPTY_SCOPE
|
|
245
|
+
return Object.freeze(merged) as Readonly<CSSVariables>
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** The CSS variable names that the `var()` calls in `values` read, sorted. */
|
|
249
|
+
export function referencedVariableNames(values: Readonly<CSSVariables>): string[] {
|
|
250
|
+
const names = new Set<string>()
|
|
251
|
+
for (const value of Object.values(values)) {
|
|
252
|
+
if (typeof value !== 'string' || !hasVarCall(value)) continue
|
|
253
|
+
for (const name of extractVariableNames(value)) names.add(name)
|
|
254
|
+
}
|
|
255
|
+
return [...names].sort()
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** Creates the variable registry for a NitroWind instance and validates variable names / theme coverage. */
|
|
259
|
+
export function createVariableRegistry(
|
|
260
|
+
shared: Readonly<CSSVariables> | undefined,
|
|
261
|
+
themes: Readonly<Record<string, ThemeDefinition>>,
|
|
262
|
+
isDev: boolean,
|
|
263
|
+
): VariableRegistry {
|
|
264
|
+
const sharedVars: Record<string, CSSVariableValue> = copyVariables(shared)
|
|
265
|
+
const themeVars: Record<string, Record<string, CSSVariableValue>> = {}
|
|
266
|
+
const allThemeDeclared = new Set<string>()
|
|
267
|
+
const themeNames = Object.keys(themes)
|
|
268
|
+
|
|
269
|
+
for (const themeName of themeNames) {
|
|
270
|
+
const vars = copyVariables(themes[themeName]?.variables)
|
|
271
|
+
themeVars[themeName] = vars
|
|
272
|
+
for (const key of Object.keys(vars)) allThemeDeclared.add(key)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (isDev && allThemeDeclared.size > 0) {
|
|
276
|
+
for (const varName of [...allThemeDeclared].sort()) {
|
|
277
|
+
if (Object.prototype.hasOwnProperty.call(sharedVars, varName)) continue
|
|
278
|
+
const missingThemes = themeNames.filter(
|
|
279
|
+
(themeName) => !Object.prototype.hasOwnProperty.call(themeVars[themeName], varName),
|
|
280
|
+
)
|
|
281
|
+
if (missingThemes.length > 0) {
|
|
282
|
+
console.warn(
|
|
283
|
+
`[nitrowind] CSS variable "${varName}" is declared in some themes but missing in: ${missingThemes.join(', ')}`,
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function lookupRaw(
|
|
290
|
+
name: string,
|
|
291
|
+
theme: string,
|
|
292
|
+
scope?: Readonly<CSSVariables>,
|
|
293
|
+
outerScope?: Readonly<CSSVariables>,
|
|
294
|
+
): CSSVariableValue | undefined {
|
|
295
|
+
if (scope !== undefined && Object.prototype.hasOwnProperty.call(scope, name)) {
|
|
296
|
+
const scoped = scope[name as keyof CSSVariables]
|
|
297
|
+
if (scoped !== undefined) return scoped
|
|
298
|
+
}
|
|
299
|
+
const perTheme = themeVars[theme]
|
|
300
|
+
if (perTheme !== undefined && Object.prototype.hasOwnProperty.call(perTheme, name)) {
|
|
301
|
+
return perTheme[name]
|
|
302
|
+
}
|
|
303
|
+
if (outerScope !== undefined && Object.prototype.hasOwnProperty.call(outerScope, name)) {
|
|
304
|
+
const outer = outerScope[name as keyof CSSVariables]
|
|
305
|
+
if (outer !== undefined) return outer
|
|
306
|
+
}
|
|
307
|
+
if (Object.prototype.hasOwnProperty.call(sharedVars, name)) {
|
|
308
|
+
return sharedVars[name]
|
|
309
|
+
}
|
|
310
|
+
return undefined
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function expandInner(
|
|
314
|
+
expr: string,
|
|
315
|
+
theme: string,
|
|
316
|
+
scope: Readonly<CSSVariables> | undefined,
|
|
317
|
+
outerScope: Readonly<CSSVariables> | undefined,
|
|
318
|
+
visiting: Set<string>,
|
|
319
|
+
readVars: Set<string>,
|
|
320
|
+
): { ok: true; value: string } | { ok: false; kind: 'missing' | 'cycle'; variable?: string } {
|
|
321
|
+
let out = ''
|
|
322
|
+
let i = 0
|
|
323
|
+
while (i < expr.length) {
|
|
324
|
+
if (
|
|
325
|
+
(expr[i] === 'v' || expr[i] === 'V') &&
|
|
326
|
+
expr.slice(i, i + 4).toLowerCase() === 'var(' &&
|
|
327
|
+
(i === 0 || !/[A-Za-z0-9_-]/.test(expr[i - 1]!))
|
|
328
|
+
) {
|
|
329
|
+
const close = findMatchingClose(expr, i + 3, '(', ')')
|
|
330
|
+
if (close === -1) {
|
|
331
|
+
out += expr.slice(i)
|
|
332
|
+
break
|
|
333
|
+
}
|
|
334
|
+
const inner = expr.slice(i + 4, close)
|
|
335
|
+
const parsed = splitVarArgs(inner)
|
|
336
|
+
if (!parsed) {
|
|
337
|
+
return { ok: false, kind: 'missing' }
|
|
338
|
+
}
|
|
339
|
+
const { name, fallback } = parsed
|
|
340
|
+
readVars.add(name)
|
|
341
|
+
if (visiting.has(name)) {
|
|
342
|
+
return { ok: false, kind: 'cycle', variable: name }
|
|
343
|
+
}
|
|
344
|
+
const raw = lookupRaw(name, theme, scope, outerScope)
|
|
345
|
+
if (raw === undefined) {
|
|
346
|
+
if (fallback !== undefined) {
|
|
347
|
+
const expandedFallback = expandInner(fallback, theme, scope, outerScope, visiting, readVars)
|
|
348
|
+
if (!expandedFallback.ok) return expandedFallback
|
|
349
|
+
out += expandedFallback.value
|
|
350
|
+
i = close + 1
|
|
351
|
+
continue
|
|
352
|
+
}
|
|
353
|
+
return { ok: false, kind: 'missing', variable: name }
|
|
354
|
+
}
|
|
355
|
+
const rawStr = String(raw)
|
|
356
|
+
visiting.add(name)
|
|
357
|
+
try {
|
|
358
|
+
const expanded = expandInner(rawStr, theme, scope, outerScope, visiting, readVars)
|
|
359
|
+
if (!expanded.ok) {
|
|
360
|
+
return expanded.variable === undefined ? { ...expanded, variable: name } : expanded
|
|
361
|
+
}
|
|
362
|
+
out += expanded.value
|
|
363
|
+
} finally {
|
|
364
|
+
visiting.delete(name)
|
|
365
|
+
}
|
|
366
|
+
i = close + 1
|
|
367
|
+
continue
|
|
368
|
+
}
|
|
369
|
+
out += expr[i]
|
|
370
|
+
i++
|
|
371
|
+
}
|
|
372
|
+
return { ok: true, value: out }
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
function expandExpression(
|
|
376
|
+
expr: string,
|
|
377
|
+
theme: string,
|
|
378
|
+
scope?: Readonly<CSSVariables>,
|
|
379
|
+
options?: { outerScope?: Readonly<CSSVariables> },
|
|
380
|
+
): ExpandResult {
|
|
381
|
+
const visiting = new Set<string>()
|
|
382
|
+
const readVars = new Set<string>()
|
|
383
|
+
const result = expandInner(expr, theme, scope, options?.outerScope, visiting, readVars)
|
|
384
|
+
if (!result.ok) return result
|
|
385
|
+
return { ok: true, value: result.value, variables: [...readVars] }
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function expandVariable(
|
|
389
|
+
name: string,
|
|
390
|
+
theme: string,
|
|
391
|
+
scope?: Readonly<CSSVariables>,
|
|
392
|
+
options?: { outerScope?: Readonly<CSSVariables> },
|
|
393
|
+
): ExpandResult {
|
|
394
|
+
return expandExpression(`var(${name})`, theme, scope, options)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function getCSSVariable(
|
|
398
|
+
name: string,
|
|
399
|
+
theme: string,
|
|
400
|
+
scope?: Readonly<CSSVariables>,
|
|
401
|
+
outerScope?: Readonly<CSSVariables>,
|
|
402
|
+
): string | undefined {
|
|
403
|
+
const result = expandVariable(name, theme, scope, { outerScope })
|
|
404
|
+
return result.ok ? result.value : undefined
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function expandAffected(directlyChanged: ReadonlySet<string>, theme: string): Set<string> {
|
|
408
|
+
const affected = new Set<string>(directlyChanged)
|
|
409
|
+
const allNames = new Set<string>(Object.keys(sharedVars))
|
|
410
|
+
const perTheme = themeVars[theme]
|
|
411
|
+
if (perTheme !== undefined) {
|
|
412
|
+
for (const key of Object.keys(perTheme)) allNames.add(key)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
const refs = new Map<string, string[]>()
|
|
416
|
+
for (const name of allNames) {
|
|
417
|
+
if (affected.has(name)) continue
|
|
418
|
+
const raw = lookupRaw(name, theme)
|
|
419
|
+
if (typeof raw === 'string' && hasVarCall(raw)) {
|
|
420
|
+
refs.set(name, extractVariableNames(raw))
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
let grew = true
|
|
425
|
+
while (grew) {
|
|
426
|
+
grew = false
|
|
427
|
+
for (const [name, deps] of refs) {
|
|
428
|
+
if (affected.has(name)) continue
|
|
429
|
+
if (deps.some((dep) => affected.has(dep))) {
|
|
430
|
+
affected.add(name)
|
|
431
|
+
grew = true
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return affected
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function update(
|
|
440
|
+
updates: Readonly<CSSVariables>,
|
|
441
|
+
activeTheme: string,
|
|
442
|
+
targetTheme?: string,
|
|
443
|
+
observedThemes?: Iterable<string>,
|
|
444
|
+
): ReadonlyMap<string, ReadonlySet<string>> {
|
|
445
|
+
if (targetTheme !== undefined && !Object.prototype.hasOwnProperty.call(themeVars, targetTheme)) {
|
|
446
|
+
throw new Error(`[nitrowind] Unregistered theme "${targetTheme}"`)
|
|
447
|
+
}
|
|
448
|
+
const target = targetTheme !== undefined ? themeVars[targetTheme]! : sharedVars
|
|
449
|
+
const themesToCheck = new Set<string>()
|
|
450
|
+
if (targetTheme === undefined) {
|
|
451
|
+
themesToCheck.add(activeTheme)
|
|
452
|
+
if (observedThemes !== undefined) {
|
|
453
|
+
for (const theme of observedThemes) themesToCheck.add(theme)
|
|
454
|
+
}
|
|
455
|
+
} else if (targetTheme === activeTheme) {
|
|
456
|
+
themesToCheck.add(targetTheme)
|
|
457
|
+
} else if (observedThemes !== undefined) {
|
|
458
|
+
for (const theme of observedThemes) {
|
|
459
|
+
if (theme === targetTheme) {
|
|
460
|
+
themesToCheck.add(targetTheme)
|
|
461
|
+
break
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
const directlyChangedByTheme = new Map<string, Set<string>>()
|
|
467
|
+
const beforeByTheme = new Map<string, CSSVariableValue | undefined>()
|
|
468
|
+
|
|
469
|
+
for (const [name, value] of Object.entries(updates)) {
|
|
470
|
+
if (value === undefined) continue
|
|
471
|
+
if (!isValidVariableName(name)) {
|
|
472
|
+
console.warn(`[nitrowind] CSS variable name must start with "--": "${name}"`)
|
|
473
|
+
continue
|
|
474
|
+
}
|
|
475
|
+
if (themesToCheck.size > 0) {
|
|
476
|
+
beforeByTheme.clear()
|
|
477
|
+
for (const theme of themesToCheck) {
|
|
478
|
+
beforeByTheme.set(theme, lookupRaw(name, theme))
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const hadOwnBefore = targetTheme !== undefined && Object.prototype.hasOwnProperty.call(target, name)
|
|
482
|
+
target[name] = value
|
|
483
|
+
for (const theme of themesToCheck) {
|
|
484
|
+
const after = lookupRaw(name, theme)
|
|
485
|
+
// Newly declaring a variable on a theme changes its layer from shared to per-theme,
|
|
486
|
+
// which can shadow an outer ScopedVariables even when the value equals sharedVars[name].
|
|
487
|
+
const newlyDeclaredOnTheme = targetTheme === theme && !hadOwnBefore
|
|
488
|
+
if (newlyDeclaredOnTheme || !Object.is(beforeByTheme.get(theme), after)) {
|
|
489
|
+
let changed = directlyChangedByTheme.get(theme)
|
|
490
|
+
if (changed === undefined) {
|
|
491
|
+
changed = new Set()
|
|
492
|
+
directlyChangedByTheme.set(theme, changed)
|
|
493
|
+
}
|
|
494
|
+
changed.add(name)
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const affectedByTheme = new Map<string, ReadonlySet<string>>()
|
|
500
|
+
for (const [theme, directlyChanged] of directlyChangedByTheme) {
|
|
501
|
+
if (directlyChanged.size > 0) {
|
|
502
|
+
affectedByTheme.set(theme, expandAffected(directlyChanged, theme))
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
return affectedByTheme
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
return { lookupRaw, expandExpression, expandVariable, getCSSVariable, update }
|
|
509
|
+
}
|
package/src/version.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ComponentType, createElement, type ReactElement } from 'react'
|
|
2
|
+
import { useResolvedClasses } from './hooks'
|
|
3
|
+
import { mergeMappedProps, normalizePropMapping } from './props'
|
|
4
|
+
import type { PropMappingTarget, ResolvedClasses, WithNitroWindProps } from './types'
|
|
5
|
+
|
|
6
|
+
type Props = Record<string, unknown>
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Wraps a custom or third-party React component so `className` (and any optional
|
|
10
|
+
* custom `*ClassName` props specified in `propMapping`) compiles reactively into
|
|
11
|
+
* NitroUI `layout`, `style`, or mapped target props.
|
|
12
|
+
*
|
|
13
|
+
* Explicit props passed to the wrapped component always win over values from classes.
|
|
14
|
+
*/
|
|
15
|
+
export function withNitroWind<P extends object, K extends string = never>(
|
|
16
|
+
Component: ComponentType<P>,
|
|
17
|
+
propMapping?: Readonly<Record<K, PropMappingTarget>>,
|
|
18
|
+
): ComponentType<WithNitroWindProps<P, K>> {
|
|
19
|
+
const entries = normalizePropMapping(propMapping)
|
|
20
|
+
|
|
21
|
+
function NitroWindWrapped(props: WithNitroWindProps<P, K>): ReactElement {
|
|
22
|
+
const record = props as Props
|
|
23
|
+
const resolvedList: ResolvedClasses[] = new Array(entries.length)
|
|
24
|
+
for (let i = 0; i < entries.length; i++) {
|
|
25
|
+
const raw = record[entries[i]![0]]
|
|
26
|
+
// biome-ignore lint/correctness/useHookAtTopLevel: `entries` is fixed for this wrapper, so the hook count never changes between renders.
|
|
27
|
+
resolvedList[i] = useResolvedClasses(typeof raw === 'string' ? raw : undefined)
|
|
28
|
+
}
|
|
29
|
+
return createElement(Component, mergeMappedProps(record, entries, resolvedList) as P)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const innerName = Component.displayName || Component.name || 'Component'
|
|
33
|
+
NitroWindWrapped.displayName = `withNitroWind(${innerName})`
|
|
34
|
+
|
|
35
|
+
return NitroWindWrapped
|
|
36
|
+
}
|