diff --git a/src/useClickAway.ts b/src/useClickAway.ts index d55bdc6c39..b5788863ef 100644 --- a/src/useClickAway.ts +++ b/src/useClickAway.ts @@ -1,15 +1,25 @@ import { RefObject, useEffect } from 'react'; import { off, on } from './util'; -const useClickAway = (ref: RefObject, onClickAway: (event: KeyboardEvent) => void) => { +const defaultEvents = ['mousedown', 'touchstart']; + +const useClickAway = ( + ref: RefObject, + onClickAway: (event: KeyboardEvent) => void, + events: string[] = defaultEvents +) => { useEffect(() => { const handler = event => { const { current: el } = ref; el && !el.contains(event.target) && onClickAway(event); }; - on(document, 'click', handler); + for (const eventName of events) { + on(document, eventName, handler); + } return () => { - off(document, 'click', handler); + for (const eventName of events) { + off(document, eventName, handler); + } }; }); };