forked from callstack/react-native-paper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchableRipple.js
149 lines (139 loc) · 3.41 KB
/
TouchableRipple.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* @flow */
import * as React from 'react';
import {
TouchableNativeFeedback,
TouchableHighlight,
TouchableWithoutFeedback,
Platform,
View,
} from 'react-native';
import color from 'color';
import { withTheme } from '../core/theming';
import type { Theme } from '../types';
const ANDROID_VERSION_LOLLIPOP = 21;
type Props = React.ElementConfig<typeof TouchableWithoutFeedback> & {|
/**
* Whether to render the ripple outside the view bounds.
*/
borderless?: boolean,
/**
* Type of background drawabale to display the feedback.
* https://facebook.github.io/react-native/docs/touchablenativefeedback.html#background
*/
background?: Object,
/**
* Whether to prevent interaction with the touchable.
*/
disabled?: boolean,
/**
* Function to execute on press. If not set, will cause the touchable to be disabled.
*/
onPress?: ?Function,
/**
* Color of the ripple effect.
*/
rippleColor?: string,
/**
* Color of the underlay for the highlight effect.
*/
underlayColor?: string,
/**
* Content of the `TouchableRipple`.
*/
children: React.Node,
style?: any,
/**
* @optional
*/
theme: Theme,
|};
/**
* A wrapper for views that should respond to touches.
* Provides a material "ink ripple" interaction effect for supported platforms (>= Android Lollipop).
* On unsupported platforms, it falls back to a highlight effect.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { View } from 'react-native';
* import { Text, TouchableRipple } from 'react-native-paper';
*
* const MyComponent = () => (
* <TouchableRipple
* onPress={() => console.log('Pressed')}
* rippleColor="rgba(0, 0, 0, .32)"
* >
* <Text>Press me</Text>
* </TouchableRipple>
* );
*
* export default MyComponent;
* ```
*/
class TouchableRipple extends React.Component<Props, void> {
static defaultProps = {
borderless: false,
};
/**
* Whether ripple effect is supported.
*/
static supported =
Platform.OS === 'android' && Platform.Version >= ANDROID_VERSION_LOLLIPOP;
render() {
const {
style,
background,
borderless,
disabled: disabledProp,
rippleColor,
underlayColor,
children,
theme,
...rest
} = this.props;
const { dark, colors } = theme;
const disabled = disabledProp || !this.props.onPress;
const calculatedRippleColor =
rippleColor ||
color(colors.text)
.alpha(dark ? 0.32 : 0.2)
.rgb()
.string();
if (TouchableRipple.supported) {
return (
<TouchableNativeFeedback
{...rest}
disabled={disabled}
background={
background != null
? background
: TouchableNativeFeedback.Ripple(
calculatedRippleColor,
borderless
)
}
>
<View style={style}>{React.Children.only(children)}</View>
</TouchableNativeFeedback>
);
}
return (
<TouchableHighlight
{...rest}
disabled={disabled}
style={style}
underlayColor={
underlayColor != null
? underlayColor
: color(calculatedRippleColor)
.fade(0.5)
.rgb()
.string()
}
>
{React.Children.only(children)}
</TouchableHighlight>
);
}
}
export default withTheme(TouchableRipple);