PortalWrapper.js
9.09 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/* eslint-disable no-underscore-dangle,react/require-default-props */
import * as React from 'react';
import raf from './raf';
import Portal from './Portal';
import switchScrollingEffect from './switchScrollingEffect';
import setStyle from './setStyle';
import canUseDom from './Dom/canUseDom';
var openCount = 0;
var supportDom = canUseDom();
/** @private Test usage only */
export function getOpenCount() {
return process.env.NODE_ENV === 'test' ? openCount : 0;
} // https://github.com/ant-design/ant-design/issues/19340
// https://github.com/ant-design/ant-design/issues/19332
var cacheOverflow = {};
var getParent = function getParent(getContainer) {
if (!supportDom) {
return null;
}
if (getContainer) {
if (typeof getContainer === 'string') {
return document.querySelectorAll(getContainer)[0];
}
if (typeof getContainer === 'function') {
return getContainer();
}
if (_typeof(getContainer) === 'object' && getContainer instanceof window.HTMLElement) {
return getContainer;
}
}
return document.body;
};
var PortalWrapper = /*#__PURE__*/function (_React$Component) {
_inherits(PortalWrapper, _React$Component);
var _super = _createSuper(PortalWrapper);
function PortalWrapper() {
var _this;
_classCallCheck(this, PortalWrapper);
_this = _super.apply(this, arguments);
_this.componentRef = React.createRef();
_this.updateOpenCount = function (prevProps) {
var _ref = prevProps || {},
prevVisible = _ref.visible,
prevGetContainer = _ref.getContainer;
var _this$props = _this.props,
visible = _this$props.visible,
getContainer = _this$props.getContainer; // Update count
if (visible !== prevVisible && supportDom && getParent(getContainer) === document.body) {
if (visible && !prevVisible) {
openCount += 1;
} else if (prevProps) {
openCount -= 1;
}
} // Clean up container if needed
var getContainerIsFunc = typeof getContainer === 'function' && typeof prevGetContainer === 'function';
if (getContainerIsFunc ? getContainer.toString() !== prevGetContainer.toString() : getContainer !== prevGetContainer) {
_this.removeCurrentContainer();
}
};
_this.attachToParent = function () {
var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
if (force || _this.container && !_this.container.parentNode) {
var parent = getParent(_this.props.getContainer);
if (parent) {
parent.appendChild(_this.container);
return true;
}
return false;
}
return true;
};
_this.getContainer = function () {
if (!supportDom) {
return null;
}
if (!_this.container) {
_this.container = document.createElement('div');
_this.attachToParent(true);
}
_this.setWrapperClassName();
return _this.container;
};
_this.setWrapperClassName = function () {
var wrapperClassName = _this.props.wrapperClassName;
if (_this.container && wrapperClassName && wrapperClassName !== _this.container.className) {
_this.container.className = wrapperClassName;
}
};
_this.removeCurrentContainer = function () {
var _this$container, _this$container$paren;
// Portal will remove from `parentNode`.
// Let's handle this again to avoid refactor issue.
(_this$container = _this.container) === null || _this$container === void 0 ? void 0 : (_this$container$paren = _this$container.parentNode) === null || _this$container$paren === void 0 ? void 0 : _this$container$paren.removeChild(_this.container);
};
/**
* Enhance ./switchScrollingEffect
* 1. Simulate document body scroll bar with
* 2. Record body has overflow style and recover when all of PortalWrapper invisible
* 3. Disable body scroll when PortalWrapper has open
*
* @memberof PortalWrapper
*/
_this.switchScrollingEffect = function () {
if (openCount === 1 && !Object.keys(cacheOverflow).length) {
switchScrollingEffect(); // Must be set after switchScrollingEffect
cacheOverflow = setStyle({
overflow: 'hidden',
overflowX: 'hidden',
overflowY: 'hidden'
});
} else if (!openCount) {
setStyle(cacheOverflow);
cacheOverflow = {};
switchScrollingEffect(true);
}
};
return _this;
}
_createClass(PortalWrapper, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
this.updateOpenCount();
if (!this.attachToParent()) {
this.rafId = raf(function () {
_this2.forceUpdate();
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
this.updateOpenCount(prevProps);
this.setWrapperClassName();
this.attachToParent();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var _this$props2 = this.props,
visible = _this$props2.visible,
getContainer = _this$props2.getContainer;
if (supportDom && getParent(getContainer) === document.body) {
// 离开时不会 render, 导到离开时数值不变,改用 func 。。
openCount = visible && openCount ? openCount - 1 : openCount;
}
this.removeCurrentContainer();
raf.cancel(this.rafId);
}
}, {
key: "render",
value: function render() {
var _this$props3 = this.props,
children = _this$props3.children,
forceRender = _this$props3.forceRender,
visible = _this$props3.visible;
var portal = null;
var childProps = {
getOpenCount: function getOpenCount() {
return openCount;
},
getContainer: this.getContainer,
switchScrollingEffect: this.switchScrollingEffect
};
if (forceRender || visible || this.componentRef.current) {
portal = React.createElement(Portal, {
getContainer: this.getContainer,
ref: this.componentRef
}, children(childProps));
}
return portal;
}
}]);
return PortalWrapper;
}(React.Component);
export default PortalWrapper;