-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoverlay.tsx
More file actions
executable file
·72 lines (66 loc) · 1.86 KB
/
overlay.tsx
File metadata and controls
executable file
·72 lines (66 loc) · 1.86 KB
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
import { useContext, useEffect, useRef } from 'react';
import classNames from 'classnames';
import Portal from '../portal';
import Transition from '../transition';
import { ConfigContext } from '../config-provider/config-context';
import { getPrefixCls } from '../_utils/general';
import { OverlayProps } from './types';
let scrollLockCount = 0;
const Overlay = (props: OverlayProps): JSX.Element => {
const {
isShow = false,
blurred = false,
unmountOnExit = true,
zIndex = 1000,
type = 'default',
clickCallback,
onEnter,
onEntered,
onExit,
onExited,
children,
style,
prefixCls: customisedCls,
} = props;
const configContext = useContext(ConfigContext);
const prefixCls = getPrefixCls('overlay', configContext.prefixCls, customisedCls);
const cls = classNames(prefixCls, `${prefixCls}_${type}`, { [`${prefixCls}_blurred`]: blurred });
const nodeRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (isShow) {
scrollLockCount++;
document.body.style.overflow = 'hidden';
}
return () => {
if (isShow) {
scrollLockCount--;
if (scrollLockCount <= 0) {
scrollLockCount = 0;
document.body.style.overflow = '';
}
}
};
}, [isShow]);
return (
<Portal>
<Transition
appear={true}
nodeRef={nodeRef}
onEnter={onEnter}
onEntered={onEntered}
onExit={onExit}
onExited={onExited}
in={isShow}
mountOnEnter={true}
unmountOnExit={unmountOnExit}
classNames={`${prefixCls}_fade`}
timeout={{ exit: 300, enter: 0 }}>
<div ref={nodeRef} tabIndex={-1} className={cls} onClick={clickCallback} style={{ zIndex, ...style }}>
{children}
</div>
</Transition>
</Portal>
);
};
Overlay.displayName = 'Overlay';
export default Overlay;