import {designComponent, getComponentCls, mergeAttrs, nextIndex, onMounted, reactive, useClasses, useModel, useRefs, useStyles, watch} from "@peryl/react-compose"; import {delay} from "@peryl/utils/delay"; import Loading from "../Loading"; import './loading-mask.scss'; import Transition from "../Transition"; export const LoadingMask = designComponent({ name: 'loading-mask', inheritPropsType: HTMLDivElement, inheritAttrs: false, props: { modelValue: { type: Boolean }, // 是否打开loading遮罩 message: { type: String }, // 提示信息 noMask: { type: Boolean }, // 去掉遮罩 loadingType: { type: String, default: 'delta' }, // loading类型 maskColor: { type: String, }, // 遮罩背景色 fixedPosition: { type: Boolean }, // 是否为页面文档根节点的加载遮罩 inDirective: { type: Boolean }, // 是否为遮罩指令服务的实例 }, emits: { onUpdateModelValue: (val: boolean | undefined) => true, }, setup({ props, event, attrs }) { const { refs, onRef } = useRefs({ el: HTMLElement }); const modelValue = useModel(() => props.modelValue, event.emit.onUpdateModelValue); const state = reactive({ isMounted: false, zIndex: nextIndex() + (!props.fixedPosition ? -1500 : 0) + 1, }); const classes = useClasses(() => [ getComponentCls('loading-mask'), { 'loading-no-mask': props.noMask, 'loading-mask-fixed-position': props.fixedPosition } ]); const styles = useStyles(style => { !!props.maskColor && (style.background = props.maskColor); style.zIndex = state.zIndex; }); const utils = { /** * 检查父节点的position是否为 absolute,relative或者fixed,不是的话,设置为 relative * @author 韦胜健 * @date 2020/11/3 16:42 */ resetParentPosition: async () => { if (props.fixedPosition) { return; } await delay(23); const el = refs.el; if (el) { let parentNode = el.parentNode as HTMLElement; if (props.inDirective) { parentNode = parentNode.parentNode as HTMLElement; } if (!!parentNode) { const styles = window.getComputedStyle(parentNode); const position = styles.position; if (['absolute', 'relative', 'fixed'].indexOf(position) === -1) { parentNode.style.position = 'relative'; } } } } }; watch(() => modelValue.value && state.isMounted, async val => { !!val && (state.zIndex = nextIndex() + (!props.fixedPosition ? -1500 : 0) + 1); await utils.resetParentPosition(); }); onMounted(() => { utils.resetParentPosition(); setTimeout(() => {state.isMounted = true;}, 23); }); return { refer: { refs }, render: () => (
{!!props.message && {props.message}}
) }; }, });