Skip to content

Commit

Permalink
fix: use theme in PlatformPressable
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Mar 12, 2021
1 parent 24b3f73 commit 40439cc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
15 changes: 5 additions & 10 deletions packages/elements/src/Header/HeaderBackButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export default function HeaderBackButton({
labelVisible = Platform.OS === 'ios',
onLabelLayout,
onPress,
pressColorAndroid: customPressColorAndroid,
pressColor,
pressOpacity,
screenLayout,
tintColor: customTintColor,
titleLayout,
Expand All @@ -31,7 +32,7 @@ export default function HeaderBackButton({
testID,
style,
}: HeaderBackButtonProps) {
const { dark, colors } = useTheme();
const { colors } = useTheme();

const [initialLabelWidth, setInitialLabelWidth] = React.useState<
undefined | number
Expand All @@ -45,13 +46,6 @@ export default function HeaderBackButton({
default: colors.text,
});

const pressColorAndroid =
customPressColorAndroid !== undefined
? customPressColorAndroid
: dark
? 'rgba(255, 255, 255, .32)'
: 'rgba(0, 0, 0, .32)';

const handleLabelLayout = (e: LayoutChangeEvent) => {
onLabelLayout?.(e);

Expand Down Expand Up @@ -156,7 +150,8 @@ export default function HeaderBackButton({
accessibilityLabel={accessibilityLabel}
testID={testID}
onPress={disabled ? undefined : handlePress}
pressColor={pressColorAndroid}
pressColor={pressColor}
pressOpacity={pressOpacity}
android_ripple={{ borderless: true }}
style={[styles.container, disabled && styles.disabled, style]}
hitSlop={Platform.select({
Expand Down
21 changes: 14 additions & 7 deletions packages/elements/src/PlatformPressable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { Platform, Pressable, PressableProps } from 'react-native';
import { useTheme } from '@react-navigation/native';

export type Props = PressableProps & {
pressColor?: string;
Expand All @@ -12,24 +13,30 @@ const ANDROID_SUPPORTS_RIPPLE =
Platform.OS === 'android' && Platform.Version >= ANDROID_VERSION_LOLLIPOP;

/**
* PlatformPressable provides an abstraction on top of TouchableNativeFeedback and
* TouchableOpacity to handle platform differences.
*
* On Android, you can pass the props of TouchableNativeFeedback.
* On other platforms, you can pass the props of TouchableOpacity.
* PlatformPressable provides an abstraction on top of Pressable to handle platform differences.
*/
export default function PlatformPressable({
android_ripple,
pressColor = 'rgba(0, 0, 0, .32)',
pressColor,
pressOpacity,
style,
...rest
}: Props) {
const { dark } = useTheme();

return (
<Pressable
android_ripple={
ANDROID_SUPPORTS_RIPPLE
? { color: pressColor, ...android_ripple }
? {
color:
pressColor !== undefined
? pressColor
: dark
? 'rgba(255, 255, 255, .32)'
: 'rgba(0, 0, 0, .32)',
...android_ripple,
}
: undefined
}
style={({ pressed }) => [
Expand Down
7 changes: 5 additions & 2 deletions packages/elements/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ export type HeaderBackButtonProps = {
disabled?: boolean;
/**
* Callback to call when the button is pressed.
* By default, this triggers `goBack`.
*/
onPress?: () => void;
/**
* Color for material ripple (Android >= 5.0 only).
*/
pressColorAndroid?: string;
pressColor?: string;
/**
* Opacity when the button is pressed, used when ripple is not supported.
*/
pressOpacity?: number;
/**
* Function which returns a React Element to display custom image in header's back button.
*/
Expand Down

0 comments on commit 40439cc

Please sign in to comment.