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

Add {ignoreRef} to useClickOutside & Dialog #64

Open
wants to merge 2 commits into
base: main
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
17 changes: 11 additions & 6 deletions components/core/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ type DialogContent = {
children: React.ReactNode;
className?: string;
style?: React.CSSProperties;
ignoreRef?: React.RefObject<HTMLElement>;
};

function DialogContent({ children, className, style }: DialogContent) {
function DialogContent({ children, className, style, ignoreRef }: DialogContent) {
const { setIsOpen, isOpen, uniqueId, triggerRef } = useDialog();
const containerRef = useRef<HTMLDivElement>(null);
const [firstFocusableElement, setFirstFocusableElement] =
Expand Down Expand Up @@ -182,11 +183,15 @@ function DialogContent({ children, className, style }: DialogContent) {
}
}, [isOpen, triggerRef]);

useClickOutside(containerRef, () => {
if (isOpen) {
setIsOpen(false);
}
});
useClickOutside(
containerRef,
() => {
if (isOpen) {
setIsOpen(false)
}
},
ignoreRef,
)

return (
<motion.div
Expand Down
43 changes: 25 additions & 18 deletions hooks/useClickOutside.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { RefObject, useEffect } from 'react';
import { RefObject, useEffect } from "react"

function useClickOutside<T extends HTMLElement>(
ref: RefObject<T>,
handler: (event: MouseEvent | TouchEvent) => void
ref: RefObject<T>,
handler: (event: MouseEvent | TouchEvent) => void,
ignoreRef?: RefObject<T>,
): void {
useEffect(() => {
const handleClickOutside = (event: MouseEvent | TouchEvent) => {
if (!ref || !ref.current || ref.current.contains(event.target as Node)) {
return;
}
useEffect(() => {
const handleClickOutside = (event: MouseEvent | TouchEvent) => {
// Check if the event target is within the main ref or the ignoreRef
if (
!ref ||
!ref.current ||
ref.current.contains(event.target as Node) ||
(ignoreRef && ignoreRef.current && ignoreRef.current.contains(event.target as Node))
) {
return
}

handler(event);
};
handler(event)
}

document.addEventListener('mousedown', handleClickOutside);
document.addEventListener('touchstart', handleClickOutside);
document.addEventListener("mousedown", handleClickOutside)
document.addEventListener("touchstart", handleClickOutside)

return () => {
document.removeEventListener('mousedown', handleClickOutside);
document.removeEventListener('touchstart', handleClickOutside);
};
}, [ref, handler]);
return () => {
document.removeEventListener("mousedown", handleClickOutside)
document.removeEventListener("touchstart", handleClickOutside)
}
}, [ref, ignoreRef, handler])
}

export default useClickOutside;
export default useClickOutside