Skip to content

Commit ec114be

Browse files
committed
fix: styling
1 parent 87ed8ff commit ec114be

5 files changed

Lines changed: 43 additions & 51 deletions

File tree

cpp/HybridStyleRegistry.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "HybridStyleRegistry.hpp"
22
#include "Computed.hpp"
33
#include "Observable.hpp"
4-
#include "Helpers.hpp"
54
#include "ShadowTreeUpdateManager.hpp"
65
#include "StyledComputedFactory.hpp"
76
#include "Environment.hpp"
@@ -301,8 +300,6 @@ namespace margelo::nitro::cssnitro {
301300

302301
/** processColor **/
303302
auto maybeProcessColorFn = args[0].asObject(runtime).getProperty(runtime, "processColor");
304-
helpers::assertThat(runtime, maybeProcessColorFn.isObject(),
305-
"react-native-css: Can't load processColor function from JS.");
306303
jsi::Function processColorFn = maybeProcessColorFn.asObject(runtime).asFunction(runtime);
307304

308305
shadowUpdates_->registerProcessColorFunction(std::move(processColorFn));

cpp/ShadowTreeUpdateManager.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ namespace margelo::nitro::cssnitro {
7979
// Overload: convert an AnyMap directly (top-level style entry)
8080
static folly::dynamic convert(ShadowTreeUpdateManager &self,
8181
Runtime &runtime,
82-
const nitro_ns::AnyMap &entry) {
82+
const std::shared_ptr<::margelo::nitro::AnyMap> &entry) {
8383
folly::dynamic obj = folly::dynamic::object();
84-
for (const auto &kv: entry.getMap()) {
84+
for (const auto &kv: entry->getMap()) {
8585
const auto &v = static_cast<const nitro_ns::VariantType &>(kv.second);
8686
auto dynVal = convert(self, runtime, v);
8787
if (containsColorInsensitive(kv.first)) {
@@ -108,31 +108,21 @@ namespace margelo::nitro::cssnitro {
108108

109109
void ShadowTreeUpdateManager::addUpdates(
110110
const std::string &componentId,
111-
const std::vector<std::shared_ptr<nitro_ns::AnyMap>> &styleEntries) {
111+
const std::shared_ptr<::margelo::nitro::AnyMap> &styleMap) {
112112
auto it = component_links_.find(componentId);
113113
if (it == component_links_.end()) return;
114114

115115
ComponentLink &link = it->second;
116116
if (link.runtime == nullptr) return;
117117

118+
// Convert the single AnyMap to folly::dynamic
119+
auto payload = VariantConverter::convert(*this, *link.runtime, styleMap);
120+
118121
auto &obs = runtime_updates_[link.runtime];
119122
if (!obs) {
120123
obs = Observable<UpdatesMap>::create(UpdatesMap{});
121124
}
122125

123-
folly::dynamic payload = folly::dynamic::object();
124-
for (const auto &p: styleEntries) {
125-
if (!p) {
126-
continue;
127-
}
128-
auto obj = VariantConverter::convert(*this, *link.runtime, *p);
129-
if (obj.isObject()) {
130-
for (auto &kv: obj.items()) {
131-
payload[kv.first] = std::move(kv.second);
132-
}
133-
}
134-
}
135-
136126
UpdatesMap cur = obs->get();
137127
cur[link.tag] = std::move(payload);
138128
obs->set(std::move(cur));

cpp/ShadowTreeUpdateManager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace margelo::nitro::cssnitro {
4242
void unlinkComponent(const std::string &componentId);
4343

4444
void addUpdates(const std::string &componentId,
45-
const std::vector<std::shared_ptr<::margelo::nitro::AnyMap>> &styleEntries);
45+
const std::shared_ptr<::margelo::nitro::AnyMap> &styleEntries);
4646

4747
void registerProcessColorFunction(jsi::Function &&fn);
4848

cpp/StyledComputedFactory.cpp

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "Styled+Equality.hpp"
33
#include "ShadowTreeUpdateManager.hpp"
44
#include "Rules.hpp"
5+
#include "Helpers.hpp"
56

67
#include <regex>
78
#include <variant>
@@ -25,7 +26,11 @@ namespace margelo::nitro::cssnitro {
2526
(void) prev;
2627

2728
Styled next{};
28-
std::vector<std::shared_ptr<AnyMap>> styleEntries;
29+
/**
30+
* Ideally this should an AnyMap, but settings values on an AnyMap doesn't work?
31+
* So we use an unordered_map and convert it to AnyMap at the end.
32+
*/
33+
std::unordered_map<std::string, AnyValue> mergedStyles;
2934

3035
std::regex whitespace{"\\s+"};
3136
std::sregex_token_iterator tokenIt(classNames.begin(), classNames.end(),
@@ -52,40 +57,39 @@ namespace margelo::nitro::cssnitro {
5257
}
5358

5459
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());
57-
58-
while (!stack.empty()) {
59-
auto current = std::move(stack.back());
60-
stack.pop_back();
61-
62-
std::visit(
63-
[&stack, &styleEntries](auto &&value) {
64-
using ValueType = std::decay_t<decltype(value)>;
65-
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-
}
75-
}
76-
}
77-
},
78-
current);
60+
// Get the tuple from styleRule.d
61+
const auto &dTuple = styleRule.d.value();
62+
63+
// Merge styleRule.d[0] into mergedStyles
64+
const auto &firstMap = std::get<0>(std::get<0>(dTuple));
65+
for (const auto &kv: firstMap->getMap()) {
66+
mergedStyles[kv.first] = kv.second;
7967
}
68+
// Ignore the other entries for now
69+
8070
}
8171
}
8272
}
8373

84-
if (!styleEntries.empty()) {
85-
next.style = std::move(styleEntries);
74+
// Convert mergedStyles to AnyMap and set next.style
75+
if (!mergedStyles.empty()) {
76+
auto anyMap = AnyMap::make(mergedStyles.size());
77+
for (const auto &kv: mergedStyles) {
78+
if (kv.first == "rotate") {
79+
if (!anyMap->contains("transform")) {
80+
anyMap->setArray("transform", AnyArray{});
81+
}
82+
83+
const auto &transformArray = anyMap->getArray("transform");
84+
// find the value in the array with the key "rotate" and set the key "rotate" to kv.second
85+
}
86+
87+
anyMap->setAny(kv.first, kv.second);
88+
}
89+
next.style = anyMap;
8690
}
8791

88-
// Notify ShadowTreeUpdateManager with the resolved style entries
92+
// Notify ShadowTreeUpdateManager with the resolved style
8993
if (next.style.has_value()) {
9094
shadowUpdates.addUpdates(componentId, next.style.value());
9195
}

src/specs/StyleRegistry/HybridStyleRegistry.nitro.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type RuntimeGuard = (
6969
) => boolean;
7070

7171
export interface Styled {
72-
style?: AnyMap[];
72+
style?: AnyMap;
7373
importantStyle?: AnyMap;
7474
props?: AnyMap;
7575
importantProps?: AnyMap;
@@ -105,14 +105,15 @@ export type StyleConfigNativeStyleToProp = [string, string[]];
105105
interface HybridStyleRule {
106106
s: SpecificityArray;
107107
v?: HybridVariableDescriptor[];
108-
d?: HybridStyleDescriptor[];
108+
109+
/** Declarations */
110+
d?: [AnyMap] | [AnyMap, AnyMap] | [AnyMap, AnyMap | undefined, AnyMap];
109111

110112
/** MediaQuery */
111113
m?: AnyMap;
112114
}
113115

114116
type SpecificityArray = number[];
115-
type HybridStyleDescriptor = AnyMap;
116117

117118
/****************************** Variables *******************************/
118119

0 commit comments

Comments
 (0)