1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
4 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.default = void 0;
|
9 | exports.getOpenCount = getOpenCount;
|
10 | var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
11 | var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
12 | var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
13 | var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
14 | var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
|
15 | var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
16 | var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
17 | var React = _interopRequireWildcard(require("react"));
|
18 | var _raf = _interopRequireDefault(require("./raf"));
|
19 | var _Portal = _interopRequireDefault(require("./Portal"));
|
20 | var _canUseDom = _interopRequireDefault(require("./Dom/canUseDom"));
|
21 | var _switchScrollingEffect = _interopRequireDefault(require("./switchScrollingEffect"));
|
22 | var _setStyle = _interopRequireDefault(require("./setStyle"));
|
23 | var _scrollLocker = _interopRequireDefault(require("./Dom/scrollLocker"));
|
24 |
|
25 |
|
26 | var openCount = 0;
|
27 | var supportDom = (0, _canUseDom.default)();
|
28 |
|
29 |
|
30 | function getOpenCount() {
|
31 | return process.env.NODE_ENV === 'test' ? openCount : 0;
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | var cacheOverflow = {};
|
37 | var getParent = function getParent(getContainer) {
|
38 | if (!supportDom) {
|
39 | return null;
|
40 | }
|
41 | if (getContainer) {
|
42 | if (typeof getContainer === 'string') {
|
43 | return document.querySelectorAll(getContainer)[0];
|
44 | }
|
45 | if (typeof getContainer === 'function') {
|
46 | return getContainer();
|
47 | }
|
48 | if ((0, _typeof2.default)(getContainer) === 'object' && getContainer instanceof window.HTMLElement) {
|
49 | return getContainer;
|
50 | }
|
51 | }
|
52 | return document.body;
|
53 | };
|
54 | var PortalWrapper = function (_React$Component) {
|
55 | (0, _inherits2.default)(PortalWrapper, _React$Component);
|
56 | var _super = (0, _createSuper2.default)(PortalWrapper);
|
57 | function PortalWrapper(props) {
|
58 | var _this;
|
59 | (0, _classCallCheck2.default)(this, PortalWrapper);
|
60 | _this = _super.call(this, props);
|
61 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "container", void 0);
|
62 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "componentRef", React.createRef());
|
63 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "rafId", void 0);
|
64 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "scrollLocker", void 0);
|
65 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderComponent", void 0);
|
66 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "updateScrollLocker", function (prevProps) {
|
67 | var _ref = prevProps || {},
|
68 | prevVisible = _ref.visible;
|
69 | var _this$props = _this.props,
|
70 | getContainer = _this$props.getContainer,
|
71 | visible = _this$props.visible;
|
72 | if (visible && visible !== prevVisible && supportDom && getParent(getContainer) !== _this.scrollLocker.getContainer()) {
|
73 | _this.scrollLocker.reLock({
|
74 | container: getParent(getContainer)
|
75 | });
|
76 | }
|
77 | });
|
78 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "updateOpenCount", function (prevProps) {
|
79 | var _ref2 = prevProps || {},
|
80 | prevVisible = _ref2.visible,
|
81 | prevGetContainer = _ref2.getContainer;
|
82 | var _this$props2 = _this.props,
|
83 | visible = _this$props2.visible,
|
84 | getContainer = _this$props2.getContainer;
|
85 |
|
86 |
|
87 | if (visible !== prevVisible && supportDom && getParent(getContainer) === document.body) {
|
88 | if (visible && !prevVisible) {
|
89 | openCount += 1;
|
90 | } else if (prevProps) {
|
91 | openCount -= 1;
|
92 | }
|
93 | }
|
94 |
|
95 |
|
96 | var getContainerIsFunc = typeof getContainer === 'function' && typeof prevGetContainer === 'function';
|
97 | if (getContainerIsFunc ? getContainer.toString() !== prevGetContainer.toString() : getContainer !== prevGetContainer) {
|
98 | _this.removeCurrentContainer();
|
99 | }
|
100 | });
|
101 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "attachToParent", function () {
|
102 | var force = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
103 | if (force || _this.container && !_this.container.parentNode) {
|
104 | var parent = getParent(_this.props.getContainer);
|
105 | if (parent) {
|
106 | parent.appendChild(_this.container);
|
107 | return true;
|
108 | }
|
109 | return false;
|
110 | }
|
111 | return true;
|
112 | });
|
113 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getContainer", function () {
|
114 | if (!supportDom) {
|
115 | return null;
|
116 | }
|
117 | if (!_this.container) {
|
118 | _this.container = document.createElement('div');
|
119 | _this.attachToParent(true);
|
120 | }
|
121 | _this.setWrapperClassName();
|
122 | return _this.container;
|
123 | });
|
124 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "setWrapperClassName", function () {
|
125 | var wrapperClassName = _this.props.wrapperClassName;
|
126 | if (_this.container && wrapperClassName && wrapperClassName !== _this.container.className) {
|
127 | _this.container.className = wrapperClassName;
|
128 | }
|
129 | });
|
130 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "removeCurrentContainer", function () {
|
131 | var _this$container;
|
132 |
|
133 |
|
134 | (_this$container = _this.container) === null || _this$container === void 0 || (_this$container = _this$container.parentNode) === null || _this$container === void 0 || _this$container.removeChild(_this.container);
|
135 | });
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "switchScrollingEffect", function () {
|
145 | if (openCount === 1 && !Object.keys(cacheOverflow).length) {
|
146 | (0, _switchScrollingEffect.default)();
|
147 |
|
148 | cacheOverflow = (0, _setStyle.default)({
|
149 | overflow: 'hidden',
|
150 | overflowX: 'hidden',
|
151 | overflowY: 'hidden'
|
152 | });
|
153 | } else if (!openCount) {
|
154 | (0, _setStyle.default)(cacheOverflow);
|
155 | cacheOverflow = {};
|
156 | (0, _switchScrollingEffect.default)(true);
|
157 | }
|
158 | });
|
159 | _this.scrollLocker = new _scrollLocker.default({
|
160 | container: getParent(props.getContainer)
|
161 | });
|
162 | return _this;
|
163 | }
|
164 | (0, _createClass2.default)(PortalWrapper, [{
|
165 | key: "componentDidMount",
|
166 | value: function componentDidMount() {
|
167 | var _this2 = this;
|
168 | this.updateOpenCount();
|
169 | if (!this.attachToParent()) {
|
170 | this.rafId = (0, _raf.default)(function () {
|
171 | _this2.forceUpdate();
|
172 | });
|
173 | }
|
174 | }
|
175 | }, {
|
176 | key: "componentDidUpdate",
|
177 | value: function componentDidUpdate(prevProps) {
|
178 | this.updateOpenCount(prevProps);
|
179 | this.updateScrollLocker(prevProps);
|
180 | this.setWrapperClassName();
|
181 | this.attachToParent();
|
182 | }
|
183 | }, {
|
184 | key: "componentWillUnmount",
|
185 | value: function componentWillUnmount() {
|
186 | var _this$props3 = this.props,
|
187 | visible = _this$props3.visible,
|
188 | getContainer = _this$props3.getContainer;
|
189 | if (supportDom && getParent(getContainer) === document.body) {
|
190 |
|
191 | openCount = visible && openCount ? openCount - 1 : openCount;
|
192 | }
|
193 | this.removeCurrentContainer();
|
194 | _raf.default.cancel(this.rafId);
|
195 | }
|
196 | }, {
|
197 | key: "render",
|
198 | value: function render() {
|
199 | var _this$props4 = this.props,
|
200 | children = _this$props4.children,
|
201 | forceRender = _this$props4.forceRender,
|
202 | visible = _this$props4.visible;
|
203 | var portal = null;
|
204 | var childProps = {
|
205 | getOpenCount: function getOpenCount() {
|
206 | return openCount;
|
207 | },
|
208 | getContainer: this.getContainer,
|
209 | switchScrollingEffect: this.switchScrollingEffect,
|
210 | scrollLocker: this.scrollLocker
|
211 | };
|
212 | if (forceRender || visible || this.componentRef.current) {
|
213 | portal = React.createElement(_Portal.default, {
|
214 | getContainer: this.getContainer,
|
215 | ref: this.componentRef
|
216 | }, children(childProps));
|
217 | }
|
218 | return portal;
|
219 | }
|
220 | }]);
|
221 | return PortalWrapper;
|
222 | }(React.Component);
|
223 | var _default = exports.default = PortalWrapper; |
\ | No newline at end of file |