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 year popup goes away when clicking outside #280

Closed
wants to merge 1 commit into from

Conversation

AyushSat
Copy link
Contributor

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.

@AyushSat AyushSat self-assigned this Mar 19, 2023
@github-actions
Copy link

Deployed staging instance to https://staging-280.peterportal.org

@js0mmer js0mmer self-requested a review May 27, 2023 03:02
Copy link
Member

@js0mmer js0mmer left a 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.

image

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?

Comment on lines +21 to +38
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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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]);

Copy link
Member

@ethanwong16 ethanwong16 left a 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(() => {
Copy link
Member

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

@js0mmer
Copy link
Member

js0mmer commented Dec 7, 2023

Proper way of doing this would actually be using an OverlayTrigger. Edit: another link for getting it to close when clicking buttons

@js0mmer
Copy link
Member

js0mmer commented Jan 18, 2024

Will be redone using an OverlayTrigger.

@js0mmer js0mmer closed this Jan 18, 2024
@js0mmer js0mmer linked an issue Jan 18, 2024 that may be closed by this pull request
@js0mmer js0mmer deleted the ayush/popup-issue branch January 25, 2024 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Close AddYear Popup when click away
4 participants