-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathvariables.ts
More file actions
88 lines (71 loc) · 2.12 KB
/
variables.ts
File metadata and controls
88 lines (71 loc) · 2.12 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
import {
rootVariables,
universalVariables,
} from "react-native-css/style-collection";
import type { StyleDescriptor, StyleFunction } from "../../../compiler";
import { isStyleDescriptorArray } from "../../utils";
import { VAR_SYMBOL, type Getter } from "../reactivity";
import type { ResolveValueOptions, SimpleResolveValue } from "./resolve";
export function varResolver(
resolve: SimpleResolveValue,
fn: StyleFunction,
get: Getter,
options: ResolveValueOptions,
) {
const {
renderGuards,
inheritedVariables: variables = { [VAR_SYMBOL]: true },
inlineVariables,
variableHistory = new Set(),
} = options;
const args = fn[2];
let name: string | undefined;
let fallback: StyleDescriptor | undefined;
if (typeof args === "string") {
name = args;
} else {
const result = resolve(args);
if (isStyleDescriptorArray(result)) {
name = result[0] as string;
fallback = result[1];
}
}
if (typeof name !== "string") {
return;
}
// If this recurses back to the same variable, we need to stop
if (variableHistory.has(name)) {
return;
}
if (name in variables) {
renderGuards?.push(["v", name, variables[name]]);
return resolve(variables[name]);
}
variableHistory.add(name);
let value = resolve(inlineVariables?.[name] as StyleDescriptor);
if (value !== undefined) {
options.inlineVariables ??= { [VAR_SYMBOL]: "inline" };
options.inlineVariables[name] = value;
return value;
}
value = resolve(variables[name]);
if (value !== undefined) {
renderGuards?.push(["v", name, value]);
options.inlineVariables ??= { [VAR_SYMBOL]: "inline" };
options.inlineVariables[name] = value;
return value;
}
value = resolve(get(universalVariables(name)));
if (value !== undefined) {
options.inlineVariables ??= { [VAR_SYMBOL]: "inline" };
options.inlineVariables[name] = value;
return value;
}
value = resolve(get(rootVariables(name)));
if (value !== undefined) {
options.inlineVariables ??= { [VAR_SYMBOL]: "inline" };
options.inlineVariables[name] = value;
return value;
}
return resolve(fallback);
}