Skip to content

Conversation

@xBlizZer
Copy link

@xBlizZer xBlizZer commented Nov 26, 2025

This pull request fixes issues with focus handling and keyboard navigation in the Select component, when used within a ShadowDOM. It ensures that keyboard interactions work as expected.

Also, this PR is based on work from #46775, and I want to give a shoutout to the original author. That PR was unfortunately closed, and since it became inactive, I decided to open this new one.

This PR fixes #34061


Before: https://stackblitz.com/edit/github-lvftcg7k-cvmzq3ha?file=src%2Fmain.tsx
After: https://stackblitz.com/edit/lw79g65z-whah7neb?file=src%2Fmain.tsx

@mui-bot
Copy link

mui-bot commented Nov 26, 2025

Netlify deploy preview

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

Bundle size report

Bundle Parsed size Gzip size
@mui/material ▼-112B(-0.02%) 🔺+4B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against ea8697b

@ZeeshanTamboli ZeeshanTamboli changed the title Fixes focus handling / keyboard navigation in a Select component while running in shadowDom [select] Fix focus keyboard navigation while running in shadowDom Nov 27, 2025
@ZeeshanTamboli ZeeshanTamboli changed the title [select] Fix focus keyboard navigation while running in shadowDom [select] Fix focus keyboard navigation while running in shadow DOM Nov 27, 2025
@ZeeshanTamboli ZeeshanTamboli added type: bug It doesn't behave as expected. scope: select Changes related to the select. labels Nov 27, 2025
Copy link
Member

Choose a reason for hiding this comment

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

Why to make changes in FocusTrap file if the issue is for Select?

Copy link
Author

Choose a reason for hiding this comment

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

We need to change the FocusTrap because otherwise, when the Select opens, items don’t get focused automatically.

Select → Menu → Popover → Modal → FocusTrap

Copy link
Member

@ZeeshanTamboli ZeeshanTamboli left a comment

Choose a reason for hiding this comment

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

@xBlizZer Thanks for the pull request. But I tried with the PR preview build https://pkg.pr.new/mui/material-ui/@mui/material@918977526ccfd5ea5d4ec6d95e53d30bb1c7f65c but it is not working as expected. See https://stackblitz.com/edit/lw79g65z?file=src%2Fmain.tsx

@xBlizZer
Copy link
Author

@xBlizZer Thanks for the pull request. But I tried with the PR preview build https://pkg.pr.new/mui/material-ui/@mui/material@918977526ccfd5ea5d4ec6d95e53d30bb1c7f65c but it is not working as expected. See https://stackblitz.com/edit/lw79g65z?file=src%2Fmain.tsx

@ZeeshanTamboli Why isn’t this working as expected? It should behave exactly like a Select without ShadowDOM. If you’re using MenuItem without values, everything gets marked and the last item ends up hovered. That’s must be the same behavior as a regular Select without Shadow DOM - if that’s what you were going for.

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Nov 27, 2025

@xBlizZer Thanks for the pull request. But I tried with the PR preview build https://pkg.pr.new/mui/material-ui/@mui/material@918977526ccfd5ea5d4ec6d95e53d30bb1c7f65c but it is not working as expected. See https://stackblitz.com/edit/lw79g65z?file=src%2Fmain.tsx

@ZeeshanTamboli Why isn’t this working as expected? It should behave exactly like a Select without ShadowDOM. If you’re using MenuItem without values, everything gets marked and the last item ends up hovered. That’s must be the same behavior as a regular Select without Shadow DOM - if that’s what you were going for.

Sorry my bad, the code was wrong. Updated the code: https://stackblitz.com/edit/lw79g65z?file=src%2Fmain.tsx but it still isn't working. Any idea why? When I open Select and press ArrowDown it doesn't navigate like a Select without Shadow DOM: https://mui.com/material-ui/react-select/#basic-select.

@xBlizZer
Copy link
Author

@xBlizZer Thanks for the pull request. But I tried with the PR preview build https://pkg.pr.new/mui/material-ui/@mui/material@918977526ccfd5ea5d4ec6d95e53d30bb1c7f65c but it is not working as expected. See https://stackblitz.com/edit/lw79g65z?file=src%2Fmain.tsx

@ZeeshanTamboli Why isn’t this working as expected? It should behave exactly like a Select without ShadowDOM. If you’re using MenuItem without values, everything gets marked and the last item ends up hovered. That’s must be the same behavior as a regular Select without Shadow DOM - if that’s what you were going for.

Sorry my bad, the code was wrong. Updated the code: https://stackblitz.com/edit/lw79g65z?file=src%2Fmain.tsx but it still isn't working. Any idea why? When I open Select and press ArrowDown it doesn't navigate like a Select without Shadow DOM: https://mui.com/material-ui/react-select/#basic-select.

I understand — I didn’t notice the issue initially because I primarily opened the field using a keyboard. However, I realized that we might need to treat this bug separately. I updated the mui/material version to the latest (without my changes) and removed the ShadowDOM, and I was still able to reproduce the problem. The focus effect on the MenuList item appears briefly and then disappears, which means keyboard navigation is no longer possible afterward.

I tested my change using the container property in MenuProps like this:
<Select MenuProps={{ container: shadowRootElement }}>

With this adjustment, my fix works as expected.

@ZeeshanTamboli ZeeshanTamboli changed the title [select] Fix focus keyboard navigation while running in shadow DOM [select] Fix keyboard navigation while rendering in shadow DOM Nov 28, 2025
@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented Nov 28, 2025

I understand — I didn’t notice the issue initially because I primarily opened the field using a keyboard. However, I realized that we might need to treat this bug separately. I updated the mui/material version to the latest (without my changes) and removed the ShadowDOM, and I was still able to reproduce the problem. The focus effect on the MenuList item appears briefly and then disappears, which means keyboard navigation is no longer possible afterward.

There's already an issue open on it #34218

I tested my change using the container property in MenuProps like this: <Select MenuProps={{ container: shadowRootElement }}>

With this adjustment, my fix works as expected.

Yes, looks good.

@xBlizZer
Copy link
Author

Thanks for adding some tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: select Changes related to the select. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In shadow DOM, keyboard not working for Select menu

3 participants