Skip to content

Commit f14e1d7

Browse files
committed
fix: πŸ› support click away on iOS, allow user to chose events
1 parent 56a868d commit f14e1d7

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

β€Žsrc/useClickAway.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { RefObject, useEffect } from 'react';
22
import { off, on } from './util';
33

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+
) => {
511
useEffect(() => {
612
const handler = event => {
713
const { current: el } = ref;
814
el && !el.contains(event.target) && onClickAway(event);
915
};
10-
on(document, 'click', handler);
16+
for (const eventName of events) {
17+
on(document, eventName, handler);
18+
}
1119
return () => {
12-
off(document, 'click', handler);
20+
for (const eventName of events) {
21+
off(document, eventName, handler);
22+
}
1323
};
1424
});
1525
};

0 commit comments

Comments
Β (0)