UNPKG

21.4 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
15
16var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
18var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
19
20var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
21
22var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
23
24var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
25
26var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
27
28var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
29
30var React = _interopRequireWildcard(require("react"));
31
32var _classnames2 = _interopRequireDefault(require("classnames"));
33
34var _getScrollBarSize = _interopRequireDefault(require("rc-util/lib/getScrollBarSize"));
35
36var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
37
38var _omit = _interopRequireDefault(require("rc-util/lib/omit"));
39
40var _utils = require("./utils");
41
42var _excluded = ["className", "children", "style", "width", "height", "defaultOpen", "open", "prefixCls", "placement", "level", "levelMove", "ease", "duration", "getContainer", "handler", "onChange", "afterVisibleChange", "showMask", "maskClosable", "maskStyle", "onClose", "onHandleClick", "keyboard", "getOpenCount", "scrollLocker", "contentWrapperStyle"];
43var currentDrawer = {};
44
45var DrawerChild = /*#__PURE__*/function (_React$Component) {
46 (0, _inherits2.default)(DrawerChild, _React$Component);
47
48 var _super = (0, _createSuper2.default)(DrawerChild);
49
50 function DrawerChild(props) {
51 var _this;
52
53 (0, _classCallCheck2.default)(this, DrawerChild);
54 _this = _super.call(this, props);
55 _this.levelDom = void 0;
56 _this.dom = void 0;
57 _this.contentWrapper = void 0;
58 _this.contentDom = void 0;
59 _this.maskDom = void 0;
60 _this.handlerDom = void 0;
61 _this.drawerId = void 0;
62 _this.timeout = void 0;
63 _this.passive = void 0;
64 _this.startPos = void 0;
65
66 _this.domFocus = function () {
67 if (_this.dom) {
68 _this.dom.focus();
69 }
70 };
71
72 _this.removeStartHandler = function (e) {
73 if (e.touches.length > 1) {
74 // need clear the startPos when another touch event happens
75 _this.startPos = null;
76 return;
77 }
78
79 _this.startPos = {
80 x: e.touches[0].clientX,
81 y: e.touches[0].clientY
82 };
83 };
84
85 _this.removeMoveHandler = function (e) {
86 // the startPos may be null or undefined
87 if (e.changedTouches.length > 1 || !_this.startPos) {
88 return;
89 }
90
91 var currentTarget = e.currentTarget;
92 var differX = e.changedTouches[0].clientX - _this.startPos.x;
93 var differY = e.changedTouches[0].clientY - _this.startPos.y;
94
95 if ((currentTarget === _this.maskDom || currentTarget === _this.handlerDom || currentTarget === _this.contentDom && (0, _utils.getTouchParentScroll)(currentTarget, e.target, differX, differY)) && e.cancelable) {
96 e.preventDefault();
97 }
98 };
99
100 _this.transitionEnd = function (e) {
101 var dom = e.target;
102 (0, _utils.removeEventListener)(dom, _utils.transitionEnd, _this.transitionEnd);
103 dom.style.transition = '';
104 };
105
106 _this.onKeyDown = function (e) {
107 if (e.keyCode === _KeyCode.default.ESC) {
108 var onClose = _this.props.onClose;
109 e.stopPropagation();
110
111 if (onClose) {
112 onClose(e);
113 }
114 }
115 };
116
117 _this.onWrapperTransitionEnd = function (e) {
118 var _this$props = _this.props,
119 open = _this$props.open,
120 afterVisibleChange = _this$props.afterVisibleChange;
121
122 if (e.target === _this.contentWrapper && e.propertyName.match(/transform$/)) {
123 _this.dom.style.transition = '';
124
125 if (!open && _this.getCurrentDrawerSome()) {
126 document.body.style.overflowX = '';
127
128 if (_this.maskDom) {
129 _this.maskDom.style.left = '';
130 _this.maskDom.style.width = '';
131 }
132 }
133
134 if (afterVisibleChange) {
135 afterVisibleChange(!!open);
136 }
137 }
138 };
139
140 _this.openLevelTransition = function () {
141 var _this$props2 = _this.props,
142 open = _this$props2.open,
143 width = _this$props2.width,
144 height = _this$props2.height;
145
146 var _this$getHorizontalBo = _this.getHorizontalBoolAndPlacementName(),
147 isHorizontal = _this$getHorizontalBo.isHorizontal,
148 placementName = _this$getHorizontalBo.placementName;
149
150 var contentValue = _this.contentDom ? _this.contentDom.getBoundingClientRect()[isHorizontal ? 'width' : 'height'] : 0;
151 var value = (isHorizontal ? width : height) || contentValue;
152
153 _this.setLevelAndScrolling(open, placementName, value);
154 };
155
156 _this.setLevelTransform = function (open, placementName, value, right) {
157 var _this$props3 = _this.props,
158 placement = _this$props3.placement,
159 levelMove = _this$props3.levelMove,
160 duration = _this$props3.duration,
161 ease = _this$props3.ease,
162 showMask = _this$props3.showMask; // router 切换时可能会导至页面失去滚动条,所以需要时时获取。
163
164 _this.levelDom.forEach(function (dom) {
165 dom.style.transition = "transform ".concat(duration, " ").concat(ease);
166 (0, _utils.addEventListener)(dom, _utils.transitionEnd, _this.transitionEnd);
167 var levelValue = open ? value : 0;
168
169 if (levelMove) {
170 var $levelMove = (0, _utils.transformArguments)(levelMove, {
171 target: dom,
172 open: open
173 });
174 levelValue = open ? $levelMove[0] : $levelMove[1] || 0;
175 }
176
177 var $value = typeof levelValue === 'number' ? "".concat(levelValue, "px") : levelValue;
178 var placementPos = placement === 'left' || placement === 'top' ? $value : "-".concat($value);
179 placementPos = showMask && placement === 'right' && right ? "calc(".concat(placementPos, " + ").concat(right, "px)") : placementPos;
180 dom.style.transform = levelValue ? "".concat(placementName, "(").concat(placementPos, ")") : '';
181 });
182 };
183
184 _this.setLevelAndScrolling = function (open, placementName, value) {
185 var onChange = _this.props.onChange;
186
187 if (!_utils.windowIsUndefined) {
188 var right = document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight) && window.innerWidth > document.body.offsetWidth ? (0, _getScrollBarSize.default)(true) : 0;
189
190 _this.setLevelTransform(open, placementName, value, right);
191
192 _this.toggleScrollingToDrawerAndBody(right);
193 }
194
195 if (onChange) {
196 onChange(open);
197 }
198 };
199
200 _this.toggleScrollingToDrawerAndBody = function (right) {
201 var _this$props4 = _this.props,
202 getContainer = _this$props4.getContainer,
203 showMask = _this$props4.showMask,
204 open = _this$props4.open;
205 var container = getContainer && getContainer(); // 处理 body 滚动
206
207 if (container && container.parentNode === document.body && showMask) {
208 var eventArray = ['touchstart'];
209 var domArray = [document.body, _this.maskDom, _this.handlerDom, _this.contentDom];
210
211 if (open && document.body.style.overflow !== 'hidden') {
212 if (right) {
213 _this.addScrollingEffect(right);
214 }
215
216 document.body.style.touchAction = 'none'; // 手机禁滚
217
218 domArray.forEach(function (item, i) {
219 if (!item) {
220 return;
221 }
222
223 (0, _utils.addEventListener)(item, eventArray[i] || 'touchmove', i ? _this.removeMoveHandler : _this.removeStartHandler, _this.passive);
224 });
225 } else if (_this.getCurrentDrawerSome()) {
226 document.body.style.touchAction = '';
227
228 if (right) {
229 _this.remScrollingEffect(right);
230 } // 恢复事件
231
232
233 domArray.forEach(function (item, i) {
234 if (!item) {
235 return;
236 }
237
238 (0, _utils.removeEventListener)(item, eventArray[i] || 'touchmove', i ? _this.removeMoveHandler : _this.removeStartHandler, _this.passive);
239 });
240 }
241 }
242 };
243
244 _this.addScrollingEffect = function (right) {
245 var _this$props5 = _this.props,
246 placement = _this$props5.placement,
247 duration = _this$props5.duration,
248 ease = _this$props5.ease;
249 var widthTransition = "width ".concat(duration, " ").concat(ease);
250 var transformTransition = "transform ".concat(duration, " ").concat(ease);
251 _this.dom.style.transition = 'none';
252
253 switch (placement) {
254 case 'right':
255 _this.dom.style.transform = "translateX(-".concat(right, "px)");
256 break;
257
258 case 'top':
259 case 'bottom':
260 _this.dom.style.width = "calc(100% - ".concat(right, "px)");
261 _this.dom.style.transform = 'translateZ(0)';
262 break;
263
264 default:
265 break;
266 }
267
268 clearTimeout(_this.timeout);
269 _this.timeout = setTimeout(function () {
270 if (_this.dom) {
271 _this.dom.style.transition = "".concat(transformTransition, ",").concat(widthTransition);
272 _this.dom.style.width = '';
273 _this.dom.style.transform = '';
274 }
275 });
276 };
277
278 _this.remScrollingEffect = function (right) {
279 var _this$props6 = _this.props,
280 placement = _this$props6.placement,
281 duration = _this$props6.duration,
282 ease = _this$props6.ease;
283
284 if (_utils.transitionStr) {
285 document.body.style.overflowX = 'hidden';
286 }
287
288 _this.dom.style.transition = 'none';
289 var heightTransition;
290 var widthTransition = "width ".concat(duration, " ").concat(ease);
291 var transformTransition = "transform ".concat(duration, " ").concat(ease);
292
293 switch (placement) {
294 case 'left':
295 {
296 _this.dom.style.width = '100%';
297 widthTransition = "width 0s ".concat(ease, " ").concat(duration);
298 break;
299 }
300
301 case 'right':
302 {
303 _this.dom.style.transform = "translateX(".concat(right, "px)");
304 _this.dom.style.width = '100%';
305 widthTransition = "width 0s ".concat(ease, " ").concat(duration);
306
307 if (_this.maskDom) {
308 _this.maskDom.style.left = "-".concat(right, "px");
309 _this.maskDom.style.width = "calc(100% + ".concat(right, "px)");
310 }
311
312 break;
313 }
314
315 case 'top':
316 case 'bottom':
317 {
318 _this.dom.style.width = "calc(100% + ".concat(right, "px)");
319 _this.dom.style.height = '100%';
320 _this.dom.style.transform = 'translateZ(0)';
321 heightTransition = "height 0s ".concat(ease, " ").concat(duration);
322 break;
323 }
324
325 default:
326 break;
327 }
328
329 clearTimeout(_this.timeout);
330 _this.timeout = setTimeout(function () {
331 if (_this.dom) {
332 _this.dom.style.transition = "".concat(transformTransition, ",").concat(heightTransition ? "".concat(heightTransition, ",") : '').concat(widthTransition);
333 _this.dom.style.transform = '';
334 _this.dom.style.width = '';
335 _this.dom.style.height = '';
336 }
337 });
338 };
339
340 _this.getCurrentDrawerSome = function () {
341 return !Object.keys(currentDrawer).some(function (key) {
342 return currentDrawer[key];
343 });
344 };
345
346 _this.getLevelDom = function (_ref) {
347 var level = _ref.level,
348 getContainer = _ref.getContainer;
349
350 if (_utils.windowIsUndefined) {
351 return;
352 }
353
354 var container = getContainer && getContainer();
355 var parent = container ? container.parentNode : null;
356 _this.levelDom = [];
357
358 if (level === 'all') {
359 var children = parent ? Array.prototype.slice.call(parent.children) : [];
360 children.forEach(function (child) {
361 if (child.nodeName !== 'SCRIPT' && child.nodeName !== 'STYLE' && child.nodeName !== 'LINK' && child !== container) {
362 _this.levelDom.push(child);
363 }
364 });
365 } else if (level) {
366 (0, _utils.dataToArray)(level).forEach(function (key) {
367 document.querySelectorAll(key).forEach(function (item) {
368 _this.levelDom.push(item);
369 });
370 });
371 }
372 };
373
374 _this.getHorizontalBoolAndPlacementName = function () {
375 var placement = _this.props.placement;
376 var isHorizontal = placement === 'left' || placement === 'right';
377 var placementName = "translate".concat(isHorizontal ? 'X' : 'Y');
378 return {
379 isHorizontal: isHorizontal,
380 placementName: placementName
381 };
382 };
383
384 _this.state = {
385 _self: (0, _assertThisInitialized2.default)(_this)
386 };
387 return _this;
388 }
389
390 (0, _createClass2.default)(DrawerChild, [{
391 key: "componentDidMount",
392 value: function componentDidMount() {
393 var _this2 = this;
394
395 if (!_utils.windowIsUndefined) {
396 var passiveSupported = false;
397
398 try {
399 window.addEventListener('test', null, Object.defineProperty({}, 'passive', {
400 get: function get() {
401 passiveSupported = true;
402 return null;
403 }
404 }));
405 } catch (err) {}
406
407 this.passive = passiveSupported ? {
408 passive: false
409 } : false;
410 }
411
412 var _this$props7 = this.props,
413 open = _this$props7.open,
414 getContainer = _this$props7.getContainer,
415 showMask = _this$props7.showMask,
416 autoFocus = _this$props7.autoFocus;
417 var container = getContainer && getContainer();
418 this.drawerId = "drawer_id_".concat(Number((Date.now() + Math.random()).toString().replace('.', Math.round(Math.random() * 9).toString())).toString(16));
419 this.getLevelDom(this.props);
420
421 if (open) {
422 if (container && container.parentNode === document.body) {
423 currentDrawer[this.drawerId] = open;
424 } // 默认打开状态时推出 level;
425
426
427 this.openLevelTransition();
428 this.forceUpdate(function () {
429 if (autoFocus) {
430 _this2.domFocus();
431 }
432 });
433
434 if (showMask) {
435 var _this$props$scrollLoc;
436
437 (_this$props$scrollLoc = this.props.scrollLocker) === null || _this$props$scrollLoc === void 0 ? void 0 : _this$props$scrollLoc.lock();
438 }
439 }
440 }
441 }, {
442 key: "componentDidUpdate",
443 value: function componentDidUpdate(prevProps) {
444 var _this$props8 = this.props,
445 open = _this$props8.open,
446 getContainer = _this$props8.getContainer,
447 scrollLocker = _this$props8.scrollLocker,
448 showMask = _this$props8.showMask,
449 autoFocus = _this$props8.autoFocus;
450 var container = getContainer && getContainer();
451
452 if (open !== prevProps.open) {
453 if (container && container.parentNode === document.body) {
454 currentDrawer[this.drawerId] = !!open;
455 }
456
457 this.openLevelTransition();
458
459 if (open) {
460 if (autoFocus) {
461 this.domFocus();
462 }
463
464 if (showMask) {
465 scrollLocker === null || scrollLocker === void 0 ? void 0 : scrollLocker.lock();
466 }
467 } else {
468 scrollLocker === null || scrollLocker === void 0 ? void 0 : scrollLocker.unLock();
469 }
470 }
471 }
472 }, {
473 key: "componentWillUnmount",
474 value: function componentWillUnmount() {
475 var _this$props9 = this.props,
476 open = _this$props9.open,
477 scrollLocker = _this$props9.scrollLocker;
478 delete currentDrawer[this.drawerId];
479
480 if (open) {
481 this.setLevelTransform(false);
482 document.body.style.touchAction = '';
483 }
484
485 scrollLocker === null || scrollLocker === void 0 ? void 0 : scrollLocker.unLock();
486 }
487 }, {
488 key: "render",
489 value: // tslint:disable-next-line:member-ordering
490 function render() {
491 var _classnames,
492 _this3 = this;
493
494 var _this$props10 = this.props,
495 className = _this$props10.className,
496 children = _this$props10.children,
497 style = _this$props10.style,
498 width = _this$props10.width,
499 height = _this$props10.height,
500 defaultOpen = _this$props10.defaultOpen,
501 $open = _this$props10.open,
502 prefixCls = _this$props10.prefixCls,
503 placement = _this$props10.placement,
504 level = _this$props10.level,
505 levelMove = _this$props10.levelMove,
506 ease = _this$props10.ease,
507 duration = _this$props10.duration,
508 getContainer = _this$props10.getContainer,
509 handler = _this$props10.handler,
510 onChange = _this$props10.onChange,
511 afterVisibleChange = _this$props10.afterVisibleChange,
512 showMask = _this$props10.showMask,
513 maskClosable = _this$props10.maskClosable,
514 maskStyle = _this$props10.maskStyle,
515 onClose = _this$props10.onClose,
516 onHandleClick = _this$props10.onHandleClick,
517 keyboard = _this$props10.keyboard,
518 getOpenCount = _this$props10.getOpenCount,
519 scrollLocker = _this$props10.scrollLocker,
520 contentWrapperStyle = _this$props10.contentWrapperStyle,
521 props = (0, _objectWithoutProperties2.default)(_this$props10, _excluded); // 首次渲染都将是关闭状态。
522
523 var open = this.dom ? $open : false;
524 var wrapperClassName = (0, _classnames2.default)(prefixCls, (_classnames = {}, (0, _defineProperty2.default)(_classnames, "".concat(prefixCls, "-").concat(placement), true), (0, _defineProperty2.default)(_classnames, "".concat(prefixCls, "-open"), open), (0, _defineProperty2.default)(_classnames, className || '', !!className), (0, _defineProperty2.default)(_classnames, 'no-mask', !showMask), _classnames));
525
526 var _this$getHorizontalBo2 = this.getHorizontalBoolAndPlacementName(),
527 placementName = _this$getHorizontalBo2.placementName; // 百分比与像素动画不同步,第一次打用后全用像素动画。
528 // const defaultValue = !this.contentDom || !level ? '100%' : `${value}px`;
529
530
531 var placementPos = placement === 'left' || placement === 'top' ? '-100%' : '100%';
532 var transform = open ? '' : "".concat(placementName, "(").concat(placementPos, ")");
533 var handlerChildren = handler && /*#__PURE__*/React.cloneElement(handler, {
534 onClick: function onClick(e) {
535 if (handler.props.onClick) {
536 handler.props.onClick();
537 }
538
539 if (onHandleClick) {
540 onHandleClick(e);
541 }
542 },
543 ref: function ref(c) {
544 _this3.handlerDom = c;
545 }
546 });
547 return /*#__PURE__*/React.createElement("div", (0, _extends2.default)({}, (0, _omit.default)(props, ['switchScrollingEffect', 'autoFocus']), {
548 tabIndex: -1,
549 className: wrapperClassName,
550 style: style,
551 ref: function ref(c) {
552 _this3.dom = c;
553 },
554 onKeyDown: open && keyboard ? this.onKeyDown : undefined,
555 onTransitionEnd: this.onWrapperTransitionEnd
556 }), showMask && /*#__PURE__*/React.createElement("div", {
557 className: "".concat(prefixCls, "-mask"),
558 onClick: maskClosable ? onClose : undefined,
559 style: maskStyle,
560 ref: function ref(c) {
561 _this3.maskDom = c;
562 }
563 }), /*#__PURE__*/React.createElement("div", {
564 className: "".concat(prefixCls, "-content-wrapper"),
565 style: (0, _objectSpread2.default)({
566 transform: transform,
567 msTransform: transform,
568 width: (0, _utils.isNumeric)(width) ? "".concat(width, "px") : width,
569 height: (0, _utils.isNumeric)(height) ? "".concat(height, "px") : height
570 }, contentWrapperStyle),
571 ref: function ref(c) {
572 _this3.contentWrapper = c;
573 }
574 }, /*#__PURE__*/React.createElement("div", {
575 className: "".concat(prefixCls, "-content"),
576 ref: function ref(c) {
577 _this3.contentDom = c;
578 }
579 }, children), handlerChildren));
580 }
581 }], [{
582 key: "getDerivedStateFromProps",
583 value: function getDerivedStateFromProps(props, _ref2) {
584 var prevProps = _ref2.prevProps,
585 _self = _ref2._self;
586 var nextState = {
587 prevProps: props
588 };
589
590 if (prevProps !== undefined) {
591 var placement = props.placement,
592 level = props.level;
593
594 if (placement !== prevProps.placement) {
595 // test 的 bug, 有动画过场,删除 dom
596 _self.contentDom = null;
597 }
598
599 if (level !== prevProps.level) {
600 _self.getLevelDom(props);
601 }
602 }
603
604 return nextState;
605 }
606 }]);
607 return DrawerChild;
608}(React.Component);
609
610var _default = DrawerChild;
611exports.default = _default;
\No newline at end of file