React-Scrollbar-Size
Installation
React-Scrollbar-Size is available as an npm package:
$ npm install react-scrollbar-size
Usage
The useScrollbarSize
custom hook returns an object with two properties:
Name | Description |
---|
width | The current width of the vertical scrollbar |
height | The current height of the horizontal scrollbar |
Examples
To see a live example, follow these instructions.
TypeScript
import { CSSProperties, FunctionComponent } from 'react';
import useScrollbarSize from 'react-scrollbar-size';
const styles: CSSProperties = {
margin: '1rem',
textAlign: 'center',
};
const ScrollbarSizeDemo: FunctionComponent = () => {
const { height, width } = useScrollbarSize();
return (
<div style={styles}>
<h2>React Scrollbar Size Demo</h2>
<h4>Tip: Change browser zoom level to see scrollbar sizes change.</h4>
<p>
The current height of the scrollbar is {height}px.
<br />
The current width of the scrollbar is {width}px.
</p>
</div>
);
};
JavaScript
import useScrollbarSize from 'react-scrollbar-size';
const styles = {
margin: '1rem',
textAlign: 'center',
};
const ScrollbarSizeDemo = () => {
const { height, width } = useScrollbarSize();
return (
<div style={styles}>
<h2>React Scrollbar Size Demo</h2>
<h4>Tip: Change browser zoom level to see scrollbar sizes change.</h4>
<p>
The current height of the scrollbar is {height}px.
<br />
The current width of the scrollbar is {width}px.
</p>
</div>
);
};
License
This project is licensed under the terms of the
MIT license.