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][Dialog] Fix Keyboard scrolling doesn't work in fullscreen modal ( #42989 ) #43934

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,35 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {

const doc = ownerDocument(rootRef.current);

const findScrollableElement = (element: HTMLElement): HTMLElement | null => {
if (element.scrollHeight > element.clientHeight) {
return element;
}

for (const child of Array.from(element.children) as HTMLElement[]) {
const scrollable = findScrollableElement(child);
if (scrollable) {
return scrollable;
}
}

return null;
};

const focusScrollableElement = () => {
if (!rootRef.current) {
return;
}

const scrollableElement = findScrollableElement(rootRef.current);

if (scrollableElement) {
scrollableElement.tabIndex = -1;
scrollableElement.focus();
} else {
rootRef.current.focus();
}
};
if (!rootRef.current.contains(doc.activeElement)) {
if (!rootRef.current.hasAttribute('tabIndex')) {
if (process.env.NODE_ENV !== 'production') {
Expand All @@ -180,7 +209,7 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
}

if (activated.current) {
rootRef.current.focus();
focusScrollableElement();
}
}

Expand Down
Loading