index.js
5.33 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
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _extends from "@babel/runtime/helpers/esm/extends";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import { useRef } from 'react';
import classNames from 'classnames';
import CSSMotion from 'rc-motion';
import { offset } from '../../util';
import MemoChildren from './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 = useRef();
var sentinelEndRef = useRef();
var dialogRef = 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 = _slicedToArray(_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 = 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", _extends({
className: "".concat(prefixCls, "-body"),
style: bodyStyle
}, bodyProps), children), footerNode);
return /*#__PURE__*/React.createElement(CSSMotion, {
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: _objectSpread(_objectSpread(_objectSpread({}, motionStyle), style), contentStyle),
className: classNames(prefixCls, className, motionClassName),
onMouseDown: onMouseDown,
onMouseUp: onMouseUp
}, /*#__PURE__*/React.createElement("div", {
tabIndex: 0,
ref: sentinelStartRef,
style: sentinelStyle,
"aria-hidden": "true"
}), /*#__PURE__*/React.createElement(MemoChildren, {
shouldUpdate: visible || forceRender
}, modalRender ? modalRender(content) : content), /*#__PURE__*/React.createElement("div", {
tabIndex: 0,
ref: sentinelEndRef,
style: sentinelStyle,
"aria-hidden": "true"
}));
});
});
Content.displayName = 'Content';
export default Content;