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
Show file tree
Hide file tree
Changes from 4 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
11 changes: 8 additions & 3 deletions src/hooks/useRestyle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useMemo} from 'react';
import {StyleProp, ViewStyle, TextStyle, ImageStyle} from 'react-native';
import {StyleProp} from 'react-native';

import {BaseTheme, RNStyle, Dimensions} from '../types';
import {getKeys} from '../typeHelpers';
Expand Down Expand Up @@ -53,7 +53,7 @@ const useRestyle = <
theme: Theme;
dimensions: Dimensions;
},
) => ViewStyle | TextStyle | ImageStyle;
) => RNStyle;
properties: (keyof TProps)[];
propertiesMap: Record<keyof TProps, boolean>;
},
Expand All @@ -73,7 +73,12 @@ const useRestyle = <
dimensions,
});

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?

}

return [style, styleProp].filter(Boolean);
// We disable the exhaustive deps rule here in order to trigger the useMemo
// when the serialized string of restyleProps changes instead of the object
// reference which will change on every render.
Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ImageStyle, TextStyle, ViewStyle} from 'react-native';
import {ImageStyle, TextStyle, ViewStyle, StyleProp} from 'react-native';

export type AtLeastOneResponsiveValue<
Value,
Expand Down Expand Up @@ -70,7 +70,11 @@ export type RestyleFunction<
[key in S]?: any;
};

export type RNStyle = ViewStyle | TextStyle | ImageStyle;
export type RNStyle =
| ViewStyle
| TextStyle
| ImageStyle
| ((...args: any[]) => StyleProp<ViewStyle>);

export type RNStyleProperty =
| keyof ViewStyle
Expand Down