forked from vonovak/react-navigation-header-buttons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchableItem.js
61 lines (58 loc) · 1.57 KB
/
TouchableItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as React from 'react';
import {
Platform,
TouchableNativeFeedback,
TouchableOpacity,
View,
type ViewProps,
} from 'react-native';
type Props = {
...ViewProps,
pressColor?: string,
disabled?: boolean,
borderless?: boolean,
delayPressIn?: number,
onPress?: () => void,
children: React.Node,
};
const ANDROID_VERSION_LOLLIPOP = 21;
const CAN_USE_RIPLLE = Platform.OS === 'android' && Platform.Version >= ANDROID_VERSION_LOLLIPOP;
export default function TouchableItem({
borderless = false,
pressColor = 'rgba(0, 0, 0, .32)',
rippleRadius,
style,
children,
...rest
}: Props) {
const background = React.useMemo(() => {
return CAN_USE_RIPLLE
? TouchableNativeFeedback.Ripple(pressColor, borderless, rippleRadius)
: undefined;
}, [pressColor, borderless, rippleRadius]);
/*
* TouchableNativeFeedback.Ripple causes a crash on old Android versions,
* therefore only enable it on Android Lollipop and above.
*
* All touchables on Android should have the ripple effect according to
* platform design guidelines.
* We need to pass the background prop to specify a borderless ripple effect.
*/
if (CAN_USE_RIPLLE) {
return (
<TouchableNativeFeedback
{...rest}
useForeground={TouchableNativeFeedback.canUseNativeForeground()}
background={background}
>
<View style={style}>{React.Children.only(children)}</View>
</TouchableNativeFeedback>
);
} else {
return (
<TouchableOpacity style={style} {...rest}>
{children}
</TouchableOpacity>
);
}
}