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

Handle Pressable function style prop #138

Merged
merged 5 commits into from
Apr 11, 2022
Merged

Conversation

codybrouwers
Copy link
Contributor

Currently when using the style prop along with Restyle provided props, only style objects are accepted but the Pressable component also accepts a function in order to access the pressed state.

This PR detects if the style prop is a function and if so, returns another function that merges the Restyle generated styles and the style prop styles.

I just threw together this fix in a few minutes so maybe there is some edge cases I haven't thought of, feel free to disregard if the problem is actually a lot harder then my small fix. It does work locally for me at least.

Thanks!

Should resolve #58

@ghost ghost added the cla-needed label Mar 15, 2022
@sbalay
Copy link
Contributor

sbalay commented Mar 25, 2022

Hey @codybrouwers, thanks for looking into this!

The solution looks fine but it currently throws some typescript errors because the style prop is typed as StyleProp<ViewStyle | TextStyle | ImageStyle>, which doesn't support functions.

Could you have a look at fixing that?

@codybrouwers
Copy link
Contributor Author

@sbalay sure thing, will take a look this weekend!

@codybrouwers
Copy link
Contributor Author

@sbalay Made some adjustments and got all of the types working, let me know what you think!

@sbalay
Copy link
Contributor

sbalay commented Apr 4, 2022

Thanks @codybrouwers! I'll have a look during this week.

return [style, props.style].filter(Boolean);
const styleProp = props.style;
if (typeof styleProp === 'function') {
return (...args: any[]) => [style, styleProp(...args)];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is worth having the .filter(Boolean) on this returned array as well?

@sbalay sbalay merged commit 8633b58 into Shopify:master Apr 11, 2022
@shopify-shipit shopify-shipit bot temporarily deployed to production April 11, 2022 07:28 Inactive
@chj-damon
Copy link
Contributor

should we revert this PR in order to solve #159 ?

@desfero
Copy link

desfero commented Feb 10, 2023

Agree, @sbalay would that be possible to revert this change or rewrite in a way that doesn't break typesafety?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using Style function to override styling
5 participants