Skip to content

Commit 45a3fbf

Browse files
committed
feat: resolveVar
1 parent f732bc2 commit 45a3fbf

4 files changed

Lines changed: 99 additions & 32 deletions

File tree

cpp/StyleFunction.cpp

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,71 @@
33
//
44

55
#include "StyleFunction.hpp"
6+
#include "VariableContext.hpp"
67
#include <NitroModules/AnyMap.hpp>
78

89
namespace margelo::nitro::cssnitro {
910

1011
AnyValue StyleFunction::resolveStyleFn(
11-
const std::string &fnName,
1212
const AnyArray &fnArgs,
1313
typename reactnativecss::Effect::GetProxy &get,
1414
const std::string &variableScope
1515
) {
16-
(void) fnName;
17-
(void) fnArgs;
18-
(void) get;
19-
(void) variableScope;
16+
// Check if fnArgs has at least 3 elements and first is "fn"
17+
if (fnArgs.size() >= 3 &&
18+
std::holds_alternative<std::string>(fnArgs[0]) &&
19+
std::get<std::string>(fnArgs[0]) == "fn") {
20+
21+
// Check if second element is "var"
22+
if (std::holds_alternative<std::string>(fnArgs[1]) &&
23+
std::get<std::string>(fnArgs[1]) == "var") {
24+
25+
// Need at least ["fn", "var", name]
26+
if (fnArgs.size() >= 3 && std::holds_alternative<std::string>(fnArgs[2])) {
27+
const std::string &varName = std::get<std::string>(fnArgs[2]);
28+
29+
// Get fallback value (if exists, it's at index 3)
30+
AnyValue fallback;
31+
if (fnArgs.size() >= 4) {
32+
fallback = fnArgs[3];
33+
}
34+
35+
return resolveVar(varName, fallback, get, variableScope);
36+
}
37+
}
38+
}
2039

2140
return AnyValue();
2241
}
2342

43+
AnyValue StyleFunction::resolveVar(
44+
const std::string &name,
45+
const AnyValue &fallback,
46+
typename reactnativecss::Effect::GetProxy &get,
47+
const std::string &variableScope
48+
) {
49+
auto result = VariableContext::getVariable(variableScope, name, get);
50+
51+
if (result.has_value()) {
52+
return result.value();
53+
}
54+
55+
return resolveAnyValue(fallback, get, variableScope);
56+
}
57+
58+
AnyValue StyleFunction::resolveAnyValue(
59+
const AnyValue &value,
60+
typename reactnativecss::Effect::GetProxy &get,
61+
const std::string &variableScope
62+
) {
63+
// Check if value is an array
64+
if (std::holds_alternative<AnyArray>(value)) {
65+
const auto &arr = std::get<AnyArray>(value);
66+
return resolveStyleFn(arr, get, variableScope);
67+
}
68+
69+
// Return the value as-is if it's not an array
70+
return value;
71+
}
72+
2473
} // namespace margelo::nitro::cssnitro

cpp/StyleFunction.hpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,48 @@ namespace margelo::nitro::cssnitro {
2424
class StyleFunction {
2525
public:
2626
/**
27-
* Resolve a style function by name with the given arguments.
27+
* Resolve a style function with the given arguments.
2828
*
29-
* @param fnName The name of the function to resolve
30-
* @param fnArgs The arguments to pass to the function
29+
* @param fnArgs The arguments array (first element should be the function name)
3130
* @param get The Effect::GetProxy for reactive dependencies
3231
* @param variableScope The variable scope context for resolving variables
3332
* @return The resolved style value
3433
*/
3534
static AnyValue resolveStyleFn(
36-
const std::string &fnName,
3735
const AnyArray &fnArgs,
3836
typename reactnativecss::Effect::GetProxy &get,
3937
const std::string &variableScope
4038
);
39+
40+
/**
41+
* Resolve a CSS variable from the variable context.
42+
*
43+
* @param name The name of the variable to resolve
44+
* @param fallback The fallback value if the variable is not found
45+
* @param get The Effect::GetProxy for reactive dependencies
46+
* @param variableScope The variable scope context for resolving variables
47+
* @return The resolved variable value or fallback
48+
*/
49+
static AnyValue resolveVar(
50+
const std::string &name,
51+
const AnyValue &fallback,
52+
typename reactnativecss::Effect::GetProxy &get,
53+
const std::string &variableScope
54+
);
55+
56+
/**
57+
* Resolve an AnyValue, checking if it contains a "var" function call.
58+
*
59+
* @param value The value to resolve
60+
* @param get The Effect::GetProxy for reactive dependencies
61+
* @param variableScope The variable scope context for resolving variables
62+
* @return The resolved value
63+
*/
64+
static AnyValue resolveAnyValue(
65+
const AnyValue &value,
66+
typename reactnativecss::Effect::GetProxy &get,
67+
const std::string &variableScope
68+
);
4169
};
4270

4371
} // namespace margelo::nitro::cssnitro

cpp/StyledComputedFactory.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,17 @@ namespace margelo::nitro::cssnitro {
7171
if (!arr.empty() &&
7272
std::holds_alternative<std::string>(arr[0]) &&
7373
std::get<std::string>(arr[0]) == "fn") {
74-
// arr[1] is the function name, arr[2] is the arguments array
75-
if (arr.size() == 3 &&
76-
std::holds_alternative<std::string>(
77-
arr[1]) &&
78-
std::holds_alternative<AnyArray>(arr[2])) {
79-
const auto &fnName = std::get<std::string>(
80-
arr[1]);
81-
const auto &fnArgs = std::get<AnyArray>(
82-
arr[2]);
83-
auto result = StyleFunction::resolveStyleFn(
84-
fnName, fnArgs, get, variableScope);
85-
86-
// Skip if resolveStyleFn returns nullptr
87-
if (std::holds_alternative<std::monostate>(
88-
result)) {
89-
continue;
90-
}
91-
92-
mergedStyles[kv.first] = result;
74+
auto result = StyleFunction::resolveStyleFn(
75+
arr, get, variableScope);
76+
77+
// Skip if resolveStyleFn returns nullptr
78+
if (std::holds_alternative<std::monostate>(
79+
result)) {
9380
continue;
9481
}
82+
83+
mergedStyles[kv.first] = result;
84+
continue;
9585
}
9686
}
9787
mergedStyles[kv.first] = kv.second;

example/src/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ StyleRegistry.addStyleSheet({
1414
d: [{ color: "green" }],
1515
m: { orientation: ["=", "landscape"] },
1616
},
17-
{ s: [], d: [{ color: ["fn", "test", []] }] },
17+
{ s: [], d: [{ color: ["fn", "var", "test", "yellow"] }] },
1818
],
1919
],
2020
],
2121
});
2222

23-
StyleRegistry.setRootVariables({
24-
test: [{ v: "blue" }],
25-
});
23+
// StyleRegistry.setRootVariables({
24+
// test: [{ v: "blue" }],
25+
// });
2626

2727
export default function App() {
2828
return (

0 commit comments

Comments
 (0)