Use BorderlessButton on iOS and animate the opacity

This commit is contained in:
Brent Vatne 2018-10-31 15:09:03 -07:00
parent d08d584c73
commit 9b006bc8d3
2 changed files with 74 additions and 5 deletions

View File

@ -0,0 +1,53 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Animated, Platform } from 'react-native';
import { BaseButton } from 'react-native-gesture-handler';
const AnimatedBaseButton = Animated.createAnimatedComponent(BaseButton);
export default class BorderlessButton extends React.Component {
static propTypes = {
...BaseButton.propTypes,
borderless: PropTypes.bool,
};
static defaultProps = {
activeOpacity: 0.3,
borderless: true,
};
constructor(props) {
super(props);
this._opacity = new Animated.Value(1);
}
_onActiveStateChange = active => {
if (Platform.OS !== 'android') {
Animated.spring(this._opacity, {
stiffness: 1000,
damping: 500,
mass: 3,
overshootClamping: true,
restDisplacementThreshold: 0.01,
restSpeedThreshold: 0.01,
toValue: active ? this.props.activeOpacity : 1,
}).start();
}
this.props.onActiveStateChange && this.props.onActiveStateChange(active);
};
render() {
const { children, style, ...rest } = this.props;
return (
<AnimatedBaseButton
{...rest}
onActiveStateChange={this._onActiveStateChange}
style={[style, Platform.OS === 'ios' && { opacity: this._opacity }]}
>
{children}
</AnimatedBaseButton>
);
}
}

View File

@ -15,6 +15,8 @@ import {
View,
} from 'react-native';
import BorderlessButton from './BorderlessButton';
const ANDROID_VERSION_LOLLIPOP = 21;
export default class TouchableItem extends React.Component {
@ -46,13 +48,27 @@ export default class TouchableItem extends React.Component {
this.props.borderless
)}
>
<View style={style}>{React.Children.only(this.props.children)}</View>
<View style={[style, { backgroundColor: '#fff' }]}>
{React.Children.only(this.props.children)}
</View>
</TouchableNativeFeedback>
);
}
} else if (Platform.OS === 'ios') {
return (
<TouchableOpacity {...this.props}>{this.props.children}</TouchableOpacity>
<BorderlessButton
hitSlop={{ top: 10, bottom: 10, right: 10, left: 10 }}
disallowInterruption
{...this.props}
>
{this.props.children}
</BorderlessButton>
);
} else {
return (
<TouchableOpacity {...this.props}>
{this.props.children}
</TouchableOpacity>
);
}
}
}