ScrollArea: non-passive `wheel` listener on `document` makes the whole page a scroll-blocking region
Dependencies check up
- I have verified that I use latest version of all @mantine/* packages
What version of @mantine/* packages do you have in package.json?
9.6.3
What package has an issue?
@mantine/core
What framework do you use?
Vite
In which browsers you can reproduce the issue?
Other, I will specify in the bug description
Describe the bug
While a scrollbar is mounted, Scrollbar adds a non-passive wheel listener to document (Scrollbar.tsx#L57-L67):
useEffect(() => {
const handleWheel = (event: WheelEvent) => {
const element = event.target as HTMLElement;
const isScrollbarWheel = scrollbar?.contains(element);
if (isScrollbarWheel) {
handleWheelScroll(event, maxScrollPos);
}
};
document.addEventListener('wheel', handleWheel, { passive: false });
return () => document.removeEventListener('wheel', handleWheel, { passive: false } as any);
}, [viewport, scrollbar, maxScrollPos]);The handler only acts on wheel events that target the scrollbar. Because the listener is on document, though, every wheel event on the page goes through a non-passive listener. Chrome and WebKit only scroll on the compositor when every wheel listener on the event path is passive (Chrome, WebKit). Otherwise the scroll waits for the main thread to run the listeners.
A scrollbar is mounted:
- always for
type="always"; - while the content overflows for
type="auto"; - while the pointer is over the area for the default
type="hover"; - during scrolling for
type="scroll".
With the default type, every wheel scroll inside a ScrollArea therefore waits for the main thread. With always or auto, every wheel scroll anywhere on the page does. On a page with main-thread load, scrolls start late.
The effect also depends on maxScrollPos, so the listener is removed and added again whenever the content size changes.
Radix ScrollArea has the same code (scroll-area.tsx#L807-L819).
Reproduction:
import { ScrollArea } from '@mantine/core';
export default function Demo() {
return (
<div style={{ display: 'flex', gap: 16, height: '100vh' }}>
<div style={{ flex: 1, overflowY: 'auto' }}>
{Array.from({ length: 1000 }, (_, i) => <p key={i}>Native row {i}</p>)}
</div>
<ScrollArea type="always" h="100%" w={240}>
{Array.from({ length: 100 }, (_, i) => <p key={i}>ScrollArea row {i}</p>)}
</ScrollArea>
</div>
);
}- In DevTools, open Rendering and enable "Scrolling performance issues". The whole page is marked "mousewheel event listener", the native scroller included. Without the
ScrollArea, no region is marked "mousewheel event listener". - Measure the time until a wheel event is acknowledged (CDP
Input.dispatchMouseEvent, idle main thread, 20 events each; Chrome 153, React 19.2.8):
| Wheel over | 9.6.3 | With the fix below |
|---|---|---|
Native scroller, no ScrollArea on the page |
0.7 ms | — |
Native scroller, type="always" ScrollArea beside it |
15.6 ms | 0.6 ms |
Inside a type="hover" ScrollArea |
15.6 ms | 0.6 ms |
At 15.6 ms (about one frame at 60 Hz) the event has gone through the main thread before being acknowledged. Under main-thread load, that wait grows with the load.
"Scrolling performance issues" overlay, type="always" ScrollArea beside a native scroller:
| 9.6.3 | With the fix below |
|---|---|
If possible, include a link to a codesandbox with a minimal reproduction
The component above is the complete reproduction; it needs nothing beyond @mantine/core.
Possible fix
Listen on the scrollbar element instead of document, and pass maxScrollPos through the effect event, so the listener is registered once per scrollbar element:
@@ -40,9 +40,8 @@
const composeRefs = useMergedRef(ref, setScrollbar);
const rectRef = useRef<DOMRect | null>(null);
const prevWebkitUserSelectRef = useRef<string>('');
- const { viewport } = context;
const maxScrollPos = sizes.content - sizes.viewport;
- const handleWheelScroll = useEffectEvent(onWheelScroll);
+ const handleWheelScroll = useEffectEvent((event: WheelEvent) => onWheelScroll(event, maxScrollPos));
const handleThumbPositionChange = useCallbackRef(onThumbPositionChange);
const handleResize = useDebouncedCallback(onResize, 10);
@@ -55,17 +54,15 @@
};
useEffect(() => {
- const handleWheel = (event: WheelEvent) => {
- const element = event.target as HTMLElement;
- const isScrollbarWheel = scrollbar?.contains(element);
- if (isScrollbarWheel) {
- handleWheelScroll(event, maxScrollPos);
- }
- };
- document.addEventListener('wheel', handleWheel, { passive: false });
- return () => document.removeEventListener('wheel', handleWheel, { passive: false } as any);
- }, [viewport, scrollbar, maxScrollPos]);
+ if (!scrollbar) {
+ return undefined;
+ }
+ const handleWheel = (event: WheelEvent) => handleWheelScroll(event);
+ scrollbar.addEventListener('wheel', handleWheel, { passive: false });
+ return () => scrollbar.removeEventListener('wheel', handleWheel);
+ }, [scrollbar]);
+
useEffect(handleThumbPositionChange, [sizes, handleThumbPositionChange]);A wheel over the scrollbar still scrolls the viewport and does not scroll the page. Checked on 9.6.3 with the change applied: viewport scrollTop 0 → 100, window.scrollY 0 → 0, the same as without it. After the change only the scrollbars are marked as wheel listener regions, and the latencies are those in the last column of the table.
Self-service
- I would be willing to implement a fix for this issue
Source: mantinedev/mantine