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

[docs-infra] Fix product selector popup not closing on route change #41166

Merged
merged 6 commits into from
Feb 23, 2024

Conversation

divyammadhok
Copy link
Contributor

@divyammadhok divyammadhok commented Feb 18, 2024

fixes #41128

Description

The issue arose from the fact that on route changes since the layout is not rendered but the content panel is, in some cases when previous route was cached the popper never closed on clicking a link in it. Now, we track for route changes in the component, another approach could've been to send handleClose through props but it would need special handling when new elements are added in the popper.

@mui-bot
Copy link

mui-bot commented Feb 18, 2024

Netlify deploy preview

https://deploy-preview-41166--material-ui.netlify.app/

Bundle size report

No bundle size changes (Toolpad)
No bundle size changes

Generated by 🚫 dangerJS against c2d2ffa

@oliviertassinari oliviertassinari added the PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI label Feb 18, 2024
@danilo-leal danilo-leal changed the title Fix Product Selector popup not closing on route change [docs-infra] Fix product selector popup not closing on route change Feb 19, 2024
@danilo-leal danilo-leal added bug 🐛 Something doesn't work scope: docs-infra Specific to the docs-infra product and removed PR: needs revision The pull request can't be merged. More details is available on the code review or fails in the CI labels Feb 19, 2024
Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

I'm not super confident with the idea of check if element is a link, or it's parent is a link to handle that.

Did you consider the approach

let link = event.target.closest('a'); // get closest link
if (!link) return; // was not  link
if (!event.currentTarget.contains(link)) return; // the link is outside

@@ -54,6 +55,18 @@ function ProductDrawerButton(props) {
setAnchorEl(null);
};

const handleEventDelegation = (e) => {
// In case of key down events, and any key apart from `enter` is clicked then don't close the menu.
if (e?.type === 'keydown' && e.keyCode !== 13) {
Copy link
Member

Choose a reason for hiding this comment

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

We avoid the use of keyCode. See this PR message for more context: #22569

Copy link
Member

Choose a reason for hiding this comment

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

By the way, Enter should also trigger onClick so no need to add this kind of verification

Copy link

@divyammadhok21 divyammadhok21 Feb 19, 2024

Choose a reason for hiding this comment

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

Hey @alexfauquette I have moved to useing .key instead. This check is just to ensure that whenever links are moved through keyboard and clicked on using 'enter', but without the handling it works too. So removed unnecessary code.

Copy link
Member

@alexfauquette alexfauquette 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

@@ -54,6 +55,15 @@ function ProductDrawerButton(props) {
setAnchorEl(null);
};

const handleEventDelegation = (e) => {
// Assert whether an 'a' tag resides in the parent of the clicked element through which the event bubbles out.
const isLinkInParentTree = e?.target ? Boolean(e.target.closest('a')) : false;
Copy link
Member

Choose a reason for hiding this comment

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

In which case would you get the event undefined?

Suggested change
const isLinkInParentTree = e?.target ? Boolean(e.target.closest('a')) : false;
const isLinkInParentTree = Boolean(event.target.closest('a')) ;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I was checking the event handler type and thought that target is optional there but it wasn't. Done the other change too.

@@ -54,6 +55,15 @@ function ProductDrawerButton(props) {
setAnchorEl(null);
};

const handleEventDelegation = (e) => {
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
const handleEventDelegation = (e) => {
const handleEventDelegation = (event) => {

just for consistency with the rest of the codebase

Copy link
Member

@alexfauquette alexfauquette left a comment

Choose a reason for hiding this comment

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

🎉 Thanks for your contribution

@alexfauquette alexfauquette merged commit 1d02294 into mui:master Feb 23, 2024
19 checks passed
@oliviertassinari oliviertassinari removed the request for review from siriwatknp February 23, 2024 15:19
mnajdova pushed a commit to mnajdova/material-ui that referenced this pull request Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work scope: docs-infra Specific to the docs-infra product
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[docs-infra] Product switcher popup not closing
6 participants