From f6e27295c5a31e14f85e331ac9816b8304e002f9 Mon Sep 17 00:00:00 2001 From: sonjs6789 Date: Sun, 29 Sep 2024 15:09:12 +0900 Subject: [PATCH 1/2] [material-ui][Dialog] Keyboard scrolling doesn't work in fullscreen mode (#42989) --- .../src/Unstable_TrapFocus/FocusTrap.tsx | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx b/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx index 0f6085f5b9f1d9..37e77be82ba39b 100644 --- a/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx +++ b/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx @@ -165,6 +165,32 @@ 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 = () => { + 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') { @@ -180,7 +206,7 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element { } if (activated.current) { - rootRef.current.focus(); + focusScrollableElement(); } } From 85aa4dfe91b573506eea54b65745aee0b3781c83 Mon Sep 17 00:00:00 2001 From: sonjs6789 Date: Sun, 29 Sep 2024 16:17:52 +0900 Subject: [PATCH 2/2] fix : typescript error --- packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx b/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx index 37e77be82ba39b..79577ee384e0a9 100644 --- a/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx +++ b/packages/mui-material/src/Unstable_TrapFocus/FocusTrap.tsx @@ -181,7 +181,11 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element { }; const focusScrollableElement = () => { - const scrollableElement = findScrollableElement(rootRef.current!); + if (!rootRef.current) { + return; + } + + const scrollableElement = findScrollableElement(rootRef.current); if (scrollableElement) { scrollableElement.tabIndex = -1; @@ -190,7 +194,6 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element { rootRef.current.focus(); } }; - if (!rootRef.current.contains(doc.activeElement)) { if (!rootRef.current.hasAttribute('tabIndex')) { if (process.env.NODE_ENV !== 'production') {