react-native-animated-overlay
Version:
React Native Animated Overlay support IOS & Android.
111 lines (95 loc) • 2.46 kB
JavaScript
// @flow
import React, { Component } from 'react';
import { StyleSheet, Dimensions, TouchableOpacity, Animated } from 'react-native';
const styles = StyleSheet.create({
overlay: {
flex: 1,
top: 0,
left: 0,
position: 'absolute',
},
});
class AnimatedOverlay extends Component {
props: {
onPress?: () => void;
backgroundColor?: string;
opacity?: number;
duration?: number;
overlayShow?: boolean;
pointerEvents?: string;
initValue?: number;
onAnimationFinished?: (value: number) => void;
style?: any;
useNativeDriver: boolean;
children?: any;
}
static defaultProps = {
onPress: () => {},
pointerEvents: null,
backgroundColor: '#000',
opacity: 0.5,
duration: 200,
overlayShow: false,
initValue: 0,
onAnimationFinished: () => {},
style: null,
useNativeDriver: false,
children: null,
}
constructor(props) {
super(props);
this.state = {
opacity: new Animated.Value(props.initValue),
overlayShow: props.overlayShow,
};
}
componentDidMount() {
this.doAnimation();
}
componentWillReceiveProps(nextProps) {
if (this.props.overlayShow !== nextProps.overlayShow) {
this.setState({ overlayShow: nextProps.overlayShow });
}
}
componentDidUpdate() {
this.doAnimation();
}
doAnimation() {
const {
overlayShow,
opacity,
duration,
onAnimationFinished,
useNativeDriver,
} = this.props;
const toValue = overlayShow ? opacity : 0;
Animated.timing(this.state.opacity, {
toValue,
duration,
useNativeDriver,
}).start(() => {
onAnimationFinished(toValue);
});
}
render() {
let { pointerEvents } = this.props;
const { onPress, style, children } = this.props;
const backgroundColor = { backgroundColor: this.props.backgroundColor };
const opacity = { opacity: this.state.opacity };
const size = {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
};
if (!pointerEvents) pointerEvents = this.state.overlayShow ? 'auto' : 'none';
return (
<Animated.View
pointerEvents={pointerEvents}
style={[styles.overlay, backgroundColor, size, style, opacity]}
>
<TouchableOpacity onPress={onPress} style={[styles.overlay, size]} />
{children}
</Animated.View>
);
}
}
export default AnimatedOverlay;