Skip to content

Commit

Permalink
fix: typescript errors in helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorien Grönwald committed Jun 5, 2024
1 parent 5f5ac5e commit d565db0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
8 changes: 5 additions & 3 deletions src/tsx/components/navigation/MainNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ interface MainNavigationProps {

const MainNavigation: React.FC<MainNavigationProps> = ({ isOpen, onClose }) => {

const ref = useOutsideClick(() => {
if (event.target !== document.getElementById('main-navigation-toggle')) {
onClose();
const ref = useOutsideClick((event: MouseEvent) => {
const toggleElement = document.getElementById('main-navigation-toggle');
if (toggleElement && (event.target === toggleElement || toggleElement.contains(event.target as Node))) {
return;
}
onClose();
});

return (
Expand Down
19 changes: 11 additions & 8 deletions src/tsx/hooks/useOutsideClick.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { useEffect, useRef } from 'react';

export function useOutsideClick(callback: () => void) {
const ref = useRef(null);
export const useOutsideClick = (callback: (event: MouseEvent) => void) => {
const ref = useRef<HTMLElement | null>(null);

useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (ref.current && !ref.current.contains(event.target)) {
callback();
const handleClickOutside = (event: MouseEvent) => {
if (ref.current && !ref.current.contains(event.target as Node)) {
callback(event);
}
}
};

document.addEventListener('mousedown', handleClickOutside);

return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [ref, callback]);
}, [callback]);

return ref;
}
};

export default useOutsideClick;

0 comments on commit d565db0

Please sign in to comment.