react-use/src/useScrollbarWidth.ts
Anton Zinovyev 125c7e96a1
feat: useScrollbarWidth hook; (#825)
* 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.
2019-12-09 15:47:44 +03:00

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;
}