useOriginScroll.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _react = require("react");
  7. var _default = exports.default = function _default(isScrollAtTop, isScrollAtBottom, isScrollAtLeft, isScrollAtRight) {
  8. // Do lock for a wheel when scrolling
  9. var lockRef = (0, _react.useRef)(false);
  10. var lockTimeoutRef = (0, _react.useRef)(null);
  11. function lockScroll() {
  12. clearTimeout(lockTimeoutRef.current);
  13. lockRef.current = true;
  14. lockTimeoutRef.current = setTimeout(function () {
  15. lockRef.current = false;
  16. }, 50);
  17. }
  18. // Pass to ref since global add is in closure
  19. var scrollPingRef = (0, _react.useRef)({
  20. top: isScrollAtTop,
  21. bottom: isScrollAtBottom,
  22. left: isScrollAtLeft,
  23. right: isScrollAtRight
  24. });
  25. scrollPingRef.current.top = isScrollAtTop;
  26. scrollPingRef.current.bottom = isScrollAtBottom;
  27. scrollPingRef.current.left = isScrollAtLeft;
  28. scrollPingRef.current.right = isScrollAtRight;
  29. return function (isHorizontal, delta) {
  30. var smoothOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  31. var originScroll = isHorizontal ?
  32. // Pass origin wheel when on the left
  33. delta < 0 && scrollPingRef.current.left ||
  34. // Pass origin wheel when on the right
  35. delta > 0 && scrollPingRef.current.right // Pass origin wheel when on the top
  36. : delta < 0 && scrollPingRef.current.top ||
  37. // Pass origin wheel when on the bottom
  38. delta > 0 && scrollPingRef.current.bottom;
  39. if (smoothOffset && originScroll) {
  40. // No need lock anymore when it's smooth offset from touchMove interval
  41. clearTimeout(lockTimeoutRef.current);
  42. lockRef.current = false;
  43. } else if (!originScroll || lockRef.current) {
  44. lockScroll();
  45. }
  46. return !lockRef.current && originScroll;
  47. };
  48. };