UNPKG

pay-sdk-react

Version:

A cross-platform payment SDK for React, supporting Alipay, WeChat Pay, PayPal, Stripe, Payssion, and Airwallex, compatible with H5, PC, and App environments.

156 lines 7.45 kB
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import cs from '../utils/classNames'; import React, { useState, useRef } from 'react'; import { useIsomorphicLayoutEffect, useUnmountedRef } from 'ahooks'; import { withNativeProps } from '../utils/native-props'; import { useLockScroll } from '../hooks/use-lock-scroll'; import { useSpring, animated } from '@react-spring/web'; import { useDrag } from '@use-gesture/react'; import { ShouldRender } from '../utils/should-render'; import { withStopPropagation } from '../utils/with-stop-propagation'; import { useInnerVisible } from '../hooks/use-inner-visible'; import { mergeProps } from '../utils/with-default-props'; import { renderToContainer } from '../utils/render-to-container'; import Mask from '../Mask'; import { IconClose } from '../icons'; import { getPrefixCls } from '../utils/getPrefixCls'; const classPrefix = getPrefixCls('popup'); const defaultPopupBaseProps = { closeOnMaskClick: true, closeIcon: /*#__PURE__*/React.createElement(IconClose, null), destroyOnClose: false, disableBodyScroll: true, forceRender: false, getContainer: () => document.body, mask: true, showCloseButton: false, stopPropagation: ['click'], visible: false }; const defaultProps = _objectSpread(_objectSpread({}, defaultPopupBaseProps), {}, { closeOnSwipe: false, position: 'bottom', round: false }); const PayPopup = p => { const props = mergeProps(defaultProps, p); const bodyCls = cs("".concat(classPrefix, "-body"), props.bodyClassName, "".concat(classPrefix, "-body-position-").concat(props.position), { ["".concat(classPrefix, "-body-with-close")]: props.showCloseButton, ["".concat(classPrefix, "-body-round")]: props.round }); const [active, setActive] = useState(props.visible); const ref = useRef(null); useLockScroll(ref, props.disableBodyScroll && active ? 'strict' : false); useIsomorphicLayoutEffect(() => { if (props.visible) { setActive(true); } }, [props.visible]); const unmountedRef = useUnmountedRef(); const { percent } = useSpring({ percent: props.visible ? 0 : 100, config: { precision: 0.1, mass: 0.4, tension: 300, friction: 30 }, onRest: () => { if (unmountedRef.current) return; setActive(props.visible); if (props.visible) { var _props$afterShow; (_props$afterShow = props.afterShow) === null || _props$afterShow === void 0 || _props$afterShow.call(props); } else { var _props$afterClose; (_props$afterClose = props.afterClose) === null || _props$afterClose === void 0 || _props$afterClose.call(props); } } }); const bind = useDrag(_ref => { let { swipe: [, swipeY] } = _ref; if (!props.closeOnSwipe) return; if (swipeY === 1 && props.position === 'bottom' || swipeY === -1 && props.position === 'top') { var _props$onClose; (_props$onClose = props.onClose) === null || _props$onClose === void 0 || _props$onClose.call(props); } }, { axis: 'y', enabled: ['top', 'bottom'].includes(props.position) }); const maskVisible = useInnerVisible(active && props.visible); const node = withStopPropagation(props.stopPropagation, withNativeProps(props, /*#__PURE__*/React.createElement("div", _extends({ className: classPrefix, onClick: props.onClick, style: { display: active ? undefined : 'none', touchAction: ['top', 'bottom'].includes(props.position) ? 'none' : 'auto' } }, bind()), props.mask && /*#__PURE__*/React.createElement(Mask, { visible: maskVisible, forceRender: props.forceRender, destroyOnClose: props.destroyOnClose, onMaskClick: e => { var _props$onMaskClick; (_props$onMaskClick = props.onMaskClick) === null || _props$onMaskClick === void 0 || _props$onMaskClick.call(props, e); if (props.closeOnMaskClick) { var _props$onClose2; (_props$onClose2 = props.onClose) === null || _props$onClose2 === void 0 || _props$onClose2.call(props); } }, className: props.maskClassName, style: props.maskStyle, disableBodyScroll: false, stopPropagation: props.stopPropagation }), /*#__PURE__*/React.createElement(animated.div, { className: cs("".concat(classPrefix, "-body"), "".concat(classPrefix, "-body-position-").concat(props.position), { ["".concat(classPrefix, "-body-with-close")]: props.showCloseButton, ["".concat(classPrefix, "-body-round")]: props.round }, bodyCls), style: _objectSpread(_objectSpread({}, props.bodyStyle), {}, { pointerEvents: percent.to(v => v === 0 ? 'unset' : 'none'), transform: percent.to(v => { if (props.position === 'bottom') { return "translate(0, ".concat(v, "%)"); } if (props.position === 'top') { return "translate(0, -".concat(v, "%)"); } if (props.position === 'left') { return "translate(-".concat(v, "%, 0)"); } if (props.position === 'right') { return "translate(".concat(v, "%, 0)"); } return 'none'; }) }), ref: ref }, props.showCloseButton && /*#__PURE__*/React.createElement("a", { className: cs("".concat(classPrefix, "-close-icon"), "".concat(classPrefix, "-close-button")), onClick: () => { var _props$onClose3; (_props$onClose3 = props.onClose) === null || _props$onClose3 === void 0 || _props$onClose3.call(props); }, role: "button", "aria-label": 'close' }, props.closeIcon), /*#__PURE__*/React.createElement("div", { className: "".concat(classPrefix, "-content") }, props.children))))); return /*#__PURE__*/React.createElement(ShouldRender, { active: active, forceRender: props.forceRender, destroyOnClose: props.destroyOnClose }, renderToContainer(props.getContainer, node)); }; export * from './interface'; export default PayPopup;