Skip to content

Commit

Permalink
fix: Firefox should prevent scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Sep 14, 2020
1 parent a4cb1c4 commit 4a123b3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Demo = () => {

return (
<React.StrictMode>
<div>
<div style={{ height: '200vh' }}>
<h2>Basic</h2>
{TYPES.map(({ name, type: nType }) => (
<label key={nType}>
Expand Down
8 changes: 8 additions & 0 deletions src/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,19 @@ export function RawList<T>(props: ListProps<T>, ref: React.Ref<ListRef>) {
});

React.useEffect(() => {
// Firefox only
function onMozMousePixelScroll(e: Event) {
e.preventDefault();
}

componentRef.current.addEventListener('wheel', onRawWheel);
componentRef.current.addEventListener('DOMMouseScroll', onFireFoxScroll as any);
componentRef.current.addEventListener('MozMousePixelScroll', onMozMousePixelScroll);

return () => {
componentRef.current.removeEventListener('wheel', onRawWheel);
componentRef.current.removeEventListener('DOMMouseScroll', onFireFoxScroll as any);
componentRef.current.removeEventListener('MozMousePixelScroll', onMozMousePixelScroll as any);
};
}, [inVirtual]);

Expand Down
3 changes: 0 additions & 3 deletions src/hooks/useFrameWheel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ export default function useFrameWheel(
function onFireFoxScroll(event: FireFoxDOMMouseScrollEvent) {
if (!inVirtual) return;

// Firefox level stop
event.preventDefault();

isMouseScrollRef.current = event.detail === wheelValueRef.current;
}

Expand Down
7 changes: 6 additions & 1 deletion tests/scroll-Firefox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ describe('List.Firefox-Scroll', () => {
wheelEvent.preventDefault = wheelPreventDefault;
ulElement.dispatchEvent(wheelEvent);

const firefoxPixelScrollEvent = new Event('MozMousePixelScroll');
firefoxPixelScrollEvent.detail = 6;
firefoxPixelScrollEvent.preventDefault = firefoxPreventDefault;
ulElement.dispatchEvent(firefoxPixelScrollEvent);

const firefoxScrollEvent = new Event('DOMMouseScroll');
firefoxScrollEvent.detail = 3;
firefoxScrollEvent.preventDefault = firefoxPreventDefault;
Expand All @@ -77,6 +82,6 @@ describe('List.Firefox-Scroll', () => {
});

expect(wheelPreventDefault).not.toHaveBeenCalled();
expect(firefoxPreventDefault).toHaveBeenCalled();
expect(firefoxPreventDefault).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 4a123b3

Please sign in to comment.