Design comparison
Solution retrospective
Any suggestions on adding a feature to close the menu on clicking/tapping outside of the menu?
Community feedback
- @trafargarlawPosted over 2 years ago
Hey, for closing the menu when you click or tap outside of it, since you're using react you could make a custom hook "useClickOutside" for example that takes a ref object of the menu and a handler as parameters it would look something like this : export const useClickOutside = ( ref: React.RefObject<HTMLElement>, handler: () => void ) => { useEffect(() => { const listener = (event: MouseEvent) => { if (!ref.current || ref.current.contains(event.target as Node)) { return } handler() } document.addEventListener('mousedown', listener) return () => { document.removeEventListener('mousedown', listener) } }, [ref, handler]) }
0
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord