UNPKG

antd-mobile-taro-ui

Version:

以antd-mobile为设计标准,基于taro框架的微信小程序组件库

97 lines (92 loc) 3.13 kB
import { withNativeProps } from 'antd-mobile/es/utils/native-props'; import React, { useEffect, useMemo, useRef, useState } from 'react'; import { useUnmountedRef } from 'ahooks'; import { mergeProps } from 'antd-mobile/es/utils/with-default-props'; import { ShouldRender } from 'antd-mobile/es/utils/should-render'; import { withStopPropagation } from 'antd-mobile/es/utils/with-stop-propagation'; import { View } from '@tarojs/components'; import { useConfig } from '../config-provider'; import { useLockScroll } from '../../utils/use-lock-scroll'; const classPrefix = `adm-mask`; const opacityRecord = { default: 0.55, thin: 0.35, thick: 0.75 }; const defaultProps = { visible: true, destroyOnClose: false, forceRender: false, color: 'black', opacity: 'default', disableBodyScroll: true, stopPropagation: ['click'] }; export const Mask = p => { const props = mergeProps(defaultProps, p); const { locale } = useConfig(); const ref = useRef(null); useLockScroll(props.visible && props.disableBodyScroll); const background = useMemo(() => { var _a; const opacity = (_a = opacityRecord[props.opacity]) !== null && _a !== void 0 ? _a : props.opacity; const rgb = props.color === 'white' ? '255, 255, 255' : '0, 0, 0'; return `rgba(${rgb}, ${opacity})`; }, [props.color, props.opacity]); const [active, setActive] = useState(props.visible); const unmountedRef = useUnmountedRef(); const init = useRef(true); const [wrapperVisible, setWrapperVisible] = useState(props.visible); useEffect(() => { if (init.current) return; if (unmountedRef.current) return; setActive(props.visible); if (props.visible) { setWrapperVisible(props.visible); setTimeout(() => { var _a; (_a = props.afterShow) === null || _a === void 0 ? void 0 : _a.call(props); }, 500); } else { setTimeout(() => { var _a; setWrapperVisible(props.visible); (_a = props.afterClose) === null || _a === void 0 ? void 0 : _a.call(props); }, 500); } }, [props.visible]); useEffect(() => { if (init.current) { setActive(true); init.current = false; } }, []); const node = withStopPropagation(props.stopPropagation, withNativeProps(props, React.createElement(View, { className: classPrefix, ref: ref, style: Object.assign(Object.assign({}, props.style), { background, opacity: props.visible ? 1 : 0, display: wrapperVisible ? undefined : 'none' }), onClick: e => { var _a; if (e.target === e.currentTarget) { (_a = props.onMaskClick) === null || _a === void 0 ? void 0 : _a.call(props, e); } } }, props.onMaskClick && React.createElement(View, { className: `${classPrefix}-aria-button`, "aria-label": locale.Mask.name, onClick: props.onMaskClick }), React.createElement(View, { className: `${classPrefix}-content` }, props.children)))); return React.createElement(ShouldRender, { active: active, forceRender: props.forceRender, destroyOnClose: props.destroyOnClose }, node); };