index.js
5.92 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
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var React = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _rcMotion = _interopRequireDefault(require("rc-motion"));
var _util = require("../../util");
var _MemoChildren = _interopRequireDefault(require("./MemoChildren"));
var sentinelStyle = {
width: 0,
height: 0,
overflow: 'hidden',
outline: 'none'
};
var Content = /*#__PURE__*/React.forwardRef(function (props, ref) {
var closable = props.closable,
prefixCls = props.prefixCls,
width = props.width,
height = props.height,
footer = props.footer,
title = props.title,
closeIcon = props.closeIcon,
style = props.style,
className = props.className,
visible = props.visible,
forceRender = props.forceRender,
bodyStyle = props.bodyStyle,
bodyProps = props.bodyProps,
children = props.children,
destroyOnClose = props.destroyOnClose,
modalRender = props.modalRender,
motionName = props.motionName,
ariaId = props.ariaId,
onClose = props.onClose,
onVisibleChanged = props.onVisibleChanged,
onMouseDown = props.onMouseDown,
onMouseUp = props.onMouseUp,
mousePosition = props.mousePosition;
var sentinelStartRef = (0, React.useRef)();
var sentinelEndRef = (0, React.useRef)();
var dialogRef = (0, React.useRef)(); // ============================== Ref ===============================
React.useImperativeHandle(ref, function () {
return {
focus: function focus() {
var _sentinelStartRef$cur;
(_sentinelStartRef$cur = sentinelStartRef.current) === null || _sentinelStartRef$cur === void 0 ? void 0 : _sentinelStartRef$cur.focus();
},
getDOM: function getDOM() {
return dialogRef.current;
},
changeActive: function changeActive(next) {
var _document = document,
activeElement = _document.activeElement;
if (next && activeElement === sentinelEndRef.current) {
sentinelStartRef.current.focus();
} else if (!next && activeElement === sentinelStartRef.current) {
sentinelEndRef.current.focus();
}
}
};
}); // ============================= Style ==============================
var _React$useState = React.useState(),
_React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
transformOrigin = _React$useState2[0],
setTransformOrigin = _React$useState2[1];
var contentStyle = {};
if (width !== undefined) {
contentStyle.width = width;
}
if (height !== undefined) {
contentStyle.height = height;
}
if (transformOrigin) {
contentStyle.transformOrigin = transformOrigin;
}
function onPrepare() {
var elementOffset = (0, _util.offset)(dialogRef.current);
setTransformOrigin(mousePosition ? "".concat(mousePosition.x - elementOffset.left, "px ").concat(mousePosition.y - elementOffset.top, "px") : '');
} // ============================= Render =============================
var footerNode;
if (footer) {
footerNode = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-footer")
}, footer);
}
var headerNode;
if (title) {
headerNode = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-header")
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-title"),
id: ariaId
}, title));
}
var closer;
if (closable) {
closer = /*#__PURE__*/React.createElement("button", {
type: "button",
onClick: onClose,
"aria-label": "Close",
className: "".concat(prefixCls, "-close")
}, closeIcon || /*#__PURE__*/React.createElement("span", {
className: "".concat(prefixCls, "-close-x")
}));
}
var content = /*#__PURE__*/React.createElement("div", {
className: "".concat(prefixCls, "-content")
}, closer, headerNode, /*#__PURE__*/React.createElement("div", (0, _extends2.default)({
className: "".concat(prefixCls, "-body"),
style: bodyStyle
}, bodyProps), children), footerNode);
return /*#__PURE__*/React.createElement(_rcMotion.default, {
visible: visible,
onVisibleChanged: onVisibleChanged,
onAppearPrepare: onPrepare,
onEnterPrepare: onPrepare,
forceRender: forceRender,
motionName: motionName,
removeOnLeave: destroyOnClose,
ref: dialogRef
}, function (_ref, motionRef) {
var motionClassName = _ref.className,
motionStyle = _ref.style;
return /*#__PURE__*/React.createElement("div", {
key: "dialog-element",
role: "document",
ref: motionRef,
style: (0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, motionStyle), style), contentStyle),
className: (0, _classnames.default)(prefixCls, className, motionClassName),
onMouseDown: onMouseDown,
onMouseUp: onMouseUp
}, /*#__PURE__*/React.createElement("div", {
tabIndex: 0,
ref: sentinelStartRef,
style: sentinelStyle,
"aria-hidden": "true"
}), /*#__PURE__*/React.createElement(_MemoChildren.default, {
shouldUpdate: visible || forceRender
}, modalRender ? modalRender(content) : content), /*#__PURE__*/React.createElement("div", {
tabIndex: 0,
ref: sentinelEndRef,
style: sentinelStyle,
"aria-hidden": "true"
}));
});
});
Content.displayName = 'Content';
var _default = Content;
exports.default = _default;