-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
index.tsx
182 lines (172 loc) · 5.34 KB
/
index.tsx
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Overlay 组件
* ---------------
* 动画库 react-transition-group 文档
* 老的文档
* https://facebook.github.io/react/docs/animation.html
* 新的文档
* https://reactcommunity.org/react-transition-group/
* 动画效果
* https://daneden.github.io/animate.css/
*/
import React, { cloneElement, useEffect, useRef, useState } from 'react';
import { CSSTransition } from 'react-transition-group';
import { TransitionProps } from 'react-transition-group/Transition';
import Portal, { PortalProps } from '@uiw/react-portal';
import { IProps, noop } from '@uiw/utils';
import './style/index.less';
export interface OverlayProps extends IProps, Omit<TransitionProps, 'timeout'> {
timeout?: TransitionProps['timeout'];
isOpen?: boolean;
usePortal?: boolean;
maskClosable?: boolean;
dialogProps?: React.HTMLProps<HTMLElement>;
backdropProps?: React.HTMLProps<HTMLDivElement>;
portalProps?: PortalProps;
hasBackdrop?: boolean;
unmountOnExit?: boolean;
transitionName?: string;
onEnter?: (node: HTMLElement, isAppearing: boolean) => void;
onOpening?: (node: HTMLElement, isAppearing: boolean) => void;
onOpened?: (node: HTMLElement, isAppearing: boolean) => void;
onClosing?: (node: HTMLElement) => void;
onClosed?: (node: HTMLElement | React.MouseEvent<HTMLButtonElement, MouseEvent>) => void;
onClose?: (evn: React.MouseEvent<HTMLElement, MouseEvent>) => void;
}
export default function Overlay(props: OverlayProps) {
const {
className,
style,
isOpen: _ = false,
prefixCls = 'w-overlay',
usePortal = true,
maskClosable = true,
backdropProps = {},
portalProps = {},
hasBackdrop = true,
unmountOnExit = true, // 设置 true 销毁根节点
timeout = 300,
transitionName = 'w-overlay',
// onEnter = noop,
onOpening = noop,
onOpened = noop,
onClosing = noop,
onClosed = noop,
onClose = noop,
children,
dialogProps = {},
...otherProps
} = props;
const [isOpen, setIsOpen] = useState<boolean>();
// const [isOpen, setIsOpen] = useState(props.isOpen || false);
const [visible, setVisible] = useState(false);
const container = useRef<HTMLDivElement>(null);
const overlay = useRef<HTMLDivElement>(null);
useEffect(() => {
if (isOpen !== props.isOpen && props.isOpen) {
setVisible(true);
}
if (isOpen !== props.isOpen && !props.isOpen) {
overlayWillClose();
setIsOpen(false);
}
}, [props.isOpen]);
useEffect(() => {
if (visible) {
overlayWillOpen();
setIsOpen(true);
}
}, [visible]);
const decoratedChild =
typeof children === 'object' ? (
cloneElement(children, {
...dialogProps,
style: { ...children.props.style, ...dialogProps.style },
className: [children.props.className, `${prefixCls}-content`].filter(Boolean).join(' ').trim(),
tabIndex: 0,
})
) : (
<span {...dialogProps} className={`${prefixCls}-content`}>
{children}
</span>
);
function handleClosed(node: HTMLElement | React.MouseEvent<HTMLButtonElement, MouseEvent>) {
setVisible(false);
onClosed && onClosed(node);
}
function handleBackdropMouseDown(e: React.MouseEvent<HTMLDivElement, MouseEvent>) {
if (e.target !== container.current && usePortal) {
return;
}
if (maskClosable && hasBackdrop) {
overlayWillClose();
setIsOpen(false);
onClose && onClose(e);
}
backdropProps && backdropProps.onMouseDown && backdropProps.onMouseDown(e);
}
function overlayWillOpen() {
if (hasBackdrop && usePortal) {
document.body.classList.add(`${prefixCls}-open`);
}
}
function overlayWillClose() {
if (hasBackdrop && usePortal) {
document.body.classList.remove(`${prefixCls}-open`);
}
// if (unmountOnExit) {
// setVisible(false)
// }
}
const TransitionGroupComp = (
<CSSTransition
classNames={transitionName}
unmountOnExit={unmountOnExit}
timeout={timeout!}
in={isOpen}
onEntering={onOpening}
onEntered={onOpened}
onExiting={onClosing}
onExited={handleClosed}
{...otherProps}
>
{(status) => {
return (
<div
style={style}
ref={overlay}
className={[
prefixCls,
className,
!usePortal ? `${prefixCls}-inline` : null,
isOpen ? `${prefixCls}-enter-done` : null,
]
.filter(Boolean)
.join(' ')
.trim()}
>
{hasBackdrop &&
cloneElement(<div />, {
...backdropProps,
onMouseDown: handleBackdropMouseDown,
className: [`${prefixCls}-backdrop`, backdropProps.className].filter(Boolean).join(' ').trim(),
tabIndex: maskClosable ? 0 : null,
})}
{usePortal ? (
<div ref={container} onMouseDown={handleBackdropMouseDown} className={`${prefixCls}-container`}>
{cloneElement(decoratedChild, { 'data-status': status })}
</div>
) : (
cloneElement(decoratedChild, { 'data-status': status })
)}
</div>
);
}}
</CSSTransition>
);
if (visible && usePortal) {
return <Portal {...{ ...portalProps }}>{TransitionGroupComp}</Portal>;
} else {
return TransitionGroupComp;
}
}