react-native-pull-ups
Version:
Native Bottom Sheet Implementations for iOS and Android. Toddler approved.
190 lines (188 loc) • 5.11 kB
JavaScript
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { useCallback } from 'react';
import { StyleSheet, TouchableWithoutFeedback, View, requireNativeComponent, Modal, Animated } from 'react-native';
/* Props for Native Android component.
* Not to be confused with the main component. */
const NativePullUp = requireNativeComponent('RNPullUpView');
const styles = StyleSheet.create({
primary: {
position: 'absolute',
bottom: 0,
width: '100%',
justifyContent: 'flex-end'
},
sheet: {
flex: 1,
backgroundColor: 'white'
},
modal: {
position: 'absolute'
},
overlay: {
flex: 1,
backgroundColor: 'black'
}
});
const PullUpBase = props => {
const {
collapsedHeight,
maxSheetWidth,
hideable = true,
onStateChanged,
style,
children,
...rest
} = props;
const onNativeStateChanged = useCallback(evt => {
onStateChanged?.(evt.nativeEvent.state);
}, [onStateChanged]);
const maxWidthStyle = typeof maxSheetWidth === 'number' && maxSheetWidth > 0 ? {
maxWidth: maxSheetWidth
} : null;
return /*#__PURE__*/React.createElement(NativePullUp, _extends({}, rest, {
hideable: hideable,
style: styles.primary,
collapsedHeight: collapsedHeight || 0,
maxSheetWidth: maxSheetWidth || 0,
onStateChanged: onNativeStateChanged
}), /*#__PURE__*/React.createElement(View, {
collapsable: false,
style: [styles.sheet, style, maxWidthStyle]
}, children));
};
class PullUpModal extends React.Component {
static defaultProps = {
hideable: true,
dismissable: true,
tapToDismissModal: true,
overlayColor: 'black',
overlayOpacity: 0.5
};
constructor(props) {
super(props);
this.opacity = new Animated.Value(0);
this.state = {
destroyed: true,
animating: false
};
}
componentDidMount() {
const {
state
} = this.props;
if (state !== 'hidden') this._animateIn();
}
componentDidUpdate(prevProps) {
const {
state
} = this.props;
const {
animating,
destroyed
} = this.state;
if (animating || state === prevProps.state) return;
if (state === 'hidden' && !destroyed) this._animateOut();
if (state !== 'hidden' && destroyed) this._animateIn();
}
_animateOut = () => {
if (this.state.animating === 'out') return;
this.setState({
animating: 'out'
});
Animated.timing(this.opacity, {
toValue: 0,
duration: 250,
useNativeDriver: true
}).start(({
finished
}) => {
if (finished) this.setState({
destroyed: true,
animating: false
});
});
};
_animateIn = () => {
if (this.state.animating === 'in') return;
this.setState({
animating: 'in'
});
setTimeout(() => this.setState({
destroyed: false
}));
Animated.timing(this.opacity, {
toValue: this.props.overlayOpacity ?? 0.5,
duration: 250,
useNativeDriver: true
}).start(({
finished
}) => {
if (finished) this.setState({
animating: false
});
});
};
_onRequestClose = () => {
const {
dismissable
} = this.props;
if (dismissable) {
this._animateOut();
}
};
_onPressOverlay = () => {
const {
dismissable,
tapToDismissModal
} = this.props;
if (dismissable && tapToDismissModal) {
this._animateOut();
}
};
_interceptOnStateChanged = newState => {
const {
animating
} = this.state;
const {
onStateChanged
} = this.props;
if (newState === 'hidden') {
if (animating === 'in') return;
if (!animating) {
this._animateOut();
}
}
onStateChanged?.(newState);
};
render() {
const {
state,
hideable,
dismissable,
overlayColor
} = this.props;
const {
destroyed,
animating
} = this.state;
if (destroyed && !animating) return null;
return /*#__PURE__*/React.createElement(Modal, {
transparent: true,
onRequestClose: this._onRequestClose
}, /*#__PURE__*/React.createElement(TouchableWithoutFeedback, {
onPress: this._onPressOverlay
}, /*#__PURE__*/React.createElement(Animated.View, {
style: [styles.overlay, {
opacity: this.opacity,
backgroundColor: overlayColor
}]
})), /*#__PURE__*/React.createElement(PullUpBase, _extends({}, this.props, {
state: destroyed || animating === 'out' ? 'hidden' : state,
onStateChanged: this._interceptOnStateChanged,
hideable: hideable && dismissable
})));
}
}
const PullUp = props => props.modal ? /*#__PURE__*/React.createElement(PullUpModal, props) : /*#__PURE__*/React.createElement(PullUpBase, props);
export default PullUp;
//# sourceMappingURL=index.android.js.map