util.js
1.79 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
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import ResizeObserver from 'resize-observer-polyfill';
import contains from "rc-util/es/Dom/contains";
export function isSamePoint(prev, next) {
if (prev === next) return true;
if (!prev || !next) return false;
if ('pageX' in next && 'pageY' in next) {
return prev.pageX === next.pageX && prev.pageY === next.pageY;
}
if ('clientX' in next && 'clientY' in next) {
return prev.clientX === next.clientX && prev.clientY === next.clientY;
}
return false;
}
export function restoreFocus(activeElement, container) {
// Focus back if is in the container
if (activeElement !== document.activeElement && contains(container, activeElement) && typeof activeElement.focus === 'function') {
activeElement.focus();
}
}
export function monitorResize(element, callback) {
var prevWidth = null;
var prevHeight = null;
function onResize(_ref) {
var _ref2 = _slicedToArray(_ref, 1),
target = _ref2[0].target;
if (!document.documentElement.contains(target)) return;
var _target$getBoundingCl = target.getBoundingClientRect(),
width = _target$getBoundingCl.width,
height = _target$getBoundingCl.height;
var fixedWidth = Math.floor(width);
var fixedHeight = Math.floor(height);
if (prevWidth !== fixedWidth || prevHeight !== fixedHeight) {
// https://webkit.org/blog/9997/resizeobserver-in-webkit/
Promise.resolve().then(function () {
callback({
width: fixedWidth,
height: fixedHeight
});
});
}
prevWidth = fixedWidth;
prevHeight = fixedHeight;
}
var resizeObserver = new ResizeObserver(onResize);
if (element) {
resizeObserver.observe(element);
}
return function () {
resizeObserver.disconnect();
};
}