util.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  2. import contains from '../vc-util/Dom/contains';
  3. import ResizeObserver from 'resize-observer-polyfill';
  4. export function isSamePoint(prev, next) {
  5. if (prev === next) return true;
  6. if (!prev || !next) return false;
  7. if ('pageX' in next && 'pageY' in next) {
  8. return prev.pageX === next.pageX && prev.pageY === next.pageY;
  9. }
  10. if ('clientX' in next && 'clientY' in next) {
  11. return prev.clientX === next.clientX && prev.clientY === next.clientY;
  12. }
  13. return false;
  14. }
  15. export function restoreFocus(activeElement, container) {
  16. // Focus back if is in the container
  17. if (activeElement !== document.activeElement && contains(container, activeElement) && typeof activeElement.focus === 'function') {
  18. activeElement.focus();
  19. }
  20. }
  21. export function monitorResize(element, callback) {
  22. var prevWidth = null;
  23. var prevHeight = null;
  24. function onResize(_ref) {
  25. var _ref2 = _slicedToArray(_ref, 1),
  26. target = _ref2[0].target;
  27. if (!document.documentElement.contains(target)) return;
  28. var _target$getBoundingCl = target.getBoundingClientRect(),
  29. width = _target$getBoundingCl.width,
  30. height = _target$getBoundingCl.height;
  31. var fixedWidth = Math.floor(width);
  32. var fixedHeight = Math.floor(height);
  33. if (prevWidth !== fixedWidth || prevHeight !== fixedHeight) {
  34. // https://webkit.org/blog/9997/resizeobserver-in-webkit/
  35. Promise.resolve().then(function () {
  36. callback({
  37. width: fixedWidth,
  38. height: fixedHeight
  39. });
  40. });
  41. }
  42. prevWidth = fixedWidth;
  43. prevHeight = fixedHeight;
  44. }
  45. var resizeObserver = new ResizeObserver(onResize);
  46. if (element) {
  47. resizeObserver.observe(element);
  48. }
  49. return function () {
  50. resizeObserver.disconnect();
  51. };
  52. }