useFrameWheel.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import raf from '../../_util/raf';
  2. import isFF from '../utils/isFirefox';
  3. import useOriginScroll from './useOriginScroll';
  4. export default function useFrameWheel(inVirtual, isScrollAtTop, isScrollAtBottom, onWheelDelta) {
  5. var offsetRef = 0;
  6. var nextFrame = null;
  7. // Firefox patch
  8. var wheelValue = null;
  9. var isMouseScroll = false;
  10. // Scroll status sync
  11. var originScroll = useOriginScroll(isScrollAtTop, isScrollAtBottom);
  12. function onWheel(event) {
  13. if (!inVirtual.value) return;
  14. raf.cancel(nextFrame);
  15. var deltaY = event.deltaY;
  16. offsetRef += deltaY;
  17. wheelValue = deltaY;
  18. // Do nothing when scroll at the edge, Skip check when is in scroll
  19. if (originScroll(deltaY)) return;
  20. // Proxy of scroll events
  21. if (!isFF) {
  22. event.preventDefault();
  23. }
  24. nextFrame = raf(function () {
  25. // Patch a multiple for Firefox to fix wheel number too small
  26. // ref: https://github.com/ant-design/ant-design/issues/26372#issuecomment-679460266
  27. var patchMultiple = isMouseScroll ? 10 : 1;
  28. onWheelDelta(offsetRef * patchMultiple);
  29. offsetRef = 0;
  30. });
  31. }
  32. // A patch for firefox
  33. function onFireFoxScroll(event) {
  34. if (!inVirtual.value) return;
  35. isMouseScroll = event.detail === wheelValue;
  36. }
  37. return [onWheel, onFireFoxScroll];
  38. }