Skip to content

Commit 90d2c22

Browse files
committed
feat: 🎸 add useOutsideClick hook
1 parent 05da37f commit 90d2c22

File tree

7 files changed

+65
-1
lines changed

7 files changed

+65
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<br/>
4949
- [**UI**](./docs/UI.md)
5050
- [`useAudio`](./docs/useAudio.md) &mdash; plays audio and exposes its controls. [![][img-demo]](https://codesandbox.io/s/2o4lo6rqy)
51+
- [`useOutsideClick`](./docs/useOutsideClick.md) &mdash; triggers callback when user clicks outside target element.
5152
- [`useSpeech`](./docs/useSpeech.md) &mdash; synthesizes speech from a text string. [![][img-demo]](https://codesandbox.io/s/n090mqz69m)
5253
- [`useVideo`](./docs/useVideo.md) &mdash; plays video, tracks its state, and exposes playback controls.
5354
<br/>

docs/useOutsideClick.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# `useOutsideClick`
2+
3+
React UI hook that triggers a callback when user
4+
clicks outside the a target element.
5+
6+
7+
## Usage
8+
9+
```jsx
10+
import {useOutsideClick} from 'react-use';
11+
12+
const Demo = () => {
13+
const ref = useRef(null);
14+
useOutsideClick(ref, () => {
15+
console.log('OUTSIDE CLICKED');
16+
});
17+
18+
return (
19+
<div ref={ref} style={{
20+
width: 200,
21+
height: 200,
22+
background: 'red',
23+
}} />
24+
);
25+
};
26+
```

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"throttle-debounce": "^2.0.1",
3636
"nano-css": "^3.4.0",
3737
"rebound": "^0.1.0",
38-
"ts-easing": "^0.1.0"
38+
"ts-easing": "^0.1.0",
39+
"use-onclickoutside": "^0.1.0"
3940
},
4041
"devDependencies": {
4142
"@storybook/react": "^3.4.11",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as React from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {useOutsideClick} from '..';
4+
import {useRef} from '../react';
5+
import ShowDocs from '../util/ShowDocs';
6+
7+
const Demo = () => {
8+
const ref = useRef(null);
9+
useOutsideClick(ref, () => {
10+
console.log('OUTSIDE CLICKED');
11+
});
12+
13+
return (
14+
<div ref={ref} style={{
15+
width: 200,
16+
height: 200,
17+
background: 'red',
18+
}} />
19+
);
20+
};
21+
22+
storiesOf('useOutsideClick', module)
23+
.add('Docs', () => <ShowDocs md={require('../../docs/useOutsideClick.md')} />)
24+
.add('Demo', () =>
25+
<Demo/>
26+
)

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import useNetwork from './useNetwork';
2525
import useNumber from './useNumber';
2626
import useObservable from './useObservable';
2727
import useOrientation from './useOrientation';
28+
import useOutsideClick from './useOutsideClick';
2829
import useRaf from './useRaf';
2930
import useSetState from './useSetState';
3031
import useSize from './useSize';
@@ -67,6 +68,7 @@ export {
6768
useNumber,
6869
useObservable,
6970
useOrientation,
71+
useOutsideClick,
7072
useRaf,
7173
useSetState,
7274
useSize,

src/useOutsideClick.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import useOutsideClick from 'use-onclickoutside';
2+
3+
export default useOutsideClick;

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9512,6 +9512,11 @@ url@^0.11.0:
95129512
punycode "1.3.2"
95139513
querystring "0.2.0"
95149514

9515+
use-onclickoutside@^0.1.0:
9516+
version "0.1.0"
9517+
resolved "https://registry.yarnpkg.com/use-onclickoutside/-/use-onclickoutside-0.1.0.tgz#bc1998fe36728b22a4b9b28c95fbb468b3033e84"
9518+
integrity sha512-yDtIbzU38VXkwZaCbDm9Yy+j5n5b4roBJw98iauFPJm2VWVa0xRKFYP8fz5OvICFizKAtgICwPVHJ3Or2/tIOA==
9519+
95159520
use@^3.1.0:
95169521
version "3.1.1"
95179522
resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f"

0 commit comments

Comments
 (0)