Skip to content

Commit

Permalink
fix: πŸ› support click away on iOS, allow user to chose events
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Apr 23, 2019
1 parent 56a868d commit f14e1d7
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/useClickAway.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import { RefObject, useEffect } from 'react';
import { off, on } from './util';

const useClickAway = (ref: RefObject<HTMLElement | null>, onClickAway: (event: KeyboardEvent) => void) => {
const defaultEvents = ['mousedown', 'touchstart'];

const useClickAway = (
ref: RefObject<HTMLElement | null>,
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);
}
};
});
};
Expand Down

0 comments on commit f14e1d7

Please sign in to comment.