Files
outline/app/hooks/useWindowScrollbarWidth.ts
T
Tom MoorandClaude Fable 5 9af5e56523 fix: useWindowScrollbarWidth measured horizontal overflow, not scrollbar width (#13030)
* fix: useWindowScrollbarWidth measured horizontal overflow, not scrollbar width

document.documentElement.scrollWidth - clientWidth measures horizontal
content overflow, which is 0 in the normal case even when a vertical
scrollbar is visible — so the hook always reported 0 on platforms with
classic (non-overlay) scrollbars, and the right sidebar's fixed-position
content in Aside was never narrowed to account for the scrollbar.

Measure window.innerWidth - documentElement.clientWidth instead, which
is the layout viewport width including the scrollbar minus the width
excluding it. Verified in headless Chromium: with a forced vertical
scrollbar the old expression returns 0 while the new one returns the
true scrollbar width (15px).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DjhYP8svLL8dU98tmQJ7RK

* chore: Remove explanatory comment

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DjhYP8svLL8dU98tmQJ7RK

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-07-18 09:35:55 -04:00

36 lines
1021 B
TypeScript

import { useState, useEffect } from "react";
/**
* Returns the width of the window's vertical scrollbar in pixels, or null
* if not yet measured. Continuously re-measures as the scrollbar appears or
* disappears.
*
* @returns the scrollbar width, or null before measurement.
*/
export default function useWindowScrollbarWidth(): number | null {
const [width, setWidth] = useState<number | null>(null);
useEffect(() => {
const htmlElement = document.documentElement;
const measure = () => {
const scrollbarWidth = window.innerWidth - htmlElement.clientWidth;
setWidth(scrollbarWidth);
};
// Defer initial measurement to after browser has painted
const timeout = setTimeout(measure);
// Re-measure when html element resizes (scrollbar appears/disappears)
const resizeObserver = new ResizeObserver(measure);
resizeObserver.observe(htmlElement);
return () => {
clearTimeout(timeout);
resizeObserver.disconnect();
};
}, []);
return width;
}