react-native-unistyles 3.0.0-alpha.14 → 3.0.0-alpha.15
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/core/UnistylesCommitHook.cpp +9 -9
- package/cxx/core/UnistylesMountHook.cpp +3 -19
- package/cxx/hybridObjects/HybridStyleSheet.cpp +3 -0
- package/cxx/parser/Parser.cpp +13 -9
- package/cxx/shadowTree/ShadowTreeManager.cpp +5 -5
- package/lib/commonjs/web/variants/useVariants.js +17 -0
- package/lib/commonjs/web/variants/useVariants.js.map +1 -1
- package/lib/module/web/variants/useVariants.js +18 -1
- package/lib/module/web/variants/useVariants.js.map +1 -1
- package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -1
- package/package.json +1 -1
- package/plugin/index.js +31 -0
- package/src/web/variants/useVariants.ts +23 -1
@@ -17,30 +17,30 @@ RootShadowNode::Unshared core::UnistylesCommitHook::shadowTreeWillCommit(
|
|
17
17
|
) noexcept {
|
18
18
|
RootShadowNode::Unshared rootNode = newRootShadowNode;
|
19
19
|
auto unistylesRootNode = std::reinterpret_pointer_cast<core::UnistylesCommitShadowNode>(newRootShadowNode);
|
20
|
-
|
20
|
+
|
21
21
|
// skip only unistyles commits
|
22
22
|
if (unistylesRootNode->hasUnistylesCommitTrait()) {
|
23
23
|
unistylesRootNode->removeUnistylesCommitTrait();
|
24
24
|
unistylesRootNode->addUnistylesMountTrait();
|
25
|
-
|
25
|
+
|
26
26
|
return newRootShadowNode;
|
27
27
|
}
|
28
28
|
|
29
29
|
auto& registry = core::UnistylesRegistry::get();
|
30
|
-
|
30
|
+
|
31
31
|
if (!registry.trafficController.hasUnistylesCommit()) {
|
32
32
|
return newRootShadowNode;
|
33
33
|
}
|
34
|
-
|
34
|
+
|
35
35
|
auto shadowLeafUpdates = this->getUnistylesUpdates();
|
36
|
-
|
36
|
+
|
37
37
|
if (shadowLeafUpdates.size() == 0) {
|
38
38
|
return newRootShadowNode;
|
39
39
|
}
|
40
40
|
|
41
41
|
// this is required, otherwise we end up with old shadow tree in mount hook
|
42
42
|
registry.trafficController.stopUnistylesTraffic();
|
43
|
-
|
43
|
+
|
44
44
|
auto affectedNodes = shadow::ShadowTreeManager::findAffectedNodes(*rootNode, shadowLeafUpdates);
|
45
45
|
|
46
46
|
return std::static_pointer_cast<RootShadowNode>(shadow::ShadowTreeManager::cloneShadowTree(
|
@@ -55,8 +55,8 @@ shadow::ShadowLeafUpdates core::UnistylesCommitHook::getUnistylesUpdates() {
|
|
55
55
|
auto& registry = core::UnistylesRegistry::get();
|
56
56
|
auto parser = parser::Parser(this->_unistylesRuntime);
|
57
57
|
auto dependencyMap = registry.buildDependencyMap(*this->_rt);
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
|
59
|
+
// don't rebuild dependency map, at this point it's already done
|
60
|
+
|
61
61
|
return parser.dependencyMapToShadowLeafUpdates(dependencyMap);
|
62
62
|
}
|
@@ -19,27 +19,11 @@ void core::UnistylesMountHook::shadowTreeDidMount(RootShadowNode::Shared const &
|
|
19
19
|
return;
|
20
20
|
}
|
21
21
|
|
22
|
-
//
|
22
|
+
// React Native commit did mount
|
23
23
|
auto& registry = core::UnistylesRegistry::get();
|
24
24
|
|
25
|
+
// so, resume Unistyles commits
|
25
26
|
registry.trafficController.resumeUnistylesTraffic();
|
26
|
-
|
27
|
-
// this will prevent crash when re-rendering view
|
28
|
-
// as Unistyles has nothing to commit yet, but dependency map
|
29
|
-
// will build all the shadow nodes
|
30
|
-
if (!registry.trafficController.hasUnistylesCommit()) {
|
31
|
-
return;
|
32
|
-
}
|
33
|
-
|
34
|
-
registry.trafficController.setHasUnistylesCommit(false);
|
35
|
-
|
36
|
-
auto shadowLeafUpdates = this->getUnistylesUpdates();
|
37
|
-
|
38
|
-
if (shadowLeafUpdates.size() == 0) {
|
39
|
-
return;
|
40
|
-
}
|
41
|
-
|
42
|
-
shadow::ShadowTreeManager::updateShadowTree(*this->_rt, shadowLeafUpdates);
|
43
27
|
}
|
44
28
|
|
45
29
|
shadow::ShadowLeafUpdates core::UnistylesMountHook::getUnistylesUpdates() {
|
@@ -47,7 +31,7 @@ shadow::ShadowLeafUpdates core::UnistylesMountHook::getUnistylesUpdates() {
|
|
47
31
|
auto parser = parser::Parser(this->_unistylesRuntime);
|
48
32
|
auto dependencyMap = registry.buildDependencyMap(*this->_rt);
|
49
33
|
|
50
|
-
|
34
|
+
// don't rebuild dependency map, at this point it's already done
|
51
35
|
|
52
36
|
return parser.dependencyMapToShadowLeafUpdates(dependencyMap);
|
53
37
|
}
|
@@ -236,6 +236,9 @@ void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependen
|
|
236
236
|
|
237
237
|
parser.rebuildUnistylesInDependencyMap(rt, dependencyMap);
|
238
238
|
|
239
|
+
// this is required, otherwise shadow tree will ignore Unistyles commit
|
240
|
+
registry.trafficController.setHasUnistylesCommit(true);
|
241
|
+
|
239
242
|
auto shadowLeafUpdates = parser.dependencyMapToShadowLeafUpdates(dependencyMap);
|
240
243
|
|
241
244
|
shadow::ShadowTreeManager::updateShadowTree(rt, shadowLeafUpdates);
|
package/cxx/parser/Parser.cpp
CHANGED
@@ -100,20 +100,24 @@ void parser::Parser::rebuildUnistylesWithVariants(jsi::Runtime& rt, std::shared_
|
|
100
100
|
|
101
101
|
// rebuild all unistyles that are affected by platform event
|
102
102
|
void parser::Parser::rebuildUnistylesInDependencyMap(jsi::Runtime& rt, DependencyMap& dependencyMap) {
|
103
|
+
std::unordered_map<std::shared_ptr<StyleSheet>, jsi::Value> parsedStyleSheets{};
|
104
|
+
|
103
105
|
for (auto& [shadowNode, unistyles] : dependencyMap) {
|
104
106
|
auto styleSheet = unistyles.begin()->get()->unistyle->parent;
|
105
|
-
|
106
|
-
|
107
|
+
|
108
|
+
if (!parsedStyleSheets.contains(styleSheet)) {
|
109
|
+
parsedStyleSheets.emplace(styleSheet, this->unwrapStyleSheet(rt, styleSheet));
|
110
|
+
}
|
107
111
|
|
108
112
|
for (auto& unistyleData : unistyles) {
|
109
113
|
auto& unistyle = unistyleData->unistyle;
|
110
114
|
|
111
115
|
// StyleSheet might have styles that are not affected
|
112
|
-
if (!
|
116
|
+
if (!parsedStyleSheets[styleSheet].asObject(rt).hasProperty(rt, unistyle->styleKey.c_str())) {
|
113
117
|
continue;
|
114
118
|
}
|
115
119
|
|
116
|
-
unistyle->rawValue =
|
120
|
+
unistyle->rawValue = parsedStyleSheets[styleSheet].asObject(rt).getProperty(rt, unistyle->styleKey.c_str()).asObject(rt);
|
117
121
|
this->rebuildUnistyle(rt, styleSheet, unistyle, unistyleData->variants, unistyleData->dynamicFunctionMetadata);
|
118
122
|
unistyleData->parsedStyle = jsi::Value(rt, unistyle->parsedStyle.value()).asObject(rt);
|
119
123
|
}
|
@@ -162,10 +166,10 @@ void parser::Parser::rebuildUnistyle(jsi::Runtime& rt, std::shared_ptr<StyleShee
|
|
162
166
|
shadow::ShadowLeafUpdates parser::Parser::dependencyMapToShadowLeafUpdates(core::DependencyMap& dependencyMap) {
|
163
167
|
shadow::ShadowLeafUpdates updates;
|
164
168
|
auto& rt = this->_unistylesRuntime->getRuntime();
|
165
|
-
|
169
|
+
|
166
170
|
for (const auto& [shadowNode, unistyles] : dependencyMap) {
|
167
171
|
auto rawProps = this->parseStylesToShadowTreeStyles(rt, unistyles);
|
168
|
-
|
172
|
+
|
169
173
|
updates.emplace(shadowNode, std::move(rawProps));
|
170
174
|
}
|
171
175
|
|
@@ -285,7 +289,7 @@ jsi::Function parser::Parser::createDynamicFunctionProxy(jsi::Runtime& rt, Unist
|
|
285
289
|
[this, unistylesRuntime, unistyle](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
|
286
290
|
auto thisObject = thisVal.asObject(rt);
|
287
291
|
auto parser = parser::Parser(unistylesRuntime);
|
288
|
-
|
292
|
+
|
289
293
|
// call user function
|
290
294
|
auto result = unistyle->rawValue.asFunction(rt).call(rt, args, count);
|
291
295
|
|
@@ -300,7 +304,7 @@ jsi::Function parser::Parser::createDynamicFunctionProxy(jsi::Runtime& rt, Unist
|
|
300
304
|
std::optional<Variants> variants = rawVariants.isUndefined()
|
301
305
|
? std::nullopt
|
302
306
|
: std::optional<Variants>(helpers::variantsToPairs(rt, rawVariants.asObject(rt)));
|
303
|
-
|
307
|
+
|
304
308
|
unistyleFn->parsedStyle = this->parseFirstLevel(rt, unistyleFn, variants);
|
305
309
|
unistyleFn->seal();
|
306
310
|
|
@@ -615,7 +619,7 @@ jsi::Value parser::Parser::parseSecondLevel(jsi::Runtime &rt, Unistyle::Shared u
|
|
615
619
|
RawProps parser::Parser::parseStylesToShadowTreeStyles(jsi::Runtime& rt, const std::vector<std::shared_ptr<UnistyleData>>& unistyles) {
|
616
620
|
jsi::Object convertedStyles = jsi::Object(rt);
|
617
621
|
auto& state = core::UnistylesRegistry::get().getState(rt);
|
618
|
-
|
622
|
+
|
619
623
|
for (const auto& unistyleData : unistyles) {
|
620
624
|
helpers::enumerateJSIObject(rt, unistyleData->parsedStyle.value(), [&](const std::string& propertyName, jsi::Value& propertyValue){
|
621
625
|
if (this->isColor(propertyName)) {
|
@@ -10,10 +10,10 @@ void shadow::ShadowTreeManager::updateShadowTree(facebook::jsi::Runtime& rt, sha
|
|
10
10
|
auto& uiManager = UIManagerBinding::getBinding(rt)->getUIManager();
|
11
11
|
const auto &shadowTreeRegistry = uiManager.getShadowTreeRegistry();
|
12
12
|
auto& registry = core::UnistylesRegistry::get();
|
13
|
-
|
13
|
+
|
14
14
|
if (registry.trafficController.shouldStop()) {
|
15
15
|
registry.trafficController.setHasUnistylesCommit(true);
|
16
|
-
|
16
|
+
|
17
17
|
return;
|
18
18
|
}
|
19
19
|
|
@@ -29,10 +29,10 @@ void shadow::ShadowTreeManager::updateShadowTree(facebook::jsi::Runtime& rt, sha
|
|
29
29
|
updates,
|
30
30
|
affectedNodes
|
31
31
|
));
|
32
|
-
|
32
|
+
|
33
33
|
// set unistyles commit trait
|
34
34
|
auto unistylesRootNode = std::reinterpret_pointer_cast<core::UnistylesCommitShadowNode>(newRootNode);
|
35
|
-
|
35
|
+
|
36
36
|
unistylesRootNode->addUnistylesCommitTrait();
|
37
37
|
|
38
38
|
return newRootNode;
|
@@ -43,7 +43,7 @@ void shadow::ShadowTreeManager::updateShadowTree(facebook::jsi::Runtime& rt, sha
|
|
43
43
|
// enableStateReconciliation: https://reactnative.dev/architecture/render-pipeline#react-native-renderer-state-updates
|
44
44
|
// mountSynchronously: must be true as this is update from C++ not React
|
45
45
|
shadowTree.commit(transaction, {false, true});
|
46
|
-
|
46
|
+
|
47
47
|
|
48
48
|
// for now we're assuming single surface, can be improved in the future
|
49
49
|
// stop = true means stop enumerating next shadow tree
|
@@ -26,12 +26,29 @@ const createUseVariants = (styles, setSelectedVariants) => {
|
|
26
26
|
}, [lastSelectedVariantsRef.current]);
|
27
27
|
combinedVariantStyles.forEach(([key]) => {
|
28
28
|
const styleEntry = styles[key];
|
29
|
+
if (!styleEntry) {
|
30
|
+
return;
|
31
|
+
}
|
29
32
|
const selectedVariantStyles = selectedVariantStylesMap.get(key);
|
30
33
|
Object.defineProperties(styleEntry, (0, _utils.reduceObject)(selectedVariantStyles ?? {}, value => ({
|
31
34
|
value,
|
32
35
|
enumerable: false,
|
33
36
|
configurable: true
|
34
37
|
})));
|
38
|
+
|
39
|
+
// Add __uni__variants to static styles
|
40
|
+
Object.keys(styleEntry).forEach(key => {
|
41
|
+
if (!key.startsWith('__uni__secrets__')) {
|
42
|
+
return;
|
43
|
+
}
|
44
|
+
const secret = (0, _utils.keyInObject)(styleEntry, key) ? styleEntry[key] : undefined;
|
45
|
+
if (!secret) {
|
46
|
+
return;
|
47
|
+
}
|
48
|
+
Object.defineProperty(secret, '__uni__variants', {
|
49
|
+
value: selectedVariants
|
50
|
+
});
|
51
|
+
});
|
35
52
|
});
|
36
53
|
};
|
37
54
|
Object.defineProperty(styles, 'useVariants', {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_react","require","_utils","_getVariants","createUseVariants","styles","setSelectedVariants","useVariants","selectedVariants","selectedVariantStylesMap","useState","Map","lastSelectedVariantsRef","useRef","variantsChanged","equal","current","combinedVariantStyles","useMemo","result","getVariants","forEach","key","value","set","styleEntry","selectedVariantStyles","get","Object","defineProperties","reduceObject","enumerable","configurable","defineProperty","exports"],"sourceRoot":"../../../../src","sources":["web/variants/useVariants.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAEO,MAAMG,iBAAiB,GAAGA,CAACC,MAAyC,EAAEC,mBAA4D,KAAK;EAC1I,MAAMC,WAAW,GAAIC,gBAAqC,IAAK;IAC3D,MAAM,CAACC,wBAAwB,CAAC,GAAG,IAAAC,eAAQ,EAAC,MAAM,IAAIC,GAAG,CAA8B,CAAC,CAAC;IACzF,MAAMC,uBAAuB,GAAG,IAAAC,aAAM,EAAsB,CAAC;IAC7D;IACA,MAAMC,eAAe,GAAG,CAAC,IAAAC,YAAK,EAACH,uBAAuB,CAACI,OAAO,EAAER,gBAAgB,CAAC;IAEjF,IAAIM,eAAe,EAAE;MACjBF,uBAAuB,CAACI,OAAO,GAAGR,gBAAgB;MAClDF,mBAAmB,CAACE,gBAAgB,CAAC;IACzC;IAEA,MAAMS,qBAAqB,GAAG,IAAAC,cAAO,EAAC,MAAM;MACxC,MAAMC,MAAM,GAAG,IAAAC,wBAAW,EAACf,MAAM,EAAEG,gBAAgB,CAAC;MAEpDW,MAAM,CAACE,OAAO,CAAC,CAAC,CAACC,GAAG,EAAEC,KAAK,CAAC,KAAK;QAC7Bd,wBAAwB,CAACe,GAAG,CAACF,GAAG,EAAEC,KAAK,CAAC;MAC5C,CAAC,CAAC;MAEF,OAAOJ,MAAM;IACjB,CAAC,EAAE,CAACP,uBAAuB,CAACI,OAAO,CAAC,CAAC;IAErCC,qBAAqB,CAACI,OAAO,CAAC,CAAC,CAACC,GAAG,CAAC,KAAK;MACrC,MAAMG,UAAU,GAAGpB,MAAM,CAACiB,GAAG,CAAC;
|
1
|
+
{"version":3,"names":["_react","require","_utils","_getVariants","createUseVariants","styles","setSelectedVariants","useVariants","selectedVariants","selectedVariantStylesMap","useState","Map","lastSelectedVariantsRef","useRef","variantsChanged","equal","current","combinedVariantStyles","useMemo","result","getVariants","forEach","key","value","set","styleEntry","selectedVariantStyles","get","Object","defineProperties","reduceObject","enumerable","configurable","keys","startsWith","secret","keyInObject","undefined","defineProperty","exports"],"sourceRoot":"../../../../src","sources":["web/variants/useVariants.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAEO,MAAMG,iBAAiB,GAAGA,CAACC,MAAyC,EAAEC,mBAA4D,KAAK;EAC1I,MAAMC,WAAW,GAAIC,gBAAqC,IAAK;IAC3D,MAAM,CAACC,wBAAwB,CAAC,GAAG,IAAAC,eAAQ,EAAC,MAAM,IAAIC,GAAG,CAA8B,CAAC,CAAC;IACzF,MAAMC,uBAAuB,GAAG,IAAAC,aAAM,EAAsB,CAAC;IAC7D;IACA,MAAMC,eAAe,GAAG,CAAC,IAAAC,YAAK,EAACH,uBAAuB,CAACI,OAAO,EAAER,gBAAgB,CAAC;IAEjF,IAAIM,eAAe,EAAE;MACjBF,uBAAuB,CAACI,OAAO,GAAGR,gBAAgB;MAClDF,mBAAmB,CAACE,gBAAgB,CAAC;IACzC;IAEA,MAAMS,qBAAqB,GAAG,IAAAC,cAAO,EAAC,MAAM;MACxC,MAAMC,MAAM,GAAG,IAAAC,wBAAW,EAACf,MAAM,EAAEG,gBAAgB,CAAC;MAEpDW,MAAM,CAACE,OAAO,CAAC,CAAC,CAACC,GAAG,EAAEC,KAAK,CAAC,KAAK;QAC7Bd,wBAAwB,CAACe,GAAG,CAACF,GAAG,EAAEC,KAAK,CAAC;MAC5C,CAAC,CAAC;MAEF,OAAOJ,MAAM;IACjB,CAAC,EAAE,CAACP,uBAAuB,CAACI,OAAO,CAAC,CAAC;IAErCC,qBAAqB,CAACI,OAAO,CAAC,CAAC,CAACC,GAAG,CAAC,KAAK;MACrC,MAAMG,UAAU,GAAGpB,MAAM,CAACiB,GAAG,CAAC;MAE9B,IAAI,CAACG,UAAU,EAAE;QACb;MACJ;MAEA,MAAMC,qBAAqB,GAAGjB,wBAAwB,CAACkB,GAAG,CAACL,GAAG,CAAC;MAE/DM,MAAM,CAACC,gBAAgB,CAACJ,UAAU,EAAE,IAAAK,mBAAY,EAACJ,qBAAqB,IAAI,CAAC,CAAC,EAAEH,KAAK,KAAK;QACpFA,KAAK;QACLQ,UAAU,EAAE,KAAK;QACjBC,YAAY,EAAE;MAClB,CAAC,CAAC,CAAC,CAAC;;MAEJ;MACAJ,MAAM,CAACK,IAAI,CAACR,UAAU,CAAC,CAACJ,OAAO,CAACC,GAAG,IAAI;QACnC,IAAI,CAACA,GAAG,CAACY,UAAU,CAAC,kBAAkB,CAAC,EAAE;UACrC;QACJ;QAEA,MAAMC,MAAM,GAAG,IAAAC,kBAAW,EAACX,UAAU,EAAEH,GAAG,CAAC,GAAGG,UAAU,CAACH,GAAG,CAAC,GAAGe,SAAS;QAEzE,IAAI,CAACF,MAAM,EAAE;UACT;QACJ;QAEAP,MAAM,CAACU,cAAc,CAACH,MAAM,EAAE,iBAAiB,EAAE;UAC7CZ,KAAK,EAAEf;QACX,CAAC,CAAC;MACN,CAAC,CAAC;IACN,CAAC,CAAC;EACN,CAAC;EAEDoB,MAAM,CAACU,cAAc,CAACjC,MAAM,EAAE,aAAa,EAAE;IACzCkB,KAAK,EAAEhB;EACX,CAAC,CAAC;AACN,CAAC;AAAAgC,OAAA,CAAAnC,iBAAA,GAAAA,iBAAA","ignoreList":[]}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
|
3
3
|
import { useMemo, useRef, useState } from 'react';
|
4
|
-
import { equal, reduceObject } from '../utils';
|
4
|
+
import { equal, keyInObject, reduceObject } from '../utils';
|
5
5
|
import { getVariants } from './getVariants';
|
6
6
|
export const createUseVariants = (styles, setSelectedVariants) => {
|
7
7
|
const useVariants = selectedVariants => {
|
@@ -22,12 +22,29 @@ export const createUseVariants = (styles, setSelectedVariants) => {
|
|
22
22
|
}, [lastSelectedVariantsRef.current]);
|
23
23
|
combinedVariantStyles.forEach(([key]) => {
|
24
24
|
const styleEntry = styles[key];
|
25
|
+
if (!styleEntry) {
|
26
|
+
return;
|
27
|
+
}
|
25
28
|
const selectedVariantStyles = selectedVariantStylesMap.get(key);
|
26
29
|
Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
|
27
30
|
value,
|
28
31
|
enumerable: false,
|
29
32
|
configurable: true
|
30
33
|
})));
|
34
|
+
|
35
|
+
// Add __uni__variants to static styles
|
36
|
+
Object.keys(styleEntry).forEach(key => {
|
37
|
+
if (!key.startsWith('__uni__secrets__')) {
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
const secret = keyInObject(styleEntry, key) ? styleEntry[key] : undefined;
|
41
|
+
if (!secret) {
|
42
|
+
return;
|
43
|
+
}
|
44
|
+
Object.defineProperty(secret, '__uni__variants', {
|
45
|
+
value: selectedVariants
|
46
|
+
});
|
47
|
+
});
|
31
48
|
});
|
32
49
|
};
|
33
50
|
Object.defineProperty(styles, 'useVariants', {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["useMemo","useRef","useState","equal","reduceObject","getVariants","createUseVariants","styles","setSelectedVariants","useVariants","selectedVariants","selectedVariantStylesMap","Map","lastSelectedVariantsRef","variantsChanged","current","combinedVariantStyles","result","forEach","key","value","set","styleEntry","selectedVariantStyles","get","Object","defineProperties","enumerable","configurable","defineProperty"],"sourceRoot":"../../../../src","sources":["web/variants/useVariants.ts"],"mappings":";;AAAA,SAASA,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAEjD,SAASC,KAAK,EAAEC,YAAY,QAAQ,UAAU;
|
1
|
+
{"version":3,"names":["useMemo","useRef","useState","equal","keyInObject","reduceObject","getVariants","createUseVariants","styles","setSelectedVariants","useVariants","selectedVariants","selectedVariantStylesMap","Map","lastSelectedVariantsRef","variantsChanged","current","combinedVariantStyles","result","forEach","key","value","set","styleEntry","selectedVariantStyles","get","Object","defineProperties","enumerable","configurable","keys","startsWith","secret","undefined","defineProperty"],"sourceRoot":"../../../../src","sources":["web/variants/useVariants.ts"],"mappings":";;AAAA,SAASA,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ,OAAO;AAEjD,SAASC,KAAK,EAAEC,WAAW,EAAEC,YAAY,QAAQ,UAAU;AAC3D,SAASC,WAAW,QAAQ,eAAe;AAE3C,OAAO,MAAMC,iBAAiB,GAAGA,CAACC,MAAyC,EAAEC,mBAA4D,KAAK;EAC1I,MAAMC,WAAW,GAAIC,gBAAqC,IAAK;IAC3D,MAAM,CAACC,wBAAwB,CAAC,GAAGV,QAAQ,CAAC,MAAM,IAAIW,GAAG,CAA8B,CAAC,CAAC;IACzF,MAAMC,uBAAuB,GAAGb,MAAM,CAAsB,CAAC;IAC7D;IACA,MAAMc,eAAe,GAAG,CAACZ,KAAK,CAACW,uBAAuB,CAACE,OAAO,EAAEL,gBAAgB,CAAC;IAEjF,IAAII,eAAe,EAAE;MACjBD,uBAAuB,CAACE,OAAO,GAAGL,gBAAgB;MAClDF,mBAAmB,CAACE,gBAAgB,CAAC;IACzC;IAEA,MAAMM,qBAAqB,GAAGjB,OAAO,CAAC,MAAM;MACxC,MAAMkB,MAAM,GAAGZ,WAAW,CAACE,MAAM,EAAEG,gBAAgB,CAAC;MAEpDO,MAAM,CAACC,OAAO,CAAC,CAAC,CAACC,GAAG,EAAEC,KAAK,CAAC,KAAK;QAC7BT,wBAAwB,CAACU,GAAG,CAACF,GAAG,EAAEC,KAAK,CAAC;MAC5C,CAAC,CAAC;MAEF,OAAOH,MAAM;IACjB,CAAC,EAAE,CAACJ,uBAAuB,CAACE,OAAO,CAAC,CAAC;IAErCC,qBAAqB,CAACE,OAAO,CAAC,CAAC,CAACC,GAAG,CAAC,KAAK;MACrC,MAAMG,UAAU,GAAGf,MAAM,CAACY,GAAG,CAAC;MAE9B,IAAI,CAACG,UAAU,EAAE;QACb;MACJ;MAEA,MAAMC,qBAAqB,GAAGZ,wBAAwB,CAACa,GAAG,CAACL,GAAG,CAAC;MAE/DM,MAAM,CAACC,gBAAgB,CAACJ,UAAU,EAAElB,YAAY,CAACmB,qBAAqB,IAAI,CAAC,CAAC,EAAEH,KAAK,KAAK;QACpFA,KAAK;QACLO,UAAU,EAAE,KAAK;QACjBC,YAAY,EAAE;MAClB,CAAC,CAAC,CAAC,CAAC;;MAEJ;MACAH,MAAM,CAACI,IAAI,CAACP,UAAU,CAAC,CAACJ,OAAO,CAACC,GAAG,IAAI;QACnC,IAAI,CAACA,GAAG,CAACW,UAAU,CAAC,kBAAkB,CAAC,EAAE;UACrC;QACJ;QAEA,MAAMC,MAAM,GAAG5B,WAAW,CAACmB,UAAU,EAAEH,GAAG,CAAC,GAAGG,UAAU,CAACH,GAAG,CAAC,GAAGa,SAAS;QAEzE,IAAI,CAACD,MAAM,EAAE;UACT;QACJ;QAEAN,MAAM,CAACQ,cAAc,CAACF,MAAM,EAAE,iBAAiB,EAAE;UAC7CX,KAAK,EAAEV;QACX,CAAC,CAAC;MACN,CAAC,CAAC;IACN,CAAC,CAAC;EACN,CAAC;EAEDe,MAAM,CAACQ,cAAc,CAAC1B,MAAM,EAAE,aAAa,EAAE;IACzCa,KAAK,EAAEX;EACX,CAAC,CAAC;AACN,CAAC","ignoreList":[]}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useVariants.d.ts","sourceRoot":"","sources":["../../../../../src/web/variants/useVariants.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAIpE,eAAO,MAAM,iBAAiB,WAAY,qBAAqB,CAAC,UAAU,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,
|
1
|
+
{"version":3,"file":"useVariants.d.ts","sourceRoot":"","sources":["../../../../../src/web/variants/useVariants.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAIpE,eAAO,MAAM,iBAAiB,WAAY,qBAAqB,CAAC,UAAU,CAAC,uBAAuB,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,IAAI,SA2DxI,CAAA"}
|
package/package.json
CHANGED
package/plugin/index.js
CHANGED
@@ -42,6 +42,37 @@ module.exports = function ({ types: t }) {
|
|
42
42
|
}
|
43
43
|
}
|
44
44
|
},
|
45
|
+
FunctionDeclaration(path, state) {
|
46
|
+
const componentName = path.node.id
|
47
|
+
? path.node.id.name
|
48
|
+
: null
|
49
|
+
|
50
|
+
if (componentName) {
|
51
|
+
state.file.hasVariants = false
|
52
|
+
}
|
53
|
+
},
|
54
|
+
ClassDeclaration(path, state) {
|
55
|
+
const componentName = path.node.id
|
56
|
+
? path.node.id.name
|
57
|
+
: null
|
58
|
+
|
59
|
+
if (componentName) {
|
60
|
+
state.file.hasVariants = false
|
61
|
+
}
|
62
|
+
},
|
63
|
+
VariableDeclaration(path, state) {
|
64
|
+
path.node.declarations.forEach((declaration) => {
|
65
|
+
if (t.isArrowFunctionExpression(declaration.init) || t.isFunctionExpression(declaration.init)) {
|
66
|
+
const componentName = declaration.id
|
67
|
+
? declaration.id.name
|
68
|
+
: null
|
69
|
+
|
70
|
+
if (componentName) {
|
71
|
+
state.file.hasVariants = false
|
72
|
+
}
|
73
|
+
}
|
74
|
+
})
|
75
|
+
},
|
45
76
|
ImportDeclaration(path, state) {
|
46
77
|
const importSource = path.node.source.value
|
47
78
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { useMemo, useRef, useState } from 'react'
|
2
2
|
import type { ReactNativeStyleSheet, StyleSheet } from '../../types'
|
3
|
-
import { equal, reduceObject } from '../utils'
|
3
|
+
import { equal, keyInObject, reduceObject } from '../utils'
|
4
4
|
import { getVariants } from './getVariants'
|
5
5
|
|
6
6
|
export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, setSelectedVariants: (variants: Record<string, any>) => void) => {
|
@@ -27,6 +27,11 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
|
|
27
27
|
|
28
28
|
combinedVariantStyles.forEach(([key]) => {
|
29
29
|
const styleEntry = styles[key]
|
30
|
+
|
31
|
+
if (!styleEntry) {
|
32
|
+
return
|
33
|
+
}
|
34
|
+
|
30
35
|
const selectedVariantStyles = selectedVariantStylesMap.get(key)
|
31
36
|
|
32
37
|
Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
|
@@ -34,6 +39,23 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
|
|
34
39
|
enumerable: false,
|
35
40
|
configurable: true
|
36
41
|
})))
|
42
|
+
|
43
|
+
// Add __uni__variants to static styles
|
44
|
+
Object.keys(styleEntry).forEach(key => {
|
45
|
+
if (!key.startsWith('__uni__secrets__')) {
|
46
|
+
return
|
47
|
+
}
|
48
|
+
|
49
|
+
const secret = keyInObject(styleEntry, key) ? styleEntry[key] : undefined
|
50
|
+
|
51
|
+
if (!secret) {
|
52
|
+
return
|
53
|
+
}
|
54
|
+
|
55
|
+
Object.defineProperty(secret, '__uni__variants', {
|
56
|
+
value: selectedVariants
|
57
|
+
})
|
58
|
+
})
|
37
59
|
})
|
38
60
|
}
|
39
61
|
|