-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathresolve.ts
More file actions
205 lines (185 loc) · 5.07 KB
/
resolve.ts
File metadata and controls
205 lines (185 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* eslint-disable */
import type {
InlineVariable,
StyleDescriptor,
StyleFunction,
} from "../../../compiler";
import type { RenderGuard } from "../conditions/guards";
import { type Getter, type VariableContextValue } from "../reactivity";
import { animation, timingFunctionResolver } from "./animation";
import { border } from "./border";
import { boxShadow } from "./box-shadow";
import { calc } from "./calc";
import type { calculateProps } from "./calculate-props";
import { transformKeys } from "./defaults";
import {
blur,
brightness,
contrast,
dropShadow,
grayscale,
hueRotate,
invert,
opacity,
saturate,
sepia,
} from "./filters";
import {
fontScale,
hairlineWidth,
pixelRatio,
pixelSizeForLayoutSize,
platformColor,
roundToNearestPixel,
} from "./platform-functions";
import { textShadow } from "./text-shadow";
import { transform } from "./transform";
import { em, rem, vh, vw } from "./units";
import { varResolver } from "./variables";
export type SimpleResolveValue = (
value: StyleDescriptor,
castToArray?: boolean,
) => unknown;
export type StyleFunctionResolver = (
resolveValue: SimpleResolveValue,
value: StyleFunction,
get: Getter,
options: ResolveValueOptions,
) => unknown;
export type StyleResolver = (
resolveValue: SimpleResolveValue,
value: StyleDescriptor,
get: Getter,
options: ResolveValueOptions,
) => unknown;
const functions: Record<string, StyleFunctionResolver> = {
"@animation": animation,
"@border": border,
"@boxShadow": boxShadow,
"@textShadow": textShadow,
"@transform": transform,
"animationName": animation,
"cubic-bezier": timingFunctionResolver,
"steps": timingFunctionResolver,
"hue-rotate": hueRotate,
"drop-shadow": dropShadow,
blur,
brightness,
calc,
contrast,
em,
fontScale,
grayscale,
hairlineWidth,
invert,
opacity,
pixelRatio,
pixelSizeForLayoutSize,
platformColor,
rem,
roundToNearestPixel,
saturate,
sepia,
vh,
vw,
};
export type ResolveValueOptions = {
castToArray?: boolean;
inheritedVariables?: VariableContextValue;
inlineVariables?: InlineVariable | undefined;
renderGuards?: RenderGuard[];
variableHistory?: Set<string>;
/** Pass down to perform recursive calculations and avoid circular dependencies */
calculateProps?: typeof calculateProps;
};
export function resolveValue(
value: StyleDescriptor,
get: Getter,
options: ResolveValueOptions,
): any {
const { castToArray } = options;
switch (typeof value) {
case "bigint":
case "symbol":
case "undefined":
case "function":
// These types are not supported
return;
case "number":
case "boolean":
return value;
case "string":
return value.endsWith("px") // Inline vars() might set a value with a px suffix
? parseInt(value.slice(0, -2), 10)
: value;
case "object": {
if (!Array.isArray(value)) {
return value;
}
if (isDescriptorArray(value)) {
value = value
.map((d) => resolveValue(d, get, options))
.filter((d) => d !== undefined);
if (castToArray && !Array.isArray(value)) {
return [value];
} else {
return value;
}
}
const name = value[1];
const simpleResolve: SimpleResolveValue = (value) => {
return resolveValue(value, get, options);
};
if (name === "var") {
return varResolver(simpleResolve, value, get, options);
} else if (name in functions) {
const fn = functions[name];
if (typeof fn !== "function") {
throw new Error(`Unknown function: ${name}`);
}
value = fn(
simpleResolve,
value as StyleFunction,
get,
options,
) as StyleDescriptor;
} else if (transformKeys.has(name)) {
// translate, rotate, scale, etc.
const args = value[2];
return simpleResolve(args, castToArray);
} else if (transformKeys.has(name.slice(1))) {
// @translate, @rotate, @scale, etc.
return { [name.slice(1)]: simpleResolve(value[2], castToArray) };
} else {
let args = simpleResolve(value[2], castToArray);
if (args === undefined) {
return;
} else if (Array.isArray(args)) {
let joinedArgs = args
.map((arg: unknown) => {
if (Array.isArray(arg)) {
return arg.flat().join(" ");
}
return arg;
})
.join(", ");
if (name === "radial-gradient") {
// Nativewind / Tailwind CSS hack which can force the 'in oklab' color space
joinedArgs = joinedArgs.replace("in oklab, ", "");
}
value = `${name}(${joinedArgs})`;
} else {
value = `${name}(${args})`;
}
}
return castToArray && value && !Array.isArray(value) ? [value] : value;
}
}
}
function isDescriptorArray(
value: StyleDescriptor | StyleDescriptor[],
): value is StyleDescriptor[] {
return Array.isArray(value) && typeof value[0] === "object"
? Array.isArray(value[0])
: true;
}