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

[material-ui][Menu] After opening Menu, it is not possible to refresh page by ctrl + R #43215

Closed
JaniecMichal opened this issue Aug 7, 2024 · 13 comments · Fixed by #43505
Closed
Assignees
Labels
bug 🐛 Something doesn't work component: menu This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted

Comments

@JaniecMichal
Copy link

JaniecMichal commented Aug 7, 2024

Steps to reproduce

Link to live example: (required)

Steps:

  1. Open Menu
  2. Choose on keyboard ctrl + r
  3. Shortcut is igonored, page has not refreshed

Current behavior

This is weird case because we using this component in our apps and we have some places when ctrl + r after open Menu don't works. We have also places when the same component but rendered for different users ones works and ones not works

Expected behavior

Refreshing by keyboard shortcut should works always?

Context

Maybe it is caused by internal feature of Menu component that after click into key is searching item which match (by first letter)

Your environment

Output from `npx @mui/envinfo` goes here. System: OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish) Binaries: Node: 20.10.0 - ~/.nvm/versions/node/v20.10.0/bin/node npm: 10.7.0 - ~/.nvm/versions/node/v20.10.0/bin/npm pnpm: 8.15.1 - ~/.nvm/versions/node/v20.10.0/bin/pnpm Browsers: Chrome: 127.0.6533.99

Search keywords: Menu, MenuItem, refresh, ctr + r

@JaniecMichal JaniecMichal added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 7, 2024
@zannager zannager added the component: menu This is the name of the generic UI component, not the React module! label Aug 7, 2024
@ZeeshanTamboli
Copy link
Member

Could you provide a minimal reproduction? A live example would be ideal. You can start with this StackBlitz template.

It works on the MUI site next.mui.com:

issue-43215.mp4

@ZeeshanTamboli ZeeshanTamboli added package: material-ui Specific to @mui/material and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 13, 2024
@ZeeshanTamboli ZeeshanTamboli changed the title Menu - MenuItem - after opening Menu is not possible to refresh page by ctrl + R [material-ui][Menu] After opening Menu, it is not possible to refresh page by ctrl + R Aug 13, 2024
@ZeeshanTamboli ZeeshanTamboli added the status: waiting for author Issue with insufficient information label Aug 13, 2024
Copy link

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

@JaniecMichal
Copy link
Author

@ZeeshanTamboli I just explored reason of this behavior. Looks that if MenuItem have content which start from R letter or any other letter then using shortcut with this letter won't work. You can see this issue also if you use attached by you this StackBlitz template.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Aug 21, 2024
@github-actions github-actions bot reopened this Aug 21, 2024
@MateuszGroth
Copy link
Contributor

@ZeeshanTamboli
Copy link
Member

@JaniecMichal The MenuItem component has focus when menu is opened, and the Menu listens for key events, such as typing the first letter of an item. This can prevent the default browser action, like refreshing the page with Ctrl + R.

You can stop the event from reaching the Menu by calling event.stopPropagation() in MenuItem's handleKeyDown when Ctrl is pressed. This prevents the Menu from intercepting the event, allowing the browser to handle the page refresh as expected.

See the example here: https://stackblitz.com/edit/react-bdf3td-pldymk?file=Demo.tsx.

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 22, 2024
@MateuszGroth
Copy link
Contributor

MateuszGroth commented Aug 22, 2024

@ZeeshanTamboli The fix does not work on mac - probably need to check for the metaKey instead of the ctrlKey

@ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli The fix does not work on mac - probably need to check of metaKey instead of the ctrlKey

Yup.

@MateuszGroth
Copy link
Contributor

MateuszGroth commented Aug 22, 2024

Shouldn't it work like that by default tho? I can imagine this being an issue for more people

@ZeeshanTamboli
Copy link
Member

Shouldn't it work like that by default tho?

Alright. I compared the Menu component with other libraries like React Aria, Radix UI, and even Base UI, which all support this feature. Maybe we should too, or wait until v7. I'd like to hear others' opinions. cc @aarongarciah @DiegoAndai @michaldudak

I can imagine this being an issue for more people

From what I found, no one has reported this issue since the Material UI Menu component was introduced.

@ZeeshanTamboli ZeeshanTamboli removed the status: waiting for author Issue with insufficient information label Aug 22, 2024
@aarongarciah
Copy link
Member

I'd consider this a bug. With the migration to Base UI in the horizon, I'm not sure we should be spending time on improving Menu.

I'll get back to you soon.

@aarongarciah aarongarciah added the bug 🐛 Something doesn't work label Aug 23, 2024
@aarongarciah
Copy link
Member

I added the bug label. Contributions are more than welcome! This is the part of the code that handles the typeahead:

} else if (key.length === 1) {
const criteria = textCriteriaRef.current;
const lowerKey = key.toLowerCase();
const currTime = performance.now();
if (criteria.keys.length > 0) {
// Reset
if (currTime - criteria.lastTime > 500) {
criteria.keys = [];
criteria.repeating = true;
criteria.previousKeyMatched = true;
} else if (criteria.repeating && lowerKey !== criteria.keys[0]) {
criteria.repeating = false;
}
}
criteria.lastTime = currTime;
criteria.keys.push(lowerKey);
const keepFocusOnCurrent =
currentFocus && !criteria.repeating && textCriteriaMatches(currentFocus, criteria);
if (
criteria.previousKeyMatched &&
(keepFocusOnCurrent ||
moveFocus(list, currentFocus, false, disabledItemsFocusable, nextItem, criteria))
) {
event.preventDefault();
} else {
criteria.previousKeyMatched = false;
}
}

We should not prevent events that include modifier keys such as ctrl, meta, alt. For reference, here's the useTypeahead implementation from Floating UI: https://github.com/floating-ui/floating-ui/blob/master/packages/react/src/hooks/useTypeahead.ts#L155-L166

@DiegoAndai DiegoAndai added this to the Material UI with Base UI milestone Aug 28, 2024
@DiegoAndai
Copy link
Member

Added to the "Material UI with Base UI" milestone in case we don't get a contribution before it.

@DiegoAndai DiegoAndai added the ready to take Help wanted. Guidance available. There is a high chance the change will be accepted label Aug 28, 2024
Copy link

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

Note

We value your feedback @JaniecMichal! How was your experience with our support team?
If you could spare a moment, we'd love to hear your thoughts in this brief Support Satisfaction survey. Your insights help us improve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: menu This is the name of the generic UI component, not the React module! package: material-ui Specific to @mui/material ready to take Help wanted. Guidance available. There is a high chance the change will be accepted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants