-
-
Notifications
You must be signed in to change notification settings - Fork 591
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
GenericPressable
prop to customize the component wrapping int…
…eractive views Also, drop `Pressable` in favor of `TouchableNativeFeedback` and `TouchableHighlight` for RN retro-compatibility. Fix #472
- Loading branch information
Showing
2 changed files
with
54 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,51 @@ | ||
import React, { PropsWithChildren } from 'react'; | ||
import { | ||
StyleSheet, | ||
Pressable, | ||
PressableProps, | ||
StyleProp, | ||
Platform | ||
Platform, | ||
TouchableHighlight, | ||
TouchableNativeFeedback, | ||
View | ||
} from 'react-native'; | ||
import { DEFAULT_PRESSABLE_RIPPLE_COLOR } from './constants'; | ||
|
||
const styles = StyleSheet.create({ | ||
pressed: { | ||
opacity: Platform.select({ default: 0.8 }) | ||
} | ||
}); | ||
|
||
export interface GenericPressableProps extends PressableProps { | ||
style?: StyleProp<any>; | ||
borderless?: boolean; | ||
} | ||
import { useSharedProps } from './context/SharedPropsProvider'; | ||
import { GenericPressableProps } from './shared-types'; | ||
|
||
export default function GenericPressable({ | ||
onPress, | ||
style, | ||
children, | ||
hitSlop, | ||
borderless = false, | ||
...otherProps | ||
}: PropsWithChildren<GenericPressableProps>) { | ||
const { | ||
pressableHightlightColor = DEFAULT_PRESSABLE_RIPPLE_COLOR, | ||
GenericPressable: UserProvidedPressable | ||
} = useSharedProps(); | ||
if (UserProvidedPressable) { | ||
return ( | ||
<UserProvidedPressable style={style} {...(otherProps as any)}> | ||
{children} | ||
</UserProvidedPressable> | ||
); | ||
} | ||
if (Platform.OS === 'android') { | ||
return ( | ||
<TouchableNativeFeedback | ||
useForeground | ||
background={TouchableNativeFeedback.Ripple( | ||
pressableHightlightColor, | ||
borderless | ||
)} | ||
style={style} | ||
{...(otherProps as any)}> | ||
<View>{children}</View> | ||
</TouchableNativeFeedback> | ||
); | ||
} | ||
return ( | ||
<Pressable | ||
android_ripple={{ borderless, color: DEFAULT_PRESSABLE_RIPPLE_COLOR }} | ||
style={({ pressed }) => [style, pressed && styles.pressed]} | ||
onPress={onPress} | ||
hitSlop={hitSlop} | ||
{...otherProps}> | ||
{children} | ||
</Pressable> | ||
<TouchableHighlight | ||
underlayColor={pressableHightlightColor} | ||
style={style} | ||
{...(otherProps as any)}> | ||
<View>{children}</View> | ||
</TouchableHighlight> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters