Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use via custom hook #263

Merged
merged 14 commits into from
Mar 11, 2021
Merged
12 changes: 10 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
extends: [
'airbnb',
'plugin:react-hooks/recommended',
'plugin:jest/recommended',
'plugin:jest/style',
'plugin:@typescript-eslint/recommended',
Expand All @@ -9,7 +10,7 @@ module.exports = {
],
env: { browser: true, es6: true, node: true, jest: true },
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'react-hooks', 'jest'],
plugins: ['@typescript-eslint', 'jest'],
rules: {
// enforce curly brace usage
curly: ['error', 'all'],
Expand Down Expand Up @@ -48,7 +49,14 @@ module.exports = {
},
},
{
files: ['**/scripts/**', '**/example/**', '**/__tests__/**', '**/*.test.ts', '**/*.test.tsx'],
files: [
'**/scripts/**',
'**/example/**',
'**/example-component/**',
'**/__tests__/**',
'**/*.test.ts',
'**/*.test.tsx',
],
rules: {
// allow dev dependencies
'import/no-extraneous-dependencies': [
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### directories
# parcel cache folder
.cache
.parcel-cache
# build folder
build
# test coverage
Expand Down
62 changes: 17 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# React-Scrollbar-Size
<div align="center">

React-Scrollbar-Size is a [React](https://reactjs.org/) component designed to calculate the size of the user agent's horizontal and vertical scrollbars.
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.

[![npm package](https://img.shields.io/npm/v/react-scrollbar-size/latest.svg)](https://www.npmjs.com/package/react-scrollbar-size)
Expand Down Expand Up @@ -29,55 +29,39 @@ $ npm install react-scrollbar-size
```

## Usage
The `useScrollbarSize` custom hook returns an object with two properties:

### Props
| Name | Description |
| ---- | ---- |
| `onChange` | Callback which will fire when the scrollbar sizes change. |
| Name | Description |
| -------- | ---------------------------------------------- |
| `width` | The current width of the vertical scrollbar |
| `height` | The current height of the horizontal scrollbar |

The callback accepts an object which will be updated with the following properties:

| Name | Description |
| ---- | ---- |
| `width` | The current width of the vertical scrollbar. |
| `height` | The current height of the horizontal scrollbar. |
:information_source: The component syntax will be deprecated in version 4. It is currently being provided for backwards compatibility.

## Examples
To see a live example, follow these [instructions](/example/README.md).

### TypeScript
```tsx
import React, { CSSProperties, FunctionComponent, useState } from 'react';
import ScrollbarSize from 'react-scrollbar-size';
import React, { CSSProperties, FunctionComponent } from 'react';
import { useScrollbarSize } from 'react-scrollbar-size';

const styles: CSSProperties = {
margin: '1rem',
textAlign: 'center',
};

const ScrollbarSizeDemo: FunctionComponent = () => {
const [currentHeight, setHeight] = useState(0);
const [currentWidth, setWidth] = useState(0);

const scrollbarSizeChange = ({ height, width }: ScrollbarSizeChangeHandlerParams) => {
if (height !== currentHeight) {
setHeight(height);
}

if (width !== currentWidth) {
setWidth(width);
}
};
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>
<ScrollbarSize onChange={scrollbarSizeChange} />
<p>
{`The current height of the scrollbar is ${currentHeight}px.`}
The current height of the scrollbar is {height}px.
<br />
{`The current width of the scrollbar is ${currentWidth}px.`}
The current width of the scrollbar is {width}px.
</p>
</div>
);
Expand All @@ -86,37 +70,25 @@ const ScrollbarSizeDemo: FunctionComponent = () => {

### JavaScript
```jsx
import React, { useState } from 'react';
import ScrollbarSize from 'react-scrollbar-size';
import React from 'react';
import { useScrollbarSize } from 'react-scrollbar-size';

const styles = {
margin: '1rem',
textAlign: 'center',
};

const ScrollbarSizeDemo = () => {
const [currentHeight, setHeight] = useState(0);
const [currentWidth, setWidth] = useState(0);

const scrollbarSizeChange = ({ height, width }) => {
if (height !== currentHeight) {
setHeight(height);
}

if (width !== currentWidth) {
setWidth(width);
}
};
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>
<ScrollbarSize onChange={scrollbarSizeChange} />
<p>
{`The current height of the scrollbar is ${currentHeight}px.`}
The current height of the scrollbar is {height}px.
<br />
{`The current width of the scrollbar is ${currentWidth}px.`}
The current width of the scrollbar is {width}px.
</p>
</div>
);
Expand Down
24 changes: 24 additions & 0 deletions example-component/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# React-Scrollbar-Size Example

This is an example package for React-Scrollbar-Size.

:warning: The componnt syntax in this example will be deprecated in version 4. The [hook](/example/README.md) syntax should be used in favor of this.
shawnmcknight marked this conversation as resolved.
Show resolved Hide resolved

## Requirements
- [Node](https://nodejs.org) 12.0 or newer

## Installation
After cloning the repository, install dependencies:
```sh
cd <project folder>/react-scrollbar-size
npm install
```

## Running

Once dependencies are installed, start the application with:
```sh
npm run example:component
```

Open `http://localhost:1234` to view the example site.
37 changes: 37 additions & 0 deletions example-component/ScrollbarSizeDemo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { CSSProperties, FunctionComponent, useState } from 'react';
import ScrollbarSize, { ScrollbarSizeChangeHandlerParams } from '../src';

const styles: CSSProperties = {
margin: '1rem',
textAlign: 'center',
};

const ScrollbarSizeDemo: FunctionComponent = () => {
const [currentHeight, setHeight] = useState(0);
const [currentWidth, setWidth] = useState(0);

const scrollbarSizeChange = ({ height, width }: ScrollbarSizeChangeHandlerParams) => {
if (height !== currentHeight) {
setHeight(height);
}

if (width !== currentWidth) {
setWidth(width);
}
};

return (
<div style={styles}>
<h2>React Scrollbar Size Component Demo</h2>
<h4>Tip: Change browser zoom level to see scrollbar sizes change.</h4>
<ScrollbarSize onChange={scrollbarSizeChange} />
<p>
The current height of the scrollbar is {currentHeight}px.
<br />
The current width of the scrollbar is {currentWidth}px.
</p>
</div>
);
};

export default ScrollbarSizeDemo;
13 changes: 13 additions & 0 deletions example-component/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
<title>React-Scrollbar-Size Component Example</title>
</head>
<body>
<div id="root"></div>
<script src='index.tsx'></script>
</body>
</html>
7 changes: 7 additions & 0 deletions example-component/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';
import { render } from 'react-dom';
import ScrollbarSizeDemo from './ScrollbarSizeDemo';
import 'normalize.css';
import './main.css';

render(<ScrollbarSizeDemo />, document.getElementById('root'));
13 changes: 13 additions & 0 deletions example-component/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
html {
font-family: 'Roboto', sans-serif;
-webkit-font-smoothing: antialiased;
}

body, h1, h2, h3, h4, h5, h6 {
margin: 0;
}

body {
font-size: 15px;
line-height: 24px;
}
22 changes: 5 additions & 17 deletions example/ScrollbarSizeDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
import React, { CSSProperties, FunctionComponent, useState } from 'react';
import ScrollbarSize, { ScrollbarSizeChangeHandlerParams } from '../src';
import React, { CSSProperties, FunctionComponent } from 'react';
import { useScrollbarSize } from '../src';

const styles: CSSProperties = {
margin: '1rem',
textAlign: 'center',
};

const ScrollbarSizeDemo: FunctionComponent = () => {
const [currentHeight, setHeight] = useState(0);
const [currentWidth, setWidth] = useState(0);

const scrollbarSizeChange = ({ height, width }: ScrollbarSizeChangeHandlerParams) => {
if (height !== currentHeight) {
setHeight(height);
}

if (width !== currentWidth) {
setWidth(width);
}
};
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>
<ScrollbarSize onChange={scrollbarSizeChange} />
<p>
{`The current height of the scrollbar is ${currentHeight}px.`}
The current height of the scrollbar is {height}px.
<br />
{`The current width of the scrollbar is ${currentWidth}px.`}
The current width of the scrollbar is {width}px.
</p>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
<title>React-Scrollbar-Size Examples</title>
<title>React-Scrollbar-Size Example</title>
</head>
<body>
<div id="root"></div>
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
resetMocks: true, // clear history and reset behavior of mocks between each test
restoreMocks: true, // restore initial behavior of mocked functions
moduleFileExtensions: ['js', 'ts', 'tsx'],
setupFilesAfterEnv: ['./test/jest-setup.ts'],
collectCoverageFrom: [
'**/src/**/*.ts', // typescript files
'**/src/**/*.tsx', // typescript files that render JSX
Expand Down
Loading