Skip to content

Commit b668c32

Browse files
committed
feat: update compiler to new types
1 parent 965f234 commit b668c32

29 files changed

+5342
-315
lines changed

cpp/HybridStyleRegistry.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,11 @@ namespace margelo::nitro::cssnitro {
6868
reactnativecss::Effect::batch([this, &stylesheet]() {
6969
// If the key "s" exists, loop over every entry
7070
if (stylesheet.s.has_value()) {
71-
const auto &styles = stylesheet.s.value();
72-
for (const auto &entry: styles) {
73-
// entry is a tuple<string, HybridStyleRule>
74-
// entry[0] is the className, entry[1] is the styleRule
75-
const std::string &className = std::get<0>(entry);
76-
const std::vector<HybridStyleRule> &styleRule = std::get<1>(entry);
77-
78-
// Call setClassname with a vector containing the single styleRule
79-
setClassname(className, styleRule);
71+
const auto &stylesMap = stylesheet.s.value();
72+
// stylesMap is std::unordered_map<string, vector<HybridStyleRule>>
73+
for (const auto &[className, styleRules]: stylesMap) {
74+
// className is string, styleRules is vector<HybridStyleRule>
75+
setClassname(className, styleRules);
8076
}
8177
}
8278
});
@@ -145,8 +141,8 @@ namespace margelo::nitro::cssnitro {
145141
}
146142

147143
// Check for pseudo-classes
148-
if (sr.p.has_value()) {
149-
const auto &pseudoClass = sr.p.value();
144+
if (sr.pq.has_value()) {
145+
const auto &pseudoClass = sr.pq.value();
150146

151147
// Check if active pseudo-class is set
152148
if (pseudoClass.a.has_value()) {

cpp/Rules.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ namespace margelo::nitro::cssnitro {
3131
}
3232
}
3333

34-
// Check pseudo-classes (rule.p)
35-
if (rule.p.has_value()) {
36-
if (!testPseudoClasses(rule.p.value(), componentId, get)) {
34+
// Check pseudo-classes (rule.pq)
35+
if (rule.pq.has_value()) {
36+
if (!testPseudoClasses(rule.pq.value(), componentId, get)) {
3737
return false;
3838
}
3939
}
4040

41-
// Check media queries (rule.m)
42-
if (rule.m.has_value() && rule.m.value()) {
43-
auto &mediaMap = *rule.m.value();
41+
// Check media queries (rule.mq)
42+
if (rule.mq.has_value() && rule.mq.value()) {
43+
auto &mediaMap = *rule.mq.value();
4444
if (!testMediaMap(mediaMap, get)) {
4545
return false;
4646
}
@@ -265,7 +265,7 @@ namespace margelo::nitro::cssnitro {
265265
return false;
266266
}
267267

268-
bool Rules::testContainerQueries(const std::vector<ContainerQuery> &containerQueries,
268+
bool Rules::testContainerQueries(const std::vector<HybridContainerQuery> &containerQueries,
269269
reactnativecss::Effect::GetProxy &get,
270270
const std::string &containerScope) {
271271
// Loop over all container queries and return false if any fail
@@ -277,7 +277,7 @@ namespace margelo::nitro::cssnitro {
277277
return true;
278278
}
279279

280-
bool Rules::testContainerQuery(const ContainerQuery &containerQuery,
280+
bool Rules::testContainerQuery(const HybridContainerQuery &containerQuery,
281281
reactnativecss::Effect::GetProxy &get,
282282
const std::string &containerScope) {
283283
std::optional<std::string> containerName = std::nullopt;

cpp/Rules.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ namespace margelo::nitro::cssnitro {
4545
testMediaQuery(const std::string &key, const std::string &op, const AnyValue &value,
4646
reactnativecss::Effect::GetProxy &get);
4747

48-
static bool testContainerQueries(const std::vector<ContainerQuery> &containerQueries,
48+
static bool testContainerQueries(const std::vector<HybridContainerQuery> &containerQueries,
4949
reactnativecss::Effect::GetProxy &get,
5050
const std::string &containerScope);
5151

52-
static bool testContainerQuery(const ContainerQuery &containerQuery,
52+
static bool testContainerQuery(const HybridContainerQuery &containerQuery,
5353
reactnativecss::Effect::GetProxy &get,
5454
const std::string &containerScope);
5555

cpp/StyledComputedFactory.cpp

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,27 @@ namespace margelo::nitro::cssnitro {
9292

9393
}
9494

95-
// Process the declarations
95+
// Process the declarations and props
9696
for (const HybridStyleRule &styleRule: allStyleRules) {
97+
// Check if this is an important rule (s[0] > 0)
98+
const bool isImportant = std::get<0>(styleRule.s) > 0;
99+
100+
// Process declarations (styles) from the "d" key
97101
if (styleRule.d.has_value()) {
98102
const auto &declarations = styleRule.d.value();
103+
auto &targetStyles = isImportant ? mergedImportantStyles : mergedStyles;
99104

100-
// Check if this is an important rule (s[0] > 0)
101-
const bool isImportant = std::get<0>(styleRule.s) > 0;
105+
StyledComputedFactory::processDeclarations(
106+
declarations, targetStyles, get, variableScope);
107+
}
102108

103-
// Determine which maps to use based on importance
104-
auto &targetStyles = isImportant ? mergedImportantStyles : mergedStyles;
109+
// Process props from the "p" key
110+
if (styleRule.p.has_value()) {
111+
const auto &props = styleRule.p.value();
105112
auto &targetProps = isImportant ? mergedImportantProps : mergedProps;
106113

107-
// Process declarations using the helper function
108114
StyledComputedFactory::processDeclarations(
109-
declarations, targetStyles, targetProps, get, variableScope);
115+
props, targetProps, get, variableScope);
110116
}
111117
}
112118

@@ -174,65 +180,33 @@ namespace margelo::nitro::cssnitro {
174180
}
175181

176182
void StyledComputedFactory::processDeclarations(
177-
const auto &declarations,
178-
std::unordered_map<std::string, AnyValue> &targetStyles,
179-
std::unordered_map<std::string, AnyValue> &targetProps,
183+
const std::shared_ptr<AnyMap> &declarations,
184+
std::unordered_map<std::string, AnyValue> &targetMap,
180185
reactnativecss::Effect::GetProxy &get,
181186
const std::string &variableScope) {
182187

183-
std::visit([&targetStyles, &targetProps, &get, &variableScope](const auto &decl) {
184-
// decl is a tuple, get the first element (the styles)
185-
const auto &dStyles = std::get<0>(decl);
186-
187-
// Check if dStyles is valid before accessing
188-
if (dStyles) {
189-
for (const auto &kv: dStyles->getMap()) {
190-
// Only set if key doesn't already exist
191-
if (targetStyles.count(kv.first) == 0) {
192-
// Use StyleResolver to resolve the style value (handles functions, variables, etc.)
193-
auto resolvedValue = StyleResolver::resolveStyle(kv.second, variableScope,
194-
get);
195-
196-
// Skip if resolveStyle returns monostate (unresolved)
197-
if (std::holds_alternative<std::monostate>(resolvedValue)) {
198-
continue;
199-
}
188+
// declarations is a shared_ptr<AnyMap> containing the style or prop values
189+
if (!declarations) {
190+
return;
191+
}
200192

201-
targetStyles[kv.first] = resolvedValue;
202-
}
203-
}
204-
}
193+
// Get the map of all key-value pairs from the AnyMap
194+
const auto &map = declarations->getMap();
205195

206-
// Check if there's a second element in the tuple (props)
207-
if constexpr (std::tuple_size<std::decay_t<decltype(decl)>>::value > 1) {
208-
const auto &dPropsOpt = std::get<1>(decl);
209-
210-
// dPropsOpt is optional, check if it has a value
211-
if (dPropsOpt.has_value()) {
212-
const auto &dProps = dPropsOpt.value();
213-
214-
// Check if dProps is valid before accessing
215-
if (dProps) {
216-
for (const auto &kv: dProps->getMap()) {
217-
// Only set if key doesn't already exist
218-
if (targetProps.count(kv.first) == 0) {
219-
// Use StyleResolver to resolve the prop value (handles functions, variables, etc.)
220-
auto resolvedValue = StyleResolver::resolveStyle(kv.second,
221-
variableScope,
222-
get);
223-
224-
// Skip if resolveStyle returns monostate (unresolved)
225-
if (std::holds_alternative<std::monostate>(resolvedValue)) {
226-
continue;
227-
}
196+
for (const auto &kv: map) {
197+
// Only set if key doesn't already exist
198+
if (targetMap.count(kv.first) == 0) {
199+
// Use StyleResolver to resolve the value (handles functions, variables, etc.)
200+
auto resolvedValue = StyleResolver::resolveStyle(kv.second, variableScope, get);
228201

229-
targetProps[kv.first] = resolvedValue;
230-
}
231-
}
232-
}
202+
// Skip if resolveStyle returns monostate (unresolved)
203+
if (std::holds_alternative<std::monostate>(resolvedValue)) {
204+
continue;
233205
}
206+
207+
targetMap[kv.first] = resolvedValue;
234208
}
235-
}, declarations);
209+
}
236210
}
237211

238212

cpp/StyledComputedFactory.hpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ namespace margelo::nitro::cssnitro {
2929
reactnativecss::Effect::GetProxy &get);
3030

3131
/**
32-
* Process declarations from a style rule and merge them into target maps.
33-
* @param declarations The variant containing style/prop declarations
34-
* @param targetStyles The map to store style declarations
35-
* @param targetProps The map to store prop declarations
32+
* Process declarations from a style rule and merge them into a target map.
33+
* @param declarations The AnyMap containing style or prop declarations
34+
* @param targetMap The map to store the processed declarations
3635
* @param get The Effect GetProxy for resolving reactive values
3736
* @param variableScope The scope for variable resolution
3837
*/
3938
static void processDeclarations(
40-
const auto &declarations,
41-
std::unordered_map<std::string, margelo::nitro::AnyValue> &targetStyles,
42-
std::unordered_map<std::string, margelo::nitro::AnyValue> &targetProps,
39+
const std::shared_ptr<margelo::nitro::AnyMap> &declarations,
40+
std::unordered_map<std::string, margelo::nitro::AnyValue> &targetMap,
4341
reactnativecss::Effect::GetProxy &get,
4442
const std::string &variableScope);
4543
};

example/src/App.tsx

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,22 @@ import { multiply, specificity, StyleRegistry } from "react-native-css-nitro";
44
import { Text } from "react-native-css-nitro/components/Text";
55

66
StyleRegistry.addStyleSheet({
7-
s: [
8-
[
9-
"text-red-500",
10-
[
11-
{
12-
s: specificity({ className: 1 }),
13-
v: {
14-
"my-custom-color": "green",
15-
},
16-
d: [
17-
{
18-
color: ["fn", "var", "my-custom-color", "blue"],
19-
animationName: "spin",
20-
animationDuration: "1s",
21-
animationIterationCount: "infinite",
22-
},
23-
],
7+
s: {
8+
"text-red-500": [
9+
{
10+
s: specificity({ className: 1 }),
11+
d: {
12+
color: "blue",
2413
},
25-
],
14+
},
2615
],
27-
],
28-
});
29-
30-
StyleRegistry.setKeyframes("spin", {
31-
"0%": {
32-
rotate: "0deg",
33-
},
34-
"100%": {
35-
rotate: "360deg",
3616
},
3717
});
3818

39-
StyleRegistry.setRootVariables({
40-
"my-custom-color": [{ v: ["fn", "var", "second-color", "green"] }],
41-
"second-color": [{ v: "purple" }],
42-
});
43-
4419
export default function App() {
4520
return (
4621
<View style={styles.container}>
47-
<Text className="text-red-500" style={{ fontSize: 30 }} selectable>
22+
<Text className="text-red-500" style={{ fontSize: 30 }}>
4823
Multiply28: {multiply(3, 7)}
4924
</Text>
5025
</View>

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878
"@react-native/eslint-config": "^0.81.1",
7979
"@react-native/typescript-config": "^0.82.0",
8080
"@release-it/conventional-changelog": "^10.0.1",
81+
"@types/debug": "^4.1.12",
8182
"@types/jest": "^29.5.14",
83+
"@types/node": "^24.8.1",
8284
"@types/react": "^19.1.0",
8385
"commitlint": "^19.8.1",
8486
"del-cli": "^6.0.0",
@@ -171,5 +173,9 @@
171173
"languages": "kotlin-swift",
172174
"type": "nitro-module",
173175
"version": "0.54.3"
176+
},
177+
"dependencies": {
178+
"colorjs.io": "^0.5.2",
179+
"debug": "^4.4.3"
174180
}
175181
}

0 commit comments

Comments
 (0)