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.

97 lines 4.43 kB
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 React, { useMemo, useRef, useState } from 'react'; import { useUnmountedRef } from 'ahooks'; import { useSpring, animated } from '@react-spring/web'; import { mergeProps } from '../utils/with-default-props'; import { ShouldRender } from '../utils/should-render'; import { withStopPropagation } from '../utils/with-stop-propagation'; import { renderToContainer } from '../utils/render-to-container'; import { useLockScroll } from '../hooks/use-lock-scroll'; import cs from '../utils/classNames'; import { getPrefixCls } from '../utils/getPrefixCls'; const classPrefix = getPrefixCls('mask'); const opacityRecord = { default: 0.7, thin: 0.35, thick: 0.75 }; const colorRecord = { black: '0, 0, 0', white: '255, 255, 255' }; const defaultProps = { visible: true, destroyOnClose: false, forceRender: false, color: 'black', opacity: 'default', disableBodyScroll: true, getContainer: () => document.body, stopPropagation: ['click'] }; const Mask = p => { const props = mergeProps(defaultProps, p); const ref = useRef(null); useLockScroll(ref, props.visible && props.disableBodyScroll); const background = useMemo(() => { var _opacityRecord; const opacity = (_opacityRecord = opacityRecord[props.opacity]) !== null && _opacityRecord !== void 0 ? _opacityRecord : props.opacity; const rgb = colorRecord[props.color]; return rgb ? "rgba(".concat(rgb, ", ").concat(opacity, ")") : props.color; }, [props.color, props.opacity]); const [active, setActive] = useState(props.visible); const unmountedRef = useUnmountedRef(); const { opacity } = useSpring({ opacity: props.visible ? 1 : 0, config: { precision: 0.01, mass: 1, tension: 250, friction: 30, clamp: true }, onStart: () => { setActive(true); }, 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 node = withStopPropagation(props.stopPropagation, /*#__PURE__*/React.createElement(animated.div, { className: cs(classPrefix, props.className), ref: ref, "aria-hidden": true, style: _objectSpread(_objectSpread({}, props.style), {}, { background, opacity, display: active ? undefined : 'none' }), onClick: e => { if (e.target === e.currentTarget) { var _props$onMaskClick; (_props$onMaskClick = props.onMaskClick) === null || _props$onMaskClick === void 0 || _props$onMaskClick.call(props, e); } } }, props.children)); return /*#__PURE__*/React.createElement(ShouldRender, { active: active, forceRender: props.forceRender, destroyOnClose: props.destroyOnClose }, renderToContainer(props.getContainer, node)); }; export * from './interface'; export default Mask;