-
Notifications
You must be signed in to change notification settings - Fork 15
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: add use-click-outside
#28
base: master
Are you sure you want to change the base?
Conversation
docs/src/pages/use-click-outside.mdx
Outdated
import { useClickOutside } from 'foxact/use-click-outside'; | ||
|
||
function Component() { | ||
const ref = useClickOutside<HTMLDivHTML>(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const ref = useClickOutside<HTMLDivHTML>(() => { | |
const ref = useClickOutside<HTMLDivElement>(() => { |
src/use-click-outside/index.ts
Outdated
useEffect(() => { | ||
const handleClick = ({ target }: MouseEvent) => { | ||
if (target && ref.current && !ref.current.contains(target as Node)) { | ||
cb(); | ||
} | ||
}; | ||
document.addEventListener('click', handleClick); | ||
return () => { | ||
document.removeEventListener('click', handleClick); | ||
}; | ||
}, [cb]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use callback ref instead of useEffect.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you Sukka sensei !
Here is my implementation, please review :)
import 'client-only';
import { useCallback } from 'react';
import type { RefCallback } from 'react';
export function useClickOutside<T extends HTMLElement>(cb: () => void): RefCallback<T> {
return useCallback((node) => {
const handleClick = ({ target }: MouseEvent) => {
if (target && node && !node.contains(target as Node)) {
cb();
}
};
document.addEventListener('click', handleClick);
return () => {
document.removeEventListener('click', handleClick);
};
}, [cb]);
}
but as far as I know, ref callback can return a cleanup function only from React 19, before React 19, React would call this callback with null
value when unmount. Shouldn't we consider this situation or am I incorrectly implementing this hook? :)
|
||
<ExportMetaInfo /> | ||
|
||
`useClickOutside` calls the callback you provide when a click event occurs outside the element associated with the returned ref callback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably document that this only works on React 19 since we use ref callback cleanup. Or is it possible to make it backward compatible with the previous React version?
please review