UNPKG

1.45 kBJavaScriptView Raw
1import React, { useEffect, useReducer } from "react";
2import styled from "styled-components";
3import createEventHub from "ynw/pattern/createEventHub";
4import { Spin } from "antd";
5
6export const mask = createEventHub();
7
8const initState = {
9 visible: false,
10 message: "",
11};
12const reducer = (state, action) => {
13 return { ...state, ...action };
14};
15/**
16 * ----------------------------------------
17 * 全局蒙版
18 * @param {Number} [zIndex = 10000] 层级高度
19 * @description
20 * import { mask } from "@comps/GlobalMask";
21 * mask.emit('show','正在上传')
22 * mask.emit('hide')
23 * ----------------------------------------
24 */
25export default function GlobalMask({ zIndex = 10000 }) {
26 const [state, dispatch] = useReducer(reducer, initState);
27
28 useEffect(() => {
29 mask.on("show", message => {
30 dispatch({ visible: true, message: message || "" });
31 });
32 mask.on("hide", () => {
33 setTimeout(() => {
34 dispatch({ visible: false });
35 }, 300);
36 });
37 }, []);
38
39 return state.visible ? (
40 <SBox style={{ zIndex }}>
41 <Spin />
42 <SMsg>{state.message}</SMsg>
43 </SBox>
44 ) : null;
45}
46
47const SBox = styled.div`
48 position: fixed;
49 left: 0;
50 top: 0;
51 width: 100%;
52 height: 100%;
53 background-color: rgba(255, 255, 255, 0.75);
54 display: flex;
55 align-items: center;
56 justify-content: center;
57 flex-direction: column;
58`;
59
60const SMsg = styled.div`
61 color: #525f88;
62 margin-top: 5px;
63 letter-spacing: 1px;
64`;