index.js
7.02 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = Dialog;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var _contains = _interopRequireDefault(require("rc-util/lib/Dom/contains"));
var _Mask = _interopRequireDefault(require("./Mask"));
var _util = require("../util");
var _Content = _interopRequireDefault(require("./Content"));
function Dialog(props) {
var _props$prefixCls = props.prefixCls,
prefixCls = _props$prefixCls === void 0 ? 'rc-dialog' : _props$prefixCls,
zIndex = props.zIndex,
_props$visible = props.visible,
visible = _props$visible === void 0 ? false : _props$visible,
_props$keyboard = props.keyboard,
keyboard = _props$keyboard === void 0 ? true : _props$keyboard,
_props$focusTriggerAf = props.focusTriggerAfterClose,
focusTriggerAfterClose = _props$focusTriggerAf === void 0 ? true : _props$focusTriggerAf,
_props$switchScrollin = props.switchScrollingEffect,
switchScrollingEffect = _props$switchScrollin === void 0 ? function () {} : _props$switchScrollin,
title = props.title,
wrapStyle = props.wrapStyle,
wrapClassName = props.wrapClassName,
wrapProps = props.wrapProps,
onClose = props.onClose,
afterClose = props.afterClose,
transitionName = props.transitionName,
animation = props.animation,
_props$closable = props.closable,
closable = _props$closable === void 0 ? true : _props$closable,
_props$mask = props.mask,
mask = _props$mask === void 0 ? true : _props$mask,
maskTransitionName = props.maskTransitionName,
maskAnimation = props.maskAnimation,
_props$maskClosable = props.maskClosable,
maskClosable = _props$maskClosable === void 0 ? true : _props$maskClosable,
maskStyle = props.maskStyle,
maskProps = props.maskProps;
var lastOutSideActiveElementRef = (0, React.useRef)();
var wrapperRef = (0, React.useRef)();
var contentRef = (0, React.useRef)();
var _React$useState = React.useState(visible),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
animatedVisible = _React$useState2[0],
setAnimatedVisible = _React$useState2[1]; // ========================== Init ==========================
var ariaIdRef = (0, React.useRef)();
if (!ariaIdRef.current) {
ariaIdRef.current = "rcDialogTitle".concat((0, _util.getUUID)());
} // ========================= Events =========================
function onDialogVisibleChanged(newVisible) {
if (newVisible) {
// Try to focus
if (!(0, _contains.default)(wrapperRef.current, document.activeElement)) {
var _contentRef$current;
lastOutSideActiveElementRef.current = document.activeElement;
(_contentRef$current = contentRef.current) === null || _contentRef$current === void 0 ? void 0 : _contentRef$current.focus();
}
} else {
// Clean up scroll bar & focus back
setAnimatedVisible(false);
switchScrollingEffect();
if (mask && lastOutSideActiveElementRef.current && focusTriggerAfterClose) {
try {
lastOutSideActiveElementRef.current.focus({
preventScroll: true
});
} catch (e) {// Do nothing
}
lastOutSideActiveElementRef.current = null;
}
afterClose === null || afterClose === void 0 ? void 0 : afterClose();
}
}
function onInternalClose(e) {
onClose === null || onClose === void 0 ? void 0 : onClose(e);
} // >>> Content
var contentClickRef = (0, React.useRef)(false);
var contentTimeoutRef = (0, React.useRef)(); // We need record content click incase content popup out of dialog
var onContentMouseDown = function onContentMouseDown() {
clearTimeout(contentTimeoutRef.current);
contentClickRef.current = true;
};
var onContentMouseUp = function onContentMouseUp() {
contentTimeoutRef.current = setTimeout(function () {
contentClickRef.current = false;
});
}; // >>> Wrapper
// Close only when element not on dialog
var onWrapperClick = null;
if (maskClosable) {
onWrapperClick = function onWrapperClick(e) {
if (contentClickRef.current) {
contentClickRef.current = false;
} else if (!(0, _contains.default)(contentRef.current.getDOM(), e.target)) {
onInternalClose(e);
}
};
}
function onWrapperKeyDown(e) {
if (keyboard && e.keyCode === _KeyCode.default.ESC) {
e.stopPropagation();
onInternalClose(e);
return;
} // keep focus inside dialog
if (visible) {
if (e.keyCode === _KeyCode.default.TAB) {
contentRef.current.changeActive(!e.shiftKey);
}
}
} // ========================= Effect =========================
(0, React.useEffect)(function () {
if (visible) {
setAnimatedVisible(true);
switchScrollingEffect();
}
}, [visible]); // Remove direct should also check the scroll bar update
(0, React.useEffect)(function () {
return function () {
switchScrollingEffect();
clearTimeout(contentTimeoutRef.current);
};
}, []); // ========================= Render =========================
return /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-root")
}, /*#__PURE__*/React.createElement(_Mask.default, {
prefixCls: prefixCls,
visible: mask && visible,
motionName: (0, _util.getMotionName)(prefixCls, maskTransitionName, maskAnimation),
style: (0, _objectSpread2.default)({
zIndex: zIndex
}, maskStyle),
maskProps: maskProps
}), /*#__PURE__*/React.createElement("div", (0, _extends2.default)({
tabIndex: -1,
onKeyDown: onWrapperKeyDown,
className: (0, _classnames.default)("".concat(prefixCls, "-wrap"), wrapClassName),
ref: wrapperRef,
onClick: onWrapperClick,
role: "dialog",
"aria-labelledby": title ? ariaIdRef.current : null,
style: (0, _objectSpread2.default)((0, _objectSpread2.default)({
zIndex: zIndex
}, wrapStyle), {}, {
display: !animatedVisible ? 'none' : null
})
}, wrapProps), /*#__PURE__*/React.createElement(_Content.default, (0, _extends2.default)({}, props, {
onMouseDown: onContentMouseDown,
onMouseUp: onContentMouseUp,
ref: contentRef,
closable: closable,
ariaId: ariaIdRef.current,
prefixCls: prefixCls,
visible: visible,
onClose: onInternalClose,
onVisibleChanged: onDialogVisibleChanged,
motionName: (0, _util.getMotionName)(prefixCls, transitionName, animation)
}))));
}