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
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { type PackedColor, parseColor } from '../color'
|
|
2
|
+
import { evaluateMathInString, hasMathFunction } from '../math'
|
|
3
|
+
import { parseBreakpointLength } from '../tokens'
|
|
4
|
+
import type { NitroFontWeight, TokenBoxShadow, TokenDropShadow } from '../tokens/types'
|
|
5
|
+
|
|
6
|
+
const ROOT_FONT_SIZE = 16
|
|
7
|
+
const LENGTH_RE = /^(-?\d*\.?\d+)(px|rem|em|pt)?$/i
|
|
8
|
+
const LENGTH_OR_PERCENT_RE = /^(-?\d*\.?\d+)(px|rem|em|pt|%)?$/i
|
|
9
|
+
const RATIO_RE = /^(\d*\.?\d+)\s*\/\s*(\d*\.?\d+)$/
|
|
10
|
+
|
|
11
|
+
const FONT_WEIGHT_KEYWORDS: Record<string, NitroFontWeight> = {
|
|
12
|
+
thin: 'regular',
|
|
13
|
+
extralight: 'regular',
|
|
14
|
+
light: 'regular',
|
|
15
|
+
normal: 'regular',
|
|
16
|
+
regular: 'regular',
|
|
17
|
+
medium: 'medium',
|
|
18
|
+
semibold: 'semibold',
|
|
19
|
+
bold: 'bold',
|
|
20
|
+
extrabold: 'bold',
|
|
21
|
+
black: 'bold',
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const MISSING_VIEWPORT = Symbol('missing-viewport')
|
|
25
|
+
|
|
26
|
+
export type ShadowLengthParser = (raw: string) => number | typeof MISSING_VIEWPORT | null
|
|
27
|
+
|
|
28
|
+
export function round(n: number): number {
|
|
29
|
+
const rounded = Math.round(n * 1e4) / 1e4
|
|
30
|
+
return rounded === 0 ? 0 : rounded
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function evalMathIfPresent(raw: string): string | null {
|
|
34
|
+
const trimmed = raw.trim()
|
|
35
|
+
if (!hasMathFunction(trimmed)) return trimmed
|
|
36
|
+
const result = evaluateMathInString(trimmed)
|
|
37
|
+
return result.ok ? result.value : null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Parses a CSS length (`px`, `pt`, `rem`, `em`, unitless, or `calc()`) into points. */
|
|
41
|
+
export function parseCSSLength(raw: string): number | null {
|
|
42
|
+
const evaluated = evalMathIfPresent(raw)
|
|
43
|
+
if (evaluated === null) return null
|
|
44
|
+
const match = LENGTH_RE.exec(evaluated.trim())
|
|
45
|
+
if (!match) return null
|
|
46
|
+
const amount = Number(match[1])
|
|
47
|
+
if (!Number.isFinite(amount)) return null
|
|
48
|
+
const unit = match[2]?.toLowerCase()
|
|
49
|
+
if (unit === 'rem' || unit === 'em') return round(amount * ROOT_FONT_SIZE)
|
|
50
|
+
return round(amount)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Parses a CSS length into points, or preserves a percentage string like `'50%'`. */
|
|
54
|
+
export function parseCSSLengthOrPercent(raw: string): number | string | null {
|
|
55
|
+
const evaluated = evalMathIfPresent(raw)
|
|
56
|
+
if (evaluated === null) return null
|
|
57
|
+
const trimmed = evaluated.trim()
|
|
58
|
+
const match = LENGTH_OR_PERCENT_RE.exec(trimmed)
|
|
59
|
+
if (!match) return null
|
|
60
|
+
const amount = Number(match[1])
|
|
61
|
+
if (!Number.isFinite(amount)) return null
|
|
62
|
+
const unit = match[2]?.toLowerCase()
|
|
63
|
+
if (unit === '%') return `${round(amount)}%`
|
|
64
|
+
if (unit === 'rem' || unit === 'em') return round(amount * ROOT_FONT_SIZE)
|
|
65
|
+
return round(amount)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Parses a CSS breakpoint length into non-negative points. */
|
|
69
|
+
export function parseCSSBreakpoint(raw: string): number | null {
|
|
70
|
+
const evaluated = evalMathIfPresent(raw)
|
|
71
|
+
if (evaluated === null) return null
|
|
72
|
+
return parseBreakpointLength(evaluated)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Parses a CSS color into a packed `0xRRGGBBAA` number. */
|
|
76
|
+
export function parseCSSColor(raw: string): PackedColor | null {
|
|
77
|
+
return parseColor(raw)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Parses a CSS aspect ratio (`16 / 9`, `1.5`, or `calc(...)`). */
|
|
81
|
+
export function parseCSSRatio(raw: string): number | null {
|
|
82
|
+
const evaluated = evalMathIfPresent(raw)
|
|
83
|
+
if (evaluated === null) return null
|
|
84
|
+
const trimmed = evaluated.trim()
|
|
85
|
+
const ratio = RATIO_RE.exec(trimmed)
|
|
86
|
+
if (ratio) {
|
|
87
|
+
const num = Number(ratio[1])
|
|
88
|
+
const den = Number(ratio[2])
|
|
89
|
+
if (!Number.isFinite(num) || !Number.isFinite(den) || num <= 0 || den <= 0) return null
|
|
90
|
+
return num / den
|
|
91
|
+
}
|
|
92
|
+
const value = Number(trimmed)
|
|
93
|
+
return trimmed !== '' && Number.isFinite(value) && value > 0 ? value : null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Parses a CSS line-height value: unitless multiplier vs explicit length in points. */
|
|
97
|
+
export function parseCSSLineHeight(
|
|
98
|
+
raw: string,
|
|
99
|
+
): { readonly kind: 'multiplier' | 'points'; readonly value: number } | null {
|
|
100
|
+
const evaluated = evalMathIfPresent(raw)
|
|
101
|
+
if (evaluated === null) return null
|
|
102
|
+
const trimmed = evaluated.trim()
|
|
103
|
+
const match = LENGTH_RE.exec(trimmed)
|
|
104
|
+
if (!match) return null
|
|
105
|
+
const amount = Number(match[1])
|
|
106
|
+
if (!Number.isFinite(amount)) return null
|
|
107
|
+
const unit = match[2]?.toLowerCase()
|
|
108
|
+
if (!unit) return { kind: 'multiplier', value: round(amount) }
|
|
109
|
+
if (unit === 'rem' || unit === 'em') return { kind: 'points', value: round(amount * ROOT_FONT_SIZE) }
|
|
110
|
+
return { kind: 'points', value: round(amount) }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Maps a CSS font-weight keyword or numeric weight (`100`..`900`) onto NitroUI's `NitroFontWeight`. */
|
|
114
|
+
export function parseCSSFontWeight(raw: string): NitroFontWeight | null {
|
|
115
|
+
const trimmed = raw.trim().toLowerCase()
|
|
116
|
+
const keyword = FONT_WEIGHT_KEYWORDS[trimmed]
|
|
117
|
+
if (keyword !== undefined) return keyword
|
|
118
|
+
if (/^[1-9]00$/.test(trimmed)) {
|
|
119
|
+
const num = Number(trimmed)
|
|
120
|
+
if (num <= 400) return 'regular'
|
|
121
|
+
if (num === 500) return 'medium'
|
|
122
|
+
if (num === 600) return 'semibold'
|
|
123
|
+
return 'bold'
|
|
124
|
+
}
|
|
125
|
+
return null
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Splits a string on top-level commas, ignoring commas inside parentheses. */
|
|
129
|
+
export function splitTopLevelCommas(raw: string): string[] {
|
|
130
|
+
const parts: string[] = []
|
|
131
|
+
let depth = 0
|
|
132
|
+
let start = 0
|
|
133
|
+
for (let i = 0; i < raw.length; i++) {
|
|
134
|
+
const ch = raw[i]
|
|
135
|
+
if (ch === '(') depth++
|
|
136
|
+
else if (ch === ')') depth = Math.max(0, depth - 1)
|
|
137
|
+
else if (ch === ',' && depth === 0) {
|
|
138
|
+
parts.push(raw.slice(start, i).trim())
|
|
139
|
+
start = i + 1
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
parts.push(raw.slice(start).trim())
|
|
143
|
+
return parts.filter(Boolean)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Splits a string on top-level whitespace, keeping parenthesized functions like `rgb(0 0 0 / 0.2)` intact. */
|
|
147
|
+
export function splitTopLevelSpaces(raw: string): string[] {
|
|
148
|
+
const parts: string[] = []
|
|
149
|
+
let depth = 0
|
|
150
|
+
let start = -1
|
|
151
|
+
for (let i = 0; i < raw.length; i++) {
|
|
152
|
+
const ch = raw[i]!
|
|
153
|
+
if (ch === '(') {
|
|
154
|
+
if (start === -1) start = i
|
|
155
|
+
depth++
|
|
156
|
+
} else if (ch === ')') {
|
|
157
|
+
depth = Math.max(0, depth - 1)
|
|
158
|
+
} else if ((ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') && depth === 0) {
|
|
159
|
+
if (start !== -1) {
|
|
160
|
+
parts.push(raw.slice(start, i))
|
|
161
|
+
start = -1
|
|
162
|
+
}
|
|
163
|
+
} else if (start === -1) {
|
|
164
|
+
start = i
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (start !== -1) parts.push(raw.slice(start))
|
|
168
|
+
return parts
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function parseBoxShadowLayer(
|
|
172
|
+
raw: string,
|
|
173
|
+
forceInset: boolean,
|
|
174
|
+
parseLengthToken: ShadowLengthParser = parseCSSLength,
|
|
175
|
+
): TokenBoxShadow | typeof MISSING_VIEWPORT | null {
|
|
176
|
+
const parts = splitTopLevelSpaces(raw.trim())
|
|
177
|
+
if (parts.length < 2) return null
|
|
178
|
+
let isInset = forceInset
|
|
179
|
+
const lengths: number[] = []
|
|
180
|
+
let color: PackedColor | null = null
|
|
181
|
+
|
|
182
|
+
for (const part of parts) {
|
|
183
|
+
if (part.toLowerCase() === 'inset') {
|
|
184
|
+
isInset = true
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
const len = parseLengthToken(part)
|
|
188
|
+
if (len === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
189
|
+
if (len !== null) {
|
|
190
|
+
lengths.push(len)
|
|
191
|
+
continue
|
|
192
|
+
}
|
|
193
|
+
const parsedColor = parseColor(part)
|
|
194
|
+
if (parsedColor !== null && color === null) {
|
|
195
|
+
color = parsedColor
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
198
|
+
return null
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (lengths.length < 2 || lengths.length > 4) return null
|
|
202
|
+
const [offsetX = 0, offsetY = 0, blurRadius = 0, spreadDistance = 0] = lengths
|
|
203
|
+
return {
|
|
204
|
+
offsetX,
|
|
205
|
+
offsetY,
|
|
206
|
+
blurRadius,
|
|
207
|
+
spreadDistance,
|
|
208
|
+
...(isInset ? { inset: true } : {}),
|
|
209
|
+
color: color ?? 0x00000040,
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Parses a CSS `box-shadow` value into `TokenBoxShadow[]`, or `null` if invalid. */
|
|
214
|
+
export function parseCSSBoxShadows(raw: string, forceInset = false): TokenBoxShadow[] | null {
|
|
215
|
+
const trimmed = raw.trim()
|
|
216
|
+
if (trimmed.toLowerCase() === 'none') return []
|
|
217
|
+
const layers = splitTopLevelCommas(trimmed)
|
|
218
|
+
if (layers.length === 0) return null
|
|
219
|
+
const out: TokenBoxShadow[] = []
|
|
220
|
+
for (const layer of layers) {
|
|
221
|
+
const parsed = parseBoxShadowLayer(layer, forceInset)
|
|
222
|
+
if (parsed === null || parsed === MISSING_VIEWPORT) return null
|
|
223
|
+
out.push(parsed)
|
|
224
|
+
}
|
|
225
|
+
return out
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export function parseDropShadowLayer(
|
|
229
|
+
raw: string,
|
|
230
|
+
parseLengthToken: ShadowLengthParser = parseCSSLength,
|
|
231
|
+
): TokenDropShadow | typeof MISSING_VIEWPORT | null {
|
|
232
|
+
const parts = splitTopLevelSpaces(raw.trim())
|
|
233
|
+
if (parts.length < 2) return null
|
|
234
|
+
const lengths: number[] = []
|
|
235
|
+
let color: PackedColor | null = null
|
|
236
|
+
|
|
237
|
+
for (const part of parts) {
|
|
238
|
+
const len = parseLengthToken(part)
|
|
239
|
+
if (len === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
240
|
+
if (len !== null) {
|
|
241
|
+
lengths.push(len)
|
|
242
|
+
continue
|
|
243
|
+
}
|
|
244
|
+
const parsedColor = parseColor(part)
|
|
245
|
+
if (parsedColor !== null && color === null) {
|
|
246
|
+
color = parsedColor
|
|
247
|
+
continue
|
|
248
|
+
}
|
|
249
|
+
return null
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (lengths.length < 2 || lengths.length > 3) return null
|
|
253
|
+
const [offsetX = 0, offsetY = 0, blur = 0] = lengths
|
|
254
|
+
return {
|
|
255
|
+
offsetX,
|
|
256
|
+
offsetY,
|
|
257
|
+
standardDeviation: round(blur / 2),
|
|
258
|
+
color: color ?? 0x00000040,
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** Parses a CSS `drop-shadow` value into `TokenDropShadow[]`, or `null` if invalid. */
|
|
263
|
+
export function parseCSSDropShadows(raw: string): TokenDropShadow[] | null {
|
|
264
|
+
const trimmed = raw.trim()
|
|
265
|
+
if (trimmed.toLowerCase() === 'none') return []
|
|
266
|
+
const layers = splitTopLevelCommas(trimmed)
|
|
267
|
+
if (layers.length === 0) return null
|
|
268
|
+
const out: TokenDropShadow[] = []
|
|
269
|
+
for (const layer of layers) {
|
|
270
|
+
let inner = layer.trim()
|
|
271
|
+
if (inner.toLowerCase().startsWith('drop-shadow(') && inner.endsWith(')')) {
|
|
272
|
+
inner = inner.slice('drop-shadow('.length, -1).trim()
|
|
273
|
+
}
|
|
274
|
+
const parsed = parseDropShadowLayer(inner)
|
|
275
|
+
if (parsed === null || parsed === MISSING_VIEWPORT) return null
|
|
276
|
+
out.push(parsed)
|
|
277
|
+
}
|
|
278
|
+
return out
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function unquote(raw: string): string | null {
|
|
282
|
+
if (raw.length < 2) return null
|
|
283
|
+
const quote = raw[0]
|
|
284
|
+
if ((quote !== '"' && quote !== "'") || raw[raw.length - 1] !== quote) return null
|
|
285
|
+
let out = ''
|
|
286
|
+
for (let i = 1; i < raw.length - 1; i++) {
|
|
287
|
+
const ch = raw[i]!
|
|
288
|
+
if (ch === '\\' && i + 1 < raw.length - 1) {
|
|
289
|
+
out += raw[++i]!
|
|
290
|
+
} else {
|
|
291
|
+
out += ch
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return out
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** Extracts the specifier string from an `@import` prelude (`'foo'`, `"foo"`, or `url(...)`). */
|
|
298
|
+
export function parseImportSpecifier(prelude: string): string | null {
|
|
299
|
+
const trimmed = prelude.trim()
|
|
300
|
+
if (trimmed.length === 0) return null
|
|
301
|
+
const quoted = unquote(trimmed)
|
|
302
|
+
if (quoted !== null) return quoted
|
|
303
|
+
const urlMatch = /^url\(\s*(.+?)\s*\)$/i.exec(trimmed)
|
|
304
|
+
if (urlMatch) {
|
|
305
|
+
const inner = urlMatch[1]!.trim()
|
|
306
|
+
return unquote(inner) ?? inner
|
|
307
|
+
}
|
|
308
|
+
return null
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Pure POSIX-style relative path resolver for cycle tracking when `resolveImport` returns a plain string. */
|
|
312
|
+
export function resolveRelativeImportKey(specifier: string, from?: string): string {
|
|
313
|
+
const baseDir = from && from.includes('/') ? from.slice(0, from.lastIndexOf('/')) : ''
|
|
314
|
+
const isAbs = baseDir.startsWith('/')
|
|
315
|
+
const rawParts = [...(baseDir ? baseDir.split('/') : []), ...specifier.split('/')]
|
|
316
|
+
const normalized: string[] = []
|
|
317
|
+
for (const part of rawParts) {
|
|
318
|
+
if (!part || part === '.') continue
|
|
319
|
+
if (part === '..') {
|
|
320
|
+
if (normalized.length > 0 && normalized[normalized.length - 1] !== '..') {
|
|
321
|
+
normalized.pop()
|
|
322
|
+
} else if (!isAbs) {
|
|
323
|
+
normalized.push('..')
|
|
324
|
+
}
|
|
325
|
+
} else {
|
|
326
|
+
normalized.push(part)
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return (isAbs ? '/' : '') + normalized.join('/')
|
|
330
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/** NitroUI-backed runtime environment. Kept apart from core.ts so tests can run without native modules. */
|
|
2
|
+
|
|
3
|
+
import { Platform as NitroPlatform } from 'nitro-ui-core'
|
|
4
|
+
import {
|
|
5
|
+
Appearance,
|
|
6
|
+
List,
|
|
7
|
+
type ListenerSubscription,
|
|
8
|
+
MaskedView,
|
|
9
|
+
ScrollView,
|
|
10
|
+
TextInput,
|
|
11
|
+
type ViewController,
|
|
12
|
+
ViewControllerContext,
|
|
13
|
+
} from 'nitro-ui-renderer'
|
|
14
|
+
import { useContext } from 'react'
|
|
15
|
+
import { setDefaultEnvironment } from './core'
|
|
16
|
+
import { registerComponent } from './transform'
|
|
17
|
+
import type { ColorScheme, NitroWindEnvironment, Unsubscribe, ViewportSize } from './types'
|
|
18
|
+
|
|
19
|
+
export function createNitroUIEnvironment(): NitroWindEnvironment {
|
|
20
|
+
let activeController: ViewController | undefined
|
|
21
|
+
let cachedViewport: ViewportSize | undefined
|
|
22
|
+
let nativeSizeSub: ListenerSubscription | undefined
|
|
23
|
+
const viewportListeners = new Set<(size: ViewportSize) => void>()
|
|
24
|
+
|
|
25
|
+
function toViewportSize(width: number, height: number): ViewportSize {
|
|
26
|
+
if (cachedViewport && cachedViewport.width === width && cachedViewport.height === height) {
|
|
27
|
+
return cachedViewport
|
|
28
|
+
}
|
|
29
|
+
cachedViewport = Object.freeze({ width, height })
|
|
30
|
+
return cachedViewport
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function emitViewport(width: number, height: number): void {
|
|
34
|
+
const next = toViewportSize(width, height)
|
|
35
|
+
for (const listener of [...viewportListeners]) listener(next)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function attachNativeSizeListener(controller: ViewController): void {
|
|
39
|
+
nativeSizeSub?.remove()
|
|
40
|
+
nativeSizeSub = controller.addOnSizeChangedListener((size) => {
|
|
41
|
+
emitViewport(size.width, size.height)
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function bindController(controller: ViewController): void {
|
|
46
|
+
if (controller === activeController) return
|
|
47
|
+
activeController = controller
|
|
48
|
+
if (viewportListeners.size > 0) {
|
|
49
|
+
attachNativeSizeListener(controller)
|
|
50
|
+
const frame = controller.view.getFrame()
|
|
51
|
+
const prev = cachedViewport
|
|
52
|
+
const next = toViewportSize(frame.width, frame.height)
|
|
53
|
+
if (next !== prev) {
|
|
54
|
+
queueMicrotask(() => {
|
|
55
|
+
for (const listener of [...viewportListeners]) listener(next)
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
// Release bundles inline `Platform.OS`, so this is a constant there.
|
|
63
|
+
platform: NitroPlatform.OS,
|
|
64
|
+
getColorScheme(): ColorScheme {
|
|
65
|
+
return Appearance.getColorScheme()
|
|
66
|
+
},
|
|
67
|
+
setColorScheme(colorScheme: ColorScheme | 'auto'): void {
|
|
68
|
+
Appearance.setColorScheme(colorScheme)
|
|
69
|
+
},
|
|
70
|
+
subscribeColorScheme(listener: (colorScheme: ColorScheme) => void): Unsubscribe {
|
|
71
|
+
const subscription = Appearance.addOnColorSchemeChangedListener(listener)
|
|
72
|
+
return () => subscription.remove()
|
|
73
|
+
},
|
|
74
|
+
getViewportSize(): ViewportSize | undefined {
|
|
75
|
+
if (!activeController) return undefined
|
|
76
|
+
const frame = activeController.view.getFrame()
|
|
77
|
+
return toViewportSize(frame.width, frame.height)
|
|
78
|
+
},
|
|
79
|
+
subscribeViewportSize(listener: (size: ViewportSize) => void): Unsubscribe {
|
|
80
|
+
viewportListeners.add(listener)
|
|
81
|
+
if (activeController && !nativeSizeSub) {
|
|
82
|
+
attachNativeSizeListener(activeController)
|
|
83
|
+
}
|
|
84
|
+
return () => {
|
|
85
|
+
viewportListeners.delete(listener)
|
|
86
|
+
if (viewportListeners.size === 0) {
|
|
87
|
+
nativeSizeSub?.remove()
|
|
88
|
+
nativeSizeSub = undefined
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
useRoot(): void {
|
|
93
|
+
const controller = useContext(ViewControllerContext)
|
|
94
|
+
if (controller && controller !== activeController) {
|
|
95
|
+
bindController(controller)
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
native: undefined,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
let registered = false
|
|
103
|
+
|
|
104
|
+
export function registerNitroUIEnvironment(): void {
|
|
105
|
+
if (registered) return
|
|
106
|
+
registered = true
|
|
107
|
+
setDefaultEnvironment(createNitroUIEnvironment())
|
|
108
|
+
registerComponent(ScrollView)
|
|
109
|
+
registerComponent(TextInput)
|
|
110
|
+
registerComponent(MaskedView)
|
|
111
|
+
registerComponent(List, { contentPaddingClassName: { edgeInsets: 'contentPadding' } })
|
|
112
|
+
}
|
package/src/hooks.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { useSyncExternalStore } from 'react'
|
|
2
|
+
import { getNitroWind, getScopedCSSVariable, variablesUseViewport } from './core'
|
|
3
|
+
import { type NitroWindScopeValue, useNitroWindScope } from './scope'
|
|
4
|
+
import type { CSSVariableName, ResolveContext, ResolvedClasses, ThemeState } from './types'
|
|
5
|
+
|
|
6
|
+
export type UseSyncStore = <T>(
|
|
7
|
+
subscribe: (onStoreChange: () => void) => () => void,
|
|
8
|
+
getSnapshot: () => T,
|
|
9
|
+
getServerSnapshot?: () => T,
|
|
10
|
+
) => T
|
|
11
|
+
|
|
12
|
+
export type UseScope = () => NitroWindScopeValue | null
|
|
13
|
+
|
|
14
|
+
const noopSubscribe: (onStoreChange: () => void) => () => void = () => () => {}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Shared hook used by `NitroWindContext` and `useTw`.
|
|
18
|
+
* Hooks run in a fixed order so adding or removing reactive classes never remounts a view.
|
|
19
|
+
*/
|
|
20
|
+
export function useResolvedClasses(
|
|
21
|
+
className: string | null | undefined,
|
|
22
|
+
useStore: UseSyncStore = useSyncExternalStore,
|
|
23
|
+
useScope?: UseScope,
|
|
24
|
+
): ResolvedClasses {
|
|
25
|
+
const nitroWind = getNitroWind()
|
|
26
|
+
nitroWind.environment.useRoot?.()
|
|
27
|
+
// biome-ignore lint/correctness/useHookAtTopLevel: `useStore` and `useScope` are fixed per caller, so the same hook runs on every render.
|
|
28
|
+
const scope = useScope ? useScope() : useStore === useSyncExternalStore ? useNitroWindScope() : null
|
|
29
|
+
const deps = nitroWind.dependencies(className)
|
|
30
|
+
const scopedTheme = scope?.theme
|
|
31
|
+
const scopedColorScheme = scope?.colorScheme
|
|
32
|
+
const scopedVariables = scope?.variables
|
|
33
|
+
const needsTheme = deps.colorScheme || deps.themes.length > 0 || deps.variables.length > 0
|
|
34
|
+
const needsStoreTheme = needsTheme && scopedTheme === undefined
|
|
35
|
+
const usesVariables = deps.variables.length > 0
|
|
36
|
+
const layers = usesVariables && scopedVariables !== undefined ? scopedVariables.getLayers() : undefined
|
|
37
|
+
const varsUseViewport =
|
|
38
|
+
usesVariables &&
|
|
39
|
+
variablesUseViewport(
|
|
40
|
+
nitroWind,
|
|
41
|
+
deps.variables,
|
|
42
|
+
scopedTheme ?? nitroWind.store.getTheme().theme,
|
|
43
|
+
layers?.inner,
|
|
44
|
+
layers?.outer,
|
|
45
|
+
)
|
|
46
|
+
const usesViewport = deps.breakpoints.length > 0 || deps.orientation || deps.viewport || varsUseViewport
|
|
47
|
+
|
|
48
|
+
const subscribeTheme = needsStoreTheme ? nitroWind.store.subscribeTheme : noopSubscribe
|
|
49
|
+
const getThemeSnapshot = (): string => {
|
|
50
|
+
if (!needsStoreTheme) return ''
|
|
51
|
+
const { theme, colorScheme } = nitroWind.store.getTheme()
|
|
52
|
+
const currentLayers = scopedVariables?.getLayers()
|
|
53
|
+
return nitroWind.signature(className, {
|
|
54
|
+
theme,
|
|
55
|
+
colorScheme,
|
|
56
|
+
variables: currentLayers?.inner,
|
|
57
|
+
outerVariables: currentLayers?.outer,
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const subscribeMotion = deps.reducedMotion ? nitroWind.store.subscribeReducedMotion : noopSubscribe
|
|
62
|
+
const getMotionSnapshot = (): boolean => (deps.reducedMotion ? nitroWind.store.getReducedMotion() : false)
|
|
63
|
+
|
|
64
|
+
const subscribeViewport = usesViewport ? nitroWind.store.subscribeViewport : noopSubscribe
|
|
65
|
+
const getViewportSnapshot = (): string => {
|
|
66
|
+
if (!usesViewport) return ''
|
|
67
|
+
if (!usesVariables) return nitroWind.signature(className, { viewport: nitroWind.store.getViewportSize() })
|
|
68
|
+
const { theme, colorScheme } =
|
|
69
|
+
scopedTheme !== undefined ? { theme: scopedTheme, colorScheme: scopedColorScheme } : nitroWind.store.getTheme()
|
|
70
|
+
const currentLayers = scopedVariables?.getLayers()
|
|
71
|
+
return nitroWind.signature(className, {
|
|
72
|
+
theme,
|
|
73
|
+
colorScheme,
|
|
74
|
+
viewport: nitroWind.store.getViewportSize(),
|
|
75
|
+
variables: currentLayers?.inner,
|
|
76
|
+
outerVariables: currentLayers?.outer,
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const subscribeVariables = usesVariables
|
|
81
|
+
? nitroWind.store.getVariablesSubscriber(deps.variables, scopedTheme)
|
|
82
|
+
: noopSubscribe
|
|
83
|
+
const subscribeScopedVariables =
|
|
84
|
+
usesVariables && scopedVariables !== undefined ? scopedVariables.getSubscriber(scopedTheme) : noopSubscribe
|
|
85
|
+
const getVariablesSnapshot = (): string => {
|
|
86
|
+
if (!usesVariables) return ''
|
|
87
|
+
const { theme, colorScheme } =
|
|
88
|
+
scopedTheme !== undefined ? { theme: scopedTheme, colorScheme: scopedColorScheme } : nitroWind.store.getTheme()
|
|
89
|
+
const currentLayers = scopedVariables?.getLayers()
|
|
90
|
+
return nitroWind.signature(className, {
|
|
91
|
+
theme,
|
|
92
|
+
colorScheme,
|
|
93
|
+
variables: currentLayers?.inner,
|
|
94
|
+
outerVariables: currentLayers?.outer,
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
useStore(subscribeTheme, getThemeSnapshot, getThemeSnapshot)
|
|
99
|
+
useStore(subscribeMotion, getMotionSnapshot, getMotionSnapshot)
|
|
100
|
+
useStore(subscribeViewport, getViewportSnapshot, getViewportSnapshot)
|
|
101
|
+
useStore(subscribeVariables, getVariablesSnapshot, getVariablesSnapshot)
|
|
102
|
+
useStore(subscribeScopedVariables, getVariablesSnapshot, getVariablesSnapshot)
|
|
103
|
+
|
|
104
|
+
const currentLayers = usesVariables && scopedVariables !== undefined ? scopedVariables.getLayers() : undefined
|
|
105
|
+
const context: ResolveContext = {
|
|
106
|
+
platform: nitroWind.environment.platform,
|
|
107
|
+
...(needsTheme
|
|
108
|
+
? scopedTheme !== undefined
|
|
109
|
+
? { theme: scopedTheme, colorScheme: scopedColorScheme }
|
|
110
|
+
: (() => {
|
|
111
|
+
const { theme, colorScheme } = nitroWind.store.getTheme()
|
|
112
|
+
return { theme, colorScheme }
|
|
113
|
+
})()
|
|
114
|
+
: undefined),
|
|
115
|
+
...(deps.reducedMotion ? { reducedMotion: nitroWind.store.getReducedMotion() } : undefined),
|
|
116
|
+
...(usesViewport ? { viewport: nitroWind.store.getViewportSize() } : undefined),
|
|
117
|
+
...(currentLayers !== undefined
|
|
118
|
+
? { variables: currentLayers.inner, outerVariables: currentLayers.outer }
|
|
119
|
+
: undefined),
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return nitroWind.resolve(className, context)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Returns `{ theme, colorScheme, preference }` and re-renders when the active theme changes. */
|
|
126
|
+
export function useTheme(useStore: UseSyncStore = useSyncExternalStore): ThemeState {
|
|
127
|
+
const nitroWind = getNitroWind()
|
|
128
|
+
return useStore(nitroWind.store.subscribeTheme, nitroWind.store.getTheme, nitroWind.store.getTheme)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Internal helper behind `useCSSVariable` allowing store/scope injection in unit tests. */
|
|
132
|
+
export function useCSSVariableWithStore(
|
|
133
|
+
nameOrNames: CSSVariableName | readonly CSSVariableName[],
|
|
134
|
+
useStore: UseSyncStore = useSyncExternalStore,
|
|
135
|
+
useScope?: UseScope,
|
|
136
|
+
): string | undefined | (string | undefined)[] {
|
|
137
|
+
const nitroWind = getNitroWind()
|
|
138
|
+
// biome-ignore lint/correctness/useHookAtTopLevel: `useStore` and `useScope` are fixed per caller, so the same hook runs on every render.
|
|
139
|
+
const scope = useScope ? useScope() : useStore === useSyncExternalStore ? useNitroWindScope() : null
|
|
140
|
+
const scopedTheme = scope?.theme
|
|
141
|
+
const scopedVariables = scope?.variables
|
|
142
|
+
const isList = Array.isArray(nameOrNames)
|
|
143
|
+
const names = isList ? (nameOrNames as readonly string[]) : [nameOrNames as string]
|
|
144
|
+
|
|
145
|
+
const readSnapshot = (): string => {
|
|
146
|
+
const theme = scopedTheme ?? nitroWind.store.getTheme().theme
|
|
147
|
+
if (names.length === 0) return ''
|
|
148
|
+
const layers = scopedVariables?.getLayers()
|
|
149
|
+
let out = ''
|
|
150
|
+
for (let i = 0; i < names.length; i++) {
|
|
151
|
+
const val = getScopedCSSVariable(nitroWind, names[i]! as CSSVariableName, theme, layers?.inner, layers?.outer)
|
|
152
|
+
if (i > 0) out += '\x01'
|
|
153
|
+
out += val === undefined ? '\x00u' : `\x00s${val}`
|
|
154
|
+
}
|
|
155
|
+
return out
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const subscribeTheme = scopedTheme === undefined && names.length > 0 ? nitroWind.store.subscribeTheme : noopSubscribe
|
|
159
|
+
const subscribeVariables =
|
|
160
|
+
names.length > 0 ? nitroWind.store.getVariablesSubscriber(names, scopedTheme) : noopSubscribe
|
|
161
|
+
const subscribeScopedVariables =
|
|
162
|
+
names.length > 0 && scopedVariables !== undefined ? scopedVariables.getSubscriber(scopedTheme) : noopSubscribe
|
|
163
|
+
|
|
164
|
+
useStore(subscribeTheme, readSnapshot, readSnapshot)
|
|
165
|
+
useStore(subscribeVariables, readSnapshot, readSnapshot)
|
|
166
|
+
useStore(subscribeScopedVariables, readSnapshot, readSnapshot)
|
|
167
|
+
|
|
168
|
+
const theme = scopedTheme ?? nitroWind.store.getTheme().theme
|
|
169
|
+
const layers = scopedVariables?.getLayers()
|
|
170
|
+
if (!isList) {
|
|
171
|
+
return getScopedCSSVariable(nitroWind, nameOrNames as CSSVariableName, theme, layers?.inner, layers?.outer)
|
|
172
|
+
}
|
|
173
|
+
return names.map((name) =>
|
|
174
|
+
getScopedCSSVariable(nitroWind, name as CSSVariableName, theme, layers?.inner, layers?.outer),
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Returns the expanded value(s) of one or more CSS variables for the active (or scoped) theme,
|
|
180
|
+
* and re-renders when any of those variables change.
|
|
181
|
+
*/
|
|
182
|
+
export function useCSSVariable(name: CSSVariableName): string | undefined
|
|
183
|
+
export function useCSSVariable<const T extends readonly CSSVariableName[]>(
|
|
184
|
+
names: T,
|
|
185
|
+
): { -readonly [K in keyof T]: string | undefined }
|
|
186
|
+
export function useCSSVariable(
|
|
187
|
+
nameOrNames: CSSVariableName | readonly CSSVariableName[],
|
|
188
|
+
): string | undefined | (string | undefined)[] {
|
|
189
|
+
return useCSSVariableWithStore(nameOrNames)
|
|
190
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { registerNitroUIEnvironment } from './environment'
|
|
2
|
+
import { useCSSVariable, useResolvedClasses, useTheme } from './hooks'
|
|
3
|
+
import type { ResolvedClasses } from './types'
|
|
4
|
+
|
|
5
|
+
registerNitroUIEnvironment()
|
|
6
|
+
|
|
7
|
+
export { formatColor, type PackedColor, parseColor, withAlpha } from './color'
|
|
8
|
+
export {
|
|
9
|
+
cn,
|
|
10
|
+
configure,
|
|
11
|
+
createNitroWind,
|
|
12
|
+
getCSSVariable,
|
|
13
|
+
getNitroWind,
|
|
14
|
+
getTheme,
|
|
15
|
+
type NitroWind,
|
|
16
|
+
type NitroWindConfig,
|
|
17
|
+
setTheme,
|
|
18
|
+
subscribeTheme,
|
|
19
|
+
tw,
|
|
20
|
+
updateCSSVariables,
|
|
21
|
+
} from './core'
|
|
22
|
+
export {
|
|
23
|
+
type CSSCustomClassDefinition,
|
|
24
|
+
type CSSCustomClassEntry,
|
|
25
|
+
type CSSCustomClassRank,
|
|
26
|
+
type CSSLocation,
|
|
27
|
+
CSSParseError,
|
|
28
|
+
type CSSThemeEntry,
|
|
29
|
+
type CSSTokenNamespace,
|
|
30
|
+
type CSSTokenResets,
|
|
31
|
+
type CSSWarning,
|
|
32
|
+
type CSSWarningCode,
|
|
33
|
+
type NitroWindCSSConfig,
|
|
34
|
+
type ParseCSSOptions,
|
|
35
|
+
parseCSS,
|
|
36
|
+
type ResolvedImport,
|
|
37
|
+
} from './css'
|
|
38
|
+
export { mergeClassProps } from './props'
|
|
39
|
+
export { ScopedTheme, type ScopedThemeProps, ScopedVariables, type ScopedVariablesProps } from './scope'
|
|
40
|
+
export type { NitroWindStore } from './store'
|
|
41
|
+
export { createTokens, tailwindTokens } from './tokens'
|
|
42
|
+
export type * from './tokens/types'
|
|
43
|
+
export type * from './types'
|
|
44
|
+
export { withNitroWind } from './withNitroWind'
|
|
45
|
+
export { useCSSVariable, useTheme }
|
|
46
|
+
|
|
47
|
+
/** Compiles classes in a component and re-renders when a channel the classes depend on changes. */
|
|
48
|
+
export function useTw(className: string | null | undefined): ResolvedClasses {
|
|
49
|
+
return useResolvedClasses(className)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
declare module 'nitro-ui-renderer' {
|
|
53
|
+
interface ViewPropsExtension {
|
|
54
|
+
/** Tailwind classes, compiled by NitroWind into `layout`, `style` and text props. */
|
|
55
|
+
className?: string
|
|
56
|
+
}
|
|
57
|
+
interface ListProps<Types> {
|
|
58
|
+
/** Tailwind padding classes (`p-*`, `px-*`, `py-*`, `pt-*`, `pr-*`, `pb-*`, `pl-*`), compiled by NitroWind into `contentPadding`. */
|
|
59
|
+
contentPaddingClassName?: string
|
|
60
|
+
}
|
|
61
|
+
}
|