Skip to content

Commit

Permalink
feat: 🎸 add usePageLeave hook
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 27, 2019
1 parent b8875a2 commit 33ac91b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
- [`useMouse` and `useMouseHovered`](./docs/useMouse.md) — tracks state of mouse position. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usemouse--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.
- [`useScroll`](./docs/useScroll.md) — tracks some HTML element's scroll position. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usescroll--docs)
- [`useSize`](./docs/useSize.md) — tracks some HTML element's dimensions.
- [`useWindowScroll`](./docs/useWindowScroll.md) — tracks `Window` scroll position. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/sensors-usewindowscroll--docs)
Expand Down
15 changes: 15 additions & 0 deletions docs/usePageLeave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# `usePageLeave`

React sensor hook that fires a callback when mouse leaves the page.

## Usage

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

const Demo = () => {
usePageLeave(() => console.log('Page left...'));

return null;
};
```
16 changes: 16 additions & 0 deletions src/__stories__/usePageLeave.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react';
import {storiesOf} from '@storybook/react';
import {action} from '@storybook/addon-actions';
import {usePageLeave} from '..';

const Demo = () => {
usePageLeave(action('onPageLeave'));

return (
<div>
Try leaving the page and see logs in <code>Actions</code> tab.
</div>
);
};

storiesOf('Sensors|usePageLeave', module).add('Default', () => <Demo />);
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import useNumber from './useNumber';
import useObservable from './useObservable';
import useOrientation from './useOrientation';
import useClickAway from './useClickAway';
import usePageLeave from './usePageLeave';
import usePromise from './usePromise';
import useRaf from './useRaf';
import useRefMounted from './useRefMounted';
Expand Down Expand Up @@ -93,6 +94,7 @@ export {
useNumber,
useObservable,
useOrientation,
usePageLeave,
usePromise,
useRaf,
useRefMounted,
Expand Down
20 changes: 20 additions & 0 deletions src/usePageLeave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {useEffect} from 'react';

const usePageLeave = (onPageLeave, args = []) => {
useEffect(() => {
if (onPageLeave) {
const handler = (event) => {
event = event ? event : (window.event as any);
const from = event.relatedTarget || event.toElement;
if (!from || (from as any).nodeName === 'HTML') onPageLeave();
};

document.addEventListener('mouseout', handler);
return () => {
document.removeEventListener('mouseout', handler);
};
}
}, args);
};

export default usePageLeave;

0 comments on commit 33ac91b

Please sign in to comment.