Files
outline/app/hooks/useWindowScrollbarWidth.ts
Apoorv MishraandTom Moor 12847d3d79 Fix: Right sidebar header misaligned (#11539)
* fix: right sidebar header misaligned

* fix: detect if vertical window scrollbar is visible

* fix: detect appearance of scrollbar using ResizeObserver

* Update Right.tsx

---------

Co-authored-by: Tom Moor <tom@getoutline.com>
2026-02-28 14:02:56 +00:00

36 lines
1.0 KiB
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 = htmlElement.scrollWidth - 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;
}