Skip to content

Commit

Permalink
feat: add useMouseWheel hook
Browse files Browse the repository at this point in the history
  • Loading branch information
tkwant committed Jun 5, 2020
1 parent 80af64b commit d714b12
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
- [`useMediaDevices`](./docs/useMediaDevices.md) — tracks state of connected hardware devices.
- [`useMotion`](./docs/useMotion.md) — tracks state of device's motion sensor.
- [`useMouse` and `useMouseHovered`](./docs/useMouse.md) — tracks state of mouse position. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usemouse--docs)
- [`useMouseWheel`](./docs/useMouse.md) — tracks deltaY of scrolled mouse wheel. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usemousewheel--docs)
- [`useNetwork`](./docs/useNetwork.md) — tracks state of user's internet connection.
- [`useOrientation`](./docs/useOrientation.md) — tracks state of device's screen orientation.
- [`usePageLeave`](./docs/usePageLeave.md) — triggers when mouse leaves page boundaries.
Expand Down
17 changes: 17 additions & 0 deletions docs/useMouseWheel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# `useMouseWheel`
React Hook to get deltaY of mouse scrolled in window.

## Usage

```jsx
import { useMouseWheel } from 'react-use';

const Demo = () => {
const mouseWheel = useMouseWheel()
return (
<>
<h3>delta Y Scrolled: {mouseWheel}</h3>
</>
);
};
```
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export { default as useMount } from './useMount';
export { default as useMountedState } from './useMountedState';
export { default as useMouse } from './useMouse';
export { default as useMouseHovered } from './useMouseHovered';
export { default as useMouseWheel } from './useMouseWheel';
export { default as useNetwork } from './useNetwork';
export { default as useNumber } from './useNumber';
export { default as useObservable } from './useObservable';
Expand Down
14 changes: 14 additions & 0 deletions src/useMouseWheel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {useEffect, useState} from 'react'

export default () => {
const [mouseWheelScrolled, setMouseWheelScrolled] = useState(0)
useEffect(()=>{
const updateScroll = (e : MouseWheelEvent) => {
setMouseWheelScrolled(e.deltaY + mouseWheelScrolled)
}
window.addEventListener('wheel', updateScroll, false)
return () => window.removeEventListener('wheel', updateScroll)
})
return mouseWheelScrolled
}

17 changes: 17 additions & 0 deletions stories/useMouseWheel.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { storiesOf } from '@storybook/react';
import * as React from 'react';
import { useMouseWheel } from '../src';
import ShowDocs from './util/ShowDocs';

const Demo: React.FC<any> = () => {
const mouseWheel = useMouseWheel()
return (
<>
<h3>delta Y Scrolled: {mouseWheel}</h3>
</>
);
};

storiesOf('Sensors|useMouseWheel', module)
.add('Docs', () => <ShowDocs md={require('../docs/useMouseWheel.md')} />)
.add('Demo', () => <Demo />);

0 comments on commit d714b12

Please sign in to comment.