-
Notifications
You must be signed in to change notification settings - Fork 167
fix: Add patch fix for firefox mouse wheel #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
import { useRef } from 'react'; | ||
import raf from 'rc-util/lib/raf'; | ||
import isFF from '../utils/isFirefox'; | ||
|
||
export default function useFrameWheel(inVirtual: boolean, onWheelDelta: (offset: number) => void) { | ||
interface FireFoxDOMMouseScrollEvent { | ||
detail: number; | ||
preventDefault: Function; | ||
} | ||
|
||
export default function useFrameWheel( | ||
inVirtual: boolean, | ||
onWheelDelta: (offset: number) => void, | ||
): [(e: WheelEvent) => void, (e: FireFoxDOMMouseScrollEvent) => void] { | ||
const offsetRef = useRef(0); | ||
const nextFrameRef = useRef<number>(null); | ||
|
||
function onWheel(event: MouseWheelEvent) { | ||
// Firefox patch | ||
const wheelValueRef = useRef<number>(null); | ||
const isMouseScrollRef = useRef<boolean>(false); | ||
|
||
function onWheel(event: WheelEvent) { | ||
if (!inVirtual) return; | ||
|
||
// Proxy of scroll events | ||
event.preventDefault(); | ||
if (!isFF) { | ||
event.preventDefault(); | ||
} | ||
|
||
raf.cancel(nextFrameRef.current); | ||
|
||
offsetRef.current += event.deltaY; | ||
wheelValueRef.current = event.deltaY; | ||
|
||
nextFrameRef.current = raf(() => { | ||
onWheelDelta(offsetRef.current); | ||
// Patch a multiple for Firefox to fix wheel number too small | ||
// ref: https://github.com/ant-design/ant-design/issues/26372#issuecomment-679460266 | ||
const patchMultiple = isMouseScrollRef.current ? 10 : 1; | ||
onWheelDelta(offsetRef.current * patchMultiple); | ||
offsetRef.current = 0; | ||
}); | ||
} | ||
|
||
return onWheel; | ||
// A patch for firefox | ||
function onFireFoxScroll(event: FireFoxDOMMouseScrollEvent) { | ||
if (!inVirtual) return; | ||
|
||
// Firefox level stop | ||
event.preventDefault(); | ||
|
||
isMouseScrollRef.current = event.detail === wheelValueRef.current; | ||
} | ||
|
||
return [onWheel, onFireFoxScroll]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const isFF = /Firefox/i.test(navigator.userAgent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to fix for serveer-side-rendering const isFF = typeof navigator !== 'undefined' && /Firefox/i.test(navigator.userAgent); Current nodejs error: ReferenceError: navigator is not defined
vpc-admin: [dev] at Object.<anonymous> (/Users/nodkz/www/ps/vpc/apps/admin/node_modules/rc-virtual-list/lib/utils/isFirefox.js:7:28) |
||
export default isFF; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { mount } from 'enzyme'; | ||
import { spyElementPrototypes } from './utils/domHook'; | ||
import List from '../src'; | ||
import isFF from '../src/utils/isFirefox'; | ||
|
||
function genData(count) { | ||
return new Array(count).fill(null).map((_, index) => ({ id: String(index) })); | ||
} | ||
|
||
jest.mock('../src/utils/isFirefox', () => true); | ||
|
||
describe('List.Firefox-Scroll', () => { | ||
let mockElement; | ||
|
||
beforeAll(() => { | ||
mockElement = spyElementPrototypes(HTMLElement, { | ||
offsetHeight: { | ||
get: () => 20, | ||
}, | ||
clientHeight: { | ||
get: () => 100, | ||
}, | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
mockElement.mockRestore(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
function genList(props) { | ||
let node = ( | ||
<List component="ul" itemKey="id" {...props}> | ||
{({ id }) => <li>{id}</li>} | ||
</List> | ||
); | ||
|
||
if (props.ref) { | ||
node = <div>{node}</div>; | ||
} | ||
|
||
return mount(node); | ||
} | ||
|
||
it('should be true', () => { | ||
expect(isFF).toBe(true); | ||
}); | ||
|
||
// https://github.com/ant-design/ant-design/issues/26372 | ||
it('FireFox should patch scroll speed', () => { | ||
const wheelPreventDefault = jest.fn(); | ||
const firefoxPreventDefault = jest.fn(); | ||
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) }); | ||
const ulElement = wrapper.find('ul').instance(); | ||
|
||
act(() => { | ||
const wheelEvent = new Event('wheel'); | ||
wheelEvent.deltaY = 3; | ||
wheelEvent.preventDefault = wheelPreventDefault; | ||
ulElement.dispatchEvent(wheelEvent); | ||
|
||
const firefoxScrollEvent = new Event('DOMMouseScroll'); | ||
firefoxScrollEvent.detail = 3; | ||
firefoxScrollEvent.preventDefault = firefoxPreventDefault; | ||
ulElement.dispatchEvent(firefoxScrollEvent); | ||
|
||
jest.runAllTimers(); | ||
}); | ||
|
||
expect(wheelPreventDefault).not.toHaveBeenCalled(); | ||
expect(firefoxPreventDefault).toHaveBeenCalled(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个数字会不会跟着操作系统和硬件发生变化?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
和 FF 内部的

mousewheel.default.delta_multiplier_y
有关,FF 本身不推荐修改它,可以忽略。如果遇到了再调整: