mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
22 lines
544 B
TypeScript
22 lines
544 B
TypeScript
import { scrollbarWidth } from '@xobotyi/scrollbar-width';
|
|
import { useEffect, useState } from 'react';
|
|
|
|
export function useScrollbarWidth(): number | undefined {
|
|
const [sbw, setSbw] = useState(scrollbarWidth());
|
|
|
|
// this needed to ensure the scrollbar width in case hook called before the DOM is ready
|
|
useEffect(() => {
|
|
if (typeof sbw !== 'undefined') {
|
|
return;
|
|
}
|
|
|
|
const raf = requestAnimationFrame(() => {
|
|
setSbw(scrollbarWidth());
|
|
});
|
|
|
|
return () => cancelAnimationFrame(raf);
|
|
}, []);
|
|
|
|
return sbw;
|
|
}
|