mirror of
https://github.com/outline/outline.git
synced 2026-12-02 09:42:07 +03:00
30 lines
984 B
TypeScript
30 lines
984 B
TypeScript
import { useSyncExternalStore } from "react";
|
|
|
|
const subscribe = (onStoreChange: () => void) => {
|
|
if (!document.documentElement) {
|
|
return () => {};
|
|
}
|
|
|
|
// Re-measure when the html element resizes (scrollbar appears/disappears).
|
|
const resizeObserver = new ResizeObserver(onStoreChange);
|
|
resizeObserver.observe(document.documentElement);
|
|
return () => resizeObserver.disconnect();
|
|
};
|
|
|
|
// documentElement can be null while some in-app browsers replace the document.
|
|
const getSnapshot = (): number | null =>
|
|
document.documentElement
|
|
? window.innerWidth - document.documentElement.clientWidth
|
|
: null;
|
|
|
|
/**
|
|
* 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 {
|
|
return useSyncExternalStore(subscribe, getSnapshot);
|
|
}
|