1 | import React, { useEffect, useReducer } from "react";
|
2 | import styled from "styled-components";
|
3 | import createEventHub from "ynw/pattern/createEventHub";
|
4 | import { Spin } from "antd";
|
5 |
|
6 | export const mask = createEventHub();
|
7 |
|
8 | const initState = {
|
9 | visible: false,
|
10 | message: "",
|
11 | };
|
12 | const reducer = (state, action) => {
|
13 | return { ...state, ...action };
|
14 | };
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | export 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 |
|
47 | const 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 |
|
60 | const SMsg = styled.div`
|
61 | color: #525f88;
|
62 | margin-top: 5px;
|
63 | letter-spacing: 1px;
|
64 | `;
|