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/utilities.ts
ADDED
|
@@ -0,0 +1,1992 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The table of Tailwind utilities NitroWind understands, and how each one maps
|
|
3
|
+
* onto NitroUI's `layout`, `style` and `Text` props.
|
|
4
|
+
*/
|
|
5
|
+
import type { Corner } from 'nitro-ui-renderer'
|
|
6
|
+
import { multiplyAlpha, type PackedColor, parseColor, withAlpha } from './color'
|
|
7
|
+
import { hasMathFunction, hasViewportUnit, isMathExpression } from './math'
|
|
8
|
+
import type { NitroFontWeight, TokenBoxShadow, TokenDropShadow, Tokens } from './tokens/types'
|
|
9
|
+
import type { LayoutLength, ViewportSize } from './types'
|
|
10
|
+
import { extractVariableNames, hasVarCall, parseDynamicValue, type TypeHint } from './variables'
|
|
11
|
+
|
|
12
|
+
type Bag = Record<string, unknown>
|
|
13
|
+
|
|
14
|
+
const MISSING_VIEWPORT = Symbol('missing-viewport')
|
|
15
|
+
type ParsedLength = LayoutLength | typeof MISSING_VIEWPORT | null
|
|
16
|
+
|
|
17
|
+
export interface VariableError {
|
|
18
|
+
readonly kind: 'missing' | 'cycle' | 'calc' | 'invalid'
|
|
19
|
+
readonly variable?: string
|
|
20
|
+
readonly value?: string
|
|
21
|
+
readonly expected?: string
|
|
22
|
+
readonly reason?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface VariableResolver {
|
|
26
|
+
expand(expr: string): { readonly value: string; readonly variables: readonly string[] } | null
|
|
27
|
+
/**
|
|
28
|
+
* Looks up a single CSS variable by name if it is currently defined.
|
|
29
|
+
* Returns `null` without reporting an error when the variable does not exist;
|
|
30
|
+
* reports cycle/calc errors if it exists and fails to expand.
|
|
31
|
+
*/
|
|
32
|
+
tryVariable(name: string): { readonly value: string; readonly variables: readonly string[] } | null
|
|
33
|
+
reportError(error: VariableError): void
|
|
34
|
+
hasError(): boolean
|
|
35
|
+
isMissingViewport?(): boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Mutable state collected while applying the classes of one class string. */
|
|
39
|
+
export interface Draft {
|
|
40
|
+
layout: Bag
|
|
41
|
+
style: Bag
|
|
42
|
+
text: Bag
|
|
43
|
+
/** Corners named by side-specific `rounded-t-*` style classes. */
|
|
44
|
+
corners?: Set<Corner>
|
|
45
|
+
gradient?: {
|
|
46
|
+
start?: { x: number; y: number }
|
|
47
|
+
end?: { x: number; y: number }
|
|
48
|
+
from?: PackedColor
|
|
49
|
+
via?: PackedColor
|
|
50
|
+
to?: PackedColor
|
|
51
|
+
}
|
|
52
|
+
shadow?: TokenBoxShadow[]
|
|
53
|
+
insetShadow?: TokenBoxShadow[]
|
|
54
|
+
shadowColor?: { color: PackedColor; explicitAlpha: boolean }
|
|
55
|
+
insetShadowColor?: { color: PackedColor; explicitAlpha: boolean }
|
|
56
|
+
dropShadow?: TokenDropShadow[]
|
|
57
|
+
/** Line height from `leading-*` or `text-lg/7`. It wins over a font size's default line height. */
|
|
58
|
+
leading?: Leading
|
|
59
|
+
/** The default line height of the last `text-<size>` class. */
|
|
60
|
+
sizeLineHeight?: number
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function createDraft(): Draft {
|
|
64
|
+
return { layout: {}, style: {}, text: {} }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type Handler = (
|
|
68
|
+
value: string,
|
|
69
|
+
draft: Draft,
|
|
70
|
+
tokens: Tokens,
|
|
71
|
+
negative: boolean,
|
|
72
|
+
vars?: VariableResolver,
|
|
73
|
+
viewport?: ViewportSize,
|
|
74
|
+
) => boolean
|
|
75
|
+
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// Values
|
|
78
|
+
|
|
79
|
+
interface ResolvedDynamic {
|
|
80
|
+
readonly raw: string
|
|
81
|
+
readonly hint: TypeHint | null
|
|
82
|
+
readonly isVar: boolean
|
|
83
|
+
readonly variable?: string
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Resolves `(--x)`, `(color:--x)`, `(length:--x)`, `[var(--x)]`, or `[...]` values. */
|
|
87
|
+
function resolveDynamic(value: string, vars?: VariableResolver): ResolvedDynamic | null {
|
|
88
|
+
const parsed = parseDynamicValue(value)
|
|
89
|
+
if (parsed === null) return null
|
|
90
|
+
if (vars !== undefined) {
|
|
91
|
+
if (parsed.isVar || hasMathFunction(parsed.expr)) {
|
|
92
|
+
const expanded = vars.expand(parsed.expr)
|
|
93
|
+
if (expanded === null) return null
|
|
94
|
+
return {
|
|
95
|
+
raw: expanded.value,
|
|
96
|
+
hint: parsed.hint,
|
|
97
|
+
isVar: parsed.isVar,
|
|
98
|
+
variable: expanded.variables[0],
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} else if (parsed.isVar) {
|
|
102
|
+
return null
|
|
103
|
+
}
|
|
104
|
+
return { raw: parsed.expr, hint: parsed.hint, isVar: false }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function roundPoints(n: number): number {
|
|
108
|
+
const rounded = Math.round(n * 1e4) / 1e4
|
|
109
|
+
return rounded === 0 ? 0 : rounded
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Parses a CSS length into points or a percentage. */
|
|
113
|
+
function parseLength(raw: string, viewport?: ViewportSize): ParsedLength {
|
|
114
|
+
const match = /^(-?\d*\.?\d+)(px|rem|em|pt|vw|vh|%)?$/.exec(raw.trim())
|
|
115
|
+
if (!match) return null
|
|
116
|
+
const value = Number(match[1])
|
|
117
|
+
switch (match[2]) {
|
|
118
|
+
case '%':
|
|
119
|
+
return `${value}%`
|
|
120
|
+
case 'rem':
|
|
121
|
+
case 'em':
|
|
122
|
+
return value * 16
|
|
123
|
+
case 'vw':
|
|
124
|
+
return viewport === undefined ? MISSING_VIEWPORT : roundPoints((value / 100) * viewport.width)
|
|
125
|
+
case 'vh':
|
|
126
|
+
return viewport === undefined ? MISSING_VIEWPORT : roundPoints((value / 100) * viewport.height)
|
|
127
|
+
default:
|
|
128
|
+
return value
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function negate(length: LayoutLength, negative: boolean): LayoutLength {
|
|
133
|
+
if (!negative) return length
|
|
134
|
+
if (typeof length === 'number') return length === 0 ? 0 : -length
|
|
135
|
+
return length.startsWith('-') ? (length.slice(1) as LayoutLength) : (`-${length}` as LayoutLength)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function isNumeric(value: string): boolean {
|
|
139
|
+
return /^\d*\.?\d+$/.test(value)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
interface SpacingOptions {
|
|
143
|
+
/** Accept named container sizes such as `md` (for widths). */
|
|
144
|
+
container?: boolean
|
|
145
|
+
/** Accept fractions such as `1/2`. */
|
|
146
|
+
fraction?: boolean
|
|
147
|
+
/** Accept `screen` resolved against viewport width or height. */
|
|
148
|
+
screen?: 'width' | 'height'
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function resolveBaseSpacing(
|
|
152
|
+
spacing: number | string,
|
|
153
|
+
vars?: VariableResolver,
|
|
154
|
+
viewport?: ViewportSize,
|
|
155
|
+
): number | typeof MISSING_VIEWPORT | null {
|
|
156
|
+
if (typeof spacing === 'number') return spacing
|
|
157
|
+
const expanded = vars?.expand(spacing)
|
|
158
|
+
if (vars?.hasError()) return null
|
|
159
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
160
|
+
if (!expanded) return null
|
|
161
|
+
const length = parseLength(expanded.value, viewport)
|
|
162
|
+
if (length === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
163
|
+
if (typeof length !== 'number') {
|
|
164
|
+
vars?.reportError({
|
|
165
|
+
kind: 'invalid',
|
|
166
|
+
variable: expanded.variables[0],
|
|
167
|
+
value: expanded.value,
|
|
168
|
+
expected: 'length',
|
|
169
|
+
})
|
|
170
|
+
return null
|
|
171
|
+
}
|
|
172
|
+
return length
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Resolves a spacing-scale value: `4`, `px`, `full`, `screen`, `1/2`, `[13px]`, `(--space)`, `md`, or `--spacing-*`. */
|
|
176
|
+
function spacingValue(
|
|
177
|
+
value: string,
|
|
178
|
+
tokens: Tokens,
|
|
179
|
+
negative: boolean,
|
|
180
|
+
options: SpacingOptions = {},
|
|
181
|
+
vars?: VariableResolver,
|
|
182
|
+
viewport?: ViewportSize,
|
|
183
|
+
): ParsedLength {
|
|
184
|
+
const dyn = resolveDynamic(value, vars)
|
|
185
|
+
if (vars?.hasError()) return null
|
|
186
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
187
|
+
if (dyn !== null) {
|
|
188
|
+
if (dyn.hint === 'color') return null
|
|
189
|
+
const length = parseLength(dyn.raw, viewport)
|
|
190
|
+
if (length === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
191
|
+
if (length === null) {
|
|
192
|
+
if (dyn.isVar) vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'length' })
|
|
193
|
+
return null
|
|
194
|
+
}
|
|
195
|
+
return negate(length, negative)
|
|
196
|
+
}
|
|
197
|
+
const scaleOverride = tokens.spacingScale !== undefined ? own(tokens.spacingScale, value) : undefined
|
|
198
|
+
if (scaleOverride !== undefined) {
|
|
199
|
+
if (typeof scaleOverride === 'number') return negate(scaleOverride, negative)
|
|
200
|
+
if (hasVarCall(scaleOverride)) {
|
|
201
|
+
const expanded = vars?.expand(scaleOverride)
|
|
202
|
+
if (vars?.hasError()) return null
|
|
203
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
204
|
+
if (!expanded) return null
|
|
205
|
+
const length = parseLength(expanded.value, viewport)
|
|
206
|
+
if (length === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
207
|
+
if (length === null) {
|
|
208
|
+
vars?.reportError({
|
|
209
|
+
kind: 'invalid',
|
|
210
|
+
variable: expanded.variables[0],
|
|
211
|
+
value: expanded.value,
|
|
212
|
+
expected: 'length',
|
|
213
|
+
})
|
|
214
|
+
return null
|
|
215
|
+
}
|
|
216
|
+
return negate(length, negative)
|
|
217
|
+
}
|
|
218
|
+
const length = parseLength(scaleOverride, viewport)
|
|
219
|
+
if (length === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
220
|
+
return length === null ? null : negate(length, negative)
|
|
221
|
+
}
|
|
222
|
+
if (value === 'px') return negate(1, negative)
|
|
223
|
+
if (value === '0') return 0
|
|
224
|
+
if (isNumeric(value)) {
|
|
225
|
+
const step = resolveBaseSpacing(tokens.spacing, vars, viewport)
|
|
226
|
+
if (step === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
227
|
+
if (step === null) return null
|
|
228
|
+
return negate(Number(value) * step, negative)
|
|
229
|
+
}
|
|
230
|
+
if (value === 'full') return negate('100%', negative)
|
|
231
|
+
if (options.screen !== undefined && value === 'screen') {
|
|
232
|
+
if (viewport === undefined) return MISSING_VIEWPORT
|
|
233
|
+
return negate(viewport[options.screen], negative)
|
|
234
|
+
}
|
|
235
|
+
if (options.fraction !== false) {
|
|
236
|
+
const fraction = /^(\d+)\/(\d+)$/.exec(value)
|
|
237
|
+
if (fraction) {
|
|
238
|
+
const percent = (Number(fraction[1]) / Number(fraction[2])) * 100
|
|
239
|
+
return negate(`${Math.round(percent * 1e4) / 1e4}%`, negative)
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (options.container) {
|
|
243
|
+
const size = own(tokens.container, value)
|
|
244
|
+
if (size !== undefined) {
|
|
245
|
+
if (typeof size === 'number') return size
|
|
246
|
+
const expanded = vars?.expand(size)
|
|
247
|
+
if (vars?.hasError()) return null
|
|
248
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
249
|
+
if (!expanded) return null
|
|
250
|
+
const length = parseLength(expanded.value, viewport)
|
|
251
|
+
if (length === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
252
|
+
if (length === null) {
|
|
253
|
+
vars?.reportError({
|
|
254
|
+
kind: 'invalid',
|
|
255
|
+
variable: expanded.variables[0],
|
|
256
|
+
value: expanded.value,
|
|
257
|
+
expected: 'length',
|
|
258
|
+
})
|
|
259
|
+
return null
|
|
260
|
+
}
|
|
261
|
+
return length
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (isValidTokenSuffix(value)) {
|
|
265
|
+
const varName = `--spacing-${value}`
|
|
266
|
+
const ns = vars?.tryVariable(varName)
|
|
267
|
+
if (vars?.hasError()) return null
|
|
268
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
269
|
+
if (ns) {
|
|
270
|
+
const length = parseLength(ns.value, viewport)
|
|
271
|
+
if (length === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
272
|
+
if (length === null) {
|
|
273
|
+
vars?.reportError({
|
|
274
|
+
kind: 'invalid',
|
|
275
|
+
variable: ns.variables[0] ?? varName,
|
|
276
|
+
value: ns.value,
|
|
277
|
+
expected: 'length',
|
|
278
|
+
})
|
|
279
|
+
return null
|
|
280
|
+
}
|
|
281
|
+
return negate(length, negative)
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return null
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Splits `red-500/50` into the color and its opacity modifier, ignoring `/` inside brackets or parentheses. */
|
|
288
|
+
function splitModifier(value: string): [string, string | undefined] {
|
|
289
|
+
let bracketDepth = 0
|
|
290
|
+
let parenDepth = 0
|
|
291
|
+
for (let i = value.length - 1; i >= 0; i--) {
|
|
292
|
+
const char = value[i]
|
|
293
|
+
if (char === ']') bracketDepth++
|
|
294
|
+
else if (char === '[') bracketDepth--
|
|
295
|
+
else if (char === ')') parenDepth++
|
|
296
|
+
else if (char === '(') parenDepth--
|
|
297
|
+
else if (char === '/' && bracketDepth === 0 && parenDepth === 0) {
|
|
298
|
+
return [value.slice(0, i), value.slice(i + 1)]
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return [value, undefined]
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function parseAlpha(modifier: string, vars?: VariableResolver): number | null {
|
|
305
|
+
const dyn = resolveDynamic(modifier, vars)
|
|
306
|
+
if (vars?.hasError()) return null
|
|
307
|
+
if (dyn !== null) {
|
|
308
|
+
const raw = dyn.raw.trim()
|
|
309
|
+
const value = raw.endsWith('%') ? Number(raw.slice(0, -1)) / 100 : Number(raw)
|
|
310
|
+
if (raw === '' || !Number.isFinite(value)) {
|
|
311
|
+
if (dyn.isVar) vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'opacity' })
|
|
312
|
+
return null
|
|
313
|
+
}
|
|
314
|
+
return value
|
|
315
|
+
}
|
|
316
|
+
return isNumeric(modifier) ? Number(modifier) / 100 : null
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
interface ColorValue {
|
|
320
|
+
color: PackedColor
|
|
321
|
+
explicitAlpha: boolean
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function own<T>(record: Record<string, T>, key: string): T | undefined {
|
|
325
|
+
return Object.prototype.hasOwnProperty.call(record, key) ? record[key] : undefined
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function isValidTokenSuffix(suffix: string): boolean {
|
|
329
|
+
return suffix.length > 0 && !/[\s,()[\]/]/.test(suffix)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const NITRO_FONT_WEIGHTS = new Set<NitroFontWeight>(['regular', 'medium', 'semibold', 'bold'])
|
|
333
|
+
|
|
334
|
+
function parseFontWeightValue(raw: string, tokens: Tokens): NitroFontWeight | number | null {
|
|
335
|
+
const trimmed = raw.trim()
|
|
336
|
+
if (NITRO_FONT_WEIGHTS.has(trimmed as NitroFontWeight)) return trimmed as NitroFontWeight
|
|
337
|
+
const mapped = own(tokens.fontWeight, trimmed)
|
|
338
|
+
if (mapped !== undefined && !hasVarCall(mapped)) return mapped as NitroFontWeight
|
|
339
|
+
if (/^[1-9]00$/.test(trimmed)) return Number(trimmed)
|
|
340
|
+
return null
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function parseLeadingRaw(raw: string, viewport?: ViewportSize): Leading | typeof MISSING_VIEWPORT | null {
|
|
344
|
+
const trimmed = raw.trim()
|
|
345
|
+
const length = parseLength(trimmed, viewport)
|
|
346
|
+
if (length === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
347
|
+
// A unitless value is a multiplier, as in CSS: `leading-[1.3]` or `--leading-copy: 1.5`.
|
|
348
|
+
if (typeof length === 'number') return /\d$/.test(trimmed) ? { multiplier: length } : { points: length }
|
|
349
|
+
return null
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Resolves `red-500`, `red-500/50`, `[#ff0]`, `(--brand)/(--o)`, `[rgb(0_0_0)]/25`, `brand`. */
|
|
353
|
+
function colorValue(value: string, tokens: Tokens, vars?: VariableResolver): ColorValue | null {
|
|
354
|
+
const [base, modifier] = splitModifier(value)
|
|
355
|
+
const dyn = resolveDynamic(base, vars)
|
|
356
|
+
if (vars?.hasError()) return null
|
|
357
|
+
let color: PackedColor | null | undefined
|
|
358
|
+
if (dyn !== null) {
|
|
359
|
+
if (dyn.hint === 'length') return null
|
|
360
|
+
color = parseColor(dyn.raw)
|
|
361
|
+
if (color === null && dyn.isVar) {
|
|
362
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'color' })
|
|
363
|
+
return null
|
|
364
|
+
}
|
|
365
|
+
} else {
|
|
366
|
+
const token = own(tokens.colors, base)
|
|
367
|
+
if (token !== undefined) {
|
|
368
|
+
if (typeof token === 'number') {
|
|
369
|
+
color = token
|
|
370
|
+
} else {
|
|
371
|
+
const expanded = vars?.expand(token)
|
|
372
|
+
if (!expanded) return null
|
|
373
|
+
color = parseColor(expanded.value)
|
|
374
|
+
if (color === null) {
|
|
375
|
+
vars?.reportError({
|
|
376
|
+
kind: 'invalid',
|
|
377
|
+
variable: expanded.variables[0],
|
|
378
|
+
value: expanded.value,
|
|
379
|
+
expected: 'color',
|
|
380
|
+
})
|
|
381
|
+
return null
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
} else if (isValidTokenSuffix(base)) {
|
|
385
|
+
const varName = `--color-${base}`
|
|
386
|
+
const ns = vars?.tryVariable(varName)
|
|
387
|
+
if (vars?.hasError()) return null
|
|
388
|
+
if (ns) {
|
|
389
|
+
color = parseColor(ns.value)
|
|
390
|
+
if (color === null) {
|
|
391
|
+
vars?.reportError({
|
|
392
|
+
kind: 'invalid',
|
|
393
|
+
variable: ns.variables[0] ?? varName,
|
|
394
|
+
value: ns.value,
|
|
395
|
+
expected: 'color',
|
|
396
|
+
})
|
|
397
|
+
return null
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
if (color == null) return null
|
|
403
|
+
if (modifier === undefined) return { color, explicitAlpha: false }
|
|
404
|
+
const alpha = parseAlpha(modifier, vars)
|
|
405
|
+
if (alpha === null) return null
|
|
406
|
+
return { color: multiplyAlpha(color, alpha), explicitAlpha: true }
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function numberValue(value: string, vars?: VariableResolver): number | null {
|
|
410
|
+
const dyn = resolveDynamic(value, vars)
|
|
411
|
+
if (vars?.hasError()) return null
|
|
412
|
+
if (dyn !== null) {
|
|
413
|
+
const raw = dyn.raw.trim()
|
|
414
|
+
const number = Number(raw.replace(/deg$/, ''))
|
|
415
|
+
if (raw === '' || !Number.isFinite(number)) {
|
|
416
|
+
if (dyn.isVar) vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'number' })
|
|
417
|
+
return null
|
|
418
|
+
}
|
|
419
|
+
return number
|
|
420
|
+
}
|
|
421
|
+
const number = Number(value.replace(/deg$/, ''))
|
|
422
|
+
return value !== '' && Number.isFinite(number) ? number : null
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// ---------------------------------------------------------------------------
|
|
426
|
+
// Handler factories
|
|
427
|
+
|
|
428
|
+
function spacingProps(
|
|
429
|
+
target: 'layout',
|
|
430
|
+
keys: string[],
|
|
431
|
+
options?: SpacingOptions & { auto?: boolean; clears?: readonly string[] },
|
|
432
|
+
): Handler {
|
|
433
|
+
return (value, draft, tokens, negative, vars, viewport) => {
|
|
434
|
+
const length =
|
|
435
|
+
options?.auto && value === 'auto' ? 'auto' : spacingValue(value, tokens, negative, options, vars, viewport)
|
|
436
|
+
if (length === MISSING_VIEWPORT) return true
|
|
437
|
+
if (length === null) return false
|
|
438
|
+
if (options?.clears) {
|
|
439
|
+
for (const key of options.clears) delete draft[target][key]
|
|
440
|
+
}
|
|
441
|
+
for (const key of keys) draft[target][key] = length
|
|
442
|
+
return true
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const SIDE_CORNERS: Record<string, Corner[]> = {
|
|
447
|
+
t: ['topLeft', 'topRight'],
|
|
448
|
+
b: ['bottomLeft', 'bottomRight'],
|
|
449
|
+
l: ['topLeft', 'bottomLeft'],
|
|
450
|
+
r: ['topRight', 'bottomRight'],
|
|
451
|
+
s: ['topLeft', 'bottomLeft'],
|
|
452
|
+
e: ['topRight', 'bottomRight'],
|
|
453
|
+
tl: ['topLeft'],
|
|
454
|
+
tr: ['topRight'],
|
|
455
|
+
bl: ['bottomLeft'],
|
|
456
|
+
br: ['bottomRight'],
|
|
457
|
+
ss: ['topLeft'],
|
|
458
|
+
se: ['topRight'],
|
|
459
|
+
es: ['bottomLeft'],
|
|
460
|
+
ee: ['bottomRight'],
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function rounded(side?: string): Handler {
|
|
464
|
+
return (value, draft, tokens, _negative, vars, viewport) => {
|
|
465
|
+
const dyn = resolveDynamic(value, vars)
|
|
466
|
+
if (vars?.hasError()) return false
|
|
467
|
+
if (vars?.isMissingViewport?.()) return true
|
|
468
|
+
let radius: ParsedLength | undefined
|
|
469
|
+
if (dyn !== null) {
|
|
470
|
+
if (dyn.hint === 'color') return false
|
|
471
|
+
radius = parseLength(dyn.raw, viewport)
|
|
472
|
+
if (radius === MISSING_VIEWPORT) return true
|
|
473
|
+
if (typeof radius !== 'number') {
|
|
474
|
+
if (dyn.isVar) {
|
|
475
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'length' })
|
|
476
|
+
}
|
|
477
|
+
return false
|
|
478
|
+
}
|
|
479
|
+
} else {
|
|
480
|
+
const token = own(tokens.radius, value)
|
|
481
|
+
if (token !== undefined) {
|
|
482
|
+
if (typeof token === 'number') {
|
|
483
|
+
radius = token
|
|
484
|
+
} else {
|
|
485
|
+
const expanded = vars?.expand(token)
|
|
486
|
+
if (vars?.isMissingViewport?.()) return true
|
|
487
|
+
if (!expanded) return false
|
|
488
|
+
radius = parseLength(expanded.value, viewport)
|
|
489
|
+
if (radius === MISSING_VIEWPORT) return true
|
|
490
|
+
if (typeof radius !== 'number') {
|
|
491
|
+
vars?.reportError({
|
|
492
|
+
kind: 'invalid',
|
|
493
|
+
variable: expanded.variables[0],
|
|
494
|
+
value: expanded.value,
|
|
495
|
+
expected: 'length',
|
|
496
|
+
})
|
|
497
|
+
return false
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
} else if (isValidTokenSuffix(value)) {
|
|
501
|
+
const varName = `--radius-${value}`
|
|
502
|
+
const ns = vars?.tryVariable(varName)
|
|
503
|
+
if (vars?.hasError()) return false
|
|
504
|
+
if (vars?.isMissingViewport?.()) return true
|
|
505
|
+
if (ns) {
|
|
506
|
+
radius = parseLength(ns.value, viewport)
|
|
507
|
+
if (radius === MISSING_VIEWPORT) return true
|
|
508
|
+
if (typeof radius !== 'number') {
|
|
509
|
+
vars?.reportError({
|
|
510
|
+
kind: 'invalid',
|
|
511
|
+
variable: ns.variables[0] ?? varName,
|
|
512
|
+
value: ns.value,
|
|
513
|
+
expected: 'length',
|
|
514
|
+
})
|
|
515
|
+
return false
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (typeof radius !== 'number') return false
|
|
521
|
+
draft.style.cornerRadius = radius
|
|
522
|
+
if (side !== undefined) {
|
|
523
|
+
draft.corners ??= new Set()
|
|
524
|
+
for (const corner of SIDE_CORNERS[side]!) draft.corners.add(corner)
|
|
525
|
+
} else {
|
|
526
|
+
draft.corners = undefined
|
|
527
|
+
}
|
|
528
|
+
return true
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function transformNumber(key: string, scale: (n: number) => number, clears?: readonly string[]): Handler {
|
|
533
|
+
return (value, draft, _tokens, negative, vars) => {
|
|
534
|
+
const number = numberValue(value, vars)
|
|
535
|
+
if (number === null) return false
|
|
536
|
+
if (clears) {
|
|
537
|
+
for (const cleared of clears) delete draft.style[cleared]
|
|
538
|
+
}
|
|
539
|
+
const result = scale(number)
|
|
540
|
+
draft.style[key] = negative ? -result : result
|
|
541
|
+
return true
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function translate(keys: string[]): Handler {
|
|
546
|
+
return (value, draft, tokens, negative, vars, viewport) => {
|
|
547
|
+
const length = spacingValue(value, tokens, negative, { fraction: false }, vars, viewport)
|
|
548
|
+
if (length === MISSING_VIEWPORT) return true
|
|
549
|
+
// NitroUI translations are in points; percentages of the view's own size aren't supported.
|
|
550
|
+
if (typeof length !== 'number') return false
|
|
551
|
+
for (const key of keys) draft.style[key] = length
|
|
552
|
+
return true
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** Gradient directions in normalized view coordinates. */
|
|
557
|
+
const GRADIENT_DIRECTIONS: Record<string, [x1: number, y1: number, x2: number, y2: number]> = {
|
|
558
|
+
t: [0.5, 1, 0.5, 0],
|
|
559
|
+
tr: [0, 1, 1, 0],
|
|
560
|
+
r: [0, 0.5, 1, 0.5],
|
|
561
|
+
br: [0, 0, 1, 1],
|
|
562
|
+
b: [0.5, 0, 0.5, 1],
|
|
563
|
+
bl: [1, 0, 0, 1],
|
|
564
|
+
l: [1, 0.5, 0, 0.5],
|
|
565
|
+
tl: [1, 1, 0, 0],
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const linearGradient: Handler = (value, draft, _tokens, negative, vars) => {
|
|
569
|
+
let points = value.startsWith('to-') ? GRADIENT_DIRECTIONS[value.slice(3)] : undefined
|
|
570
|
+
if (!points) {
|
|
571
|
+
// `bg-linear-45` / `bg-linear-[30deg]`: CSS angles, 0deg points up, clockwise.
|
|
572
|
+
const angle = numberValue(value, vars)
|
|
573
|
+
if (angle === null) return false
|
|
574
|
+
const radians = ((negative ? -angle : angle) * Math.PI) / 180
|
|
575
|
+
const dx = Math.sin(radians) / 2
|
|
576
|
+
const dy = -Math.cos(radians) / 2
|
|
577
|
+
points = [0.5 - dx, 0.5 - dy, 0.5 + dx, 0.5 + dy]
|
|
578
|
+
}
|
|
579
|
+
const [x1, y1, x2, y2] = points.map((n) => Math.round(n * 1e4) / 1e4) as [number, number, number, number]
|
|
580
|
+
draft.gradient = { ...draft.gradient, start: { x: x1, y: y1 }, end: { x: x2, y: y2 } }
|
|
581
|
+
return true
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function gradientStop(stop: 'from' | 'via' | 'to'): Handler {
|
|
585
|
+
return (value, draft, tokens, _negative, vars) => {
|
|
586
|
+
const color = colorValue(value, tokens, vars)
|
|
587
|
+
if (color === null) return false
|
|
588
|
+
draft.gradient = { ...draft.gradient, [stop]: color.color }
|
|
589
|
+
return true
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function shadowHandler(kind: 'shadow' | 'insetShadow'): Handler {
|
|
594
|
+
return (value, draft, tokens, _negative, vars, viewport) => {
|
|
595
|
+
if (kind === 'shadow') {
|
|
596
|
+
const token = own(tokens.shadow, value)
|
|
597
|
+
if (token !== undefined) {
|
|
598
|
+
if (typeof token === 'string') {
|
|
599
|
+
const expanded = vars?.expand(token)
|
|
600
|
+
if (vars?.isMissingViewport?.()) return true
|
|
601
|
+
if (!expanded) return false
|
|
602
|
+
const parsed = parseShadowList(expanded.value, false, viewport)
|
|
603
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
604
|
+
if (parsed === null) {
|
|
605
|
+
vars?.reportError({
|
|
606
|
+
kind: 'invalid',
|
|
607
|
+
variable: expanded.variables[0],
|
|
608
|
+
value: expanded.value,
|
|
609
|
+
expected: 'shadow',
|
|
610
|
+
})
|
|
611
|
+
return false
|
|
612
|
+
}
|
|
613
|
+
draft.shadow = parsed
|
|
614
|
+
return true
|
|
615
|
+
}
|
|
616
|
+
draft.shadow = token
|
|
617
|
+
return true
|
|
618
|
+
}
|
|
619
|
+
} else {
|
|
620
|
+
const layers = own(tokens.insetShadow, value)
|
|
621
|
+
if (layers !== undefined) {
|
|
622
|
+
if (typeof layers === 'string') {
|
|
623
|
+
const expanded = vars?.expand(layers)
|
|
624
|
+
if (vars?.isMissingViewport?.()) return true
|
|
625
|
+
if (!expanded) return false
|
|
626
|
+
const parsed = parseShadowList(expanded.value, true, viewport)
|
|
627
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
628
|
+
if (parsed === null) {
|
|
629
|
+
vars?.reportError({
|
|
630
|
+
kind: 'invalid',
|
|
631
|
+
variable: expanded.variables[0],
|
|
632
|
+
value: expanded.value,
|
|
633
|
+
expected: 'shadow',
|
|
634
|
+
})
|
|
635
|
+
return false
|
|
636
|
+
}
|
|
637
|
+
draft.insetShadow = parsed
|
|
638
|
+
return true
|
|
639
|
+
}
|
|
640
|
+
draft.insetShadow = layers
|
|
641
|
+
return true
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
const [base, modifier] = splitModifier(value)
|
|
645
|
+
const parsedDyn = parseDynamicValue(base)
|
|
646
|
+
if (parsedDyn !== null && parsedDyn.hint !== 'color') {
|
|
647
|
+
const dyn = resolveDynamic(value, vars)
|
|
648
|
+
if (vars?.hasError()) return false
|
|
649
|
+
if (vars?.isMissingViewport?.()) return true
|
|
650
|
+
if (dyn !== null) {
|
|
651
|
+
// `shadow-[0_4px_12px_rgb(0_0_0/0.2)]` or `shadow-(--my-shadow)`
|
|
652
|
+
const parsed = parseShadowList(dyn.raw, kind === 'insetShadow', viewport)
|
|
653
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
654
|
+
if (parsed !== null) {
|
|
655
|
+
draft[kind] = parsed
|
|
656
|
+
return true
|
|
657
|
+
}
|
|
658
|
+
if (dyn.hint === 'length') {
|
|
659
|
+
if (dyn.isVar) {
|
|
660
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'shadow' })
|
|
661
|
+
}
|
|
662
|
+
return false
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
} else if (parsedDyn === null && modifier === undefined && isValidTokenSuffix(value)) {
|
|
666
|
+
const varName = kind === 'shadow' ? `--shadow-${value}` : `--inset-shadow-${value}`
|
|
667
|
+
const ns = vars?.tryVariable(varName)
|
|
668
|
+
if (vars?.hasError()) return false
|
|
669
|
+
if (vars?.isMissingViewport?.()) return true
|
|
670
|
+
if (ns) {
|
|
671
|
+
const parsed = parseShadowList(ns.value, kind === 'insetShadow', viewport)
|
|
672
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
673
|
+
if (parsed === null) {
|
|
674
|
+
vars?.reportError({
|
|
675
|
+
kind: 'invalid',
|
|
676
|
+
variable: ns.variables[0] ?? varName,
|
|
677
|
+
value: ns.value,
|
|
678
|
+
expected: 'shadow',
|
|
679
|
+
})
|
|
680
|
+
return false
|
|
681
|
+
}
|
|
682
|
+
draft[kind] = parsed
|
|
683
|
+
return true
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
const color = colorValue(value, tokens, vars)
|
|
687
|
+
if (color === null) return false
|
|
688
|
+
draft[kind === 'shadow' ? 'shadowColor' : 'insetShadowColor'] = color
|
|
689
|
+
return true
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function splitTopLevelCommas(raw: string): string[] {
|
|
694
|
+
const parts: string[] = []
|
|
695
|
+
let depth = 0
|
|
696
|
+
let start = 0
|
|
697
|
+
for (let i = 0; i < raw.length; i++) {
|
|
698
|
+
const ch = raw[i]
|
|
699
|
+
if (ch === '(') depth++
|
|
700
|
+
else if (ch === ')') depth = Math.max(0, depth - 1)
|
|
701
|
+
else if (ch === ',' && depth === 0) {
|
|
702
|
+
parts.push(raw.slice(start, i).trim())
|
|
703
|
+
start = i + 1
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
parts.push(raw.slice(start).trim())
|
|
707
|
+
return parts.filter(Boolean)
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
function parseShadowList(
|
|
711
|
+
raw: string,
|
|
712
|
+
inset: boolean,
|
|
713
|
+
viewport?: ViewportSize,
|
|
714
|
+
): TokenBoxShadow[] | typeof MISSING_VIEWPORT | null {
|
|
715
|
+
const trimmed = raw.trim()
|
|
716
|
+
if (trimmed === 'none') return []
|
|
717
|
+
const parts = splitTopLevelCommas(trimmed)
|
|
718
|
+
if (parts.length === 0) return null
|
|
719
|
+
const out: TokenBoxShadow[] = []
|
|
720
|
+
for (const part of parts) {
|
|
721
|
+
const layer = parseShadow(part, inset, viewport)
|
|
722
|
+
if (layer === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
723
|
+
if (layer === null) return null
|
|
724
|
+
out.push(layer)
|
|
725
|
+
}
|
|
726
|
+
return out
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
function parseShadow(
|
|
730
|
+
raw: string,
|
|
731
|
+
inset: boolean,
|
|
732
|
+
viewport?: ViewportSize,
|
|
733
|
+
): TokenBoxShadow | typeof MISSING_VIEWPORT | null {
|
|
734
|
+
const colorStart = raw.search(/(rgb|hsl|oklch|#|black|white|transparent)/)
|
|
735
|
+
const head = (colorStart === -1 ? raw : raw.slice(0, colorStart)).trim().split(/\s+/).filter(Boolean)
|
|
736
|
+
const isInset = head[0] === 'inset'
|
|
737
|
+
const lengths = head.slice(isInset ? 1 : 0).map((part) => parseLength(part, viewport))
|
|
738
|
+
if (lengths.some((length) => length === MISSING_VIEWPORT)) return MISSING_VIEWPORT
|
|
739
|
+
if (lengths.length < 2 || lengths.some((length) => typeof length !== 'number')) return null
|
|
740
|
+
const [offsetX, offsetY, blurRadius = 0, spreadDistance = 0] = lengths as number[]
|
|
741
|
+
const color = colorStart === -1 ? 0x00000040 : parseColor(raw.slice(colorStart))
|
|
742
|
+
if (color === null) return null
|
|
743
|
+
return { offsetX: offsetX!, offsetY: offsetY!, blurRadius, spreadDistance, inset: inset || isInset, color }
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function parseDropShadow(raw: string, viewport?: ViewportSize): TokenDropShadow | typeof MISSING_VIEWPORT | null {
|
|
747
|
+
let inner = raw.trim()
|
|
748
|
+
if (inner.toLowerCase().startsWith('drop-shadow(') && inner.endsWith(')')) {
|
|
749
|
+
inner = inner.slice('drop-shadow('.length, -1).trim()
|
|
750
|
+
}
|
|
751
|
+
const colorStart = inner.search(/(rgb|hsl|oklch|#|black|white|transparent)/)
|
|
752
|
+
const head = (colorStart === -1 ? inner : inner.slice(0, colorStart)).trim().split(/\s+/).filter(Boolean)
|
|
753
|
+
const lengths = head.map((part) => parseLength(part, viewport))
|
|
754
|
+
if (lengths.some((length) => length === MISSING_VIEWPORT)) return MISSING_VIEWPORT
|
|
755
|
+
if (lengths.length < 2 || lengths.length > 3 || lengths.some((length) => typeof length !== 'number')) return null
|
|
756
|
+
const [offsetX, offsetY, blur = 0] = lengths as number[]
|
|
757
|
+
const color = colorStart === -1 ? 0x00000040 : parseColor(inner.slice(colorStart))
|
|
758
|
+
if (color === null) return null
|
|
759
|
+
// CSS drop-shadow blur is twice the Gaussian standard deviation.
|
|
760
|
+
return { offsetX: offsetX!, offsetY: offsetY!, standardDeviation: blur / 2, color }
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function parseDropShadowList(raw: string, viewport?: ViewportSize): TokenDropShadow[] | typeof MISSING_VIEWPORT | null {
|
|
764
|
+
const trimmed = raw.trim()
|
|
765
|
+
if (trimmed === 'none') return []
|
|
766
|
+
const parts = splitTopLevelCommas(trimmed)
|
|
767
|
+
if (parts.length === 0) return null
|
|
768
|
+
const out: TokenDropShadow[] = []
|
|
769
|
+
for (const part of parts) {
|
|
770
|
+
const layer = parseDropShadow(part, viewport)
|
|
771
|
+
if (layer === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
772
|
+
if (layer === null) return null
|
|
773
|
+
out.push(layer)
|
|
774
|
+
}
|
|
775
|
+
return out
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const dropShadow: Handler = (value, draft, tokens, _negative, vars, viewport) => {
|
|
779
|
+
const layers = own(tokens.dropShadow, value)
|
|
780
|
+
if (layers !== undefined) {
|
|
781
|
+
if (typeof layers === 'string') {
|
|
782
|
+
const expanded = vars?.expand(layers)
|
|
783
|
+
if (vars?.isMissingViewport?.()) return true
|
|
784
|
+
if (!expanded) return false
|
|
785
|
+
const parsed = parseDropShadowList(expanded.value, viewport)
|
|
786
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
787
|
+
if (parsed === null) {
|
|
788
|
+
vars?.reportError({
|
|
789
|
+
kind: 'invalid',
|
|
790
|
+
variable: expanded.variables[0],
|
|
791
|
+
value: expanded.value,
|
|
792
|
+
expected: 'drop-shadow',
|
|
793
|
+
})
|
|
794
|
+
return false
|
|
795
|
+
}
|
|
796
|
+
draft.dropShadow = parsed
|
|
797
|
+
return true
|
|
798
|
+
}
|
|
799
|
+
draft.dropShadow = layers
|
|
800
|
+
return true
|
|
801
|
+
}
|
|
802
|
+
const dyn = resolveDynamic(value, vars)
|
|
803
|
+
if (vars?.hasError()) return false
|
|
804
|
+
if (vars?.isMissingViewport?.()) return true
|
|
805
|
+
if (dyn !== null) {
|
|
806
|
+
if (dyn.hint === 'color') return false
|
|
807
|
+
const parsed = parseDropShadowList(dyn.raw, viewport)
|
|
808
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
809
|
+
if (parsed !== null) {
|
|
810
|
+
draft.dropShadow = parsed
|
|
811
|
+
return true
|
|
812
|
+
}
|
|
813
|
+
if (dyn.isVar) {
|
|
814
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'drop-shadow' })
|
|
815
|
+
}
|
|
816
|
+
return false
|
|
817
|
+
}
|
|
818
|
+
if (isValidTokenSuffix(value)) {
|
|
819
|
+
const varName = `--drop-shadow-${value}`
|
|
820
|
+
const ns = vars?.tryVariable(varName)
|
|
821
|
+
if (vars?.hasError()) return false
|
|
822
|
+
if (vars?.isMissingViewport?.()) return true
|
|
823
|
+
if (ns) {
|
|
824
|
+
const parsed = parseDropShadowList(ns.value, viewport)
|
|
825
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
826
|
+
if (parsed === null) {
|
|
827
|
+
vars?.reportError({
|
|
828
|
+
kind: 'invalid',
|
|
829
|
+
variable: ns.variables[0] ?? varName,
|
|
830
|
+
value: ns.value,
|
|
831
|
+
expected: 'drop-shadow',
|
|
832
|
+
})
|
|
833
|
+
return false
|
|
834
|
+
}
|
|
835
|
+
draft.dropShadow = parsed
|
|
836
|
+
return true
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
return false
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
function colorProp(target: 'style' | 'text', key: string): Handler {
|
|
843
|
+
return (value, draft, tokens, _negative, vars) => {
|
|
844
|
+
const color = colorValue(value, tokens, vars)
|
|
845
|
+
if (color === null) return false
|
|
846
|
+
draft[target][key] = color.color
|
|
847
|
+
return true
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
function applyFontSize(
|
|
852
|
+
draft: Draft,
|
|
853
|
+
fontSize: number,
|
|
854
|
+
sizeLineHeight: number | undefined,
|
|
855
|
+
leadingModifier: string | undefined,
|
|
856
|
+
tokens: Tokens,
|
|
857
|
+
vars?: VariableResolver,
|
|
858
|
+
viewport?: ViewportSize,
|
|
859
|
+
): boolean {
|
|
860
|
+
if (leadingModifier !== undefined) {
|
|
861
|
+
const result = leadingValue(leadingModifier, tokens, vars, viewport)
|
|
862
|
+
if (result === MISSING_VIEWPORT) {
|
|
863
|
+
draft.text.fontSize = fontSize
|
|
864
|
+
draft.sizeLineHeight = sizeLineHeight
|
|
865
|
+
return true
|
|
866
|
+
}
|
|
867
|
+
if (result === null) return false
|
|
868
|
+
draft.text.fontSize = fontSize
|
|
869
|
+
draft.sizeLineHeight = sizeLineHeight
|
|
870
|
+
draft.leading = result
|
|
871
|
+
return true
|
|
872
|
+
}
|
|
873
|
+
draft.text.fontSize = fontSize
|
|
874
|
+
draft.sizeLineHeight = sizeLineHeight
|
|
875
|
+
delete draft.leading
|
|
876
|
+
return true
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
const textUtility: Handler = (value, draft, tokens, _negative, vars, viewport) => {
|
|
880
|
+
// `text-lg`, `text-lg/7`, `text-[15px]`, `text-(length:--size)`, `text-(color:--ink)`, `text-(--x)` (color default).
|
|
881
|
+
const [size, leadingModifier] = splitModifier(value)
|
|
882
|
+
const parsedDyn = parseDynamicValue(size)
|
|
883
|
+
if (parsedDyn !== null) {
|
|
884
|
+
if (
|
|
885
|
+
parsedDyn.hint === 'color' ||
|
|
886
|
+
(parsedDyn.hint === null && parsedDyn.isVar && !isMathExpression(parsedDyn.expr))
|
|
887
|
+
) {
|
|
888
|
+
return colorProp('text', 'color')(value, draft, tokens, false, vars, viewport)
|
|
889
|
+
}
|
|
890
|
+
const dyn = resolveDynamic(size, vars)
|
|
891
|
+
if (vars?.hasError()) return false
|
|
892
|
+
if (vars?.isMissingViewport?.()) return true
|
|
893
|
+
if (dyn !== null) {
|
|
894
|
+
const rawLength = parseLength(dyn.raw, viewport)
|
|
895
|
+
if (rawLength === MISSING_VIEWPORT) return true
|
|
896
|
+
if (typeof rawLength === 'number') {
|
|
897
|
+
return applyFontSize(draft, rawLength, undefined, leadingModifier, tokens, vars, viewport)
|
|
898
|
+
}
|
|
899
|
+
if (dyn.hint === 'length') {
|
|
900
|
+
if (dyn.isVar) {
|
|
901
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'length' })
|
|
902
|
+
}
|
|
903
|
+
return false
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
return colorProp('text', 'color')(value, draft, tokens, false, vars, viewport)
|
|
907
|
+
}
|
|
908
|
+
const tokenSize = own(tokens.fontSize, size)
|
|
909
|
+
if (tokenSize !== undefined) {
|
|
910
|
+
if (typeof tokenSize === 'string') {
|
|
911
|
+
const expanded = vars?.expand(tokenSize)
|
|
912
|
+
if (vars?.isMissingViewport?.()) return true
|
|
913
|
+
if (!expanded) return false
|
|
914
|
+
const rawLength = parseLength(expanded.value, viewport)
|
|
915
|
+
if (rawLength === MISSING_VIEWPORT) return true
|
|
916
|
+
if (typeof rawLength !== 'number') {
|
|
917
|
+
vars?.reportError({
|
|
918
|
+
kind: 'invalid',
|
|
919
|
+
variable: expanded.variables[0],
|
|
920
|
+
value: expanded.value,
|
|
921
|
+
expected: 'length',
|
|
922
|
+
})
|
|
923
|
+
return false
|
|
924
|
+
}
|
|
925
|
+
return applyFontSize(draft, rawLength, undefined, leadingModifier, tokens, vars, viewport)
|
|
926
|
+
}
|
|
927
|
+
let resolvedFontSize: number
|
|
928
|
+
if (typeof tokenSize.fontSize === 'string') {
|
|
929
|
+
const expanded = vars?.expand(tokenSize.fontSize)
|
|
930
|
+
if (vars?.isMissingViewport?.()) return true
|
|
931
|
+
if (!expanded) return false
|
|
932
|
+
const rawLength = parseLength(expanded.value, viewport)
|
|
933
|
+
if (rawLength === MISSING_VIEWPORT) return true
|
|
934
|
+
if (typeof rawLength !== 'number') {
|
|
935
|
+
vars?.reportError({
|
|
936
|
+
kind: 'invalid',
|
|
937
|
+
variable: expanded.variables[0],
|
|
938
|
+
value: expanded.value,
|
|
939
|
+
expected: 'length',
|
|
940
|
+
})
|
|
941
|
+
return false
|
|
942
|
+
}
|
|
943
|
+
resolvedFontSize = rawLength
|
|
944
|
+
} else {
|
|
945
|
+
resolvedFontSize = tokenSize.fontSize
|
|
946
|
+
}
|
|
947
|
+
let resolvedLineHeight: number | undefined
|
|
948
|
+
if (leadingModifier === undefined && tokenSize.lineHeight !== undefined) {
|
|
949
|
+
if (typeof tokenSize.lineHeight === 'string') {
|
|
950
|
+
const expanded = vars?.expand(tokenSize.lineHeight)
|
|
951
|
+
if (vars?.isMissingViewport?.()) return true
|
|
952
|
+
if (!expanded) return false
|
|
953
|
+
const parsed = parseLeadingRaw(expanded.value, viewport)
|
|
954
|
+
if (parsed === MISSING_VIEWPORT) return true
|
|
955
|
+
if (parsed === null) {
|
|
956
|
+
vars?.reportError({
|
|
957
|
+
kind: 'invalid',
|
|
958
|
+
variable: expanded.variables[0],
|
|
959
|
+
value: expanded.value,
|
|
960
|
+
expected: 'length',
|
|
961
|
+
})
|
|
962
|
+
return false
|
|
963
|
+
}
|
|
964
|
+
resolvedLineHeight =
|
|
965
|
+
'points' in parsed ? parsed.points : Math.round(resolvedFontSize * parsed.multiplier * 1000) / 1000
|
|
966
|
+
} else {
|
|
967
|
+
resolvedLineHeight = tokenSize.lineHeight
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
return applyFontSize(draft, resolvedFontSize, resolvedLineHeight, leadingModifier, tokens, vars, viewport)
|
|
971
|
+
}
|
|
972
|
+
if (isValidTokenSuffix(size)) {
|
|
973
|
+
const varName = `--text-${size}`
|
|
974
|
+
const ns = vars?.tryVariable(varName)
|
|
975
|
+
if (vars?.hasError()) return false
|
|
976
|
+
if (vars?.isMissingViewport?.()) return true
|
|
977
|
+
if (ns) {
|
|
978
|
+
const rawLength = parseLength(ns.value, viewport)
|
|
979
|
+
if (rawLength === MISSING_VIEWPORT) return true
|
|
980
|
+
if (typeof rawLength !== 'number') {
|
|
981
|
+
vars?.reportError({
|
|
982
|
+
kind: 'invalid',
|
|
983
|
+
variable: ns.variables[0] ?? varName,
|
|
984
|
+
value: ns.value,
|
|
985
|
+
expected: 'length',
|
|
986
|
+
})
|
|
987
|
+
return false
|
|
988
|
+
}
|
|
989
|
+
return applyFontSize(draft, rawLength, undefined, leadingModifier, tokens, vars, viewport)
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
return colorProp('text', 'color')(value, draft, tokens, false, vars, viewport)
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
export type Leading = { points: number } | { multiplier: number }
|
|
996
|
+
|
|
997
|
+
function leadingValue(
|
|
998
|
+
value: string,
|
|
999
|
+
tokens: Tokens,
|
|
1000
|
+
vars?: VariableResolver,
|
|
1001
|
+
viewport?: ViewportSize,
|
|
1002
|
+
): Leading | typeof MISSING_VIEWPORT | null {
|
|
1003
|
+
const named = own(tokens.leading, value)
|
|
1004
|
+
if (named !== undefined) {
|
|
1005
|
+
if (typeof named === 'number') return { multiplier: named }
|
|
1006
|
+
if (typeof named === 'object') return { points: named.points }
|
|
1007
|
+
const expanded = vars?.expand(named)
|
|
1008
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
1009
|
+
if (!expanded) return null
|
|
1010
|
+
const parsed = parseLeadingRaw(expanded.value, viewport)
|
|
1011
|
+
if (parsed === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
1012
|
+
if (parsed === null) {
|
|
1013
|
+
vars?.reportError({ kind: 'invalid', variable: expanded.variables[0], value: expanded.value, expected: 'length' })
|
|
1014
|
+
return null
|
|
1015
|
+
}
|
|
1016
|
+
return parsed
|
|
1017
|
+
}
|
|
1018
|
+
if (value === 'none') return { multiplier: 1 }
|
|
1019
|
+
if (isNumeric(value)) {
|
|
1020
|
+
const step = resolveBaseSpacing(tokens.spacing, vars, viewport)
|
|
1021
|
+
if (step === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
1022
|
+
if (step === null) return null
|
|
1023
|
+
return { points: Number(value) * step }
|
|
1024
|
+
}
|
|
1025
|
+
const dyn = resolveDynamic(value, vars)
|
|
1026
|
+
if (vars?.hasError()) return null
|
|
1027
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
1028
|
+
if (dyn !== null) {
|
|
1029
|
+
if (dyn.hint === 'color') return null
|
|
1030
|
+
const parsed = parseLeadingRaw(dyn.raw, viewport)
|
|
1031
|
+
if (parsed === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
1032
|
+
if (parsed !== null) return parsed
|
|
1033
|
+
if (dyn.isVar) vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'length' })
|
|
1034
|
+
return null
|
|
1035
|
+
}
|
|
1036
|
+
if (isValidTokenSuffix(value)) {
|
|
1037
|
+
const varName = `--leading-${value}`
|
|
1038
|
+
const ns = vars?.tryVariable(varName)
|
|
1039
|
+
if (vars?.hasError()) return null
|
|
1040
|
+
if (vars?.isMissingViewport?.()) return MISSING_VIEWPORT
|
|
1041
|
+
if (ns) {
|
|
1042
|
+
const parsed = parseLeadingRaw(ns.value, viewport)
|
|
1043
|
+
if (parsed === MISSING_VIEWPORT) return MISSING_VIEWPORT
|
|
1044
|
+
if (parsed !== null) return parsed
|
|
1045
|
+
vars?.reportError({ kind: 'invalid', variable: ns.variables[0] ?? varName, value: ns.value, expected: 'length' })
|
|
1046
|
+
return null
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return null
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
const leading: Handler = (value, draft, tokens, _negative, vars, viewport) => {
|
|
1053
|
+
const result = leadingValue(value, tokens, vars, viewport)
|
|
1054
|
+
if (result === MISSING_VIEWPORT) return true
|
|
1055
|
+
if (result === null) return false
|
|
1056
|
+
draft.leading = result
|
|
1057
|
+
return true
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
const border: Handler = (value, draft, tokens, _negative, vars, viewport) => {
|
|
1061
|
+
const [base] = splitModifier(value)
|
|
1062
|
+
const parsedDyn = parseDynamicValue(base)
|
|
1063
|
+
if (parsedDyn !== null) {
|
|
1064
|
+
if (
|
|
1065
|
+
parsedDyn.hint === 'color' ||
|
|
1066
|
+
(parsedDyn.hint === null && parsedDyn.isVar && !isMathExpression(parsedDyn.expr))
|
|
1067
|
+
) {
|
|
1068
|
+
return colorProp('style', 'borderColor')(value, draft, tokens, false, vars, viewport)
|
|
1069
|
+
}
|
|
1070
|
+
const dyn = resolveDynamic(value, vars)
|
|
1071
|
+
if (vars?.hasError()) return false
|
|
1072
|
+
if (vars?.isMissingViewport?.()) return true
|
|
1073
|
+
if (dyn !== null) {
|
|
1074
|
+
const width = parseLength(dyn.raw, viewport)
|
|
1075
|
+
if (width === MISSING_VIEWPORT) return true
|
|
1076
|
+
if (typeof width === 'number') {
|
|
1077
|
+
draft.layout.borderWidth = width
|
|
1078
|
+
return true
|
|
1079
|
+
}
|
|
1080
|
+
if (dyn.hint === 'length') {
|
|
1081
|
+
if (dyn.isVar) {
|
|
1082
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'length' })
|
|
1083
|
+
}
|
|
1084
|
+
return false
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
return colorProp('style', 'borderColor')(value, draft, tokens, false, vars, viewport)
|
|
1088
|
+
}
|
|
1089
|
+
if (isNumeric(value)) {
|
|
1090
|
+
draft.layout.borderWidth = Number(value)
|
|
1091
|
+
return true
|
|
1092
|
+
}
|
|
1093
|
+
return colorProp('style', 'borderColor')(value, draft, tokens, false, vars, viewport)
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
function numberProp(
|
|
1097
|
+
target: 'layout' | 'style' | 'text',
|
|
1098
|
+
key: string,
|
|
1099
|
+
scale = (n: number) => n,
|
|
1100
|
+
clears?: readonly string[],
|
|
1101
|
+
): Handler {
|
|
1102
|
+
return (value, draft, _tokens, _negative, vars) => {
|
|
1103
|
+
const number = numberValue(value, vars)
|
|
1104
|
+
if (number === null) return false
|
|
1105
|
+
if (clears) {
|
|
1106
|
+
for (const cleared of clears) delete draft[target][cleared]
|
|
1107
|
+
}
|
|
1108
|
+
draft[target][key] = scale(number)
|
|
1109
|
+
return true
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
const opacityHandler: Handler = (value, draft, _tokens, _negative, vars) => {
|
|
1114
|
+
if (parseDynamicValue(value) !== null) {
|
|
1115
|
+
const alpha = parseAlpha(value, vars)
|
|
1116
|
+
if (alpha === null) return false
|
|
1117
|
+
draft.style.opacity = alpha
|
|
1118
|
+
return true
|
|
1119
|
+
}
|
|
1120
|
+
const number = numberValue(value, vars)
|
|
1121
|
+
if (number === null) return false
|
|
1122
|
+
draft.style.opacity = number / 100
|
|
1123
|
+
return true
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
const aspect: Handler = (value, draft, tokens, _negative, vars) => {
|
|
1127
|
+
const named = own(tokens.aspect, value)
|
|
1128
|
+
if (named !== undefined) {
|
|
1129
|
+
if (typeof named === 'number') {
|
|
1130
|
+
draft.layout.aspectRatio = named
|
|
1131
|
+
return true
|
|
1132
|
+
}
|
|
1133
|
+
const expanded = vars?.expand(named)
|
|
1134
|
+
if (!expanded) return false
|
|
1135
|
+
const raw = expanded.value.trim()
|
|
1136
|
+
const ratio = /^(\d*\.?\d+)\s*\/\s*(\d*\.?\d+)$/.exec(raw)
|
|
1137
|
+
const result = ratio ? Number(ratio[1]) / Number(ratio[2]) : Number(raw)
|
|
1138
|
+
if (raw === '' || !Number.isFinite(result)) {
|
|
1139
|
+
vars?.reportError({
|
|
1140
|
+
kind: 'invalid',
|
|
1141
|
+
variable: expanded.variables[0],
|
|
1142
|
+
value: expanded.value,
|
|
1143
|
+
expected: 'aspect ratio',
|
|
1144
|
+
})
|
|
1145
|
+
return false
|
|
1146
|
+
}
|
|
1147
|
+
draft.layout.aspectRatio = result
|
|
1148
|
+
return true
|
|
1149
|
+
}
|
|
1150
|
+
const dyn = resolveDynamic(value, vars)
|
|
1151
|
+
if (vars?.hasError()) return false
|
|
1152
|
+
const raw = dyn !== null ? dyn.raw.trim() : value
|
|
1153
|
+
const ratio = /^(\d*\.?\d+)\s*\/\s*(\d*\.?\d+)$/.exec(raw)
|
|
1154
|
+
const result = ratio ? Number(ratio[1]) / Number(ratio[2]) : Number(raw)
|
|
1155
|
+
if (raw === '' || !Number.isFinite(result)) {
|
|
1156
|
+
if (dyn?.isVar) {
|
|
1157
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'aspect ratio' })
|
|
1158
|
+
}
|
|
1159
|
+
return false
|
|
1160
|
+
}
|
|
1161
|
+
draft.layout.aspectRatio = result
|
|
1162
|
+
return true
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
const fontWeight: Handler = (value, draft, tokens, _negative, vars) => {
|
|
1166
|
+
const weight = own(tokens.fontWeight, value)
|
|
1167
|
+
if (weight !== undefined) {
|
|
1168
|
+
if (!hasVarCall(weight)) {
|
|
1169
|
+
draft.text.fontWeight = weight
|
|
1170
|
+
return true
|
|
1171
|
+
}
|
|
1172
|
+
const expanded = vars?.expand(weight)
|
|
1173
|
+
if (!expanded) return false
|
|
1174
|
+
const parsed = parseFontWeightValue(expanded.value, tokens)
|
|
1175
|
+
if (parsed === null) {
|
|
1176
|
+
vars?.reportError({
|
|
1177
|
+
kind: 'invalid',
|
|
1178
|
+
variable: expanded.variables[0],
|
|
1179
|
+
value: expanded.value,
|
|
1180
|
+
expected: 'fontWeight',
|
|
1181
|
+
})
|
|
1182
|
+
return false
|
|
1183
|
+
}
|
|
1184
|
+
draft.text.fontWeight = parsed
|
|
1185
|
+
return true
|
|
1186
|
+
}
|
|
1187
|
+
const dyn = resolveDynamic(value, vars)
|
|
1188
|
+
if (vars?.hasError()) return false
|
|
1189
|
+
if (dyn !== null) {
|
|
1190
|
+
const parsed = parseFontWeightValue(dyn.raw, tokens)
|
|
1191
|
+
if (parsed !== null) {
|
|
1192
|
+
draft.text.fontWeight = parsed
|
|
1193
|
+
return true
|
|
1194
|
+
}
|
|
1195
|
+
if (dyn.isVar) {
|
|
1196
|
+
vars?.reportError({ kind: 'invalid', variable: dyn.variable, value: dyn.raw, expected: 'fontWeight' })
|
|
1197
|
+
}
|
|
1198
|
+
return false
|
|
1199
|
+
}
|
|
1200
|
+
if (isValidTokenSuffix(value)) {
|
|
1201
|
+
const varName = `--font-weight-${value}`
|
|
1202
|
+
const ns = vars?.tryVariable(varName)
|
|
1203
|
+
if (vars?.hasError()) return false
|
|
1204
|
+
if (ns) {
|
|
1205
|
+
const parsed = parseFontWeightValue(ns.value, tokens)
|
|
1206
|
+
if (parsed !== null) {
|
|
1207
|
+
draft.text.fontWeight = parsed
|
|
1208
|
+
return true
|
|
1209
|
+
}
|
|
1210
|
+
vars?.reportError({
|
|
1211
|
+
kind: 'invalid',
|
|
1212
|
+
variable: ns.variables[0] ?? varName,
|
|
1213
|
+
value: ns.value,
|
|
1214
|
+
expected: 'fontWeight',
|
|
1215
|
+
})
|
|
1216
|
+
return false
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
return false
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
const lineClamp: Handler = (value, draft, _tokens, _negative, vars) => {
|
|
1223
|
+
if (value === 'none') {
|
|
1224
|
+
draft.text.numberOfLines = 0
|
|
1225
|
+
return true
|
|
1226
|
+
}
|
|
1227
|
+
const lines = numberValue(value, vars)
|
|
1228
|
+
if (lines === null) return false
|
|
1229
|
+
draft.text.numberOfLines = lines
|
|
1230
|
+
draft.text.ellipsizeMode = 'tail'
|
|
1231
|
+
return true
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
// ---------------------------------------------------------------------------
|
|
1235
|
+
// Tables
|
|
1236
|
+
|
|
1237
|
+
type Static = (draft: Draft) => void
|
|
1238
|
+
|
|
1239
|
+
const set =
|
|
1240
|
+
(target: 'layout' | 'style' | 'text', values: Bag, clears?: readonly string[]): Static =>
|
|
1241
|
+
(draft) => {
|
|
1242
|
+
if (clears) {
|
|
1243
|
+
for (const key of clears) delete draft[target][key]
|
|
1244
|
+
}
|
|
1245
|
+
Object.assign(draft[target], values)
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
/** Classes that take no value. */
|
|
1249
|
+
export const staticUtilities: Record<string, Static> = {
|
|
1250
|
+
// Every NitroUI view is a flex container, so `flex` itself has nothing to do.
|
|
1251
|
+
flex: () => {},
|
|
1252
|
+
'flex-row': set('layout', { flexDirection: 'row' }),
|
|
1253
|
+
'flex-row-reverse': set('layout', { flexDirection: 'row-reverse' }),
|
|
1254
|
+
'flex-col': set('layout', { flexDirection: 'column' }),
|
|
1255
|
+
'flex-col-reverse': set('layout', { flexDirection: 'column-reverse' }),
|
|
1256
|
+
'flex-wrap': set('layout', { flexWrap: 'wrap' }),
|
|
1257
|
+
'flex-wrap-reverse': set('layout', { flexWrap: 'wrap-reverse' }),
|
|
1258
|
+
'flex-nowrap': set('layout', { flexWrap: 'nowrap' }),
|
|
1259
|
+
'flex-auto': set('layout', { flexGrow: 1, flexShrink: 1 }, ['flex']),
|
|
1260
|
+
'flex-initial': set('layout', { flexGrow: 0, flexShrink: 1 }, ['flex']),
|
|
1261
|
+
'flex-none': set('layout', { flexGrow: 0, flexShrink: 0 }, ['flex']),
|
|
1262
|
+
grow: set('layout', { flexGrow: 1 }),
|
|
1263
|
+
shrink: set('layout', { flexShrink: 1 }),
|
|
1264
|
+
|
|
1265
|
+
'justify-start': set('layout', { justifyContent: 'flex-start' }),
|
|
1266
|
+
'justify-end': set('layout', { justifyContent: 'flex-end' }),
|
|
1267
|
+
'justify-center': set('layout', { justifyContent: 'center' }),
|
|
1268
|
+
'justify-between': set('layout', { justifyContent: 'space-between' }),
|
|
1269
|
+
'justify-around': set('layout', { justifyContent: 'space-around' }),
|
|
1270
|
+
'justify-evenly': set('layout', { justifyContent: 'space-evenly' }),
|
|
1271
|
+
'items-start': set('layout', { alignItems: 'flex-start' }),
|
|
1272
|
+
'items-end': set('layout', { alignItems: 'flex-end' }),
|
|
1273
|
+
'items-center': set('layout', { alignItems: 'center' }),
|
|
1274
|
+
'items-stretch': set('layout', { alignItems: 'stretch' }),
|
|
1275
|
+
'items-baseline': set('layout', { alignItems: 'baseline' }),
|
|
1276
|
+
'self-auto': set('layout', { alignSelf: 'auto' }),
|
|
1277
|
+
'self-start': set('layout', { alignSelf: 'flex-start' }),
|
|
1278
|
+
'self-end': set('layout', { alignSelf: 'flex-end' }),
|
|
1279
|
+
'self-center': set('layout', { alignSelf: 'center' }),
|
|
1280
|
+
'self-stretch': set('layout', { alignSelf: 'stretch' }),
|
|
1281
|
+
'self-baseline': set('layout', { alignSelf: 'baseline' }),
|
|
1282
|
+
|
|
1283
|
+
absolute: set('layout', { position: 'absolute' }),
|
|
1284
|
+
relative: set('layout', { position: 'relative' }),
|
|
1285
|
+
static: set('layout', { position: 'static' }),
|
|
1286
|
+
'overflow-visible': set('layout', { overflow: 'visible' }),
|
|
1287
|
+
'overflow-hidden': set('layout', { overflow: 'hidden' }),
|
|
1288
|
+
'overflow-clip': set('layout', { overflow: 'hidden' }),
|
|
1289
|
+
'overflow-scroll': set('layout', { overflow: 'scroll' }),
|
|
1290
|
+
|
|
1291
|
+
// Corner curve (NitroWind addition): `continuous` is the iOS squircle.
|
|
1292
|
+
'corner-continuous': set('style', { cornerCurve: 'continuous' }),
|
|
1293
|
+
'corner-circular': set('style', { cornerCurve: 'circular' }),
|
|
1294
|
+
squircle: set('style', { cornerCurve: 'continuous' }),
|
|
1295
|
+
|
|
1296
|
+
'text-left': set('text', { textAlign: 'start' }),
|
|
1297
|
+
'text-start': set('text', { textAlign: 'start' }),
|
|
1298
|
+
'text-center': set('text', { textAlign: 'center' }),
|
|
1299
|
+
'text-right': set('text', { textAlign: 'end' }),
|
|
1300
|
+
'text-end': set('text', { textAlign: 'end' }),
|
|
1301
|
+
'text-justify': set('text', { textAlign: 'justify' }),
|
|
1302
|
+
truncate: set('text', { numberOfLines: 1, ellipsizeMode: 'tail' }),
|
|
1303
|
+
'text-ellipsis': set('text', { ellipsizeMode: 'tail' }),
|
|
1304
|
+
'text-clip': set('text', { ellipsizeMode: 'clip' }),
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
/** Classes that take a value, keyed by prefix (`p` in `p-4`). The empty value is the bare class (`rounded`). */
|
|
1308
|
+
export const functionalUtilities: Record<string, Handler> = {
|
|
1309
|
+
// Spacing
|
|
1310
|
+
p: spacingProps('layout', ['padding'], {
|
|
1311
|
+
clears: ['paddingHorizontal', 'paddingVertical', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'],
|
|
1312
|
+
}),
|
|
1313
|
+
px: spacingProps('layout', ['paddingHorizontal'], { clears: ['paddingLeft', 'paddingRight'] }),
|
|
1314
|
+
py: spacingProps('layout', ['paddingVertical'], { clears: ['paddingTop', 'paddingBottom'] }),
|
|
1315
|
+
pt: spacingProps('layout', ['paddingTop']),
|
|
1316
|
+
pr: spacingProps('layout', ['paddingRight']),
|
|
1317
|
+
pb: spacingProps('layout', ['paddingBottom']),
|
|
1318
|
+
pl: spacingProps('layout', ['paddingLeft']),
|
|
1319
|
+
m: spacingProps('layout', ['margin'], {
|
|
1320
|
+
auto: true,
|
|
1321
|
+
clears: ['marginHorizontal', 'marginVertical', 'marginTop', 'marginRight', 'marginBottom', 'marginLeft'],
|
|
1322
|
+
}),
|
|
1323
|
+
mx: spacingProps('layout', ['marginHorizontal'], { auto: true, clears: ['marginLeft', 'marginRight'] }),
|
|
1324
|
+
my: spacingProps('layout', ['marginVertical'], { auto: true, clears: ['marginTop', 'marginBottom'] }),
|
|
1325
|
+
mt: spacingProps('layout', ['marginTop'], { auto: true }),
|
|
1326
|
+
mr: spacingProps('layout', ['marginRight'], { auto: true }),
|
|
1327
|
+
mb: spacingProps('layout', ['marginBottom'], { auto: true }),
|
|
1328
|
+
ml: spacingProps('layout', ['marginLeft'], { auto: true }),
|
|
1329
|
+
gap: spacingProps('layout', ['gap'], { clears: ['columnGap', 'rowGap'] }),
|
|
1330
|
+
'gap-x': spacingProps('layout', ['columnGap']),
|
|
1331
|
+
'gap-y': spacingProps('layout', ['rowGap']),
|
|
1332
|
+
|
|
1333
|
+
// Sizing
|
|
1334
|
+
w: spacingProps('layout', ['width'], { container: true, screen: 'width' }),
|
|
1335
|
+
h: spacingProps('layout', ['height'], { screen: 'height' }),
|
|
1336
|
+
size: spacingProps('layout', ['width', 'height']),
|
|
1337
|
+
'min-w': spacingProps('layout', ['minWidth'], { container: true, screen: 'width' }),
|
|
1338
|
+
'min-h': spacingProps('layout', ['minHeight'], { screen: 'height' }),
|
|
1339
|
+
'max-w': spacingProps('layout', ['maxWidth'], { container: true, screen: 'width' }),
|
|
1340
|
+
'max-h': spacingProps('layout', ['maxHeight'], { screen: 'height' }),
|
|
1341
|
+
aspect,
|
|
1342
|
+
|
|
1343
|
+
// Positioning
|
|
1344
|
+
inset: spacingProps('layout', ['top', 'right', 'bottom', 'left']),
|
|
1345
|
+
'inset-x': spacingProps('layout', ['left', 'right']),
|
|
1346
|
+
'inset-y': spacingProps('layout', ['top', 'bottom']),
|
|
1347
|
+
top: spacingProps('layout', ['top']),
|
|
1348
|
+
right: spacingProps('layout', ['right']),
|
|
1349
|
+
bottom: spacingProps('layout', ['bottom']),
|
|
1350
|
+
left: spacingProps('layout', ['left']),
|
|
1351
|
+
|
|
1352
|
+
// Flexbox
|
|
1353
|
+
flex: numberProp('layout', 'flex', undefined, ['flexGrow', 'flexShrink']),
|
|
1354
|
+
grow: numberProp('layout', 'flexGrow'),
|
|
1355
|
+
shrink: numberProp('layout', 'flexShrink'),
|
|
1356
|
+
|
|
1357
|
+
// Borders and corners
|
|
1358
|
+
border,
|
|
1359
|
+
rounded: rounded(),
|
|
1360
|
+
...Object.fromEntries(Object.keys(SIDE_CORNERS).map((side) => [`rounded-${side}`, rounded(side)])),
|
|
1361
|
+
|
|
1362
|
+
// Backgrounds
|
|
1363
|
+
bg: (value, draft, tokens, negative, vars) => {
|
|
1364
|
+
if (value.startsWith('linear-')) return linearGradient(value.slice(7), draft, tokens, negative, vars)
|
|
1365
|
+
// Tailwind v3 spelling.
|
|
1366
|
+
if (value.startsWith('gradient-')) return linearGradient(value.slice(9), draft, tokens, negative, vars)
|
|
1367
|
+
if (!colorProp('style', 'background')(value, draft, tokens, negative, vars)) return false
|
|
1368
|
+
delete draft.gradient
|
|
1369
|
+
return true
|
|
1370
|
+
},
|
|
1371
|
+
from: gradientStop('from'),
|
|
1372
|
+
via: gradientStop('via'),
|
|
1373
|
+
to: gradientStop('to'),
|
|
1374
|
+
|
|
1375
|
+
// Effects
|
|
1376
|
+
opacity: opacityHandler,
|
|
1377
|
+
shadow: shadowHandler('shadow'),
|
|
1378
|
+
'inset-shadow': shadowHandler('insetShadow'),
|
|
1379
|
+
'drop-shadow': dropShadow,
|
|
1380
|
+
tint: colorProp('style', 'tintColor'),
|
|
1381
|
+
|
|
1382
|
+
// Transforms
|
|
1383
|
+
'translate-x': translate(['translateX']),
|
|
1384
|
+
'translate-y': translate(['translateY']),
|
|
1385
|
+
'translate-z': translate(['translateZ']),
|
|
1386
|
+
translate: translate(['translateX', 'translateY']),
|
|
1387
|
+
scale: transformNumber('scale', (n) => n / 100, ['scaleX', 'scaleY']),
|
|
1388
|
+
'scale-x': transformNumber('scaleX', (n) => n / 100),
|
|
1389
|
+
'scale-y': transformNumber('scaleY', (n) => n / 100),
|
|
1390
|
+
rotate: transformNumber('rotate', (n) => n),
|
|
1391
|
+
|
|
1392
|
+
// Typography
|
|
1393
|
+
text: textUtility,
|
|
1394
|
+
font: fontWeight,
|
|
1395
|
+
leading,
|
|
1396
|
+
'line-clamp': lineClamp,
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
/** Bare classes that are functional utilities with an empty value. */
|
|
1400
|
+
const BARE_DEFAULTS: Record<string, string> = {
|
|
1401
|
+
border: '1',
|
|
1402
|
+
rounded: '',
|
|
1403
|
+
shadow: '',
|
|
1404
|
+
grow: '1',
|
|
1405
|
+
shrink: '1',
|
|
1406
|
+
}
|
|
1407
|
+
for (const side of Object.keys(SIDE_CORNERS)) BARE_DEFAULTS[`rounded-${side}`] = ''
|
|
1408
|
+
|
|
1409
|
+
/**
|
|
1410
|
+
* Extracts all CSS variable names a utility depends on:
|
|
1411
|
+
* - explicit `var(--x)` / `(--x)` references (including opacity/leading modifiers),
|
|
1412
|
+
* - variables referenced by matching config tokens (e.g. `tokens.extend.colors.primary = 'var(--x)'`),
|
|
1413
|
+
* - implicit namespace variables (`--color-*`, `--radius-*`, `--text-*`, `--font-weight-*`,
|
|
1414
|
+
* `--leading-*`, `--shadow-*`) when a token lookup finds nothing in `tokens`.
|
|
1415
|
+
*/
|
|
1416
|
+
export function extractUtilityVariableNames(utility: string, tokens: Tokens): string[] {
|
|
1417
|
+
const names = new Set<string>(extractVariableNames(utility))
|
|
1418
|
+
const addFromExpr = (expr: string): void => {
|
|
1419
|
+
for (const v of extractVariableNames(expr)) names.add(v)
|
|
1420
|
+
}
|
|
1421
|
+
const addTokenOrNamespace = (tokenValue: unknown, namespacePrefix: string, suffix: string): void => {
|
|
1422
|
+
if (tokenValue !== undefined) {
|
|
1423
|
+
if (typeof tokenValue === 'string' && hasVarCall(tokenValue)) addFromExpr(tokenValue)
|
|
1424
|
+
return
|
|
1425
|
+
}
|
|
1426
|
+
if (isValidTokenSuffix(suffix)) names.add(`${namespacePrefix}${suffix}`)
|
|
1427
|
+
}
|
|
1428
|
+
const addLeadingDep = (modifier: string): void => {
|
|
1429
|
+
if (parseDynamicValue(modifier) !== null || modifier === 'none') return
|
|
1430
|
+
if (isNumeric(modifier)) {
|
|
1431
|
+
if (typeof tokens.spacing === 'string' && hasVarCall(tokens.spacing)) addFromExpr(tokens.spacing)
|
|
1432
|
+
return
|
|
1433
|
+
}
|
|
1434
|
+
addTokenOrNamespace(own(tokens.leading, modifier), '--leading-', modifier)
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
const negative = utility.startsWith('-')
|
|
1438
|
+
const name = negative ? utility.slice(1) : utility
|
|
1439
|
+
if (!negative && own(staticUtilities, name) !== undefined) return [...names]
|
|
1440
|
+
|
|
1441
|
+
const bare = own(BARE_DEFAULTS, name)
|
|
1442
|
+
if (bare !== undefined) {
|
|
1443
|
+
if (name === 'rounded' || name.startsWith('rounded-')) {
|
|
1444
|
+
const token = own(tokens.radius, '')
|
|
1445
|
+
if (typeof token === 'string' && hasVarCall(token)) addFromExpr(token)
|
|
1446
|
+
} else if (name === 'shadow') {
|
|
1447
|
+
const token = own(tokens.shadow, '')
|
|
1448
|
+
if (typeof token === 'string' && hasVarCall(token)) addFromExpr(token)
|
|
1449
|
+
}
|
|
1450
|
+
return [...names]
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
const bracket = name.indexOf('[')
|
|
1454
|
+
const paren = name.indexOf('(')
|
|
1455
|
+
const searchEnd =
|
|
1456
|
+
bracket === -1 ? (paren === -1 ? name.length : paren) : paren === -1 ? bracket : Math.min(bracket, paren)
|
|
1457
|
+
|
|
1458
|
+
let prefix: string | undefined
|
|
1459
|
+
let value = ''
|
|
1460
|
+
for (let i = searchEnd - 1; i > 0; i--) {
|
|
1461
|
+
if (name[i] !== '-') continue
|
|
1462
|
+
const candidate = name.slice(0, i)
|
|
1463
|
+
if (own(functionalUtilities, candidate) !== undefined) {
|
|
1464
|
+
prefix = candidate
|
|
1465
|
+
value = name.slice(i + 1)
|
|
1466
|
+
break
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
if (prefix === undefined) return [...names]
|
|
1470
|
+
|
|
1471
|
+
if (prefix === 'bg') {
|
|
1472
|
+
if (value.startsWith('linear-') || value.startsWith('gradient-')) return [...names]
|
|
1473
|
+
const [base] = splitModifier(value)
|
|
1474
|
+
if (parseDynamicValue(base) === null) addTokenOrNamespace(own(tokens.colors, base), '--color-', base)
|
|
1475
|
+
} else if (prefix === 'from' || prefix === 'via' || prefix === 'to' || prefix === 'tint') {
|
|
1476
|
+
const [base] = splitModifier(value)
|
|
1477
|
+
if (parseDynamicValue(base) === null) addTokenOrNamespace(own(tokens.colors, base), '--color-', base)
|
|
1478
|
+
} else if (prefix === 'border') {
|
|
1479
|
+
const [base] = splitModifier(value)
|
|
1480
|
+
if (parseDynamicValue(base) === null && !isNumeric(value)) {
|
|
1481
|
+
addTokenOrNamespace(own(tokens.colors, base), '--color-', base)
|
|
1482
|
+
}
|
|
1483
|
+
} else if (prefix === 'rounded' || prefix.startsWith('rounded-')) {
|
|
1484
|
+
if (parseDynamicValue(value) === null) addTokenOrNamespace(own(tokens.radius, value), '--radius-', value)
|
|
1485
|
+
} else if (prefix === 'font') {
|
|
1486
|
+
if (parseDynamicValue(value) === null) addTokenOrNamespace(own(tokens.fontWeight, value), '--font-weight-', value)
|
|
1487
|
+
} else if (prefix === 'leading') {
|
|
1488
|
+
addLeadingDep(value)
|
|
1489
|
+
} else if (prefix === 'text') {
|
|
1490
|
+
const [base, modifier] = splitModifier(value)
|
|
1491
|
+
if (parseDynamicValue(base) === null) {
|
|
1492
|
+
const fontSizeToken = own(tokens.fontSize, base)
|
|
1493
|
+
if (fontSizeToken !== undefined) {
|
|
1494
|
+
if (typeof fontSizeToken === 'string') {
|
|
1495
|
+
if (hasVarCall(fontSizeToken)) addFromExpr(fontSizeToken)
|
|
1496
|
+
} else {
|
|
1497
|
+
if (typeof fontSizeToken.fontSize === 'string' && hasVarCall(fontSizeToken.fontSize)) {
|
|
1498
|
+
addFromExpr(fontSizeToken.fontSize)
|
|
1499
|
+
}
|
|
1500
|
+
if (
|
|
1501
|
+
modifier === undefined &&
|
|
1502
|
+
typeof fontSizeToken.lineHeight === 'string' &&
|
|
1503
|
+
hasVarCall(fontSizeToken.lineHeight)
|
|
1504
|
+
) {
|
|
1505
|
+
addFromExpr(fontSizeToken.lineHeight)
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
if (modifier !== undefined) addLeadingDep(modifier)
|
|
1509
|
+
} else {
|
|
1510
|
+
const colorToken = own(tokens.colors, base)
|
|
1511
|
+
if (colorToken !== undefined) {
|
|
1512
|
+
if (typeof colorToken === 'string' && hasVarCall(colorToken)) addFromExpr(colorToken)
|
|
1513
|
+
} else if (isValidTokenSuffix(base)) {
|
|
1514
|
+
names.add(`--text-${base}`)
|
|
1515
|
+
names.add(`--color-${base}`)
|
|
1516
|
+
if (modifier !== undefined) addLeadingDep(modifier)
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
} else if (modifier !== undefined) {
|
|
1520
|
+
addLeadingDep(modifier)
|
|
1521
|
+
}
|
|
1522
|
+
} else if (prefix === 'shadow') {
|
|
1523
|
+
const [base, modifier] = splitModifier(value)
|
|
1524
|
+
if (parseDynamicValue(base) === null) {
|
|
1525
|
+
const shadowToken = own(tokens.shadow, value)
|
|
1526
|
+
if (shadowToken !== undefined) {
|
|
1527
|
+
if (typeof shadowToken === 'string' && hasVarCall(shadowToken)) addFromExpr(shadowToken)
|
|
1528
|
+
} else {
|
|
1529
|
+
const colorToken = own(tokens.colors, base)
|
|
1530
|
+
if (colorToken !== undefined) {
|
|
1531
|
+
if (typeof colorToken === 'string' && hasVarCall(colorToken)) addFromExpr(colorToken)
|
|
1532
|
+
} else {
|
|
1533
|
+
if (modifier === undefined && isValidTokenSuffix(value)) names.add(`--shadow-${value}`)
|
|
1534
|
+
if (isValidTokenSuffix(base)) names.add(`--color-${base}`)
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
} else if (prefix === 'inset-shadow') {
|
|
1539
|
+
const [base, modifier] = splitModifier(value)
|
|
1540
|
+
if (parseDynamicValue(base) === null) {
|
|
1541
|
+
const insetShadowToken = own(tokens.insetShadow, value)
|
|
1542
|
+
if (insetShadowToken !== undefined) {
|
|
1543
|
+
if (typeof insetShadowToken === 'string' && hasVarCall(insetShadowToken)) addFromExpr(insetShadowToken)
|
|
1544
|
+
} else {
|
|
1545
|
+
const colorToken = own(tokens.colors, base)
|
|
1546
|
+
if (colorToken !== undefined) {
|
|
1547
|
+
if (typeof colorToken === 'string' && hasVarCall(colorToken)) addFromExpr(colorToken)
|
|
1548
|
+
} else {
|
|
1549
|
+
if (modifier === undefined && isValidTokenSuffix(value)) names.add(`--inset-shadow-${value}`)
|
|
1550
|
+
if (isValidTokenSuffix(base)) names.add(`--color-${base}`)
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
} else if (prefix === 'drop-shadow') {
|
|
1555
|
+
if (parseDynamicValue(value) === null) addTokenOrNamespace(own(tokens.dropShadow, value), '--drop-shadow-', value)
|
|
1556
|
+
} else if (prefix === 'aspect') {
|
|
1557
|
+
if (parseDynamicValue(value) === null) {
|
|
1558
|
+
const aspectToken = own(tokens.aspect, value)
|
|
1559
|
+
if (typeof aspectToken === 'string' && hasVarCall(aspectToken)) addFromExpr(aspectToken)
|
|
1560
|
+
}
|
|
1561
|
+
} else {
|
|
1562
|
+
const spacing = own(SPACING_CONFLICTS, prefix)
|
|
1563
|
+
if (spacing !== undefined && parseDynamicValue(value) === null) {
|
|
1564
|
+
const scaleOverride = tokens.spacingScale !== undefined ? own(tokens.spacingScale, value) : undefined
|
|
1565
|
+
if (scaleOverride !== undefined) {
|
|
1566
|
+
if (typeof scaleOverride === 'string' && hasVarCall(scaleOverride)) addFromExpr(scaleOverride)
|
|
1567
|
+
} else if (isNumeric(value) && value !== '0') {
|
|
1568
|
+
if (typeof tokens.spacing === 'string' && hasVarCall(tokens.spacing)) addFromExpr(tokens.spacing)
|
|
1569
|
+
} else if (spacing.options?.container && own(tokens.container, value) !== undefined) {
|
|
1570
|
+
const containerToken = own(tokens.container, value)
|
|
1571
|
+
if (typeof containerToken === 'string' && hasVarCall(containerToken)) addFromExpr(containerToken)
|
|
1572
|
+
} else if (!isSpacingLike(value, tokens, spacing.options) && isValidTokenSuffix(value)) {
|
|
1573
|
+
names.add(`--spacing-${value}`)
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
return [...names]
|
|
1579
|
+
}
|
|
1580
|
+
|
|
1581
|
+
const SCREEN_UTILITIES = new Set([
|
|
1582
|
+
'w-screen',
|
|
1583
|
+
'h-screen',
|
|
1584
|
+
'min-w-screen',
|
|
1585
|
+
'min-h-screen',
|
|
1586
|
+
'max-w-screen',
|
|
1587
|
+
'max-h-screen',
|
|
1588
|
+
])
|
|
1589
|
+
|
|
1590
|
+
/** Returns true when `utility` directly references viewport dimensions (`*-screen` or `vw`/`vh`). */
|
|
1591
|
+
export function utilityUsesViewport(utility: string): boolean {
|
|
1592
|
+
const name = utility.startsWith('-') ? utility.slice(1) : utility
|
|
1593
|
+
if (SCREEN_UTILITIES.has(name)) return true
|
|
1594
|
+
const bracket = name.indexOf('[')
|
|
1595
|
+
if (bracket === -1) return false
|
|
1596
|
+
return hasViewportUnit(name.slice(bracket))
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/**
|
|
1600
|
+
* Applies one utility (no variants) to the draft. Returns false when the class is unknown.
|
|
1601
|
+
*/
|
|
1602
|
+
export function applyUtility(
|
|
1603
|
+
utility: string,
|
|
1604
|
+
draft: Draft,
|
|
1605
|
+
tokens: Tokens,
|
|
1606
|
+
vars?: VariableResolver,
|
|
1607
|
+
viewport?: ViewportSize,
|
|
1608
|
+
): boolean {
|
|
1609
|
+
const negative = utility.startsWith('-')
|
|
1610
|
+
const name = negative ? utility.slice(1) : utility
|
|
1611
|
+
|
|
1612
|
+
if (!negative) {
|
|
1613
|
+
const fixed = staticUtilities[name]
|
|
1614
|
+
if (fixed) {
|
|
1615
|
+
fixed(draft)
|
|
1616
|
+
return true
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
const bare = BARE_DEFAULTS[name]
|
|
1621
|
+
if (bare !== undefined) return functionalUtilities[name]!(bare, draft, tokens, negative, vars, viewport)
|
|
1622
|
+
|
|
1623
|
+
// Try the longest prefix first, splitting only before an arbitrary `[...]` or `(...)` value.
|
|
1624
|
+
const bracket = name.indexOf('[')
|
|
1625
|
+
const paren = name.indexOf('(')
|
|
1626
|
+
const searchEnd =
|
|
1627
|
+
bracket === -1 ? (paren === -1 ? name.length : paren) : paren === -1 ? bracket : Math.min(bracket, paren)
|
|
1628
|
+
for (let i = searchEnd - 1; i > 0; i--) {
|
|
1629
|
+
if (name[i] !== '-') continue
|
|
1630
|
+
const handler = functionalUtilities[name.slice(0, i)]
|
|
1631
|
+
if (handler) {
|
|
1632
|
+
if (handler(name.slice(i + 1), draft, tokens, negative, vars, viewport)) return true
|
|
1633
|
+
if (vars?.hasError() || vars?.isMissingViewport?.()) return false
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
return false
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
/** Turns the draft into NitroUI props, resolving classes that depend on each other. */
|
|
1640
|
+
export function finalizeDraft(draft: Draft): void {
|
|
1641
|
+
if (draft.corners) draft.style.maskedCorners = [...draft.corners]
|
|
1642
|
+
|
|
1643
|
+
const gradient = draft.gradient
|
|
1644
|
+
if (gradient?.start && gradient.end && (gradient.from !== undefined || gradient.to !== undefined)) {
|
|
1645
|
+
const from = gradient.from ?? withAlpha(gradient.to!, 0)
|
|
1646
|
+
const to = gradient.to ?? withAlpha(from, 0)
|
|
1647
|
+
const colors = gradient.via === undefined ? [from, to] : [from, gradient.via, to]
|
|
1648
|
+
draft.style.background = { type: 'linear-gradient', colors, start: gradient.start, end: gradient.end }
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
const tint = (layers: TokenBoxShadow[], tintColor?: { color: PackedColor; explicitAlpha: boolean }) =>
|
|
1652
|
+
tintColor === undefined
|
|
1653
|
+
? layers
|
|
1654
|
+
: layers.map((layer) => ({
|
|
1655
|
+
...layer,
|
|
1656
|
+
// Like Tailwind, a shadow color keeps each layer's alpha unless it has its own opacity modifier.
|
|
1657
|
+
color: tintColor.explicitAlpha
|
|
1658
|
+
? tintColor.color
|
|
1659
|
+
: multiplyAlpha(withAlpha(tintColor.color, 1), (layer.color & 0xff) / 255),
|
|
1660
|
+
}))
|
|
1661
|
+
const shadows = [
|
|
1662
|
+
...tint(draft.insetShadow ?? [], draft.insetShadowColor),
|
|
1663
|
+
...tint(draft.shadow ?? [], draft.shadowColor),
|
|
1664
|
+
]
|
|
1665
|
+
if (draft.shadow !== undefined || draft.insetShadow !== undefined) draft.style.boxShadow = shadows
|
|
1666
|
+
|
|
1667
|
+
if (draft.dropShadow !== undefined) {
|
|
1668
|
+
draft.style.filter = draft.dropShadow.map((dropShadow) => ({ dropShadow }))
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
const fontSize = (draft.text.fontSize as number | undefined) ?? 17
|
|
1672
|
+
if (draft.leading !== undefined) {
|
|
1673
|
+
draft.text.lineHeight =
|
|
1674
|
+
'points' in draft.leading
|
|
1675
|
+
? draft.leading.points
|
|
1676
|
+
: // Without a font size class this scales NitroUI's default Text size of 17.
|
|
1677
|
+
Math.round(fontSize * draft.leading.multiplier * 1000) / 1000
|
|
1678
|
+
} else if (draft.sizeLineHeight !== undefined) {
|
|
1679
|
+
draft.text.lineHeight = draft.sizeLineHeight
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
// ---------------------------------------------------------------------------
|
|
1684
|
+
// Conflict classification (for `cn()`)
|
|
1685
|
+
|
|
1686
|
+
export interface UtilityConflict {
|
|
1687
|
+
readonly group: string
|
|
1688
|
+
readonly overrides?: readonly string[]
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
const ROUNDED_SIDE_GROUPS: readonly string[] = Object.keys(SIDE_CORNERS).map((side) => `rounded-${side}`)
|
|
1692
|
+
|
|
1693
|
+
const FLEX_CONFLICT: UtilityConflict = { group: 'flex', overrides: ['flex-grow', 'flex-shrink'] }
|
|
1694
|
+
const ROUNDED_CONFLICT: UtilityConflict = { group: 'rounded', overrides: ROUNDED_SIDE_GROUPS }
|
|
1695
|
+
const FONT_SIZE_CONFLICT: UtilityConflict = { group: 'font-size', overrides: ['leading'] }
|
|
1696
|
+
const BG_COLOR_CONFLICT: UtilityConflict = { group: 'bg-color', overrides: ['bg-gradient', 'from', 'via', 'to'] }
|
|
1697
|
+
const BG_GRADIENT_CONFLICT: UtilityConflict = { group: 'bg-gradient', overrides: ['bg-color'] }
|
|
1698
|
+
|
|
1699
|
+
const STATIC_CONFLICTS: Record<string, UtilityConflict> = {
|
|
1700
|
+
flex: { group: 'display-flex' },
|
|
1701
|
+
'flex-row': { group: 'flex-direction' },
|
|
1702
|
+
'flex-row-reverse': { group: 'flex-direction' },
|
|
1703
|
+
'flex-col': { group: 'flex-direction' },
|
|
1704
|
+
'flex-col-reverse': { group: 'flex-direction' },
|
|
1705
|
+
'flex-wrap': { group: 'flex-wrap' },
|
|
1706
|
+
'flex-wrap-reverse': { group: 'flex-wrap' },
|
|
1707
|
+
'flex-nowrap': { group: 'flex-wrap' },
|
|
1708
|
+
'flex-auto': FLEX_CONFLICT,
|
|
1709
|
+
'flex-initial': FLEX_CONFLICT,
|
|
1710
|
+
'flex-none': FLEX_CONFLICT,
|
|
1711
|
+
grow: { group: 'flex-grow' },
|
|
1712
|
+
shrink: { group: 'flex-shrink' },
|
|
1713
|
+
|
|
1714
|
+
'justify-start': { group: 'justify-content' },
|
|
1715
|
+
'justify-end': { group: 'justify-content' },
|
|
1716
|
+
'justify-center': { group: 'justify-content' },
|
|
1717
|
+
'justify-between': { group: 'justify-content' },
|
|
1718
|
+
'justify-around': { group: 'justify-content' },
|
|
1719
|
+
'justify-evenly': { group: 'justify-content' },
|
|
1720
|
+
'items-start': { group: 'align-items' },
|
|
1721
|
+
'items-end': { group: 'align-items' },
|
|
1722
|
+
'items-center': { group: 'align-items' },
|
|
1723
|
+
'items-stretch': { group: 'align-items' },
|
|
1724
|
+
'items-baseline': { group: 'align-items' },
|
|
1725
|
+
'self-auto': { group: 'align-self' },
|
|
1726
|
+
'self-start': { group: 'align-self' },
|
|
1727
|
+
'self-end': { group: 'align-self' },
|
|
1728
|
+
'self-center': { group: 'align-self' },
|
|
1729
|
+
'self-stretch': { group: 'align-self' },
|
|
1730
|
+
'self-baseline': { group: 'align-self' },
|
|
1731
|
+
|
|
1732
|
+
absolute: { group: 'position' },
|
|
1733
|
+
relative: { group: 'position' },
|
|
1734
|
+
static: { group: 'position' },
|
|
1735
|
+
'overflow-visible': { group: 'overflow' },
|
|
1736
|
+
'overflow-hidden': { group: 'overflow' },
|
|
1737
|
+
'overflow-clip': { group: 'overflow' },
|
|
1738
|
+
'overflow-scroll': { group: 'overflow' },
|
|
1739
|
+
|
|
1740
|
+
'corner-continuous': { group: 'corner-curve' },
|
|
1741
|
+
'corner-circular': { group: 'corner-curve' },
|
|
1742
|
+
squircle: { group: 'corner-curve' },
|
|
1743
|
+
|
|
1744
|
+
'text-left': { group: 'text-align' },
|
|
1745
|
+
'text-start': { group: 'text-align' },
|
|
1746
|
+
'text-center': { group: 'text-align' },
|
|
1747
|
+
'text-right': { group: 'text-align' },
|
|
1748
|
+
'text-end': { group: 'text-align' },
|
|
1749
|
+
'text-justify': { group: 'text-align' },
|
|
1750
|
+
truncate: { group: 'line-clamp', overrides: ['text-overflow'] },
|
|
1751
|
+
'text-ellipsis': { group: 'text-overflow' },
|
|
1752
|
+
'text-clip': { group: 'text-overflow' },
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
const SPACING_CONFLICTS: Record<string, { conflict: UtilityConflict; options?: SpacingOptions & { auto?: boolean } }> =
|
|
1756
|
+
{
|
|
1757
|
+
p: { conflict: { group: 'p', overrides: ['px', 'py', 'pt', 'pr', 'pb', 'pl'] } },
|
|
1758
|
+
px: { conflict: { group: 'px', overrides: ['pl', 'pr'] } },
|
|
1759
|
+
py: { conflict: { group: 'py', overrides: ['pt', 'pb'] } },
|
|
1760
|
+
pt: { conflict: { group: 'pt' } },
|
|
1761
|
+
pr: { conflict: { group: 'pr' } },
|
|
1762
|
+
pb: { conflict: { group: 'pb' } },
|
|
1763
|
+
pl: { conflict: { group: 'pl' } },
|
|
1764
|
+
m: { conflict: { group: 'm', overrides: ['mx', 'my', 'mt', 'mr', 'mb', 'ml'] }, options: { auto: true } },
|
|
1765
|
+
mx: { conflict: { group: 'mx', overrides: ['ml', 'mr'] }, options: { auto: true } },
|
|
1766
|
+
my: { conflict: { group: 'my', overrides: ['mt', 'mb'] }, options: { auto: true } },
|
|
1767
|
+
mt: { conflict: { group: 'mt' }, options: { auto: true } },
|
|
1768
|
+
mr: { conflict: { group: 'mr' }, options: { auto: true } },
|
|
1769
|
+
mb: { conflict: { group: 'mb' }, options: { auto: true } },
|
|
1770
|
+
ml: { conflict: { group: 'ml' }, options: { auto: true } },
|
|
1771
|
+
gap: { conflict: { group: 'gap', overrides: ['gap-x', 'gap-y'] } },
|
|
1772
|
+
'gap-x': { conflict: { group: 'gap-x' } },
|
|
1773
|
+
'gap-y': { conflict: { group: 'gap-y' } },
|
|
1774
|
+
w: { conflict: { group: 'w' }, options: { container: true, screen: 'width' } },
|
|
1775
|
+
h: { conflict: { group: 'h' }, options: { screen: 'height' } },
|
|
1776
|
+
size: { conflict: { group: 'size', overrides: ['w', 'h'] } },
|
|
1777
|
+
'min-w': { conflict: { group: 'min-w' }, options: { container: true, screen: 'width' } },
|
|
1778
|
+
'min-h': { conflict: { group: 'min-h' }, options: { screen: 'height' } },
|
|
1779
|
+
'max-w': { conflict: { group: 'max-w' }, options: { container: true, screen: 'width' } },
|
|
1780
|
+
'max-h': { conflict: { group: 'max-h' }, options: { screen: 'height' } },
|
|
1781
|
+
inset: { conflict: { group: 'inset', overrides: ['inset-x', 'inset-y', 'top', 'right', 'bottom', 'left'] } },
|
|
1782
|
+
'inset-x': { conflict: { group: 'inset-x', overrides: ['left', 'right'] } },
|
|
1783
|
+
'inset-y': { conflict: { group: 'inset-y', overrides: ['top', 'bottom'] } },
|
|
1784
|
+
top: { conflict: { group: 'top' } },
|
|
1785
|
+
right: { conflict: { group: 'right' } },
|
|
1786
|
+
bottom: { conflict: { group: 'bottom' } },
|
|
1787
|
+
left: { conflict: { group: 'left' } },
|
|
1788
|
+
translate: {
|
|
1789
|
+
conflict: { group: 'translate', overrides: ['translate-x', 'translate-y'] },
|
|
1790
|
+
options: { fraction: false },
|
|
1791
|
+
},
|
|
1792
|
+
'translate-x': { conflict: { group: 'translate-x' }, options: { fraction: false } },
|
|
1793
|
+
'translate-y': { conflict: { group: 'translate-y' }, options: { fraction: false } },
|
|
1794
|
+
'translate-z': { conflict: { group: 'translate-z' }, options: { fraction: false } },
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
function isSpacingLike(value: string, tokens: Tokens, options?: SpacingOptions & { auto?: boolean }): boolean {
|
|
1798
|
+
const dyn = parseDynamicValue(value)
|
|
1799
|
+
if (dyn !== null) return dyn.hint !== 'color'
|
|
1800
|
+
if (tokens.spacingScale !== undefined && own(tokens.spacingScale, value) !== undefined) return true
|
|
1801
|
+
if (options?.auto && value === 'auto') return true
|
|
1802
|
+
if (options?.screen !== undefined && value === 'screen') return true
|
|
1803
|
+
if (value === 'px' || value === '0' || value === 'full' || isNumeric(value)) return true
|
|
1804
|
+
if (options?.fraction !== false && /^\d+\/\d+$/.test(value)) return true
|
|
1805
|
+
if (options?.container && own(tokens.container, value) !== undefined) return true
|
|
1806
|
+
return false
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
function isNumberLike(value: string): boolean {
|
|
1810
|
+
if (parseDynamicValue(value) !== null) return true
|
|
1811
|
+
const stripped = value.replace(/deg$/, '')
|
|
1812
|
+
return stripped !== '' && Number.isFinite(Number(stripped))
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
function isColorLike(value: string, tokens: Tokens): boolean {
|
|
1816
|
+
const [base, modifier] = splitModifier(value)
|
|
1817
|
+
if (modifier !== undefined && parseDynamicValue(modifier) === null && !isNumeric(modifier)) return false
|
|
1818
|
+
const dyn = parseDynamicValue(base)
|
|
1819
|
+
if (dyn !== null) {
|
|
1820
|
+
if (dyn.hint === 'length') return false
|
|
1821
|
+
if (dyn.isVar || dyn.hint === 'color') return true
|
|
1822
|
+
return parseColor(dyn.expr) !== null
|
|
1823
|
+
}
|
|
1824
|
+
return own(tokens.colors, base) !== undefined
|
|
1825
|
+
}
|
|
1826
|
+
|
|
1827
|
+
/**
|
|
1828
|
+
* Classifies one utility (without variants or `!`) into its conflict group and
|
|
1829
|
+
* any sub-groups it overrides, or returns `null` for unknown/unrecognized tokens.
|
|
1830
|
+
*/
|
|
1831
|
+
export function classifyUtility(utility: string, tokens: Tokens): UtilityConflict | null {
|
|
1832
|
+
const negative = utility.startsWith('-')
|
|
1833
|
+
const name = negative ? utility.slice(1) : utility
|
|
1834
|
+
|
|
1835
|
+
if (!negative) {
|
|
1836
|
+
const fixed = own(STATIC_CONFLICTS, name)
|
|
1837
|
+
if (fixed !== undefined) return fixed
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
if (own(BARE_DEFAULTS, name) !== undefined) {
|
|
1841
|
+
if (name === 'border') return { group: 'border-width' }
|
|
1842
|
+
if (name === 'rounded') return ROUNDED_CONFLICT
|
|
1843
|
+
if (name.startsWith('rounded-')) return { group: name }
|
|
1844
|
+
if (name === 'shadow') return { group: 'shadow' }
|
|
1845
|
+
if (name === 'grow') return { group: 'flex-grow' }
|
|
1846
|
+
if (name === 'shrink') return { group: 'flex-shrink' }
|
|
1847
|
+
}
|
|
1848
|
+
|
|
1849
|
+
const bracket = name.indexOf('[')
|
|
1850
|
+
const paren = name.indexOf('(')
|
|
1851
|
+
const searchEnd =
|
|
1852
|
+
bracket === -1 ? (paren === -1 ? name.length : paren) : paren === -1 ? bracket : Math.min(bracket, paren)
|
|
1853
|
+
|
|
1854
|
+
let prefix: string | undefined
|
|
1855
|
+
let value = ''
|
|
1856
|
+
for (let i = searchEnd - 1; i > 0; i--) {
|
|
1857
|
+
if (name[i] !== '-') continue
|
|
1858
|
+
const candidate = name.slice(0, i)
|
|
1859
|
+
if (own(functionalUtilities, candidate) !== undefined) {
|
|
1860
|
+
prefix = candidate
|
|
1861
|
+
value = name.slice(i + 1)
|
|
1862
|
+
break
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
if (prefix === undefined) return null
|
|
1866
|
+
|
|
1867
|
+
const spacing = own(SPACING_CONFLICTS, prefix)
|
|
1868
|
+
if (spacing !== undefined) {
|
|
1869
|
+
return isSpacingLike(value, tokens, spacing.options) ? spacing.conflict : null
|
|
1870
|
+
}
|
|
1871
|
+
|
|
1872
|
+
switch (prefix) {
|
|
1873
|
+
case 'aspect':
|
|
1874
|
+
return own(tokens.aspect, value) !== undefined ||
|
|
1875
|
+
parseDynamicValue(value) !== null ||
|
|
1876
|
+
/^\d*\.?\d+\s*\/\s*\d*\.?\d+$/.test(value) ||
|
|
1877
|
+
isNumeric(value)
|
|
1878
|
+
? { group: 'aspect' }
|
|
1879
|
+
: null
|
|
1880
|
+
case 'flex':
|
|
1881
|
+
return isNumberLike(value) ? FLEX_CONFLICT : null
|
|
1882
|
+
case 'grow':
|
|
1883
|
+
return isNumberLike(value) ? { group: 'flex-grow' } : null
|
|
1884
|
+
case 'shrink':
|
|
1885
|
+
return isNumberLike(value) ? { group: 'flex-shrink' } : null
|
|
1886
|
+
case 'border': {
|
|
1887
|
+
if (isNumeric(value)) return { group: 'border-width' }
|
|
1888
|
+
const [base] = splitModifier(value)
|
|
1889
|
+
const dyn = parseDynamicValue(base)
|
|
1890
|
+
if (dyn !== null) {
|
|
1891
|
+
if (dyn.hint === 'color' || (dyn.hint === null && dyn.isVar && !isMathExpression(dyn.expr))) {
|
|
1892
|
+
return { group: 'border-color' }
|
|
1893
|
+
}
|
|
1894
|
+
const len = parseLength(dyn.expr)
|
|
1895
|
+
if (
|
|
1896
|
+
dyn.hint === 'length' ||
|
|
1897
|
+
typeof len === 'number' ||
|
|
1898
|
+
len === MISSING_VIEWPORT ||
|
|
1899
|
+
isMathExpression(dyn.expr)
|
|
1900
|
+
) {
|
|
1901
|
+
return { group: 'border-width' }
|
|
1902
|
+
}
|
|
1903
|
+
return isColorLike(value, tokens) ? { group: 'border-color' } : null
|
|
1904
|
+
}
|
|
1905
|
+
return isColorLike(value, tokens) ? { group: 'border-color' } : null
|
|
1906
|
+
}
|
|
1907
|
+
case 'rounded':
|
|
1908
|
+
return parseDynamicValue(value) !== null || own(tokens.radius, value) !== undefined ? ROUNDED_CONFLICT : null
|
|
1909
|
+
case 'bg': {
|
|
1910
|
+
if (value.startsWith('linear-') || value.startsWith('gradient-')) return BG_GRADIENT_CONFLICT
|
|
1911
|
+
return isColorLike(value, tokens) ? BG_COLOR_CONFLICT : null
|
|
1912
|
+
}
|
|
1913
|
+
case 'from':
|
|
1914
|
+
case 'via':
|
|
1915
|
+
case 'to':
|
|
1916
|
+
case 'tint':
|
|
1917
|
+
return isColorLike(value, tokens) ? { group: prefix } : null
|
|
1918
|
+
case 'opacity':
|
|
1919
|
+
return parseDynamicValue(value) !== null || isNumberLike(value) ? { group: 'opacity' } : null
|
|
1920
|
+
case 'shadow': {
|
|
1921
|
+
if (own(tokens.shadow, value) !== undefined) return { group: 'shadow' }
|
|
1922
|
+
const [base] = splitModifier(value)
|
|
1923
|
+
const dyn = parseDynamicValue(base)
|
|
1924
|
+
if (dyn !== null && dyn.hint !== 'color') {
|
|
1925
|
+
if (dyn.hint === 'length' || (!dyn.isVar && parseShadowList(dyn.expr, false) !== null)) {
|
|
1926
|
+
return { group: 'shadow' }
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
return isColorLike(value, tokens) ? { group: 'shadow-color' } : null
|
|
1930
|
+
}
|
|
1931
|
+
case 'inset-shadow': {
|
|
1932
|
+
if (own(tokens.insetShadow, value) !== undefined) return { group: 'inset-shadow' }
|
|
1933
|
+
const [base] = splitModifier(value)
|
|
1934
|
+
const dyn = parseDynamicValue(base)
|
|
1935
|
+
if (dyn !== null && dyn.hint !== 'color') {
|
|
1936
|
+
if (dyn.hint === 'length' || (!dyn.isVar && parseShadowList(dyn.expr, true) !== null)) {
|
|
1937
|
+
return { group: 'inset-shadow' }
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
return isColorLike(value, tokens) ? { group: 'inset-shadow-color' } : null
|
|
1941
|
+
}
|
|
1942
|
+
case 'drop-shadow':
|
|
1943
|
+
return own(tokens.dropShadow, value) !== undefined || parseDynamicValue(value) !== null
|
|
1944
|
+
? { group: 'drop-shadow' }
|
|
1945
|
+
: null
|
|
1946
|
+
case 'scale':
|
|
1947
|
+
return isNumberLike(value) ? { group: 'scale', overrides: ['scale-x', 'scale-y'] } : null
|
|
1948
|
+
case 'scale-x':
|
|
1949
|
+
case 'scale-y':
|
|
1950
|
+
case 'rotate':
|
|
1951
|
+
return isNumberLike(value) ? { group: prefix } : null
|
|
1952
|
+
case 'text': {
|
|
1953
|
+
const [base] = splitModifier(value)
|
|
1954
|
+
const dyn = parseDynamicValue(base)
|
|
1955
|
+
if (dyn !== null) {
|
|
1956
|
+
if (dyn.hint === 'color' || (dyn.hint === null && dyn.isVar && !isMathExpression(dyn.expr))) {
|
|
1957
|
+
return { group: 'text-color' }
|
|
1958
|
+
}
|
|
1959
|
+
const len = parseLength(dyn.expr)
|
|
1960
|
+
if (
|
|
1961
|
+
dyn.hint === 'length' ||
|
|
1962
|
+
typeof len === 'number' ||
|
|
1963
|
+
len === MISSING_VIEWPORT ||
|
|
1964
|
+
isMathExpression(dyn.expr)
|
|
1965
|
+
) {
|
|
1966
|
+
return FONT_SIZE_CONFLICT
|
|
1967
|
+
}
|
|
1968
|
+
return isColorLike(value, tokens) ? { group: 'text-color' } : null
|
|
1969
|
+
}
|
|
1970
|
+
if (own(tokens.fontSize, base) !== undefined) return FONT_SIZE_CONFLICT
|
|
1971
|
+
return isColorLike(value, tokens) ? { group: 'text-color' } : null
|
|
1972
|
+
}
|
|
1973
|
+
case 'font':
|
|
1974
|
+
return own(tokens.fontWeight, value) !== undefined || parseDynamicValue(value) !== null
|
|
1975
|
+
? { group: 'font-weight' }
|
|
1976
|
+
: null
|
|
1977
|
+
case 'leading':
|
|
1978
|
+
return own(tokens.leading, value) !== undefined ||
|
|
1979
|
+
value === 'none' ||
|
|
1980
|
+
isNumeric(value) ||
|
|
1981
|
+
parseDynamicValue(value) !== null
|
|
1982
|
+
? { group: 'leading' }
|
|
1983
|
+
: null
|
|
1984
|
+
case 'line-clamp':
|
|
1985
|
+
return value === 'none' || isNumberLike(value) ? { group: 'line-clamp' } : null
|
|
1986
|
+
default:
|
|
1987
|
+
if (prefix.startsWith('rounded-')) {
|
|
1988
|
+
return parseDynamicValue(value) !== null || own(tokens.radius, value) !== undefined ? { group: prefix } : null
|
|
1989
|
+
}
|
|
1990
|
+
return null
|
|
1991
|
+
}
|
|
1992
|
+
}
|