-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
BlockUI.js
111 lines (90 loc) · 3.36 KB
/
BlockUI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as React from 'react';
import PrimeReact from '../api/Api';
import { useMountEffect, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { Portal } from '../portal/Portal';
import { classNames, DomHandler, ObjectUtils, ZIndexUtils } from '../utils/Utils';
import { BlockUIBase } from './BlockUIBase';
export const BlockUI = React.forwardRef((inProps, ref) => {
const props = BlockUIBase.getProps(inProps);
const [visibleState, setVisibleState] = React.useState(props.blocked);
const elementRef = React.useRef(null);
const maskRef = React.useRef(null);
const block = () => {
setVisibleState(true);
};
const unblock = () => {
const callback = () => {
setVisibleState(false);
props.fullScreen && DomHandler.removeClass(document.body, 'p-overflow-hidden');
props.onUnblocked && props.onUnblocked();
};
if (maskRef.current) {
DomHandler.addClass(maskRef.current, 'p-component-overlay-leave');
maskRef.current.addEventListener('animationend', () => {
ZIndexUtils.clear(maskRef.current);
callback();
});
} else {
callback();
}
};
const onPortalMounted = () => {
if (props.fullScreen) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.activeElement.blur();
}
if (props.autoZIndex) {
const key = props.fullScreen ? 'modal' : 'overlay';
ZIndexUtils.set(key, maskRef.current, PrimeReact.autoZIndex, props.baseZIndex || PrimeReact.zIndex[key]);
}
props.onBlocked && props.onBlocked();
};
useMountEffect(() => {
visibleState && block();
});
useUpdateEffect(() => {
props.blocked ? block() : unblock();
}, [props.blocked]);
useUnmountEffect(() => {
if (props.fullScreen) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
ZIndexUtils.clear(maskRef.current);
});
React.useImperativeHandle(ref, () => ({
props,
block,
unblock,
getElement: () => elementRef.current
}));
const createMask = () => {
if (visibleState) {
const appendTo = props.fullScreen ? document.body : 'self';
const className = classNames(
'p-blockui p-component-overlay p-component-overlay-enter',
{
'p-blockui-document': props.fullScreen
},
props.className
);
const content = props.template ? ObjectUtils.getJSXElement(props.template, props) : null;
const mask = (
<div ref={maskRef} className={className} style={props.style}>
{content}
</div>
);
return <Portal element={mask} appendTo={appendTo} onMounted={onPortalMounted} />;
}
return null;
};
const otherProps = BlockUIBase.getOtherProps(props);
const mask = createMask();
const className = classNames('p-blockui-container', props.containerClassName);
return (
<div id={props.id} ref={elementRef} className={className} style={props.containerStyle} {...otherProps}>
{props.children}
{mask}
</div>
);
});
BlockUI.displayName = 'BlockUI';