Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove babel pressable style modifiction #420

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions cxx/core/UnistylesRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,19 @@ std::vector<std::shared_ptr<core::StyleSheet>> core::UnistylesRegistry::getStyle

return stylesheetsToRefresh;
}

const core::Variants& core::UnistylesRegistry::getScopedVariants() {
return this->_scopedVariants;
}

const std::optional<std::string> core::UnistylesRegistry::getScopedTheme() {
return this->_scopedTheme;
}

void core::UnistylesRegistry::setScopedVariants(core::Variants&& variants) {
this->_scopedVariants = std::move(variants);
}

void core::UnistylesRegistry::setScopedTheme(std::optional<std::string> themeName) {
this->_scopedTheme = std::move(themeName);
}
8 changes: 7 additions & 1 deletion cxx/core/UnistylesRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ struct UnistylesRegistry: public StyleSheetRegistry {
DependencyMap buildDependencyMap(jsi::Runtime& rt, std::vector<UnistyleDependency>& deps);
void shadowLeafUpdateFromUnistyle(jsi::Runtime& rt, Unistyle::Shared unistyle, jsi::Value& maybePressableId);
shadow::ShadowTrafficController trafficController{};
const core::Variants& getScopedVariants();
const std::optional<std::string> getScopedTheme();
void setScopedVariants(core::Variants&& variants);
void setScopedTheme(std::optional<std::string> themeName);

private:
UnistylesRegistry() = default;


core::Variants _scopedVariants{};
std::optional<std::string> _scopedTheme{};
std::unordered_map<jsi::Runtime*, UnistylesState> _states{};
std::unordered_map<jsi::Runtime*, std::unordered_map<int, std::shared_ptr<core::StyleSheet>>> _styleSheetRegistry{};
std::unordered_map<jsi::Runtime*, std::unordered_map<const ShadowNodeFamily*, std::vector<const std::shared_ptr<UnistyleData>>>> _shadowRegistry{};
Expand Down
34 changes: 22 additions & 12 deletions cxx/hybridObjects/HybridShadowRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa

arguments.push_back({});
}

auto scopedTheme = registry.getScopedTheme();

// check if scope theme exists
if (this->_scopedTheme.has_value()) {
auto themeName = this->_scopedTheme.value();
if (scopedTheme.has_value()) {
auto themeName = scopedTheme.value();

helpers::assertThat(rt, registry.getState(rt).hasTheme(themeName), "Unistyles: You're trying to use scoped theme '" + themeName + "' but it wasn't registered.");
}
Expand All @@ -47,15 +49,15 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa
core::Unistyle::Shared& unistyle = unistyleWrappers[i];
std::shared_ptr<core::UnistyleData> unistyleData = std::make_shared<core::UnistyleData>(
unistyle,
this->_scopedVariants,
registry.getScopedVariants(),
arguments[i],
this->_scopedTheme
scopedTheme
);

// before linking we need to check if given unistyle is affected by scoped theme
if (this->_scopedTheme.has_value()) {
if (scopedTheme.has_value()) {
if (parsedStyleSheet.isUndefined()) {
parsedStyleSheet = parser.getParsedStyleSheetForScopedTheme(rt, unistyle, this->_scopedTheme.value());
parsedStyleSheet = parser.getParsedStyleSheetForScopedTheme(rt, unistyle, scopedTheme.value());
}

// if so we need to force update
Expand Down Expand Up @@ -91,34 +93,42 @@ jsi::Value HybridShadowRegistry::unlink(jsi::Runtime &rt, const jsi::Value &this

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()) {
this->_scopedVariants = {};
registry.setScopedVariants({});
}

if (args[0].isObject()) {
this->_scopedVariants = helpers::variantsToPairs(rt, args[0].asObject(rt));
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()) {
this->_scopedTheme = std::nullopt;

registry.setScopedTheme(std::nullopt);
}

if (args[0].isString()) {
this->_scopedTheme = args[0].asString(rt).utf8(rt);
registry.setScopedTheme(args[0].asString(rt).utf8(rt));
}

return jsi::Value::undefined();
}

jsi::Value HybridShadowRegistry::getScopedTheme(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return this->_scopedTheme.has_value()
? jsi::String::createFromUtf8(rt, this->_scopedTheme.value())
auto& registry = core::UnistylesRegistry::get();
auto maybeScopedTheme = registry.getScopedTheme();

return maybeScopedTheme.has_value()
? jsi::String::createFromUtf8(rt, maybeScopedTheme.value())
: jsi::Value::undefined();
}
2 changes: 0 additions & 2 deletions cxx/hybridObjects/HybridShadowRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ struct HybridShadowRegistry: public HybridUnistylesShadowRegistrySpec {
};

private:
core::Variants _scopedVariants{};
std::optional<std::string> _scopedTheme{};
std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
};

Expand Down
4 changes: 2 additions & 2 deletions cxx/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,20 +501,20 @@ jsi::Function parser::Parser::createDynamicFunctionProxy(jsi::Runtime& rt, Unist
? thisVal.asObject(rt)
: jsi::Object(rt);
auto parser = parser::Parser(unistylesRuntime);

// call user function
auto result = unistyle->rawValue.asFunction(rt).call(rt, args, count);

// memoize metadata to call it later
auto unistyleFn = std::dynamic_pointer_cast<UnistyleDynamicFunction>(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> variants = rawVariants.isUndefined()
? std::nullopt
? registry.getScopedVariants()
: std::optional<Variants>(helpers::variantsToPairs(rt, rawVariants.asObject(rt)));

unistyleFn->parsedStyle = this->parseFirstLevel(rt, unistyleFn, variants);
Expand Down
4 changes: 2 additions & 2 deletions example/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Pressable, PressableProps, Text } from "react-native";
import { Pressable, PressableProps, Text } from 'react-native'
import { StyleSheet, UnistylesVariants } from 'react-native-unistyles'

type ButtonProps = PressableProps & UnistylesVariants<typeof styles> & {
Expand All @@ -19,7 +19,7 @@ export const Button: React.FunctionComponent<ButtonProps> = ({
});

return (
<Pressable style={() => styles.button} {...props}>
<Pressable style={styles.button} {...props}>
<Text style={styles.text}>{children}</Text>
</Pressable>
);
Expand Down
Loading
Loading