Skip to content

Commit 28952e2

Browse files
committed
fix: nested variables
1 parent 3507fbe commit 28952e2

File tree

3 files changed

+48
-79
lines changed

3 files changed

+48
-79
lines changed

cpp/StyledComputedFactory.cpp

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "Helpers.hpp"
66
#include "StyleFunction.hpp"
77
#include "Specificity.hpp"
8+
#include "StyledResolver.hpp"
89

910
#include <regex>
1011
#include <variant>
@@ -168,25 +169,16 @@ namespace margelo::nitro::cssnitro {
168169
for (const auto &kv: dStyles->getMap()) {
169170
// Only set if key doesn't already exist
170171
if (targetStyles.count(kv.first) == 0) {
171-
// if kv.second is an array with "fn" as the first key, resolve it
172-
if (dStyles->isArray(kv.first)) {
173-
const auto &arr = dStyles->getArray(kv.first);
174-
if (!arr.empty() &&
175-
std::holds_alternative<std::string>(arr[0]) &&
176-
std::get<std::string>(arr[0]) == "fn") {
177-
auto result = StyleFunction::resolveStyleFn(
178-
arr, get, variableScope);
179-
180-
// Skip if resolveStyleFn returns nullptr
181-
if (std::holds_alternative<std::monostate>(result)) {
182-
return;
183-
}
172+
// Use StyledResolver to resolve the style value (handles functions, variables, etc.)
173+
auto resolvedValue = StyledResolver::resolveStyle(kv.second, variableScope,
174+
get);
184175

185-
targetStyles[kv.first] = result;
186-
return;
187-
}
176+
// Skip if resolveStyle returns monostate (unresolved)
177+
if (std::holds_alternative<std::monostate>(resolvedValue)) {
178+
continue;
188179
}
189-
targetStyles[kv.first] = kv.second;
180+
181+
targetStyles[kv.first] = resolvedValue;
190182
}
191183
}
192184
}
@@ -204,25 +196,17 @@ namespace margelo::nitro::cssnitro {
204196
for (const auto &kv: dProps->getMap()) {
205197
// Only set if key doesn't already exist
206198
if (targetProps.count(kv.first) == 0) {
207-
// if kv.second is an array with "fn" as the first key, resolve it
208-
if (dProps->isArray(kv.first)) {
209-
const auto &arr = dProps->getArray(kv.first);
210-
if (!arr.empty() &&
211-
std::holds_alternative<std::string>(arr[0]) &&
212-
std::get<std::string>(arr[0]) == "fn") {
213-
auto result = StyleFunction::resolveStyleFn(
214-
arr, get, variableScope);
215-
216-
// Skip if resolveStyleFn returns nullptr
217-
if (std::holds_alternative<std::monostate>(result)) {
218-
continue;
219-
}
220-
221-
targetProps[kv.first] = result;
222-
continue;
223-
}
199+
// Use StyledResolver to resolve the prop value (handles functions, variables, etc.)
200+
auto resolvedValue = StyledResolver::resolveStyle(kv.second,
201+
variableScope,
202+
get);
203+
204+
// Skip if resolveStyle returns monostate (unresolved)
205+
if (std::holds_alternative<std::monostate>(resolvedValue)) {
206+
continue;
224207
}
225-
targetProps[kv.first] = kv.second;
208+
209+
targetProps[kv.first] = resolvedValue;
226210
}
227211
}
228212
}

cpp/VariableContext.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,37 +89,40 @@ namespace margelo::nitro::cssnitro {
8989
reactnativecss::Effect::GetProxy &get) {
9090
// 1. Check current key
9191
auto result = checkContext(key, name, get);
92-
if (result.has_value()) {
92+
if (result.has_value() && !std::holds_alternative<std::monostate>(result.value())) {
9393
return result;
9494
}
9595

9696
// 2. Check "universal" context (if we're not already in it)
9797
if (key != "universal") {
9898
result = checkContext("universal", name, get);
99-
if (result.has_value()) {
99+
if (result.has_value() && !std::holds_alternative<std::monostate>(result.value())) {
100100
return result;
101101
}
102102

103103
// 3. Walk up the parent chain from the original key
104-
std::string currentKey = key;
105-
auto contextIt = contexts.find(currentKey);
106-
if (contextIt != contexts.end()) {
107-
std::string parentKey = contextIt->second.parent;
108-
109-
// Walk up parent chain until we hit root (parent points to itself)
110-
while (parentKey != currentKey && !parentKey.empty()) {
111-
result = checkContext(parentKey, name, get);
112-
if (result.has_value()) {
113-
return result;
114-
}
104+
if (key != "root") {
105+
std::string currentKey = key;
106+
auto contextIt = contexts.find(currentKey);
107+
if (contextIt != contexts.end()) {
108+
std::string parentKey = contextIt->second.parent;
109+
110+
// Walk up parent chain until we hit root (parent points to itself)
111+
while (parentKey != currentKey && !parentKey.empty()) {
112+
result = checkContext(parentKey, name, get);
113+
if (result.has_value() &&
114+
!std::holds_alternative<std::monostate>(result.value())) {
115+
return result;
116+
}
115117

116-
// Move to next parent
117-
auto parentIt = contexts.find(parentKey);
118-
if (parentIt != contexts.end()) {
119-
currentKey = parentKey;
120-
parentKey = parentIt->second.parent;
121-
} else {
122-
break;
118+
// Move to next parent
119+
auto parentIt = contexts.find(parentKey);
120+
if (parentIt != contexts.end()) {
121+
currentKey = parentKey;
122+
parentKey = parentIt->second.parent;
123+
} else {
124+
break;
125+
}
123126
}
124127
}
125128
}

example/src/App.tsx

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,22 @@ StyleRegistry.addStyleSheet({
99
"text-red-500",
1010
[
1111
{
12-
s: specificity({ className: 1, important: 1 }),
12+
s: specificity({ className: 1 }),
1313
d: [
1414
{
15-
color: "green",
15+
color: ["fn", "var", "my-custom-color", "blue"],
1616
},
1717
],
1818
},
19-
{
20-
s: specificity({ className: 2 }),
21-
d: [
22-
{
23-
color: "red",
24-
// transitionProperty: "color",
25-
// transitionDuration: "5s",
26-
},
27-
{
28-
selectionColor: "orange",
29-
},
30-
],
31-
},
32-
// {
33-
// s: specificity({ className: 4 }),
34-
// d: [{ color: ["fn", "var", "my-custom-color"] }, { color: "blue" }],
35-
// aq: { a: [["true", "disabled"]] },
36-
// },
3719
],
3820
],
3921
],
4022
});
4123

42-
// StyleRegistry.setRootVariables({
43-
// "my-custom-color": ["fn", "var", "second-color"],
44-
// "second-color": "green",
45-
// });
24+
StyleRegistry.setRootVariables({
25+
"my-custom-color": [{ v: ["fn", "var", "second-color", "green"] }],
26+
"second-color": [{ v: "purple" }],
27+
});
4628

4729
export default function App() {
4830
return (

0 commit comments

Comments
 (0)