forked from vonovak/react-navigation-header-buttons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTouchableItem.ios.js
61 lines (52 loc) · 1.58 KB
/
TouchableItem.ios.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 { Animated, Platform, type ViewProps } from 'react-native';
import { BaseButton } from 'react-native-gesture-handler';
const AnimatedBaseButton = Animated.createAnimatedComponent(BaseButton);
export type Props = {
...ViewProps,
disabled?: boolean,
delayPressIn?: number,
onPress?: () => void,
children: React.Node,
activeOpacity: number,
...
};
const useNativeDriver = Platform.OS !== 'web';
export default class TouchableItem extends React.Component<Props> {
static defaultProps = {
activeOpacity: 0.3,
borderless: true,
disabled: false,
};
opacity = new Animated.Value(1);
handleActiveStateChange = (active: boolean) => {
Animated.spring(this.opacity, {
stiffness: 1000,
damping: 500,
mass: 3,
overshootClamping: true,
restDisplacementThreshold: 0.01,
restSpeedThreshold: 0.01,
toValue: active ? this.props.activeOpacity : 1,
useNativeDriver,
}).start();
this.props.onActiveStateChange?.(active);
};
render() {
const { children, style, disabled, ...rest } = this.props;
const enabled = !disabled;
return (
<AnimatedBaseButton
{...rest}
enabled={enabled}
// the component is controlled by "enabled" prop
// but should probably accept "disabled" (a bug?) so we're passing it as well for future compatibility
disabled={disabled}
onActiveStateChange={this.handleActiveStateChange}
style={[style, enabled && { opacity: this.opacity }]}
>
{children}
</AnimatedBaseButton>
);
}
}