UNPKG

react-native-pull-ups

Version:

Native Bottom Sheet Implementations for iOS and Android. Toddler approved.

190 lines (188 loc) 6.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); var _types = require("./types"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } 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); } /* Props for Native Android component. * Not to be confused with the main component. */ const NativePullUp = (0, _reactNative.requireNativeComponent)('RNPullUpView'); const styles = _reactNative.StyleSheet.create({ primary: { position: 'absolute', bottom: 0, width: '100%', justifyContent: 'flex-end' }, sheet: { flex: 1, backgroundColor: 'white' }, modal: { position: 'absolute' }, overlay: { flex: 1, backgroundColor: _types.PullUpDefaultProps.overlayColor } }); const PullUpBase = props => { const { collapsedHeight, maxSheetWidth, onStateChanged, style, children } = props; const onNativeStateChanged = (0, _react.useCallback)(evt => { onStateChanged?.(evt.nativeEvent.state); }, [onStateChanged]); const maxWidthStyle = typeof maxSheetWidth === 'number' && maxSheetWidth > 0 ? { maxWidth: maxSheetWidth } : null; return /*#__PURE__*/_react.default.createElement(NativePullUp, _extends({}, props, { style: styles.primary, collapsedHeight: collapsedHeight || 0, maxSheetWidth: maxSheetWidth || 0, onStateChanged: onNativeStateChanged }), /*#__PURE__*/_react.default.createElement(_reactNative.View, { collapsable: false, style: [styles.sheet, style, maxWidthStyle] }, children)); }; class PullUpModal extends _react.default.Component { constructor(props) { super(props); this.opacity = new _reactNative.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' }); _reactNative.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 })); _reactNative.Animated.timing(this.opacity, { toValue: this.props.overlayOpacity || _types.PullUpDefaultProps.overlayOpacity, 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.default.createElement(_reactNative.Modal, { transparent: true, onRequestClose: this._onRequestClose }, /*#__PURE__*/_react.default.createElement(_reactNative.TouchableWithoutFeedback, { onPress: this._onPressOverlay }, /*#__PURE__*/_react.default.createElement(_reactNative.Animated.View, { style: [styles.overlay, { opacity: this.opacity, backgroundColor: overlayColor }] })), /*#__PURE__*/_react.default.createElement(PullUpBase, _extends({}, this.props, { state: destroyed || animating === 'out' ? 'hidden' : state, onStateChanged: this._interceptOnStateChanged, hideable: hideable && dismissable }))); } } const PullUp = props => props.modal ? /*#__PURE__*/_react.default.createElement(PullUpModal, props) : /*#__PURE__*/_react.default.createElement(PullUpBase, props); PullUp.propTypes = _types.PullUpPropTypes; PullUp.defaultProps = _types.PullUpDefaultProps; var _default = exports.default = PullUp; //# sourceMappingURL=index.android.js.map