File tree 1 file changed +13
-3
lines changed
1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { RefObject , useEffect } from 'react' ;
2
2
import { off , on } from './util' ;
3
3
4
- const useClickAway = ( ref : RefObject < HTMLElement | null > , onClickAway : ( event : KeyboardEvent ) => void ) => {
4
+ const defaultEvents = [ 'mousedown' , 'touchstart' ] ;
5
+
6
+ const useClickAway = (
7
+ ref : RefObject < HTMLElement | null > ,
8
+ onClickAway : ( event : KeyboardEvent ) => void ,
9
+ events : string [ ] = defaultEvents
10
+ ) => {
5
11
useEffect ( ( ) => {
6
12
const handler = event => {
7
13
const { current : el } = ref ;
8
14
el && ! el . contains ( event . target ) && onClickAway ( event ) ;
9
15
} ;
10
- on ( document , 'click' , handler ) ;
16
+ for ( const eventName of events ) {
17
+ on ( document , eventName , handler ) ;
18
+ }
11
19
return ( ) => {
12
- off ( document , 'click' , handler ) ;
20
+ for ( const eventName of events ) {
21
+ off ( document , eventName , handler ) ;
22
+ }
13
23
} ;
14
24
} ) ;
15
25
} ;
You canβt perform that action at this time.
0 commit comments