Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support for Multiple Refs in useClickAway #2593

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions docs/useClickAway.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# `useClickAway`

React UI hook that triggers a callback when user
clicks outside the target element.
clicks outside the target element or elements.


## Usage
Expand All @@ -10,17 +10,40 @@ clicks outside the target element.
import {useClickAway} from 'react-use';

const Demo = () => {
const ref = useRef(null);
useClickAway(ref, () => {
console.log('OUTSIDE CLICKED');
});
const ref = useRef(null);
const ref1 = useRef(null);
const ref2 = useRef(null);

useClickAway<MouseEvent>(ref, action('outside clicked'));
useClickAway<MouseEvent>([ref1, ref2], action('outside clicked blue or green box'));

return (
<div ref={ref} style={{
width: 200,
height: 200,
background: 'red',
}} />
<>
<div
ref={ref}
style={{
width: 200,
height: 200,
background: 'red',
}}
/>
<div
ref={ref1}
style={{
width: 200,
height: 200,
background: 'green',
}}
/>
<div
ref={ref2}
style={{
width: 200,
height: 200,
background: 'blue',
}}
/>
</>
);
};
```
Expand All @@ -31,4 +54,5 @@ const Demo = () => {
useClickAway(ref, onMouseEvent)
useClickAway(ref, onMouseEvent, ['click'])
useClickAway(ref, onMouseEvent, ['mousedown', 'touchstart'])
useClickAway([ref1, ref2], onMouseEvent)
```
15 changes: 10 additions & 5 deletions src/useClickAway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import { off, on } from './misc/util';
const defaultEvents = ['mousedown', 'touchstart'];

const useClickAway = <E extends Event = Event>(
ref: RefObject<HTMLElement | null>,
refs: RefObject<HTMLElement | null> | RefObject<HTMLElement | null>[],
onClickAway: (event: E) => void,
events: string[] = defaultEvents
) => {
const savedCallback = useRef(onClickAway);

useEffect(() => {
savedCallback.current = onClickAway;
}, [onClickAway]);

useEffect(() => {
const handler = (event) => {
const { current: el } = ref;
el && !el.contains(event.target) && savedCallback.current(event);
const refArray = Array.isArray(refs) ? refs : [refs];
const existingRefs = refArray.filter((ref) => Boolean(ref?.current)); // Filter out null refs
if (existingRefs.every(ref => ref.current && !ref.current.contains(event.target))) {
savedCallback.current(event);
}
};
for (const eventName of events) {
on(document, eventName, handler);
Expand All @@ -25,7 +30,7 @@ const useClickAway = <E extends Event = Event>(
off(document, eventName, handler);
}
};
}, [events, ref]);
}, [events, refs]);
};

export default useClickAway;
export default useClickAway;
22 changes: 22 additions & 0 deletions stories/useClickAway.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import ShowDocs from './util/ShowDocs';

const Demo = () => {
const ref = useRef(null);
const ref1 = useRef(null);
const ref2 = useRef(null);

useClickAway<MouseEvent>(ref, action('outside clicked'));
useClickAway<MouseEvent>([ref1, ref2], action('outside clicked blue or green box'));

return (
<>
<div
ref={ref}
style={{
Expand All @@ -18,6 +23,23 @@ const Demo = () => {
background: 'red',
}}
/>
<div
ref={ref1}
style={{
width: 200,
height: 200,
background: 'green',
}}
/>
<div
ref={ref2}
style={{
width: 200,
height: 200,
background: 'blue',
}}
/>
</>
);
};

Expand Down