-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Popover] FocusTrap in a Shadow DOM loses the focus #34980
Comments
Thanks for reporting this. The bug stems from the FocusTrap not being aware it's inside shadow DOM. It uses The fix would likely involve replacing |
I had a patch utilizing |
Recently we had a report that the date range picker was not working with the Shadow DOM. The |
I've been trying to fix this in #35316 I've added the utility method that @m4theushw suggested, and that's working for Dialog component, but not all FocusTrap cases, going through the unit tests for FocusTrap I noticed that "focusin" event does not get triggered with every Tab inside Shadow DOM, I've been looking at custom events that address that issue, for example this one: https://gist.github.com/samthor/7b307408e73784971ef0fcf4a8af6edd |
Any update here? Seems like no movement since January. |
@CWSites I wasn't able to make my PR #35316 work, and I'm not actively working on it, but here's my workaround: import Dialog, { DialogProps } from '@mui/material/Dialog';
import FocusTrap from 'focus-trap-react';
import { useContext, useEffect } from 'react';
import { RootContext } from '../helpers/root-context';
export const AccessibleDialog = (props: DialogProps) => {
const root = useContext(RootContext) as Document;
const { disablePortal, ...otherProps } = props; // ignore disablePortal
useEffect(() => {
// disable tabbing for MUI's sentinel divs
const muiSentinels = [
root.querySelector('[data-testid=sentinelStart]'),
root.querySelector('[data-testid=sentinelEnd]'),
];
for (const comp of muiSentinels) {
if (comp && comp.attributes && comp.attributes.getNamedItem('tabindex')) {
comp.setAttribute('tabindex', '-1');
}
}
}, [root]);
return (
<FocusTrap
focusTrapOptions={{
document: root,
}}
>
<Dialog {...otherProps} disablePortal />
</FocusTrap>
);
};
import { createContext } from 'react';
export const RootContext = createContext<DocumentOrShadowRoot>(document); and when creating your Shadow DOM: //...
const shadowContainer = someElement.attachShadow({ mode: 'open' });
const shadowRootElement = document.createElement('div');
shadowContainer.appendChild(shadowRootElement);
const reactRoot = createRoot(shadowRootElement);
reactRoot.render(
<RootContext.Provider value={shadowContainer}>
<SomeReactComponentThatUsesAccessibleDialog />
</RootContext.Provider>
); (see this page from MUI docs for more info on using Shadow DOM: https://mui.com/material-ui/guides/shadow-dom/) |
@arminbashizade thank you for the details on how you got around this! |
Duplicates
Latest version
Steps to reproduce 🕹
Link to live example:
https://codesandbox.io/s/wandering-framework-25fyeg?file=/demo.tsx
Steps:
Current behavior 😯
FocusTrap doesn't enforce focus to stay inside it if the component is inside a shadow dom.
Expected behavior 🤔
FocusTrap inside a shadow dom should be working the same way it does outside of a shadow dom.
Context 🔦
No response
Your environment 🌎
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: