Skip to content

Commit

Permalink
fix: can't scroll the document on Firefox (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanszeng-chn and [email protected] authored Sep 26, 2024
1 parent 87bbdd5 commit 2945296
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,11 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {

useLayoutEffect(() => {
// Firefox only
function onMozMousePixelScroll(e: Event) {
if (useVirtual) {
function onMozMousePixelScroll(e: WheelEvent) {
// scrolling at top/bottom limit
const scrollingUpAtTop = isScrollAtTop && e.detail < 0;
const scrollingDownAtBottom = isScrollAtBottom && e.detail > 0;
if (useVirtual && !scrollingUpAtTop && !scrollingDownAtBottom) {
e.preventDefault();
}
}
Expand All @@ -454,7 +457,7 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
componentEle.removeEventListener('DOMMouseScroll', onFireFoxScroll as any);
componentEle.removeEventListener('MozMousePixelScroll', onMozMousePixelScroll as any);
};
}, [useVirtual]);
}, [useVirtual, isScrollAtTop, isScrollAtBottom]);

// Sync scroll left
useLayoutEffect(() => {
Expand Down
55 changes: 55 additions & 0 deletions tests/scroll-Firefox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,59 @@ describe('List.Firefox-Scroll', () => {
expect(wheelPreventDefault).not.toHaveBeenCalled();
expect(firefoxPreventDefault).toHaveBeenCalledTimes(1);
});

it('should call preventDefault on MozMousePixelScroll', () => {
const preventDefault = jest.fn();
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
const ulElement = wrapper.find('ul').instance();

act(() => {
const event = new Event('MozMousePixelScroll');
event.detail = 6;
event.preventDefault = preventDefault;
ulElement.dispatchEvent(event);

jest.runAllTimers();
});

expect(preventDefault).toHaveBeenCalled();
});

it('should not call preventDefault on MozMousePixelScroll when scrolling up at top boundary', () => {
const preventDefault = jest.fn();
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100) });
const ulElement = wrapper.find('ul').instance();

act(() => {
const event = new Event('MozMousePixelScroll');
event.detail = -6;
event.preventDefault = preventDefault;
ulElement.dispatchEvent(event);

jest.runAllTimers();
});

expect(preventDefault).not.toHaveBeenCalled();
});
it('should not call preventDefault on MozMousePixelScroll when scrolling down at bottom boundary', () => {
const preventDefault = jest.fn();
const listRef = React.createRef();
const wrapper = genList({ itemHeight: 20, height: 100, data: genData(100), ref: listRef });
const ulElement = wrapper.find('ul').instance();
// scroll to bottom
listRef.current.scrollTo(99999);
jest.runAllTimers();
expect(wrapper.find('ul').instance().scrollTop).toEqual(1900);

act(() => {
const event = new Event('MozMousePixelScroll');
event.detail = 6;
event.preventDefault = preventDefault;
ulElement.dispatchEvent(event);

jest.runAllTimers();
});

expect(preventDefault).not.toHaveBeenCalled();
});
});

0 comments on commit 2945296

Please sign in to comment.