@@ -2,15 +2,12 @@ import { raf } from '@rc-component/util';
22import { useRef } from 'react' ;
33import isFF from '../utils/isFirefox' ;
44import useOriginScroll from './useOriginScroll' ;
5-
65interface FireFoxDOMMouseScrollEvent {
76 detail : number ;
87 preventDefault : VoidFunction ;
98}
10-
119const LINE_HEIGHT = 16 ;
1210const PAGE_HEIGHT = 100 ;
13-
1411function normalizeWheelDelta ( delta : number , deltaMode : number ) : number {
1512 if ( deltaMode === 1 ) {
1613 return delta * LINE_HEIGHT ;
@@ -20,7 +17,6 @@ function normalizeWheelDelta(delta: number, deltaMode: number): number {
2017 }
2118 return delta ;
2219}
23-
2420export default function useFrameWheel (
2521 inVirtual : boolean ,
2622 isScrollAtTop : boolean ,
@@ -35,25 +31,20 @@ export default function useFrameWheel(
3531) : [ ( e : WheelEvent ) => void , ( e : FireFoxDOMMouseScrollEvent ) => void ] {
3632 const offsetRef = useRef ( 0 ) ;
3733 const nextFrameRef = useRef < number > ( null ) ;
38-
3934 // Firefox patch
4035 const wheelValueRef = useRef < number > ( null ) ;
4136 const isMouseScrollRef = useRef < boolean > ( false ) ;
42-
4337 // Scroll status sync
4438 const originScroll = useOriginScroll (
4539 isScrollAtTop ,
4640 isScrollAtBottom ,
4741 isScrollAtLeft ,
4842 isScrollAtRight ,
4943 ) ;
50-
51- function onWheelY ( e : WheelEvent , deltaY : number ) {
44+ function onWheelY ( e : WheelEvent , rawDeltaY : number , deltaY : number ) {
5245 raf . cancel ( nextFrameRef . current ) ;
53-
5446 // Do nothing when scroll at the edge, Skip check when is in scroll
5547 if ( originScroll ( false , deltaY ) ) return ;
56-
5748 // Skip if nest List has handled this event
5849 const event = e as WheelEvent & {
5950 _virtualHandled ?: boolean ;
@@ -63,15 +54,14 @@ export default function useFrameWheel(
6354 } else {
6455 return ;
6556 }
66-
6757 offsetRef . current += deltaY ;
68- wheelValueRef . current = deltaY ;
69-
58+ // Keep the raw delta here so the Firefox `DOMMouseScroll.detail`
59+ // comparison in `onFireFoxScroll` still matches (detail is in lines).
60+ wheelValueRef . current = rawDeltaY ;
7061 // Proxy of scroll events
7162 if ( ! isFF ) {
7263 event . preventDefault ( ) ;
7364 }
74-
7565 nextFrameRef . current = raf ( ( ) => {
7666 // Patch a multiple for Firefox to fix wheel number too small
7767 // ref: https://github.com/ant-design/ant-design/issues/26372#issuecomment-679460266
@@ -80,63 +70,48 @@ export default function useFrameWheel(
8070 offsetRef . current = 0 ;
8171 } ) ;
8272 }
83-
8473 function onWheelX ( event : WheelEvent , deltaX : number ) {
8574 onWheelDelta ( deltaX , true ) ;
86-
8775 if ( ! isFF ) {
8876 event . preventDefault ( ) ;
8977 }
9078 }
91-
9279 // Check for which direction does wheel do. `sx` means `shift + wheel`
9380 const wheelDirectionRef = useRef < 'x' | 'y' | 'sx' | null > ( null ) ;
9481 const wheelDirectionCleanRef = useRef < number > ( null ) ;
95-
9682 function onWheel ( event : WheelEvent ) {
9783 if ( ! inVirtual ) return ;
98-
9984 // Wait for 2 frame to clean direction
10085 raf . cancel ( wheelDirectionCleanRef . current ) ;
10186 wheelDirectionCleanRef . current = raf ( ( ) => {
10287 wheelDirectionRef . current = null ;
10388 } , 2 ) ;
104-
10589 const { deltaX, deltaY, deltaMode, shiftKey } = event ;
106-
10790 let mergedDeltaX = normalizeWheelDelta ( deltaX , deltaMode ) ;
10891 let mergedDeltaY = normalizeWheelDelta ( deltaY , deltaMode ) ;
109-
11092 if (
11193 wheelDirectionRef . current === 'sx' ||
112- ( ! wheelDirectionRef . current && ( shiftKey || false ) && deltaY && ! deltaX )
94+ ( ! wheelDirectionRef . current && ( shiftKey || false ) && mergedDeltaY && ! mergedDeltaX )
11395 ) {
114- mergedDeltaX = deltaY ;
96+ mergedDeltaX = mergedDeltaY ;
11597 mergedDeltaY = 0 ;
116-
11798 wheelDirectionRef . current = 'sx' ;
11899 }
119-
120100 const absX = Math . abs ( mergedDeltaX ) ;
121101 const absY = Math . abs ( mergedDeltaY ) ;
122-
123102 if ( wheelDirectionRef . current === null ) {
124103 wheelDirectionRef . current = horizontalScroll && absX > absY ? 'x' : 'y' ;
125104 }
126-
127105 if ( wheelDirectionRef . current === 'y' ) {
128- onWheelY ( event , mergedDeltaY ) ;
106+ onWheelY ( event , deltaY , mergedDeltaY ) ;
129107 } else {
130108 onWheelX ( event , mergedDeltaX ) ;
131109 }
132110 }
133-
134111 // A patch for firefox
135112 function onFireFoxScroll ( event : FireFoxDOMMouseScrollEvent ) {
136113 if ( ! inVirtual ) return ;
137-
138114 isMouseScrollRef . current = event . detail === wheelValueRef . current ;
139115 }
140-
141116 return [ onWheel , onFireFoxScroll ] ;
142117}
0 commit comments