handleScroll.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.handleScroll = exports.locationCouldBeScrolled = void 0;
  4. var alwaysContainsScroll = function (node) {
  5. // textarea will always _contain_ scroll inside self. It only can be hidden
  6. return node.tagName === 'TEXTAREA';
  7. };
  8. var elementCanBeScrolled = function (node, overflow) {
  9. var styles = window.getComputedStyle(node);
  10. return (
  11. // not-not-scrollable
  12. styles[overflow] !== 'hidden' &&
  13. // contains scroll inside self
  14. !(styles.overflowY === styles.overflowX && !alwaysContainsScroll(node) && styles[overflow] === 'visible'));
  15. };
  16. var elementCouldBeVScrolled = function (node) { return elementCanBeScrolled(node, 'overflowY'); };
  17. var elementCouldBeHScrolled = function (node) { return elementCanBeScrolled(node, 'overflowX'); };
  18. var locationCouldBeScrolled = function (axis, node) {
  19. var current = node;
  20. do {
  21. // Skip over shadow root
  22. if (typeof ShadowRoot !== 'undefined' && current instanceof ShadowRoot) {
  23. current = current.host;
  24. }
  25. var isScrollable = elementCouldBeScrolled(axis, current);
  26. if (isScrollable) {
  27. var _a = getScrollVariables(axis, current), s = _a[1], d = _a[2];
  28. if (s > d) {
  29. return true;
  30. }
  31. }
  32. current = current.parentNode;
  33. } while (current && current !== document.body);
  34. return false;
  35. };
  36. exports.locationCouldBeScrolled = locationCouldBeScrolled;
  37. var getVScrollVariables = function (_a) {
  38. var scrollTop = _a.scrollTop, scrollHeight = _a.scrollHeight, clientHeight = _a.clientHeight;
  39. return [
  40. scrollTop,
  41. scrollHeight,
  42. clientHeight,
  43. ];
  44. };
  45. var getHScrollVariables = function (_a) {
  46. var scrollLeft = _a.scrollLeft, scrollWidth = _a.scrollWidth, clientWidth = _a.clientWidth;
  47. return [
  48. scrollLeft,
  49. scrollWidth,
  50. clientWidth,
  51. ];
  52. };
  53. var elementCouldBeScrolled = function (axis, node) {
  54. return axis === 'v' ? elementCouldBeVScrolled(node) : elementCouldBeHScrolled(node);
  55. };
  56. var getScrollVariables = function (axis, node) {
  57. return axis === 'v' ? getVScrollVariables(node) : getHScrollVariables(node);
  58. };
  59. var getDirectionFactor = function (axis, direction) {
  60. /**
  61. * If the element's direction is rtl (right-to-left), then scrollLeft is 0 when the scrollbar is at its rightmost position,
  62. * and then increasingly negative as you scroll towards the end of the content.
  63. * @see https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft
  64. */
  65. return axis === 'h' && direction === 'rtl' ? -1 : 1;
  66. };
  67. var handleScroll = function (axis, endTarget, event, sourceDelta, noOverscroll) {
  68. var directionFactor = getDirectionFactor(axis, window.getComputedStyle(endTarget).direction);
  69. var delta = directionFactor * sourceDelta;
  70. // find scrollable target
  71. var target = event.target;
  72. var targetInLock = endTarget.contains(target);
  73. var shouldCancelScroll = false;
  74. var isDeltaPositive = delta > 0;
  75. var availableScroll = 0;
  76. var availableScrollTop = 0;
  77. do {
  78. var _a = getScrollVariables(axis, target), position = _a[0], scroll_1 = _a[1], capacity = _a[2];
  79. var elementScroll = scroll_1 - capacity - directionFactor * position;
  80. if (position || elementScroll) {
  81. if (elementCouldBeScrolled(axis, target)) {
  82. availableScroll += elementScroll;
  83. availableScrollTop += position;
  84. }
  85. }
  86. target = target.parentNode;
  87. } while (
  88. // portaled content
  89. (!targetInLock && target !== document.body) ||
  90. // self content
  91. (targetInLock && (endTarget.contains(target) || endTarget === target)));
  92. if (isDeltaPositive && ((noOverscroll && availableScroll === 0) || (!noOverscroll && delta > availableScroll))) {
  93. shouldCancelScroll = true;
  94. }
  95. else if (!isDeltaPositive &&
  96. ((noOverscroll && availableScrollTop === 0) || (!noOverscroll && -delta > availableScrollTop))) {
  97. shouldCancelScroll = true;
  98. }
  99. return shouldCancelScroll;
  100. };
  101. exports.handleScroll = handleScroll;