UNPKG

2.23 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.isSamePoint = isSamePoint;
9exports.monitorResize = monitorResize;
10exports.restoreFocus = restoreFocus;
11
12var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
14var _contains = _interopRequireDefault(require("../vc-util/Dom/contains"));
15
16var _resizeObserverPolyfill = _interopRequireDefault(require("resize-observer-polyfill"));
17
18function isSamePoint(prev, next) {
19 if (prev === next) return true;
20 if (!prev || !next) return false;
21
22 if ('pageX' in next && 'pageY' in next) {
23 return prev.pageX === next.pageX && prev.pageY === next.pageY;
24 }
25
26 if ('clientX' in next && 'clientY' in next) {
27 return prev.clientX === next.clientX && prev.clientY === next.clientY;
28 }
29
30 return false;
31}
32
33function restoreFocus(activeElement, container) {
34 // Focus back if is in the container
35 if (activeElement !== document.activeElement && (0, _contains.default)(container, activeElement) && typeof activeElement.focus === 'function') {
36 activeElement.focus();
37 }
38}
39
40function monitorResize(element, callback) {
41 var prevWidth = null;
42 var prevHeight = null;
43
44 function onResize(_ref) {
45 var _ref2 = (0, _slicedToArray2.default)(_ref, 1),
46 target = _ref2[0].target;
47
48 if (!document.documentElement.contains(target)) return;
49
50 var _target$getBoundingCl = target.getBoundingClientRect(),
51 width = _target$getBoundingCl.width,
52 height = _target$getBoundingCl.height;
53
54 var fixedWidth = Math.floor(width);
55 var fixedHeight = Math.floor(height);
56
57 if (prevWidth !== fixedWidth || prevHeight !== fixedHeight) {
58 // https://webkit.org/blog/9997/resizeobserver-in-webkit/
59 Promise.resolve().then(function () {
60 callback({
61 width: fixedWidth,
62 height: fixedHeight
63 });
64 });
65 }
66
67 prevWidth = fixedWidth;
68 prevHeight = fixedHeight;
69 }
70
71 var resizeObserver = new _resizeObserverPolyfill.default(onResize);
72
73 if (element) {
74 resizeObserver.observe(element);
75 }
76
77 return function () {
78 resizeObserver.disconnect();
79 };
80}
\No newline at end of file