Skip to content

Commit f732bc2

Browse files
committed
fix: ensure variableScope is correctly passed
1 parent 2fd85c1 commit f732bc2

9 files changed

Lines changed: 78 additions & 25 deletions

File tree

cpp/HybridStyleRegistry.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ namespace margelo::nitro::cssnitro {
155155
// Build computed Styled via factory
156156
auto computed = ::margelo::nitro::cssnitro::makeStyledComputed(styleRuleMap_, classNames,
157157
componentId,
158-
*shadowUpdates_);
158+
*shadowUpdates_,
159+
variableScope);
159160

160161
computedMap_[componentId] = computed;
161162

cpp/StyleFunction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ namespace margelo::nitro::cssnitro {
1010
AnyValue StyleFunction::resolveStyleFn(
1111
const std::string &fnName,
1212
const AnyArray &fnArgs,
13-
typename reactnativecss::Effect::GetProxy &get
13+
typename reactnativecss::Effect::GetProxy &get,
14+
const std::string &variableScope
1415
) {
1516
(void) fnName;
1617
(void) fnArgs;
1718
(void) get;
19+
(void) variableScope;
1820

19-
20-
return AnyValue("purple");
21+
return AnyValue();
2122
}
2223

2324
} // namespace margelo::nitro::cssnitro

cpp/StyleFunction.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ namespace margelo::nitro::cssnitro {
2929
* @param fnName The name of the function to resolve
3030
* @param fnArgs The arguments to pass to the function
3131
* @param get The Effect::GetProxy for reactive dependencies
32+
* @param variableScope The variable scope context for resolving variables
3233
* @return The resolved style value
3334
*/
3435
static AnyValue resolveStyleFn(
3536
const std::string &fnName,
3637
const AnyArray &fnArgs,
37-
typename reactnativecss::Effect::GetProxy &get
38+
typename reactnativecss::Effect::GetProxy &get,
39+
const std::string &variableScope
3840
);
3941
};
4042

cpp/StyledComputedFactory.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ namespace margelo::nitro::cssnitro {
2020
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> &styleRuleMap,
2121
const std::string &classNames,
2222
const std::string &componentId,
23-
ShadowTreeUpdateManager &shadowUpdates) {
23+
ShadowTreeUpdateManager &shadowUpdates,
24+
const std::string &variableScope) {
2425
auto computed = reactnativecss::Computed<Styled>::create(
25-
[&styleRuleMap, classNames, componentId, &shadowUpdates](const Styled &prev,
26-
typename reactnativecss::Effect::GetProxy &get) {
26+
[&styleRuleMap, classNames, componentId, &shadowUpdates, variableScope](
27+
const Styled &prev,
28+
typename reactnativecss::Effect::GetProxy &get) {
2729
(void) prev;
2830

2931
Styled next{};
@@ -78,8 +80,16 @@ namespace margelo::nitro::cssnitro {
7880
arr[1]);
7981
const auto &fnArgs = std::get<AnyArray>(
8082
arr[2]);
81-
mergedStyles[kv.first] = StyleFunction::resolveStyleFn(
82-
fnName, fnArgs, get);
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;
8393
continue;
8494
}
8595
}

cpp/StyledComputedFactory.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace margelo::nitro::cssnitro {
1919
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> &styleRuleMap,
2020
const std::string &classNames,
2121
const std::string &componentId,
22-
ShadowTreeUpdateManager &shadowUpdates);
22+
ShadowTreeUpdateManager &shadowUpdates,
23+
const std::string &variableScope);
2324

2425
} // namespace margelo::nitro::cssnitro

cpp/VariableContext.cpp

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace margelo::nitro::cssnitro {
66

77
// Initialize the static contexts map
88
std::unordered_map<std::string, VariableContext::Context> VariableContext::contexts;
9+
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>> VariableContext::root_values;
10+
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>> VariableContext::universal_values;
911

1012
void VariableContext::createContext(const std::string &key, const std::string &parent) {
1113
// Check if context already exists
@@ -173,18 +175,46 @@ namespace margelo::nitro::cssnitro {
173175

174176
void VariableContext::setTopLevelVariable(const std::string &key, const std::string &name,
175177
const AnyValue &value) {
176-
// Create a new Computed that returns the provided value
177-
auto computed = reactnativecss::Computed<AnyValue>::create(
178-
[value](const AnyValue &prev, reactnativecss::Effect::GetProxy &get) -> AnyValue {
179-
(void) prev;
180-
(void) get;
181-
return value;
182-
},
183-
AnyValue() // Initial value
184-
);
185-
186-
// Use the Computed overload to set the variable
187-
setVariable(key, name, computed);
178+
// Determine which map to use based on the key
179+
auto &targetMap = (key == "root") ? root_values : universal_values;
180+
181+
// Find or create the observable in the target map
182+
auto observableIt = targetMap.find(name);
183+
if (observableIt != targetMap.end()) {
184+
// Observable already exists, just update its value
185+
observableIt->second->set(value);
186+
} else {
187+
// Create a new Observable with the value
188+
auto observable = reactnativecss::Observable<AnyValue>::create(value);
189+
targetMap[name] = observable;
190+
}
191+
192+
// Check if the name has been set for the key context
193+
auto contextIt = contexts.find(key);
194+
if (contextIt != contexts.end()) {
195+
auto &valueMap = contextIt->second.values;
196+
auto varIt = valueMap.find(name);
197+
198+
if (varIt == valueMap.end()) {
199+
// Variable doesn't exist in context, create a Computed that reads from the target map
200+
auto computed = reactnativecss::Computed<AnyValue>::create(
201+
[&targetMap, name](const AnyValue &prev,
202+
reactnativecss::Effect::GetProxy &get) -> AnyValue {
203+
(void) prev;
204+
205+
// Read from the target map
206+
auto it = targetMap.find(name);
207+
if (it != targetMap.end()) {
208+
return get(*it->second);
209+
}
210+
return AnyValue();
211+
},
212+
AnyValue() // Initial value
213+
);
214+
215+
valueMap[name] = computed;
216+
}
217+
}
188218
}
189219

190220
} // namespace margelo::nitro::cssnitro

cpp/VariableContext.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ namespace margelo::nitro::cssnitro {
3030
// Static map: context key -> Context
3131
static std::unordered_map<std::string, Context> contexts;
3232

33+
// Static maps for root and universal values
34+
static std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>> root_values;
35+
static std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<AnyValue>>> universal_values;
36+
3337
// Create a new context with the given key and parent
3438
static void createContext(const std::string &key, const std::string &parent);
3539

example/src/App.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ StyleRegistry.addStyleSheet({
2020
],
2121
});
2222

23+
StyleRegistry.setRootVariables({
24+
test: [{ v: "blue" }],
25+
});
26+
2327
export default function App() {
2428
return (
2529
<View style={styles.container}>
2630
<Text
2731
className="text-red-500"
2832
onPress={() => {
2933
console.log("Pressed!");
30-
StyleRegistry.set("text-red-500", [
34+
StyleRegistry.setClassname("text-red-500", [
3135
{
3236
s: [],
3337
d: [{ color: "blue", fontSize: 40 }],

src/specs/StyleRegistry/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const StyleRegistry = NitroModules.createHybridObject<
1010
>("HybridStyleRegistry");
1111

1212
interface JSStyleRegistry {
13-
set(className: string, styleRule: StyleRule[]): void;
13+
setClassname(className: string, styleRule: StyleRule[]): void;
1414
}
1515

1616
const { width, height, scale, fontScale } = Dimensions.get("window");

0 commit comments

Comments
 (0)