-
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
add year popup goes away when clicking outside #280
Conversation
Deployed staging instance to https://staging-280.peterportal.org |
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.
Looks good. One minor gripe I have is that the effect seems to go off and add/remove the event listener every time the popup is toggled since show is in the dependency array. I know it will show a warning when it's not in the dependency but there's another workaround using a function as it mentions in the error message. This will fix the adding/removing the listener on every toggle.
I left a suggestion with this change.
Also what does the popupTarget ref do? I see that it references the div you added but I don't see it used anywhere except the dependency array. Is this intentional?
useEffect(() => { | ||
const handleClick = (event: MouseEvent) => { | ||
if (target.current && target.current.contains(event.target as Node)) { | ||
setShow(!show); | ||
} else { | ||
setShow(false); | ||
} | ||
}; | ||
const rootElement = document.getElementById("root"); | ||
if(rootElement){ | ||
rootElement.addEventListener('click', handleClick); | ||
} | ||
return () => { | ||
if(rootElement){ | ||
rootElement.removeEventListener('click', handleClick); | ||
} | ||
}; | ||
}, [popupTarget, target, show]); |
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.
useEffect(() => { | |
const handleClick = (event: MouseEvent) => { | |
if (target.current && target.current.contains(event.target as Node)) { | |
setShow(!show); | |
} else { | |
setShow(false); | |
} | |
}; | |
const rootElement = document.getElementById("root"); | |
if(rootElement){ | |
rootElement.addEventListener('click', handleClick); | |
} | |
return () => { | |
if(rootElement){ | |
rootElement.removeEventListener('click', handleClick); | |
} | |
}; | |
}, [popupTarget, target, show]); | |
useEffect(() => { | |
const handleClick = (event: MouseEvent) => { | |
if (target.current && target.current.contains(event.target as Node)) { | |
setShow(show => !show); | |
} else { | |
setShow(false); | |
} | |
}; | |
const rootElement = document.getElementById("root"); | |
if(rootElement){ | |
rootElement.addEventListener('click', handleClick); | |
} | |
return () => { | |
if(rootElement){ | |
rootElement.removeEventListener('click', handleClick); | |
} | |
}; | |
}, [popupTarget, target]); |
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.
LGTM! Take a quick look at my suggestion + Jacob's feedback 🔥
const handleClick = (event: React.MouseEvent) => { | ||
setShow(!show); | ||
}; | ||
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.
I'd suggest adding a quick comment here explaining the gist of the change and what it's purpose is, so it's easier to maintain or apply to other areas in the future
Proper way of doing this would actually be using an OverlayTrigger. Edit: another link for getting it to close when clicking buttons |
Will be redone using an OverlayTrigger. |
This PR will allow the add year popup to be closed when anywhere else on the page is clicked.
Added event listeners to document to check where the clicks occur, if they are in the button toggle, else turn off the popup
Go to roadmap, press the add year to see popup, click outside the popup/button and see if it goes away.