Skip to content

Commit

Permalink
Merge pull request #442 from jpudysz/feature/nested-variants
Browse files Browse the repository at this point in the history
feat: text nested variants
  • Loading branch information
jpudysz authored Dec 15, 2024
2 parents 61a7d24 + 214a44f commit da1f1a6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
9 changes: 9 additions & 0 deletions cxx/hybridObjects/HybridShadowRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,12 @@ jsi::Value HybridShadowRegistry::getScopedTheme(jsi::Runtime &rt, const jsi::Val
? 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();
}
13 changes: 9 additions & 4 deletions cxx/hybridObjects/HybridShadowRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,21 @@ 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();

registerHybrids(this, [](Prototype& prototype) {
prototype.registerRawHybridMethod("link", 2, &HybridShadowRegistry::link);
prototype.registerRawHybridMethod("unlink", 2, &HybridShadowRegistry::unlink);
prototype.registerRawHybridMethod("selectVariants", 2, &HybridShadowRegistry::selectVariants);
prototype.registerRawHybridMethod("setScopedTheme", 2, &HybridShadowRegistry::setScopedTheme);
prototype.registerRawHybridMethod("getScopedTheme", 2, &HybridShadowRegistry::getScopedTheme);
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);
});
};

Expand Down
3 changes: 2 additions & 1 deletion src/components/Variants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ const Apply: React.FunctionComponent<VariantProps> = ({ variants }) => {
return null
}
export const Variants: React.FunctionComponent<React.PropsWithChildren<VariantProps>> = ({ variants, children }) => {
const previousScopedVariants = UnistylesShadowRegistry.getVariants()
const mappedChildren = [
<Apply key='add' variants={variants} />,
children,
<Apply key='remove' />
<Apply key='remove' variants={previousScopedVariants} />
]

return (
Expand Down
2 changes: 0 additions & 2 deletions src/components/native/Pressable.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ..
? unistyles
: [unistyles]

UnistylesShadowRegistry.selectVariants(variants)
// @ts-expect-error - this is hidden from TS
UnistylesShadowRegistry.add(ref, styles)
UnistylesShadowRegistry.selectVariants(undefined)

storedRef.current = ref

Expand Down

0 comments on commit da1f1a6

Please sign in to comment.