UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

149 lines (131 loc) 4.45 kB
import _extends from "@babel/runtime/helpers/extends"; import React, { forwardRef, useRef, useImperativeHandle } from 'react'; import { useIsomorphicLayoutEffect } from '../shared/use-isomorphic-layout-effect'; import { classNames, getExtraAttrs, emit } from '../shared/utils'; import { colorClasses } from '../shared/mixins'; import { zmpready, zmp } from '../shared/zmp'; import { watchProp } from '../shared/watch-prop'; import { modalStateClasses } from '../shared/modal-state-classes'; var Popup = /*#__PURE__*/forwardRef(function (props, ref) { var _zmpPopup = useRef(null); var className = props.className, id = props.id, style = props.style, children = props.children, tabletFullscreen = props.tabletFullscreen, push = props.push, opened = props.opened, closeByBackdropClick = props.closeByBackdropClick, backdrop = props.backdrop, backdropEl = props.backdropEl, animate = props.animate, closeOnEscape = props.closeOnEscape, _props$swipeToClose = props.swipeToClose, swipeToClose = _props$swipeToClose === void 0 ? false : _props$swipeToClose, swipeHandler = props.swipeHandler, containerEl = props.containerEl; var extraAttrs = getExtraAttrs(props); var elRef = useRef(null); var isOpened = useRef(opened); var isClosing = useRef(false); var onSwipeStart = function onSwipeStart(instance) { emit(props, 'popupSwipeStart', instance); }; var onSwipeMove = function onSwipeMove(instance) { emit(props, 'popupSwipeMove', instance); }; var onSwipeEnd = function onSwipeEnd(instance) { emit(props, 'popupSwipeEnd', instance); }; var onSwipeClose = function onSwipeClose(instance) { emit(props, 'popupSwipeClose', instance); }; var onOpen = function onOpen(instance) { isOpened.current = true; isClosing.current = false; emit(props, 'popupOpen', instance); }; var onOpened = function onOpened(instance) { emit(props, 'popupOpened', instance); }; var onClose = function onClose(instance) { isOpened.current = false; isClosing.current = true; emit(props, 'popupClose', instance); }; var onClosed = function onClosed(instance) { isClosing.current = false; emit(props, 'popupClosed', instance); }; useImperativeHandle(ref, function () { return { el: elRef.current, zmpPopup: function zmpPopup() { return _zmpPopup.current; } }; }); watchProp(opened, function (value) { if (!_zmpPopup.current) return; if (value) { _zmpPopup.current.open(); } else { _zmpPopup.current.close(); } }); var onMount = function onMount() { if (!elRef.current) return; var popupParams = { el: elRef.current, on: { swipeStart: onSwipeStart, swipeMove: onSwipeMove, swipeEnd: onSwipeEnd, swipeClose: onSwipeClose, open: onOpen, opened: onOpened, close: onClose, closed: onClosed } }; if ('closeByBackdropClick' in props) popupParams.closeByBackdropClick = closeByBackdropClick; if ('closeOnEscape' in props) popupParams.closeOnEscape = closeOnEscape; if ('animate' in props) popupParams.animate = animate; if ('backdrop' in props) popupParams.backdrop = backdrop; if ('backdropEl' in props) popupParams.backdropEl = backdropEl; if ('swipeToClose' in props) popupParams.swipeToClose = swipeToClose; if ('swipeHandler' in props) popupParams.swipeHandler = swipeHandler; if ('containerEl' in props) popupParams.containerEl = containerEl; zmpready(function () { _zmpPopup.current = zmp.popup.create(popupParams); if (opened) { _zmpPopup.current.open(false); } }); }; var onDestroy = function onDestroy() { if (_zmpPopup.current) { _zmpPopup.current.destroy(); } _zmpPopup.current = null; }; useIsomorphicLayoutEffect(function () { onMount(); return onDestroy; }, []); var classes = classNames(className, 'popup', { 'popup-tablet-fullscreen': tabletFullscreen, 'popup-push': push }, modalStateClasses({ isOpened: isOpened, isClosing: isClosing }), colorClasses(props)); return /*#__PURE__*/React.createElement("div", _extends({ id: id, style: style, className: classes, ref: elRef }, extraAttrs), children); }); Popup.displayName = 'zmp-popup'; export default Popup;