Skip to content

Commit 0a5abc2

Browse files
committed
feat: switch to StyleRuleSets
1 parent 684a2cf commit 0a5abc2

6 files changed

Lines changed: 59 additions & 42 deletions

File tree

cpp/HybridStyleRegistry.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace margelo::nitro::cssnitro {
2828
struct HybridStyleRegistry::Impl {
2929
Impl();
3030

31-
void set(const std::string &className, const StyleRule &styleRule);
31+
void set(const std::string &className, const std::vector<StyleRule> &styleRule);
3232

3333
Declarations
3434
getDeclarations(const std::string &componentId, const std::string &classNames,
@@ -77,7 +77,7 @@ namespace margelo::nitro::cssnitro {
7777
private:
7878
std::unique_ptr<ShadowTreeUpdateManager> shadowUpdates_;
7979
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Computed<Styled>>> computedMap_;
80-
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<StyleRule>>> styleRuleMap_;
80+
std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<StyleRule>>>> styleRuleMap_;
8181
};
8282

8383

@@ -96,7 +96,8 @@ namespace margelo::nitro::cssnitro {
9696

9797
HybridStyleRegistry::~HybridStyleRegistry() = default;
9898

99-
void HybridStyleRegistry::set(const std::string &className, const StyleRule &styleRule) {
99+
void HybridStyleRegistry::set(const std::string &className,
100+
const std::vector<StyleRule> &styleRule) {
100101
impl_->set(className, styleRule);
101102
}
102103

@@ -168,10 +169,10 @@ namespace margelo::nitro::cssnitro {
168169
}
169170

170171
void HybridStyleRegistry::Impl::set(const std::string &className,
171-
const StyleRule &styleRule) {
172+
const std::vector<StyleRule> &styleRule) {
172173
auto it = styleRuleMap_.find(className);
173174
if (it == styleRuleMap_.end()) {
174-
auto observable = reactnativecss::Observable<StyleRule>::create(styleRule);
175+
auto observable = reactnativecss::Observable<std::vector<StyleRule>>::create(styleRule);
175176
styleRuleMap_.emplace(className, std::move(observable));
176177
} else if (it->second) {
177178
it->second->set(styleRule);
@@ -202,8 +203,15 @@ namespace margelo::nitro::cssnitro {
202203
continue;
203204
}
204205

205-
const StyleRule &styleRule = styleIt->second->get();
206-
if (styleRule.v.has_value()) {
206+
const std::vector<StyleRule> &styleRules = styleIt->second->get();
207+
bool hasVars = false;
208+
for (const auto &sr: styleRules) {
209+
if (sr.v.has_value()) {
210+
hasVars = true;
211+
break;
212+
}
213+
}
214+
if (hasVars) {
207215
declarations.variableScope = componentId;
208216
break;
209217
}
@@ -228,7 +236,9 @@ namespace margelo::nitro::cssnitro {
228236
(void) rerender;
229237

230238
// Build computed Styled via factory
231-
auto computed = makeStyledComputed(styleRuleMap_, classNames, componentId, *shadowUpdates_);
239+
auto computed = ::margelo::nitro::cssnitro::makeStyledComputed(styleRuleMap_, classNames,
240+
componentId,
241+
*shadowUpdates_);
232242

233243
computedMap_[componentId] = computed;
234244

cpp/HybridStyleRegistry.hpp

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

3838
// ----- public API forwarded to Impl -----
3939
void set(const std::string &className,
40-
const StyleRule &styleRule) override;
40+
const std::vector<StyleRule> &styleRule) override;
4141

4242
Declarations getDeclarations(const std::string &componentId, const std::string &classNames,
4343
const std::string &variableScope,

cpp/StyledComputedFactory.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace margelo::nitro::cssnitro {
1515
using AnyMap = ::margelo::nitro::AnyMap;
1616

1717
std::shared_ptr<reactnativecss::Computed<Styled>> makeStyledComputed(
18-
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<StyleRule>>> &styleRuleMap,
18+
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<StyleRule>>>> &styleRuleMap,
1919
const std::string &classNames,
2020
const std::string &componentId,
2121
ShadowTreeUpdateManager &shadowUpdates) {
@@ -43,38 +43,40 @@ namespace margelo::nitro::cssnitro {
4343
continue;
4444
}
4545

46-
const StyleRule &styleRule = get(*styleIt->second);
46+
const std::vector<StyleRule> &styleRules = get(*styleIt->second);
4747

48-
// Skip rule if its media conditions don't pass
49-
if (!Rules::testRule(styleRule, get)) {
50-
continue;
51-
}
48+
for (const StyleRule &styleRule: styleRules) {
49+
// Skip rule if its media conditions don't pass
50+
if (!Rules::testRule(styleRule, get)) {
51+
continue;
52+
}
5253

53-
if (styleRule.d.has_value()) {
54-
std::vector<std::variant<std::shared_ptr<AnyMap>, std::vector<std::shared_ptr<AnyMap>>>> stack(
55-
styleRule.d->begin(), styleRule.d->end());
54+
if (styleRule.d.has_value()) {
55+
std::vector<std::variant<std::shared_ptr<AnyMap>, std::vector<std::shared_ptr<AnyMap>>>> stack(
56+
styleRule.d->begin(), styleRule.d->end());
5657

57-
while (!stack.empty()) {
58-
auto current = std::move(stack.back());
59-
stack.pop_back();
58+
while (!stack.empty()) {
59+
auto current = std::move(stack.back());
60+
stack.pop_back();
6061

61-
std::visit(
62-
[&stack, &styleEntries](auto &&value) {
63-
using ValueType = std::decay_t<decltype(value)>;
62+
std::visit(
63+
[&stack, &styleEntries](auto &&value) {
64+
using ValueType = std::decay_t<decltype(value)>;
6465

65-
if constexpr (std::is_same_v<ValueType, std::shared_ptr<AnyMap>>) {
66-
if (value) {
67-
styleEntries.push_back(value);
68-
}
69-
} else if constexpr (std::is_same_v<ValueType, std::vector<std::shared_ptr<AnyMap>>>) {
70-
for (const auto &nested: value) {
71-
if (nested) {
72-
stack.emplace_back(nested);
66+
if constexpr (std::is_same_v<ValueType, std::shared_ptr<AnyMap>>) {
67+
if (value) {
68+
styleEntries.push_back(value);
69+
}
70+
} else if constexpr (std::is_same_v<ValueType, std::vector<std::shared_ptr<AnyMap>>>) {
71+
for (const auto &nested: value) {
72+
if (nested) {
73+
stack.emplace_back(nested);
74+
}
7375
}
7476
}
75-
}
76-
},
77-
current);
77+
},
78+
current);
79+
}
7880
}
7981
}
8082
}

cpp/StyledComputedFactory.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace margelo::nitro::cssnitro {
1616
// Build a Computed<Styled> that resolves styles from classNames against the styleRuleMap
1717
// and notifies ShadowTreeUpdateManager with the value of next.style for the given componentId.
1818
std::shared_ptr<reactnativecss::Computed<Styled>> makeStyledComputed(
19-
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<StyleRule>>> &styleRuleMap,
19+
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<StyleRule>>>> &styleRuleMap,
2020
const std::string &classNames,
2121
const std::string &componentId,
2222
ShadowTreeUpdateManager &shadowUpdates);

example/src/App.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import { StyleSheet, View } from "react-native";
33
import { multiply, StyleRegistry } from "react-native-css-nitro";
44
import { Text } from "react-native-css-nitro/components/Text";
55

6-
StyleRegistry.set("text-red-500", { s: [], d: [{ color: "red" }] });
6+
StyleRegistry.set("text-red-500", [
7+
{ s: [], d: [{ color: "red" }] },
8+
{ s: [], d: [{ color: "green" }] },
9+
]);
710

811
export default function App() {
912
return (
@@ -12,10 +15,12 @@ export default function App() {
1215
className="text-red-500"
1316
onPress={() => {
1417
console.log("Pressed!");
15-
StyleRegistry.set("text-red-500", {
16-
s: [],
17-
d: [{ color: "blue", fontSize: 40 }],
18-
});
18+
StyleRegistry.set("text-red-500", [
19+
{
20+
s: [],
21+
d: [{ color: "blue", fontSize: 40 }],
22+
},
23+
]);
1924
}}
2025
>
2126
Multiply: {multiply(3, 7)}

src/specs/StyleRegistry/HybridStyleRegistry.nitro.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type HybridStyleRegistry = StyleRegistry & RawStyleRegistry;
1010

1111
export interface StyleRegistry
1212
extends HybridObject<{ ios: "c++"; android: "c++" }> {
13-
set(className: string, styleRule: StyleRule): void;
13+
set(className: string, styleRule: StyleRule[]): void;
1414
getDeclarations(
1515
componentId: string,
1616
classNames: string,

0 commit comments

Comments
 (0)