DrawerChild.js
21.3 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
"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 _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _classnames2 = _interopRequireDefault(require("classnames"));
var _getScrollBarSize = _interopRequireDefault(require("rc-util/lib/getScrollBarSize"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var React = _interopRequireWildcard(require("react"));
var _utils = require("./utils");
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2.default)(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2.default)(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2.default)(this, result); }; }
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; } }
var currentDrawer = {};
var DrawerChild = /*#__PURE__*/function (_React$Component) {
(0, _inherits2.default)(DrawerChild, _React$Component);
var _super = _createSuper(DrawerChild);
function DrawerChild(props) {
var _this;
(0, _classCallCheck2.default)(this, DrawerChild);
_this = _super.call(this, props);
_this.domFocus = function () {
if (_this.dom) {
_this.dom.focus();
}
};
_this.removeStartHandler = function (e) {
if (e.touches.length > 1) {
return;
}
_this.startPos = {
x: e.touches[0].clientX,
y: e.touches[0].clientY
};
};
_this.removeMoveHandler = function (e) {
if (e.changedTouches.length > 1) {
return;
}
var currentTarget = e.currentTarget;
var differX = e.changedTouches[0].clientX - _this.startPos.x;
var differY = e.changedTouches[0].clientY - _this.startPos.y;
if ((currentTarget === _this.maskDom || currentTarget === _this.handlerDom || currentTarget === _this.contentDom && (0, _utils.getTouchParentScroll)(currentTarget, e.target, differX, differY)) && e.cancelable) {
e.preventDefault();
}
};
_this.transitionEnd = function (e) {
var dom = e.target;
(0, _utils.removeEventListener)(dom, _utils.transitionEnd, _this.transitionEnd);
dom.style.transition = '';
};
_this.onKeyDown = function (e) {
if (e.keyCode === _KeyCode.default.ESC) {
var onClose = _this.props.onClose;
e.stopPropagation();
if (onClose) {
onClose(e);
}
}
};
_this.onWrapperTransitionEnd = function (e) {
var _this$props = _this.props,
open = _this$props.open,
afterVisibleChange = _this$props.afterVisibleChange;
if (e.target === _this.contentWrapper && e.propertyName.match(/transform$/)) {
_this.dom.style.transition = '';
if (!open && _this.getCurrentDrawerSome()) {
document.body.style.overflowX = '';
if (_this.maskDom) {
_this.maskDom.style.left = '';
_this.maskDom.style.width = '';
}
}
if (afterVisibleChange) {
afterVisibleChange(!!open);
}
}
};
_this.openLevelTransition = function () {
var _this$props2 = _this.props,
open = _this$props2.open,
width = _this$props2.width,
height = _this$props2.height;
var _this$getHorizontalBo = _this.getHorizontalBoolAndPlacementName(),
isHorizontal = _this$getHorizontalBo.isHorizontal,
placementName = _this$getHorizontalBo.placementName;
var contentValue = _this.contentDom ? _this.contentDom.getBoundingClientRect()[isHorizontal ? 'width' : 'height'] : 0;
var value = (isHorizontal ? width : height) || contentValue;
_this.setLevelAndScrolling(open, placementName, value);
};
_this.setLevelTransform = function (open, placementName, value, right) {
var _this$props3 = _this.props,
placement = _this$props3.placement,
levelMove = _this$props3.levelMove,
duration = _this$props3.duration,
ease = _this$props3.ease,
showMask = _this$props3.showMask; // router 切换时可能会导至页面失去滚动条,所以需要时时获取。
_this.levelDom.forEach(function (dom) {
dom.style.transition = "transform ".concat(duration, " ").concat(ease);
(0, _utils.addEventListener)(dom, _utils.transitionEnd, _this.transitionEnd);
var levelValue = open ? value : 0;
if (levelMove) {
var $levelMove = (0, _utils.transformArguments)(levelMove, {
target: dom,
open: open
});
levelValue = open ? $levelMove[0] : $levelMove[1] || 0;
}
var $value = typeof levelValue === 'number' ? "".concat(levelValue, "px") : levelValue;
var placementPos = placement === 'left' || placement === 'top' ? $value : "-".concat($value);
placementPos = showMask && placement === 'right' && right ? "calc(".concat(placementPos, " + ").concat(right, "px)") : placementPos;
dom.style.transform = levelValue ? "".concat(placementName, "(").concat(placementPos, ")") : '';
});
};
_this.setLevelAndScrolling = function (open, placementName, value) {
var onChange = _this.props.onChange;
if (!_utils.windowIsUndefined) {
var right = document.body.scrollHeight > (window.innerHeight || document.documentElement.clientHeight) && window.innerWidth > document.body.offsetWidth ? (0, _getScrollBarSize.default)(true) : 0;
_this.setLevelTransform(open, placementName, value, right);
_this.toggleScrollingToDrawerAndBody(right);
}
if (onChange) {
onChange(open);
}
};
_this.toggleScrollingToDrawerAndBody = function (right) {
var _this$props4 = _this.props,
getOpenCount = _this$props4.getOpenCount,
getContainer = _this$props4.getContainer,
showMask = _this$props4.showMask,
open = _this$props4.open;
var container = getContainer && getContainer();
var openCount = getOpenCount && getOpenCount(); // 处理 body 滚动
if (container && container.parentNode === document.body && showMask) {
var eventArray = ['touchstart'];
var domArray = [document.body, _this.maskDom, _this.handlerDom, _this.contentDom];
if (open && document.body.style.overflow !== 'hidden') {
if (right) {
_this.addScrollingEffect(right);
}
if (openCount === 1) {
document.body.style.overflow = 'hidden';
}
document.body.style.touchAction = 'none'; // 手机禁滚
domArray.forEach(function (item, i) {
if (!item) {
return;
}
(0, _utils.addEventListener)(item, eventArray[i] || 'touchmove', i ? _this.removeMoveHandler : _this.removeStartHandler, _this.passive);
});
} else if (_this.getCurrentDrawerSome()) {
// 没有弹框的状态下清除 overflow;
if (!openCount) {
document.body.style.overflow = '';
}
document.body.style.touchAction = '';
if (right) {
_this.remScrollingEffect(right);
} // 恢复事件
domArray.forEach(function (item, i) {
if (!item) {
return;
}
(0, _utils.removeEventListener)(item, eventArray[i] || 'touchmove', i ? _this.removeMoveHandler : _this.removeStartHandler, _this.passive);
});
}
}
};
_this.addScrollingEffect = function (right) {
var _this$props5 = _this.props,
placement = _this$props5.placement,
duration = _this$props5.duration,
ease = _this$props5.ease,
getOpenCount = _this$props5.getOpenCount,
switchScrollingEffect = _this$props5.switchScrollingEffect;
var openCount = getOpenCount && getOpenCount();
if (openCount === 1) {
switchScrollingEffect();
}
var widthTransition = "width ".concat(duration, " ").concat(ease);
var transformTransition = "transform ".concat(duration, " ").concat(ease);
_this.dom.style.transition = 'none';
switch (placement) {
case 'right':
_this.dom.style.transform = "translateX(-".concat(right, "px)");
break;
case 'top':
case 'bottom':
_this.dom.style.width = "calc(100% - ".concat(right, "px)");
_this.dom.style.transform = 'translateZ(0)';
break;
default:
break;
}
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function () {
if (_this.dom) {
_this.dom.style.transition = "".concat(transformTransition, ",").concat(widthTransition);
_this.dom.style.width = '';
_this.dom.style.transform = '';
}
});
};
_this.remScrollingEffect = function (right) {
var _this$props6 = _this.props,
placement = _this$props6.placement,
duration = _this$props6.duration,
ease = _this$props6.ease,
getOpenCount = _this$props6.getOpenCount,
switchScrollingEffect = _this$props6.switchScrollingEffect;
var openCount = getOpenCount && getOpenCount();
if (!openCount) {
switchScrollingEffect(true);
}
if (_utils.transitionStr) {
document.body.style.overflowX = 'hidden';
}
_this.dom.style.transition = 'none';
var heightTransition;
var widthTransition = "width ".concat(duration, " ").concat(ease);
var transformTransition = "transform ".concat(duration, " ").concat(ease);
switch (placement) {
case 'left':
{
_this.dom.style.width = '100%';
widthTransition = "width 0s ".concat(ease, " ").concat(duration);
break;
}
case 'right':
{
_this.dom.style.transform = "translateX(".concat(right, "px)");
_this.dom.style.width = '100%';
widthTransition = "width 0s ".concat(ease, " ").concat(duration);
if (_this.maskDom) {
_this.maskDom.style.left = "-".concat(right, "px");
_this.maskDom.style.width = "calc(100% + ".concat(right, "px)");
}
break;
}
case 'top':
case 'bottom':
{
_this.dom.style.width = "calc(100% + ".concat(right, "px)");
_this.dom.style.height = '100%';
_this.dom.style.transform = 'translateZ(0)';
heightTransition = "height 0s ".concat(ease, " ").concat(duration);
break;
}
default:
break;
}
clearTimeout(_this.timeout);
_this.timeout = setTimeout(function () {
if (_this.dom) {
_this.dom.style.transition = "".concat(transformTransition, ",").concat(heightTransition ? "".concat(heightTransition, ",") : '').concat(widthTransition);
_this.dom.style.transform = '';
_this.dom.style.width = '';
_this.dom.style.height = '';
}
});
};
_this.getCurrentDrawerSome = function () {
return !Object.keys(currentDrawer).some(function (key) {
return currentDrawer[key];
});
};
_this.getLevelDom = function (_ref) {
var level = _ref.level,
getContainer = _ref.getContainer;
if (_utils.windowIsUndefined) {
return;
}
var container = getContainer && getContainer();
var parent = container ? container.parentNode : null;
_this.levelDom = [];
if (level === 'all') {
var children = parent ? Array.prototype.slice.call(parent.children) : [];
children.forEach(function (child) {
if (child.nodeName !== 'SCRIPT' && child.nodeName !== 'STYLE' && child.nodeName !== 'LINK' && child !== container) {
_this.levelDom.push(child);
}
});
} else if (level) {
(0, _utils.dataToArray)(level).forEach(function (key) {
document.querySelectorAll(key).forEach(function (item) {
_this.levelDom.push(item);
});
});
}
};
_this.getHorizontalBoolAndPlacementName = function () {
var placement = _this.props.placement;
var isHorizontal = placement === 'left' || placement === 'right';
var placementName = "translate".concat(isHorizontal ? 'X' : 'Y');
return {
isHorizontal: isHorizontal,
placementName: placementName
};
};
_this.state = {
_self: (0, _assertThisInitialized2.default)(_this)
};
return _this;
}
(0, _createClass2.default)(DrawerChild, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
if (!_utils.windowIsUndefined) {
var passiveSupported = false;
try {
window.addEventListener('test', null, Object.defineProperty({}, 'passive', {
get: function get() {
passiveSupported = true;
return null;
}
}));
} catch (err) {}
this.passive = passiveSupported ? {
passive: false
} : false;
}
var _this$props7 = this.props,
open = _this$props7.open,
getContainer = _this$props7.getContainer;
var container = getContainer && getContainer();
this.drawerId = "drawer_id_".concat(Number((Date.now() + Math.random()).toString().replace('.', Math.round(Math.random() * 9).toString())).toString(16));
this.getLevelDom(this.props);
if (open) {
if (container && container.parentNode === document.body) {
currentDrawer[this.drawerId] = open;
} // 默认打开状态时推出 level;
this.openLevelTransition();
this.forceUpdate(function () {
_this2.domFocus();
});
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
var _this$props8 = this.props,
open = _this$props8.open,
getContainer = _this$props8.getContainer;
var container = getContainer && getContainer();
if (open !== prevProps.open) {
if (open) {
this.domFocus();
}
if (container && container.parentNode === document.body) {
currentDrawer[this.drawerId] = !!open;
}
this.openLevelTransition();
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
var _this$props9 = this.props,
getOpenCount = _this$props9.getOpenCount,
open = _this$props9.open,
switchScrollingEffect = _this$props9.switchScrollingEffect;
var openCount = typeof getOpenCount === 'function' && getOpenCount();
delete currentDrawer[this.drawerId];
if (open) {
this.setLevelTransform(false);
document.body.style.touchAction = '';
}
if (!openCount) {
document.body.style.overflow = '';
switchScrollingEffect(true);
}
} // tslint:disable-next-line:member-ordering
}, {
key: "render",
value: function render() {
var _classnames,
_this3 = this;
var _this$props10 = this.props,
className = _this$props10.className,
children = _this$props10.children,
style = _this$props10.style,
width = _this$props10.width,
height = _this$props10.height,
defaultOpen = _this$props10.defaultOpen,
$open = _this$props10.open,
prefixCls = _this$props10.prefixCls,
placement = _this$props10.placement,
level = _this$props10.level,
levelMove = _this$props10.levelMove,
ease = _this$props10.ease,
duration = _this$props10.duration,
getContainer = _this$props10.getContainer,
handler = _this$props10.handler,
onChange = _this$props10.onChange,
afterVisibleChange = _this$props10.afterVisibleChange,
showMask = _this$props10.showMask,
maskClosable = _this$props10.maskClosable,
maskStyle = _this$props10.maskStyle,
onClose = _this$props10.onClose,
onHandleClick = _this$props10.onHandleClick,
keyboard = _this$props10.keyboard,
getOpenCount = _this$props10.getOpenCount,
switchScrollingEffect = _this$props10.switchScrollingEffect,
props = (0, _objectWithoutProperties2.default)(_this$props10, ["className", "children", "style", "width", "height", "defaultOpen", "open", "prefixCls", "placement", "level", "levelMove", "ease", "duration", "getContainer", "handler", "onChange", "afterVisibleChange", "showMask", "maskClosable", "maskStyle", "onClose", "onHandleClick", "keyboard", "getOpenCount", "switchScrollingEffect"]); // 首次渲染都将是关闭状态。
var open = this.dom ? $open : false;
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));
var _this$getHorizontalBo2 = this.getHorizontalBoolAndPlacementName(),
placementName = _this$getHorizontalBo2.placementName; // 百分比与像素动画不同步,第一次打用后全用像素动画。
// const defaultValue = !this.contentDom || !level ? '100%' : `${value}px`;
var placementPos = placement === 'left' || placement === 'top' ? '-100%' : '100%';
var transform = open ? '' : "".concat(placementName, "(").concat(placementPos, ")");
var handlerChildren = handler && React.cloneElement(handler, {
onClick: function onClick(e) {
if (handler.props.onClick) {
handler.props.onClick();
}
if (onHandleClick) {
onHandleClick(e);
}
},
ref: function ref(c) {
_this3.handlerDom = c;
}
});
return React.createElement("div", Object.assign({}, props, {
tabIndex: -1,
className: wrapperClassName,
style: style,
ref: function ref(c) {
_this3.dom = c;
},
onKeyDown: open && keyboard ? this.onKeyDown : undefined,
onTransitionEnd: this.onWrapperTransitionEnd
}), showMask && React.createElement("div", {
className: "".concat(prefixCls, "-mask"),
onClick: maskClosable ? onClose : undefined,
style: maskStyle,
ref: function ref(c) {
_this3.maskDom = c;
}
}), React.createElement("div", {
className: "".concat(prefixCls, "-content-wrapper"),
style: {
transform: transform,
msTransform: transform,
width: (0, _utils.isNumeric)(width) ? "".concat(width, "px") : width,
height: (0, _utils.isNumeric)(height) ? "".concat(height, "px") : height
},
ref: function ref(c) {
_this3.contentWrapper = c;
}
}, React.createElement("div", {
className: "".concat(prefixCls, "-content"),
ref: function ref(c) {
_this3.contentDom = c;
},
onTouchStart: open && showMask ? this.removeStartHandler : undefined,
onTouchMove: open && showMask ? this.removeMoveHandler : undefined
}, children), handlerChildren));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(props, _ref2) {
var prevProps = _ref2.prevProps,
_self = _ref2._self;
var nextState = {
prevProps: props
};
if (prevProps !== undefined) {
var placement = props.placement,
level = props.level;
if (placement !== prevProps.placement) {
// test 的 bug, 有动画过场,删除 dom
_self.contentDom = null;
}
if (level !== prevProps.level) {
_self.getLevelDom(props);
}
}
return nextState;
}
}]);
return DrawerChild;
}(React.Component);
DrawerChild.defaultProps = {
switchScrollingEffect: function switchScrollingEffect() {}
};
var _default = DrawerChild;
exports.default = _default;