Skip to content

Commit 151719b

Browse files
committed
fix: wip recursive style resolvers
1 parent 2104f03 commit 151719b

6 files changed

Lines changed: 96 additions & 19 deletions

File tree

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ These are the features that are "done", in that they pass basic testing. More co
3939
- [ ] Metro
4040
- [ ] Update compiler to new syntax (switch tuples to objects)
4141
- [x] Upgrading elements
42-
- [ ] Upgrading elements warning
4342
- [ ] Animations
44-
- [ ] Transitions
43+
- [x] Transitions
4544
- [ ] Important styles
4645
- [ ] Important props
4746
- [ ] Shorthand runtime styles

cpp/StyledComputedFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ namespace margelo::nitro::cssnitro {
7474

7575
// Now process the sorted style rules
7676
for (const HybridStyleRule &styleRule: allStyleRules) {
77-
// Skip rule if its media conditions don't pass
77+
// Skip rule if its conditions are not valid
7878
if (!Rules::testRule(styleRule, get, componentId, containerScope,
7979
validAttributeQueries)) {
8080
continue;

cpp/StyledResolver.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Created on October 19, 2025.
3+
//
4+
5+
#include "StyledResolver.hpp"
6+
#include "StyleFunction.hpp"
7+
#include <variant>
8+
9+
namespace margelo::nitro::cssnitro {
10+
11+
AnyValue StyledResolver::resolveStyle(
12+
const AnyValue &value,
13+
const std::string &variableScope,
14+
typename reactnativecss::Effect::GetProxy &get
15+
) {
16+
// Check if value is an array
17+
if (std::holds_alternative<AnyArray>(value)) {
18+
const auto &arr = std::get<AnyArray>(value);
19+
20+
// Check if array has at least one element and first element is "fn"
21+
if (!arr.empty() &&
22+
std::holds_alternative<std::string>(arr[0]) &&
23+
std::get<std::string>(arr[0]) == "fn") {
24+
25+
// Resolve the function
26+
return StyleFunction::resolveStyleFn(arr, get, variableScope);
27+
}
28+
}
29+
30+
// Otherwise return the value as-is
31+
return value;
32+
}
33+
34+
} // namespace margelo::nitro::cssnitro
35+

cpp/StyledResolver.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Created on October 19, 2025.
3+
//
4+
5+
#pragma once
6+
7+
#include <string>
8+
#include "Effect.hpp"
9+
#include <NitroModules/AnyMap.hpp>
10+
11+
namespace margelo::nitro {
12+
struct AnyValue;
13+
using AnyArray = std::vector<AnyValue>;
14+
}
15+
16+
namespace margelo::nitro::cssnitro {
17+
18+
using AnyValue = ::margelo::nitro::AnyValue;
19+
using AnyArray = ::margelo::nitro::AnyArray;
20+
21+
class StyledResolver {
22+
public:
23+
/**
24+
* Resolve a style value, checking if it's a function that needs to be resolved.
25+
*
26+
* @param value The value to resolve
27+
* @param variableScope The variable scope context for resolving variables
28+
* @param get The Effect::GetProxy for reactive dependencies
29+
* @return The resolved style value
30+
*/
31+
static AnyValue resolveStyle(
32+
const AnyValue &value,
33+
const std::string &variableScope,
34+
typename reactnativecss::Effect::GetProxy &get
35+
);
36+
};
37+
38+
} // namespace margelo::nitro::cssnitro
39+

cpp/VariableContext.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "VariableContext.hpp"
22
#include "Rules.hpp"
3+
#include "StyledResolver.hpp"
34

45
namespace margelo::nitro::cssnitro {
56

@@ -56,11 +57,8 @@ namespace margelo::nitro::cssnitro {
5657
auto &valueMap = contextIt->second.values;
5758
auto varIt = valueMap.find(name);
5859
if (varIt != valueMap.end()) {
59-
auto result = getValue(varIt->second, get);
60-
// If the value is not nullopt, return it
61-
if (!std::holds_alternative<std::monostate>(result)) {
62-
return result;
63-
}
60+
AnyValue value = getValue(varIt->second, get);
61+
return StyledResolver::resolveStyle(value, contextKey, get);
6462
} else {
6563
// Variable doesn't exist in this context
6664
// Check if this is a root or universal context
@@ -71,10 +69,8 @@ namespace margelo::nitro::cssnitro {
7169
valueMap[name] = computed;
7270

7371
// Get the initial value from the computed
74-
auto result = get(*computed);
75-
if (!std::holds_alternative<std::monostate>(result)) {
76-
return result;
77-
}
72+
AnyValue value = getValue(computed, get);
73+
return StyledResolver::resolveStyle(value, contextKey, get);
7874
} else {
7975
// For other contexts, create a new Observable with nullptr
8076
auto observable = reactnativecss::Observable<AnyValue>::create(AnyValue());

example/src/App.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,29 @@ StyleRegistry.addStyleSheet({
1313
d: [
1414
{
1515
color: "red",
16-
transitionProperty: "color",
17-
transitionDuration: "5s",
16+
// transitionProperty: "color",
17+
// transitionDuration: "5s",
18+
},
19+
{
20+
selectionColor: "green",
1821
},
1922
],
2023
},
21-
{
22-
s: specificity({ className: 4 }),
23-
d: [{ color: "purple" }],
24-
aq: { a: [["true", "disabled"]] },
25-
},
24+
// {
25+
// s: specificity({ className: 4 }),
26+
// d: [{ color: ["fn", "var", "my-custom-color"] }, { color: "blue" }],
27+
// aq: { a: [["true", "disabled"]] },
28+
// },
2629
],
2730
],
2831
],
2932
});
3033

34+
// StyleRegistry.setRootVariables({
35+
// "my-custom-color": ["fn", "var", "second-color"],
36+
// "second-color": "green",
37+
// });
38+
3139
export default function App() {
3240
return (
3341
<View style={styles.container}>

0 commit comments

Comments
 (0)