https://github.com/shawnmcknight/react-scrollbar-size
React hook to calculate the size of browser scrollbars.
https://github.com/shawnmcknight/react-scrollbar-size
react scrollbar
Last synced: about 1 year ago
JSON representation
React hook to calculate the size of browser scrollbars.
- Host: GitHub
- URL: https://github.com/shawnmcknight/react-scrollbar-size
- Owner: shawnmcknight
- License: mit
- Created: 2017-03-25T18:50:34.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2023-11-13T19:41:16.000Z (over 2 years ago)
- Last Synced: 2025-04-21T21:03:24.114Z (over 1 year ago)
- Topics: react, scrollbar
- Language: TypeScript
- Homepage:
- Size: 10.9 MB
- Stars: 28
- Watchers: 2
- Forks: 12
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# React-Scrollbar-Size
React-Scrollbar-Size is a [React](https://reactjs.org/) hook designed to calculate the size of the user agent's horizontal and vertical scrollbars.
It will also detect when the size of the scrollbars change, such as when the user agent's zoom factor changes.
[](https://www.npmjs.com/package/react-scrollbar-size)
[](https://www.npmjs.com/package/react-scrollbar-size)
[](https://circleci.com/gh/shawnmcknight/react-scrollbar-size/tree/main)
[](https://codeclimate.com/github/shawnmcknight/react-scrollbar-size)
[](https://github.com/airbnb/javascript)
[](https://prettier.io/)
[](https://libraries.io/npm/react-scrollbar-size)
[](https://isitmaintained.com/project/shawnmcknight/react-scrollbar-size)

[](code_of_conduct.md)
[](https://github.com/shawnmcknight/react-scrollbar-size)
## Installation
React-Scrollbar-Size is available as an [npm package](https://www.npmjs.com/package/react-scrollbar-size):
```sh
$ 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](/example/README.md).
### TypeScript
```tsx
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 (
React Scrollbar Size Demo
Tip: Change browser zoom level to see scrollbar sizes change.
The current height of the scrollbar is {height}px.
The current width of the scrollbar is {width}px.
);
};
```
### JavaScript
```jsx
import useScrollbarSize from 'react-scrollbar-size';
const styles = {
margin: '1rem',
textAlign: 'center',
};
const ScrollbarSizeDemo = () => {
const { height, width } = useScrollbarSize();
return (
React Scrollbar Size Demo
Tip: Change browser zoom level to see scrollbar sizes change.
The current height of the scrollbar is {height}px.
The current width of the scrollbar is {width}px.
);
};
```
## License
This project is licensed under the terms of the
[MIT license](/LICENSE).