diff --git a/components/native/ActivityIndicator/package.json b/components/native/ActivityIndicator/package.json index c120c13c..d8bcd2b3 100644 --- a/components/native/ActivityIndicator/package.json +++ b/components/native/ActivityIndicator/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/ActivityIndicator.js", - "module": "../../../lib/module/components/native/ActivityIndicator.js" + "module": "../../../lib/module/components/native/ActivityIndicator.js", + "react-native": "../../../src/components/native/ActivityIndicator.tsx" } diff --git a/components/native/FlatList/package.json b/components/native/FlatList/package.json index 5320f2b8..6b15aaab 100644 --- a/components/native/FlatList/package.json +++ b/components/native/FlatList/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/FlatList.js", - "module": "../../../lib/module/components/native/FlatList.js" + "module": "../../../lib/module/components/native/FlatList.js", + "react-native": "../../../src/components/native/FlatList.tsx" } diff --git a/components/native/Image/package.json b/components/native/Image/package.json index 5052384e..7381b61b 100644 --- a/components/native/Image/package.json +++ b/components/native/Image/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/Image.js", - "module": "../../../lib/module/components/native/Image.js" + "module": "../../../lib/module/components/native/Image.js", + "react-native": "../../../src/components/native/Image.tsx" } diff --git a/components/native/ImageBackground/package.json b/components/native/ImageBackground/package.json index 0a82c59b..a5ea8301 100644 --- a/components/native/ImageBackground/package.json +++ b/components/native/ImageBackground/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/ImageBackground.js", - "module": "../../../lib/module/components/native/ImageBackground.js" + "module": "../../../lib/module/components/native/ImageBackground.js", + "react-native": "../../../src/components/native/ImageBackground.native.tsx" } diff --git a/components/native/KeyboardAvoidingView/package.json b/components/native/KeyboardAvoidingView/package.json index 044a62e3..b4dd6b9b 100644 --- a/components/native/KeyboardAvoidingView/package.json +++ b/components/native/KeyboardAvoidingView/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/KeyboardAvoidingView.js", - "module": "../../../lib/module/components/native/KeyboardAvoidingView.js" + "module": "../../../lib/module/components/native/KeyboardAvoidingView.js", + "react-native": "../../../src/components/native/KeyboardAvoidingView.tsx" } diff --git a/components/native/Pressable/package.json b/components/native/Pressable/package.json index 45ff0f70..12952aa3 100644 --- a/components/native/Pressable/package.json +++ b/components/native/Pressable/package.json @@ -1,5 +1,5 @@ { "main": "../../../lib/commonjs/components/native/Pressable.js", "module": "../../../lib/module/components/native/Pressable.js", - "react-native": "../../../lib/commonjs/components/native/Pressable.native.js" + "react-native": "../../../src/components/native/Pressable.native.tsx" } diff --git a/components/native/RefreshControl/package.json b/components/native/RefreshControl/package.json index 5138f48a..fd4eed59 100644 --- a/components/native/RefreshControl/package.json +++ b/components/native/RefreshControl/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/RefreshControl.js", - "module": "../../../lib/module/components/native/RefreshControl.js" + "module": "../../../lib/module/components/native/RefreshControl.js", + "react-native": "../../../src/components/native/RefreshControl.tsx" } diff --git a/components/native/ScrollView/package.json b/components/native/ScrollView/package.json index ac6c5bf6..eb0a73ff 100644 --- a/components/native/ScrollView/package.json +++ b/components/native/ScrollView/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/ScrollView.js", - "module": "../../../lib/module/components/native/ScrollView.js" + "module": "../../../lib/module/components/native/ScrollView.js", + "react-native": "../../../src/components/native/ScrollView.tsx" } diff --git a/components/native/SectionList/package.json b/components/native/SectionList/package.json index c8c80ec1..a222a37e 100644 --- a/components/native/SectionList/package.json +++ b/components/native/SectionList/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/SectionList.js", - "module": "../../../lib/module/components/native/SectionList.js" + "module": "../../../lib/module/components/native/SectionList.js", + "react-native": "../../../src/components/native/SectionList.tsx" } diff --git a/components/native/Switch/package.json b/components/native/Switch/package.json index fcb0d895..bb9c8629 100644 --- a/components/native/Switch/package.json +++ b/components/native/Switch/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/Switch.js", - "module": "../../../lib/module/components/native/Switch.js" + "module": "../../../lib/module/components/native/Switch.js", + "react-native": "../../../src/components/native/Switch.tsx" } diff --git a/components/native/Text/package.json b/components/native/Text/package.json index 4536cd41..b3e77379 100644 --- a/components/native/Text/package.json +++ b/components/native/Text/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/Text.js", - "module": "../../../lib/module/components/native/Text.js" + "module": "../../../lib/module/components/native/Text.js", + "react-native": "../../../src/components/native/Text.tsx" } diff --git a/components/native/TextInput/package.json b/components/native/TextInput/package.json index aa720daa..a6670ec8 100644 --- a/components/native/TextInput/package.json +++ b/components/native/TextInput/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/TextInput.js", - "module": "../../../lib/module/components/native/TextInput.js" + "module": "../../../lib/module/components/native/TextInput.js", + "react-native": "../../../src/components/native/TextInput.tsx" } diff --git a/components/native/TouchableHighlight/package.json b/components/native/TouchableHighlight/package.json index cf342106..f129b578 100644 --- a/components/native/TouchableHighlight/package.json +++ b/components/native/TouchableHighlight/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/TouchableHighlight.js", - "module": "../../../lib/module/components/native/TouchableHighlight.js" + "module": "../../../lib/module/components/native/TouchableHighlight.js", + "react-native": "../../../src/components/native/TouchableHighlight.tsx" } diff --git a/components/native/TouchableOpacity/package.json b/components/native/TouchableOpacity/package.json index 9576a871..2e477220 100644 --- a/components/native/TouchableOpacity/package.json +++ b/components/native/TouchableOpacity/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/TouchableOpacity.js", - "module": "../../../lib/module/components/native/TouchableOpacity.js" + "module": "../../../lib/module/components/native/TouchableOpacity.js", + "react-native": "../../../src/components/native/TouchableOpacity.tsx" } diff --git a/components/native/View/package.json b/components/native/View/package.json index b16fa6f9..7b6a7a96 100644 --- a/components/native/View/package.json +++ b/components/native/View/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/View.js", - "module": "../../../lib/module/components/native/View.js" + "module": "../../../lib/module/components/native/View.js", + "react-native": "../../../src/components/native/View.tsx" } diff --git a/components/native/VirtualizedList/package.json b/components/native/VirtualizedList/package.json index 1dfdac0c..3175f079 100644 --- a/components/native/VirtualizedList/package.json +++ b/components/native/VirtualizedList/package.json @@ -1,4 +1,5 @@ { "main": "../../../lib/commonjs/components/native/VirtualizedList.js", - "module": "../../../lib/module/components/native/VirtualizedList.js" + "module": "../../../lib/module/components/native/VirtualizedList.js", + "react-native": "../../../src/components/native/VirtualizedList.tsx" } diff --git a/cxx/common/Constants.h b/cxx/common/Constants.h index cc8b817a..d0fee3f0 100644 --- a/cxx/common/Constants.h +++ b/cxx/common/Constants.h @@ -2,14 +2,14 @@ namespace margelo::nitro::unistyles::helpers { -static const std::string UNISTYLES_ID = "__unid"; +static const std::string STYLESHEET_ID = "__stylesheetID"; +static const std::string UNISTYLE_ID = "__unistyleID"; static const std::string ADD_VARIANTS_FN = "useVariants"; static const std::string STYLE_DEPENDENCIES = "uni__dependencies"; -static const std::string STYLE_VARIANTS = "uni__variants"; +static const std::string STYLESHEET_VARIANTS = "__stylesheetVariants"; static const std::string WEB_STYLE_KEY = "_web"; static const std::string EXOTIC_STYLE_KEY = "_exotic"; -static const std::string NAME_STYLE_KEY = "__unistyles_name"; -static const std::string SECRETS = "__uni__secrets"; static const std::string ARGUMENTS = "__uni__args"; +static const std::string GET_STYLES = "uni__getStyles"; } diff --git a/cxx/common/Helpers.h b/cxx/common/Helpers.h index e8959428..570baea7 100644 --- a/cxx/common/Helpers.h +++ b/cxx/common/Helpers.h @@ -147,7 +147,7 @@ inline jsi::Object variantsToValue(jsi::Runtime& rt, Variants& variants) { return rawVariants; } -inline std::vector parseDynamicFunctionArguments(jsi::Runtime& rt, jsi::Array&& arguments) { +inline std::vector parseDynamicFunctionArguments(jsi::Runtime& rt, jsi::Array& arguments) { std::vector parsedArgument{}; size_t count = arguments.size(rt); diff --git a/cxx/core/HashGenerator.h b/cxx/core/HashGenerator.h new file mode 100644 index 00000000..e0d4ca69 --- /dev/null +++ b/cxx/core/HashGenerator.h @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include + +namespace margelo::nitro::unistyles::helpers { + + +struct HashGenerator { + static std::atomic count; + + static std::string generateHash(const std::string& input) { + std::hash stringHasher; + uint64_t inputHash = stringHasher(input); + + unsigned int counterValue = count.fetch_add(1, std::memory_order_relaxed); + uint64_t combinedHash = inputHash ^ (static_cast(counterValue) << 32); + + std::stringstream ss; + ss << std::hex << std::setfill('0') << std::setw(8) << (combinedHash & 0xFFFFFFFF); + + return "unistyles-" + ss.str(); + } +}; + +std::atomic HashGenerator::count(0); + +} diff --git a/cxx/core/HostStyle.cpp b/cxx/core/HostStyle.cpp deleted file mode 100644 index 2ffd4b29..00000000 --- a/cxx/core/HostStyle.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "HostStyle.h" - -using namespace margelo::nitro::unistyles::core; -using namespace margelo::nitro::unistyles::parser; -using namespace facebook; - -std::vector HostStyle::getPropertyNames(jsi::Runtime& rt) { - auto propertyNames = std::vector {}; - - propertyNames.reserve(8); - - for (const auto& pair : this->_styleSheet->unistyles) { - propertyNames.emplace_back(jsi::PropNameID::forUtf8(rt, pair.first)); - } - - return propertyNames; -} - -jsi::Value HostStyle::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) { - auto propertyName = propNameId.utf8(rt); - - if (propertyName == helpers::UNISTYLES_ID) { - return jsi::Value(this->_styleSheet->tag); - } - - if (propertyName == helpers::ADD_VARIANTS_FN) { - return this->createAddVariantsProxyFunction(rt); - } - - if (this->_styleSheet->unistyles.contains(propertyName)) { - if (this->_styleCache.contains(propertyName)) { - return jsi::Value(rt, this->_styleCache[propertyName]); - } - - this->_styleCache[propertyName] = valueFromUnistyle( - rt, - this->_unistylesRuntime, - this->_styleSheet->unistyles[propertyName], - this->_variants - ); - - return jsi::Value(rt, this->_styleCache[propertyName]); - } - - if (propertyName == helpers::STYLE_VARIANTS) { - return helpers::variantsToValue(rt, this->_variants); - } - - return jsi::Value::undefined(); -} - -jsi::Function HostStyle::createAddVariantsProxyFunction(jsi::Runtime& rt) { - auto useVariantsFnName = jsi::PropNameID::forUtf8(rt, helpers::ADD_VARIANTS_FN); - - return jsi::Function::createFromHostFunction(rt, useVariantsFnName, 1, [&](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count){ - helpers::assertThat(rt, count == 1, "Unistyles: useVariants expected to be called with one argument."); - helpers::assertThat(rt, arguments[0].isObject(), "Unistyles: useVariants expected to be called with object."); - - auto pairs = helpers::variantsToPairs(rt, arguments[0].asObject(rt)); - - if (this->hasVariantsSet && pairs == this->_variants) { - return jsi::Value::undefined(); - } - - // new variants or empty variants - this->hasVariantsSet = true; - this->_variants = pairs; - - return jsi::Value::undefined(); - }); -} - -void HostStyle::set(jsi::Runtime& rt, const jsi::PropNameID& propNameId, const jsi::Value& value) {} diff --git a/cxx/core/HostStyle.h b/cxx/core/HostStyle.h deleted file mode 100644 index 4f6cb646..00000000 --- a/cxx/core/HostStyle.h +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include -#include "StyleSheet.h" -#include "Constants.h" -#include "Parser.h" -#include "UnistyleWrapper.h" -#include "HybridUnistylesRuntime.h" - -namespace margelo::nitro::unistyles::core { - -using Variants = std::vector>; - -struct JSI_EXPORT HostStyle : public jsi::HostObject { - HostStyle(std::shared_ptr styleSheet, std::shared_ptr unistylesRuntime) - : _styleSheet{styleSheet}, _unistylesRuntime{unistylesRuntime} {}; - - std::vector getPropertyNames(jsi::Runtime& rt); - jsi::Value get(jsi::Runtime& rt, const jsi::PropNameID& propNameId); - void set(jsi::Runtime& rt, const jsi::PropNameID& propNameId, const jsi::Value& value); - jsi::Function createAddVariantsProxyFunction(jsi::Runtime& rt); - -private: - // to additionally distinguish between empty variants - // that are set at the beginning - bool hasVariantsSet = false; - - std::shared_ptr _styleSheet; - std::shared_ptr _unistylesRuntime; - std::vector> _variants{}; - std::unordered_map _styleCache{}; -}; - -} diff --git a/cxx/core/HostUnistyle.cpp b/cxx/core/HostUnistyle.cpp new file mode 100644 index 00000000..04bf3845 --- /dev/null +++ b/cxx/core/HostUnistyle.cpp @@ -0,0 +1,73 @@ +#include "HostUnistyle.h" + +using namespace margelo::nitro::unistyles::core; +using namespace facebook; + +std::vector HostUnistyle::getPropertyNames(jsi::Runtime& rt) { + auto propertyNames = std::vector {}; + + propertyNames.reserve(8); + + for (const auto& pair : this->_stylesheet->unistyles) { + propertyNames.emplace_back(jsi::PropNameID::forUtf8(rt, pair.first)); + } + + propertyNames.emplace_back(jsi::PropNameID::forUtf8(rt, helpers::ADD_VARIANTS_FN.c_str())); + + return propertyNames; +} + +jsi::Value HostUnistyle::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) { + auto propertyName = propNameId.utf8(rt); + + if (propertyName == helpers::STYLESHEET_ID.c_str()) { + return jsi::Value(this->_stylesheet->tag); + } + + if (propertyName == helpers::STYLESHEET_VARIANTS.c_str()) { + return helpers::variantsToValue(rt, this->_variants); + } + + if (propertyName == helpers::ADD_VARIANTS_FN.c_str()) { + return this->createAddVariantsProxyFunction(rt); + } + + if (!this->_stylesheet->unistyles.contains(propertyName)) { + return jsi::Value::undefined(); + } + + return valueFromUnistyle(rt, this->_unistylesRuntime, this->_stylesheet->unistyles[propertyName], this->_variants); +} + +void HostUnistyle::set(jsi::Runtime& rt, const jsi::PropNameID& propNameId, const jsi::Value& value) {} + +jsi::Function HostUnistyle::createAddVariantsProxyFunction(jsi::Runtime& rt) { + auto useVariantsFnName = jsi::PropNameID::forUtf8(rt, helpers::ADD_VARIANTS_FN); + auto parser = parser::Parser(this->_unistylesRuntime); + + return jsi::Function::createFromHostFunction(rt, useVariantsFnName, 1, [this, &parser](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count){ + helpers::assertThat(rt, count == 1, "Unistyles: useVariants expected to be called with one argument."); + helpers::assertThat(rt, arguments[0].isObject(), "Unistyles: useVariants expected to be called with object."); + + Variants variants = helpers::variantsToPairs(rt, arguments[0].asObject(rt)); + + helpers::enumerateJSIObject(rt, thisVal.asObject(rt), [this, &parser, &rt, &variants](const std::string& name, jsi::Value& value){ + if (name == helpers::ADD_VARIANTS_FN || !this->_stylesheet->unistyles.contains(name)) { + return; + } + + auto unistyle = this->_stylesheet->unistyles[name]; + + if (unistyle->dependsOn(UnistyleDependency::VARIANTS)) { + parser.rebuildUnistyle(rt, unistyle, variants, std::nullopt); + } + }); + + this->_variants = variants; + + auto style = std::make_shared(this->_stylesheet, this->_unistylesRuntime, variants); + auto styleHostObject = jsi::Object::createFromHostObject(rt, style); + + return styleHostObject; + }); +} diff --git a/cxx/core/HostUnistyle.h b/cxx/core/HostUnistyle.h new file mode 100644 index 00000000..54524bef --- /dev/null +++ b/cxx/core/HostUnistyle.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include "Parser.h" +#include "UnistyleWrapper.h" + +namespace margelo::nitro::unistyles::core { + +using Variants = std::vector>; + +struct JSI_EXPORT HostUnistyle : public jsi::HostObject { + HostUnistyle(std::shared_ptr stylesheet, std::shared_ptr unistylesRuntime, Variants& variants) + : _stylesheet(stylesheet), _unistylesRuntime{unistylesRuntime}, _variants{std::move(variants)} {}; + + std::vector getPropertyNames(jsi::Runtime& rt); + jsi::Value get(jsi::Runtime& rt, const jsi::PropNameID& propNameId); + void set(jsi::Runtime& rt, const jsi::PropNameID& propNameId, const jsi::Value& value); + + jsi::Function createAddVariantsProxyFunction(jsi::Runtime& rt); + +private: + Variants _variants; + std::shared_ptr _stylesheet; + std::shared_ptr _unistylesRuntime; +}; + +} diff --git a/cxx/core/RNStyle.h b/cxx/core/RNStyle.h new file mode 100644 index 00000000..71c45dd9 --- /dev/null +++ b/cxx/core/RNStyle.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include "Helpers.h" +#include "Parser.h" +#include "HostUnistyle.h" +#include "UnistyleWrapper.h" + +namespace margelo::nitro::unistyles::core { + +jsi::Object toRNStyle(jsi::Runtime& rt, std::shared_ptr stylesheet, std::shared_ptr unistylesRuntime, Variants&& variants) { + auto style = std::make_shared(stylesheet, unistylesRuntime, variants); + auto styleHostObject = jsi::Object::createFromHostObject(rt, style); + + return styleHostObject; +} + +} diff --git a/cxx/core/Unistyle.h b/cxx/core/Unistyle.h index 86421074..27728050 100644 --- a/cxx/core/Unistyle.h +++ b/cxx/core/Unistyle.h @@ -19,8 +19,8 @@ enum class UnistyleType { struct Unistyle { using Shared = std::shared_ptr; - Unistyle(UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr styleSheet) - : styleKey{styleKey}, type{type}, rawValue{std::move(rawObject)}, parent{styleSheet} {} + Unistyle(std::string hash, UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr styleSheet) + : unid{hash}, styleKey{styleKey}, type{type}, rawValue{std::move(rawObject)}, parent{styleSheet} {} virtual ~Unistyle() = default; Unistyle(const Unistyle&) = delete; @@ -28,6 +28,7 @@ struct Unistyle { UnistyleType type; std::string styleKey; + std::string unid; jsi::Object rawValue; std::optional parsedStyle; std::vector dependencies{}; @@ -65,8 +66,8 @@ struct UnistyleDynamicFunction: public Unistyle { // unprocessedValue <- object generated after calling proxy and user's original function // parsedStyle <- parsed with Unistyle's parser - UnistyleDynamicFunction(UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr styleSheet) - : Unistyle(type, styleKey, rawObject, styleSheet) {} + UnistyleDynamicFunction(std::string hash, UnistyleType type, std::string styleKey, jsi::Object& rawObject, std::shared_ptr styleSheet) + : Unistyle(hash, type, styleKey, rawObject, styleSheet) {} UnistyleDynamicFunction(const UnistyleDynamicFunction&) = delete; UnistyleDynamicFunction(UnistyleDynamicFunction&& other) = delete; diff --git a/cxx/core/UnistyleWrapper.h b/cxx/core/UnistyleWrapper.h index b4a0d67a..d7bdbf64 100644 --- a/cxx/core/UnistyleWrapper.h +++ b/cxx/core/UnistyleWrapper.h @@ -21,6 +21,7 @@ struct UnistyleWrapper: public jsi::NativeState { inline static Unistyle::Shared unistyleFromStaticStyleSheet(jsi::Runtime& rt, jsi::Object& value) { auto exoticUnistyle = std::make_shared( + helpers::HashGenerator::generateHash(helpers::EXOTIC_STYLE_KEY), UnistyleType::Object, helpers::EXOTIC_STYLE_KEY, value, @@ -32,56 +33,63 @@ inline static Unistyle::Shared unistyleFromStaticStyleSheet(jsi::Runtime& rt, js return exoticUnistyle; } -inline static std::vector unistylesFromNonExistentNativeState(jsi::Runtime& rt, jsi::Object& value) { - auto hasUnistyleName = value.hasProperty(rt, helpers::NAME_STYLE_KEY.c_str()); - - // return wrapped RN/inline style - if (!hasUnistyleName) { - return {unistyleFromStaticStyleSheet(rt, value)}; - } +inline static std::vector getUnistylesHashKeys(jsi::Runtime& rt, jsi::Object& object) { + std::vector matchingKeys{}; + const std::string prefix = "unistyles-"; - throw jsi::JSError(rt, R"(Unistyles: Style is not bound! + auto propertyNames = object.getPropertyNames(rt); + size_t length = propertyNames.length(rt); -Potential reasons: -- You likely used the spread operator on a Unistyle style outside of a JSX component + for (size_t i = 0; i < length; i++) { + auto propertyName = propertyNames.getValueAtIndex(rt, i).getString(rt); + std::string key = propertyName.utf8(rt); -If you need to merge styles, do it within the style prop of your JSX component: + if (key.compare(0, prefix.length(), prefix) == 0) { + matchingKeys.push_back(key); + } + } -style={{...styles.container, ...styles.otherProp}} -or -style={[styles.container, styles.otherProp]} + return matchingKeys; +} -If you pass computed style prop to component use array syntax: +inline static std::vector unistylesFromHashKeys(jsi::Runtime& rt, jsi::Object& object, std::vector keys) { + std::vector unistyles{}; + auto& registry = UnistylesRegistry::get(); -customStyleProp={[styles.container, styles.otherProp]} + for (auto& key: keys) { + unistyles.emplace_back(registry.getUnistyleById(rt, key)); + } -Copying a Unistyle style outside of a JSX element will remove its internal C++ state, leading to unexpected behavior.)"); + return unistyles; } -inline static jsi::Object generateUnistylesPrototype( - jsi::Runtime& rt, - std::shared_ptr unistylesRuntime, - Unistyle::Shared unistyle, - std::optional variants, - std::optional arguments -) { - // add prototype metadata for createUnistylesComponent - auto proto = jsi::Object(rt); - auto hostFn = jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forUtf8(rt, "getStyle"), 0, [unistyle, unistylesRuntime](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count){ - auto variants = helpers::variantsToPairs(rt, thisValue.asObject(rt).getProperty(rt, "variants").asObject(rt)); - auto arguments = helpers::parseDynamicFunctionArguments(rt, thisValue.asObject(rt).getProperty(rt, "arguments").asObject(rt).asArray(rt)); - - parser::Parser(unistylesRuntime).rebuildUnistyle(rt, unistyle->parent, unistyle, variants, std::make_optional>(arguments)); +inline static std::vector unistylesFromNonExistentNativeState(jsi::Runtime& rt, jsi::Object& value) { + auto unistyleHashKeys = getUnistylesHashKeys(rt, value); - return jsi::Value(rt, unistyle->parsedStyle.value()).asObject(rt); + // return wrapped RN/inline style + if (unistyleHashKeys.empty()) { + return {unistyleFromStaticStyleSheet(rt, value)}; + } + + // last chance to fallback and get unistyle based on hash + auto unistyles = unistylesFromHashKeys(rt, value, unistyleHashKeys); + auto areValid = std::all_of(unistyles.begin(), unistyles.end(), [](Unistyle::Shared unistyle){ + return unistyle != nullptr; }); - proto.setProperty(rt, "getStyle", std::move(hostFn)); - proto.setProperty(rt, "arguments", arguments.has_value() ? std::move(arguments.value()) : jsi::Array(rt, 0)); - proto.setProperty(rt, "variants", variants.has_value() ? helpers::pairsToVariantsValue(rt, variants.value()) : jsi::Object(rt)); - proto.setProperty(rt, helpers::STYLE_DEPENDENCIES.c_str(), helpers::dependenciesToJSIArray(rt, unistyle->dependencies)); + if (!areValid) { + throw jsi::JSError(rt, R"(Unistyles: Style is not bound! - return proto; +You likely altered unistyle hash key and we're not able to recover C++ state attached to this node.)"); + } + + // someone merged unistyles, and will be warned in JS + // the best we can do is to return first unistyle + if (unistyles.size() > 1) { + return {unistyles.at(0)}; + } + + return unistyles; } inline static std::vector unistyleFromValue(jsi::Runtime& rt, const jsi::Value& value) { @@ -116,28 +124,65 @@ inline static std::vector unistyleFromValue(jsi::Runtime& rt, return unistyles; } -inline static jsi::Value valueFromUnistyle(jsi::Runtime& rt, std::shared_ptr unistylesRuntime, Unistyle::Shared unistyle, Variants& variants) { +inline static jsi::Value objectFromUnistyle(jsi::Runtime& rt, std::shared_ptr unistylesRuntime, Unistyle::Shared unistyle, Variants& variants, std::optional arguments) { auto wrappedUnistyle = std::make_shared(unistyle); + auto unistyleID = jsi::PropNameID::forUtf8(rt, unistyle->unid); - if (unistyle->type == UnistyleType::Object) { - jsi::Object obj = jsi::Object(rt); + jsi::Object obj = jsi::Object(rt); - obj.setNativeState(rt, std::move(wrappedUnistyle)); - obj.setProperty(rt, helpers::NAME_STYLE_KEY.c_str(), jsi::String::createFromUtf8(rt, unistyle->styleKey)); + obj.setNativeState(rt, std::move(wrappedUnistyle)); - helpers::defineHiddenProperty(rt, obj, helpers::STYLE_DEPENDENCIES.c_str(), helpers::dependenciesToJSIArray(rt, unistyle->dependencies)); - helpers::mergeJSIObjects(rt, obj, unistyle->parsedStyle.value()); + auto secrets = jsi::Object(rt); - obj.setProperty(rt, "__proto__", generateUnistylesPrototype(rt, unistylesRuntime, unistyle, variants, std::nullopt)); + auto parsedArguments = arguments.has_value() + ? helpers::parseDynamicFunctionArguments(rt, arguments.value()) + : std::optional>{}; + + if (arguments.has_value()) { + // this is required for HybridShadowRegistry::link + helpers::defineHiddenProperty(rt, secrets, helpers::ARGUMENTS.c_str(), arguments.value()); + } + + // this is required for HybridShadowRegistry::link + helpers::defineHiddenProperty(rt, secrets, helpers::STYLESHEET_VARIANTS.c_str(), helpers::variantsToValue(rt, variants)); + + // this is required for withUnistyles + helpers::defineHiddenProperty(rt, secrets, helpers::STYLE_DEPENDENCIES.c_str(), helpers::dependenciesToJSIArray(rt, unistyle->dependencies)); + + // this is required for withUnistyles + auto hostFn = jsi::Function::createFromHostFunction( + rt, + jsi::PropNameID::forUtf8(rt, helpers::GET_STYLES.c_str()), + 0, + [unistyle, unistylesRuntime, variants, parsedArguments](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count + ) { + parser::Parser(unistylesRuntime).rebuildUnistyle(rt, unistyle, variants, parsedArguments); + + return jsi::Value(rt, unistyle->parsedStyle.value()).asObject(rt); + }); + + helpers::defineHiddenProperty(rt, secrets, helpers::GET_STYLES.c_str(), std::move(hostFn)); - return obj; + obj.setProperty(rt, unistyleID, secrets); + + helpers::mergeJSIObjects(rt, obj, unistyle->parsedStyle.value()); + + return obj; +} + +inline static jsi::Value valueFromUnistyle(jsi::Runtime& rt, std::shared_ptr unistylesRuntime, Unistyle::Shared unistyle, Variants& variants) { + if (unistyle->type == UnistyleType::Object) { + return objectFromUnistyle(rt, unistylesRuntime, unistyle, variants, std::nullopt); } + auto wrappedUnistyle = std::make_shared(unistyle); + auto unistyleID = jsi::PropNameID::forUtf8(rt, unistyle->unid); + auto unistyleFn = std::dynamic_pointer_cast(unistyle); auto hostFn = jsi::Value(rt, unistyleFn->proxiedFunction.value()).asObject(rt).asFunction(rt); hostFn.setNativeState(rt, std::move(wrappedUnistyle)); - hostFn.setProperty(rt, helpers::NAME_STYLE_KEY.c_str(), jsi::String::createFromUtf8(rt, unistyleFn->styleKey)); + hostFn.setProperty(rt, unistyleID, jsi::Object(rt)); return std::move(hostFn); } diff --git a/cxx/core/UnistylesRegistry.cpp b/cxx/core/UnistylesRegistry.cpp index 2e65e002..6ab24a39 100644 --- a/cxx/core/UnistylesRegistry.cpp +++ b/cxx/core/UnistylesRegistry.cpp @@ -75,7 +75,7 @@ void core::UnistylesRegistry::linkShadowNodeWithUnistyle( ) { shadow::ShadowLeafUpdates updates; auto parser = parser::Parser(nullptr); - + std::for_each(unistylesData.begin(), unistylesData.end(), [this, &rt, shadowNodeFamily](std::shared_ptr unistyleData){ this->_shadowRegistry[&rt][shadowNodeFamily].emplace_back(unistyleData); }); @@ -159,7 +159,7 @@ std::vector> core::UnistylesRegistry::getStyle UnistyleDependency::THEME); auto themeDidChange = themeDidChangeIt != unistylesDependencies.end(); auto runtimeDidChange = (themeDidChange && unistylesDependencies.size() > 1) || unistylesDependencies.size() > 0; - + // if nothing changed, skip further lookup if (!themeDidChange && !runtimeDidChange) { return stylesheetsToRefresh; @@ -173,7 +173,7 @@ std::vector> core::UnistylesRegistry::getStyle if (styleSheet->type == StyleSheetType::ThemableWithMiniRuntime) { for (const auto& unistylePair: styleSheet->unistyles) { auto& [_, unistyle] = unistylePair; - + bool hasAnyOfDependencies = std::any_of( unistyle->dependencies.begin(), unistyle->dependencies.end(), @@ -181,10 +181,10 @@ std::vector> core::UnistylesRegistry::getStyle return std::find(unistylesDependencies.begin(), unistylesDependencies.end(), dep) != unistylesDependencies.end(); } ); - + if (hasAnyOfDependencies) { stylesheetsToRefresh.emplace_back(styleSheet); - + return; } } @@ -198,18 +198,26 @@ std::vector> core::UnistylesRegistry::getStyle return stylesheetsToRefresh; } -const core::Variants& core::UnistylesRegistry::getScopedVariants() { - return this->_scopedVariants; +core::Unistyle::Shared core::UnistylesRegistry::getUnistyleById(jsi::Runtime& rt, std::string unistyleID) { + for (auto& pair: this->_styleSheetRegistry[&rt]) { + auto [_, stylesheet] = pair; + + for (auto unistylePair: stylesheet->unistyles) { + auto [_, unistyle] = unistylePair; + + if (unistyle->unid == unistyleID) { + return unistyle; + } + } + } + + return nullptr; } const std::optional core::UnistylesRegistry::getScopedTheme() { return this->_scopedTheme; } -void core::UnistylesRegistry::setScopedVariants(core::Variants&& variants) { - this->_scopedVariants = std::move(variants); -} - void core::UnistylesRegistry::setScopedTheme(std::optional themeName) { this->_scopedTheme = std::move(themeName); } diff --git a/cxx/core/UnistylesRegistry.h b/cxx/core/UnistylesRegistry.h index 2426b64b..f08fcf94 100644 --- a/cxx/core/UnistylesRegistry.h +++ b/cxx/core/UnistylesRegistry.h @@ -43,15 +43,13 @@ struct UnistylesRegistry: public StyleSheetRegistry { DependencyMap buildDependencyMap(jsi::Runtime& rt, std::vector& deps); void shadowLeafUpdateFromUnistyle(jsi::Runtime& rt, Unistyle::Shared unistyle, jsi::Value& maybePressableId); shadow::ShadowTrafficController trafficController{}; - const core::Variants& getScopedVariants(); const std::optional getScopedTheme(); - void setScopedVariants(core::Variants&& variants); void setScopedTheme(std::optional themeName); + core::Unistyle::Shared getUnistyleById(jsi::Runtime& rt, std::string unistyleID); private: UnistylesRegistry() = default; - - core::Variants _scopedVariants{}; + std::optional _scopedTheme{}; std::unordered_map _states{}; std::unordered_map>> _styleSheetRegistry{}; diff --git a/cxx/hybridObjects/HybridShadowRegistry.cpp b/cxx/hybridObjects/HybridShadowRegistry.cpp index fd256bee..cbca2349 100644 --- a/cxx/hybridObjects/HybridShadowRegistry.cpp +++ b/cxx/hybridObjects/HybridShadowRegistry.cpp @@ -7,30 +7,31 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry link' expected 2 arguments."); ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]); + std::vector unistyleWrappers = core::unistyleFromValue(rt, args[1]); std::vector> arguments; auto& registry = core::UnistylesRegistry::get(); for (size_t i = 0; i < unistyleWrappers.size(); i++) { if (unistyleWrappers[i]->type == core::UnistyleType::DynamicFunction) { - auto rawStyle = args[1].asObject(rt).asArray(rt).getValueAtIndex(rt, i); - - helpers::assertThat(rt, rawStyle.isObject(), "Unistyles: Dynamic function is not bound!"); - - auto maybeSecrets = rawStyle.getObject(rt).getProperty(rt, helpers::SECRETS.c_str()); - - helpers::assertThat(rt, maybeSecrets.isObject(), "Unistyles: Dynamic function is not bound!"); - - auto secrets = maybeSecrets.asObject(rt).getProperty(rt, helpers::ARGUMENTS.c_str()); - - arguments.push_back(helpers::parseDynamicFunctionArguments(rt, secrets.asObject(rt).asArray(rt))); - - continue; + try { + auto rawStyle = args[1].asObject(rt).asArray(rt).getValueAtIndex(rt, i); + auto rawStyleObj = rawStyle.getObject(rt); + auto unistyleHashKeys = core::getUnistylesHashKeys(rt, rawStyleObj); + auto secrets = rawStyleObj.getProperty(rt, unistyleHashKeys.at(0).c_str()).asObject(rt); + auto secretArguments = secrets.getProperty(rt, helpers::ARGUMENTS.c_str()).asObject(rt).asArray(rt); + + arguments.push_back(helpers::parseDynamicFunctionArguments(rt, secretArguments)); + + continue; + } catch (...) { + arguments.push_back({}); + } } arguments.push_back({}); } - + auto scopedTheme = registry.getScopedTheme(); // check if scope theme exists @@ -47,9 +48,23 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa // create unistyleData based on wrappers for (size_t i = 0; i < unistyleWrappers.size(); i++) { core::Unistyle::Shared& unistyle = unistyleWrappers[i]; + auto rawStyle = args[1].asObject(rt).asArray(rt).getValueAtIndex(rt, i); + auto rawStyleObj = rawStyle.getObject(rt); + auto unistyleHashKeys = core::getUnistylesHashKeys(rt, rawStyleObj); + core::Variants variants{}; + + if (unistyleHashKeys.size() == 1) { + auto secrets = rawStyleObj.getProperty(rt, unistyleHashKeys.at(0).c_str()).asObject(rt); + auto hasVariants = secrets.hasProperty(rt, helpers::STYLESHEET_VARIANTS.c_str()); + + if (hasVariants) { + variants = helpers::variantsToPairs(rt, secrets.getProperty(rt, helpers::STYLESHEET_VARIANTS.c_str()).asObject(rt)); + } + } + std::shared_ptr unistyleData = std::make_shared( unistyle, - registry.getScopedVariants(), + variants, arguments[i], scopedTheme ); @@ -62,9 +77,6 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa // if so we need to force update parser.rebuildUnistyleWithScopedTheme(rt, parsedStyleSheet, unistyleData); - } else { - // for other styles, not scoped to theme we need to compute variants value - parser.rebuildUnistyleWithVariants(rt, unistyleData); } unistylesData.emplace_back(unistyleData); @@ -91,25 +103,9 @@ jsi::Value HybridShadowRegistry::unlink(jsi::Runtime &rt, const jsi::Value &this return jsi::Value::undefined(); } -jsi::Value HybridShadowRegistry::selectVariants(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) { - helpers::assertThat(rt, count == 1, "Unistyles: Invalid babel transform 'ShadowRegistry selectVariants' expected 1 arguments."); - - auto& registry = core::UnistylesRegistry::get(); - - if (args[0].isUndefined()) { - registry.setScopedVariants({}); - } - - if (args[0].isObject()) { - registry.setScopedVariants(helpers::variantsToPairs(rt, args[0].asObject(rt))); - } - - return jsi::Value::undefined(); -} - jsi::Value HybridShadowRegistry::setScopedTheme(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) { helpers::assertThat(rt, count == 1, "Unistyles: setScopedTheme expected 1 argument."); - + auto& registry = core::UnistylesRegistry::get(); if (args[0].isUndefined()) { @@ -127,17 +123,8 @@ jsi::Value HybridShadowRegistry::setScopedTheme(jsi::Runtime &rt, const jsi::Val jsi::Value HybridShadowRegistry::getScopedTheme(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) { auto& registry = core::UnistylesRegistry::get(); auto maybeScopedTheme = registry.getScopedTheme(); - + return maybeScopedTheme.has_value() ? jsi::String::createFromUtf8(rt, maybeScopedTheme.value()) : jsi::Value::undefined(); } - -jsi::Value HybridShadowRegistry::getVariants(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) { - auto& registry = core::UnistylesRegistry::get(); - auto maybeScopedVariants = registry.getScopedVariants(); - - return maybeScopedVariants.size() > 0 - ? helpers::variantsToValue(rt, maybeScopedVariants) - : jsi::Value::undefined(); -} diff --git a/cxx/hybridObjects/HybridShadowRegistry.h b/cxx/hybridObjects/HybridShadowRegistry.h index 13a40a62..9ff2cb5e 100644 --- a/cxx/hybridObjects/HybridShadowRegistry.h +++ b/cxx/hybridObjects/HybridShadowRegistry.h @@ -20,10 +20,6 @@ struct HybridShadowRegistry: public HybridUnistylesShadowRegistrySpec { const jsi::Value& thisValue, const jsi::Value* args, size_t count); - jsi::Value selectVariants(jsi::Runtime& rt, - const jsi::Value& thisValue, - const jsi::Value* args, - size_t count); jsi::Value setScopedTheme(jsi::Runtime& rt, const jsi::Value& thisValue, const jsi::Value* args, @@ -32,10 +28,6 @@ struct HybridShadowRegistry: public HybridUnistylesShadowRegistrySpec { const jsi::Value& thisValue, const jsi::Value* args, size_t count); - jsi::Value getVariants(jsi::Runtime& rt, - const jsi::Value& thisValue, - const jsi::Value* args, - size_t count); void loadHybridMethods() override { HybridUnistylesShadowRegistrySpec::loadHybridMethods(); @@ -43,10 +35,8 @@ struct HybridShadowRegistry: public HybridUnistylesShadowRegistrySpec { registerHybrids(this, [](Prototype& prototype) { prototype.registerRawHybridMethod("link", 2, &HybridShadowRegistry::link); prototype.registerRawHybridMethod("unlink", 1, &HybridShadowRegistry::unlink); - prototype.registerRawHybridMethod("selectVariants", 1, &HybridShadowRegistry::selectVariants); prototype.registerRawHybridMethod("setScopedTheme", 1, &HybridShadowRegistry::setScopedTheme); prototype.registerRawHybridMethod("getScopedTheme", 0, &HybridShadowRegistry::getScopedTheme); - prototype.registerRawHybridMethod("getVariants", 0, &HybridShadowRegistry::getVariants); }); }; diff --git a/cxx/hybridObjects/HybridStyleSheet.cpp b/cxx/hybridObjects/HybridStyleSheet.cpp index 814545fe..37823bc9 100644 --- a/cxx/hybridObjects/HybridStyleSheet.cpp +++ b/cxx/hybridObjects/HybridStyleSheet.cpp @@ -36,10 +36,7 @@ jsi::Value HybridStyleSheet::create(jsi::Runtime& rt, const jsi::Value &thisVal, parser.buildUnistyles(rt, registeredStyleSheet); parser.parseUnistyles(rt, registeredStyleSheet); - auto style = std::make_shared(registeredStyleSheet, this->_unistylesRuntime); - auto styleHostObject = jsi::Object::createFromHostObject(rt, style); - - return styleHostObject; + return core::toRNStyle(rt, registeredStyleSheet, this->_unistylesRuntime, {}); } jsi::Value HybridStyleSheet::configure(jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) { diff --git a/cxx/hybridObjects/HybridStyleSheet.h b/cxx/hybridObjects/HybridStyleSheet.h index 9124e84b..3c358dc0 100644 --- a/cxx/hybridObjects/HybridStyleSheet.h +++ b/cxx/hybridObjects/HybridStyleSheet.h @@ -4,7 +4,7 @@ #include #include "HybridUnistylesRuntime.h" #include "HybridUnistylesStyleSheetSpec.hpp" -#include "HostStyle.h" +#include "RNStyle.h" #include "Helpers.h" #include "Constants.h" #include "Breakpoints.h" diff --git a/cxx/parser/Parser.cpp b/cxx/parser/Parser.cpp index c068ed0e..919315d8 100644 --- a/cxx/parser/Parser.cpp +++ b/cxx/parser/Parser.cpp @@ -18,6 +18,7 @@ void parser::Parser::buildUnistyles(jsi::Runtime& rt, std::shared_ptrunistyles[styleKey] = std::make_shared( + helpers::HashGenerator::generateHash(styleKey), UnistyleType::DynamicFunction, styleKey, styleValue, @@ -28,6 +29,7 @@ void parser::Parser::buildUnistyles(jsi::Runtime& rt, std::shared_ptrunistyles[styleKey] = std::make_shared( + helpers::HashGenerator::generateHash(styleKey), UnistyleType::Object, styleKey, styleValue, @@ -301,7 +303,7 @@ void parser::Parser::rebuildUnistylesInDependencyMap(jsi::Runtime& rt, Dependenc ); } else { unistyle->rawValue = parsedStyleSheetsWithDefaultTheme[unistyleStyleSheet].asObject(rt).getProperty(rt, unistyle->styleKey.c_str()).asObject(rt); - this->rebuildUnistyle(rt, unistyleStyleSheet, unistyle, unistyleData->variants, unistyleData->dynamicFunctionMetadata); + this->rebuildUnistyle(rt, unistyle, unistyleData->variants, unistyleData->dynamicFunctionMetadata); unistyleData->parsedStyle = jsi::Value(rt, unistyle->parsedStyle.value()).asObject(rt); } @@ -323,7 +325,7 @@ void parser::Parser::rebuildUnistylesInDependencyMap(jsi::Runtime& rt, Dependenc } // rebuild single unistyle -void parser::Parser::rebuildUnistyle(jsi::Runtime& rt, std::shared_ptr styleSheet, Unistyle::Shared unistyle, const Variants& variants, std::optional> metadata) { +void parser::Parser::rebuildUnistyle(jsi::Runtime& rt, Unistyle::Shared unistyle, const Variants& variants, std::optional> metadata) { if (unistyle->type == core::UnistyleType::Object) { auto result = this->parseFirstLevel(rt, unistyle, variants); @@ -508,36 +510,22 @@ jsi::Function parser::Parser::createDynamicFunctionProxy(jsi::Runtime& rt, Unist // memoize metadata to call it later auto unistyleFn = std::dynamic_pointer_cast(unistyle); - auto& registry = core::UnistylesRegistry::get(); unistyleFn->unprocessedValue = jsi::Value(rt, result).asObject(rt); - jsi::Value rawVariants = thisObject.hasProperty(rt, helpers::STYLE_VARIANTS.c_str()) - ? thisObject.getProperty(rt, helpers::STYLE_VARIANTS.c_str()) - : jsi::Value::undefined(); - std::optional variants = rawVariants.isUndefined() - ? registry.getScopedVariants() - : std::optional(helpers::variantsToPairs(rt, rawVariants.asObject(rt))); + jsi::Value rawVariants = thisObject.hasProperty(rt, helpers::STYLESHEET_VARIANTS.c_str()) + ? thisObject.getProperty(rt, helpers::STYLESHEET_VARIANTS.c_str()) + : jsi::Object(rt); + + Variants variants = helpers::variantsToPairs(rt, rawVariants.asObject(rt)); unistyleFn->parsedStyle = this->parseFirstLevel(rt, unistyleFn, variants); unistyleFn->seal(); - jsi::Object style = jsi::Value(rt, unistyleFn->parsedStyle.value()).asObject(rt); - - // include dependencies for createUnistylesComponent - style.setProperty(rt, "__proto__", generateUnistylesPrototype(rt, unistylesRuntime, unistyle, variants, helpers::functionArgumentsToArray(rt, args, count))); - - jsi::Object secrets = jsi::Object(rt); - - secrets.setProperty(rt, helpers::ARGUMENTS.c_str(), helpers::functionArgumentsToArray(rt, args, count)); - - helpers::defineHiddenProperty(rt, style, helpers::SECRETS.c_str(), secrets); - - auto wrappedUnistyle = std::make_shared(unistyle); - - style.setNativeState(rt, std::move(wrappedUnistyle)); + // for compatibility purpose save last arguments to style instance. It will work ok, if user sees warning about multiple unistyles + helpers::defineHiddenProperty(rt, thisObject, helpers::ARGUMENTS.c_str() + std::string("_") + unistyleFn->styleKey, helpers::functionArgumentsToArray(rt, args, count)); - return style; + return core::objectFromUnistyle(rt, unistylesRuntime, unistyle, variants, std::make_optional(helpers::functionArgumentsToArray(rt, args, count))).asObject(rt); }); } diff --git a/cxx/parser/Parser.h b/cxx/parser/Parser.h index 94f1e211..2f927f52 100644 --- a/cxx/parser/Parser.h +++ b/cxx/parser/Parser.h @@ -10,6 +10,7 @@ #include "HybridUnistylesRuntime.h" #include "StyleSheet.h" #include "ShadowLeafUpdate.h" +#include "HashGenerator.h" namespace margelo::nitro::unistyles::parser { @@ -27,7 +28,7 @@ struct Parser { void rebuildUnistylesInDependencyMap(jsi::Runtime& rt, core::DependencyMap& dependencyMap, std::vector>& styleSheets, std::optional maybeMiniRuntime); void rebuildShadowLeafUpdates(jsi::Runtime& rt, core::DependencyMap& dependencyMap); folly::dynamic parseStylesToShadowTreeStyles(jsi::Runtime& rt, const std::vector>& unistyles); - void rebuildUnistyle(jsi::Runtime& rt, std::shared_ptr styleSheet, Unistyle::Shared unistyle, const Variants& variants, std::optional>); + void rebuildUnistyle(jsi::Runtime& rt, Unistyle::Shared unistyle, const Variants& variants, std::optional>); void rebuildUnistyleWithScopedTheme(jsi::Runtime& rt, jsi::Value& jsScopedTheme, std::shared_ptr unistyleData); jsi::Value getParsedStyleSheetForScopedTheme(jsi::Runtime& rt, core::Unistyle::Shared unistyle, std::string& scopedTheme); diff --git a/example/App2.tsx b/example/App2.tsx new file mode 100644 index 00000000..2c980a91 --- /dev/null +++ b/example/App2.tsx @@ -0,0 +1,92 @@ +import { Button, ScrollView, Text as RNText, View, ViewProps } from 'react-native' +import { withUnistyles } from 'react-native-unistyles' +import React, { PropsWithChildren, useState } from 'react' +import { st } from './st' +import './unistyles' + +type TextProps = PropsWithChildren & { + value: number, + size: 'small' | 'large' +} + +const Text: React.FunctionComponent = ({ value, children, size }) => { + styles.useVariants({ + size + }) + + return ( + + {children} + + ) +} + +const ComponentA: React.FunctionComponent = ({ style }) => { + return ( + + ) +} + +const ComponentB: React.FunctionComponent = ({ style }) => { + return ( + + ) +} + +const UniScrollView = withUnistyles(ScrollView) + +export const App = () => { + const [counter, setCounter] = useState(0) + + console.log(styles.container) + + return ( + + + + Hello world 1 + + + Hello world 2 + + + Hello world 3 + + +