mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
* feat: useScrollbarWidth hook; * chore: bump @xobotyi/scrollbar-width to 1.5.0 and add it to dependencies. * fix: remove @xobotyi/scrollbar-width from dev-deps.
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;
|
|
}
|