Skip to content

Commit 057bd9c

Browse files
committed
feat: improve variants
1 parent 091ddf8 commit 057bd9c

File tree

6 files changed

+40
-23
lines changed

6 files changed

+40
-23
lines changed

cxx/core/HostStyle.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jsi::Value HostStyle::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) {
2828
}
2929

3030
if (this->_styleSheet->unistyles.contains(propertyName)) {
31-
return valueFromUnistyle(rt, this->_unistylesRuntime, this->_styleSheet->unistyles[propertyName], this->_styleSheet->tag);
31+
return valueFromUnistyle(rt, this->_unistylesRuntime, this->_styleSheet->unistyles[propertyName]);
3232
}
3333

3434
if (propertyName == helpers::STYLE_VARIANTS) {

cxx/core/UnistyleWrapper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ inline static std::vector<Unistyle::Shared> unistyleFromValue(jsi::Runtime& rt,
116116
return unistyles;
117117
}
118118

119-
inline static jsi::Value valueFromUnistyle(jsi::Runtime& rt, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, Unistyle::Shared unistyle, int tag) {
119+
inline static jsi::Value valueFromUnistyle(jsi::Runtime& rt, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, Unistyle::Shared unistyle) {
120120
auto wrappedUnistyle = std::make_shared<UnistyleWrapper>(unistyle);
121121

122122
if (unistyle->type == UnistyleType::Object) {

cxx/hybridObjects/HybridShadowRegistry.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa
6464
parsedStyleSheet,
6565
unistyleData
6666
);
67+
68+
unistylesData.emplace_back(unistyleData);
69+
70+
continue;
71+
}
72+
73+
// rebuild unistyles if variants have been applied
74+
if (this->_scopedVariants.size() > 0) {
75+
parser.rebuildUnistyleWithVariants(rt, unistyleData);
6776
}
6877

6978
unistylesData.emplace_back(unistyleData);

cxx/parser/Parser.cpp

+27-20
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ void parser::Parser::buildUnistyles(jsi::Runtime& rt, std::shared_ptr<StyleSheet
3636
});
3737
}
3838

39+
void parser::Parser::rebuildUnistyleWithVariants(jsi::Runtime& rt, std::shared_ptr<UnistyleData> unistyleData) {
40+
if (unistyleData->unistyle->parent == nullptr || unistyleData->unistyle->parent->type == StyleSheetType::Static) {
41+
return;
42+
}
43+
44+
unistyleData->parsedStyle = this->parseFirstLevel(rt, unistyleData->unistyle, unistyleData->variants);
45+
}
46+
3947
jsi::Value parser::Parser::getParsedStyleSheetForScopedTheme(jsi::Runtime& rt, core::Unistyle::Shared unistyle, std::string& scopedTheme) {
4048
// for static stylesheets and exotic styles we don't need to do anything
4149
if (unistyle->parent == nullptr || unistyle->parent->type == StyleSheetType::Static) {
4250
return jsi::Value::undefined();
4351
}
44-
52+
4553
auto& state = core::UnistylesRegistry::get().getState(rt);
4654
auto jsTheme = state.getJSThemeByName(scopedTheme);
4755

@@ -51,9 +59,9 @@ jsi::Value parser::Parser::getParsedStyleSheetForScopedTheme(jsi::Runtime& rt, c
5159
.call(rt, std::move(jsTheme))
5260
.asObject(rt);
5361
}
54-
62+
5563
auto miniRuntime = this->_unistylesRuntime->getMiniRuntimeAsValue(rt, std::nullopt);
56-
64+
5765
return unistyle->parent->rawValue
5866
.asFunction(rt)
5967
.call(rt, std::move(jsTheme), std::move(miniRuntime))
@@ -64,29 +72,29 @@ void parser::Parser::rebuildUnistyleWithScopedTheme(jsi::Runtime& rt, jsi::Value
6472
auto parsedStyleSheet = jsScopedTheme.isUndefined()
6573
? this->getParsedStyleSheetForScopedTheme(rt, unistyleData->unistyle, unistyleData->scopedTheme.value())
6674
: jsScopedTheme.asObject(rt);
67-
75+
6876
if (parsedStyleSheet.isUndefined()) {
6977
return;
7078
}
71-
79+
7280
// get target style
7381
auto targetStyle = parsedStyleSheet.asObject(rt).getProperty(rt, unistyleData->unistyle->styleKey.c_str()).asObject(rt);
7482

7583
// for object we just need to parse it
7684
if (unistyleData->unistyle->type == UnistyleType::Object) {
7785
// we need to temporarly swap rawValue to enforce correct parings
7886
auto sharedRawValue = std::move(unistyleData->unistyle->rawValue);
79-
87+
8088
unistyleData->unistyle->rawValue = std::move(targetStyle);
8189
unistyleData->parsedStyle = this->parseFirstLevel(rt, unistyleData->unistyle, unistyleData->variants);
8290
unistyleData->unistyle->rawValue = std::move(sharedRawValue);
83-
91+
8492
return;
8593
}
86-
94+
8795
// for functions we need to call them with memoized arguments
8896
auto unistyleFn = std::dynamic_pointer_cast<UnistyleDynamicFunction>(unistyleData->unistyle);
89-
97+
9098
// convert arguments to jsi::Value
9199
std::vector<jsi::Value> args{};
92100
auto arguments = unistyleData->dynamicFunctionMetadata.value();
@@ -100,7 +108,7 @@ void parser::Parser::rebuildUnistyleWithScopedTheme(jsi::Runtime& rt, jsi::Value
100108
}
101109

102110
const jsi::Value *argStart = args.data();
103-
111+
104112
// we need to temporarly swap unprocessed value to enforce correct parings
105113
auto sharedUnprocessedValue = std::move(unistyleFn->unprocessedValue);
106114

@@ -224,29 +232,29 @@ void parser::Parser::rebuildUnistylesInDependencyMap(jsi::Runtime& rt, Dependenc
224232
if (!parsedStyleSheetsWithDefaultTheme[unistyleStyleSheet].asObject(rt).hasProperty(rt, unistyle->styleKey.c_str())) {
225233
continue;
226234
}
227-
235+
228236
// for scoped themes we need to parse unistyle exclusively
229237
if (unistyleData->scopedTheme.has_value()) {
230238
std::vector<folly::dynamic> arguments = {};
231-
239+
232240
if (unistyleData->dynamicFunctionMetadata.has_value()) {
233241
arguments = unistyleData->dynamicFunctionMetadata.value();
234242
}
235-
243+
236244
auto parsedStyleSheet = jsi::Value::undefined();
237245
auto scopedThemeName = unistyleData->scopedTheme.value();
238-
246+
239247
// check if we have theme in cache
240248
if (parsedStyleSheetsWithScopedTheme.contains(scopedThemeName)) {
241249
if (parsedStyleSheetsWithScopedTheme[scopedThemeName].contains(unistyle->parent)) {
242250
parsedStyleSheet = jsi::Value(rt, parsedStyleSheetsWithScopedTheme[scopedThemeName][unistyle->parent]);
243251
}
244252
}
245-
253+
246254
// if not, let's build it
247255
if (parsedStyleSheet.isUndefined()) {
248256
parsedStyleSheet = this->getParsedStyleSheetForScopedTheme(rt, unistyle, unistyleData->scopedTheme.value());
249-
257+
250258
if (!parsedStyleSheetsWithScopedTheme.contains(scopedThemeName)) {
251259
parsedStyleSheetsWithScopedTheme.emplace(
252260
scopedThemeName,
@@ -259,7 +267,7 @@ void parser::Parser::rebuildUnistylesInDependencyMap(jsi::Runtime& rt, Dependenc
259267
jsi::Value(rt, parsedStyleSheet)
260268
);
261269
}
262-
270+
263271
this->rebuildUnistyleWithScopedTheme(
264272
rt,
265273
parsedStyleSheet,
@@ -485,10 +493,9 @@ jsi::Function parser::Parser::createDynamicFunctionProxy(jsi::Runtime& rt, Unist
485493
? std::nullopt
486494
: std::optional<Variants>(helpers::variantsToPairs(rt, rawVariants.asObject(rt)));
487495

488-
unistyleFn->parsedStyle = this->parseFirstLevel(rt, unistyleFn, variants);
489-
unistyleFn->seal();
496+
jsi::Object style = jsi::Value(rt, this->parseFirstLevel(rt, unistyleFn, variants)).asObject(rt);
490497

491-
jsi::Object style = jsi::Value(rt, unistyleFn->parsedStyle.value()).asObject(rt);
498+
unistyleFn->seal();
492499

493500
// include dependencies for createUnistylesComponent
494501
style.setProperty(rt, "__proto__", generateUnistylesPrototype(rt, unistylesRuntime, unistyle, variants, helpers::functionArgumentsToArray(rt, args, count)));

cxx/parser/Parser.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct Parser {
2323

2424
void buildUnistyles(jsi::Runtime& rt, std::shared_ptr<StyleSheet> styleSheet);
2525
void parseUnistyles(jsi::Runtime& rt, std::shared_ptr<StyleSheet> styleSheet);
26+
void rebuildUnistyleWithVariants(jsi::Runtime& rt, std::shared_ptr<UnistyleData> unistyleData);
2627
void rebuildUnistylesWithVariants(jsi::Runtime& rt, std::shared_ptr<StyleSheet> styleSheet, Variants& variants);
2728
void rebuildUnistylesInDependencyMap(jsi::Runtime& rt, core::DependencyMap& dependencyMap, std::vector<std::shared_ptr<core::StyleSheet>>& styleSheets, std::optional<UnistylesNativeMiniRuntime> maybeMiniRuntime);
2829
void rebuildShadowLeafUpdates(jsi::Runtime& rt, core::DependencyMap& dependencyMap);

plugin/import.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function addUnistylesImport(t, path, state) {
3030
const newImport = t.importDeclaration(
3131
[t.importSpecifier(t.identifier(localName), t.identifier(name))],
3232
t.stringLiteral(state.opts.isLocal
33-
? `react-native-unistyles/../components/native/${name}`
33+
? state.file.opts.filename.split('react-native-unistyles').at(0).concat(`react-native-unistyles/src/components/native/${name}`)
3434
: `react-native-unistyles/src/components/native/${name}`
3535
)
3636
)

0 commit comments

Comments
 (0)