From 2b8ff221b091c64e1eb45c7f1060b0401a6a1d9e Mon Sep 17 00:00:00 2001 From: Zac Yungblut <66346164+zacyungblut@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:50:32 -0500 Subject: [PATCH] fix(menu): trap screen readers and move focus on open/close --- src/components/Menu/Menu.tsx | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 5c5990a2b8..7d5543fd1e 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -1,6 +1,8 @@ import * as React from 'react'; import { + AccessibilityInfo, Dimensions, + findNodeHandle, Keyboard, Platform, Pressable, @@ -122,10 +124,13 @@ const getKeyboardHeight = (fallback: number) => const DEFAULT_ELEVATION: Elevation = 2; const DEFAULT_MODE = 'elevated'; -const focusFirstDOMNode = ( +const focusFirstNode = ( el: React.ComponentRef | null | undefined ) => { - if (el && Platform.OS === 'web') { + if (!el) { + return; + } + if (Platform.OS === 'web') { // When in the browser, we want to focus the first focusable item on toggle // For example, when menu is shown, focus the first item in the menu // And when menu is dismissed, send focus back to the button to resume tabbing @@ -135,6 +140,13 @@ const focusFirstDOMNode = ( 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])' )?.focus(); } + return; + } + // On native, move the screen reader focus to the menu when it opens and + // back to the anchor when it closes, matching the web behavior above. + const node = findNodeHandle(el); + if (node) { + AccessibilityInfo.setAccessibilityFocus(node); } }; @@ -314,7 +326,7 @@ const Menu = ({ } isShownRef.current = true; - focusFirstDOMNode(menuRef.current); + focusFirstNode(menuRef.current); }); const handleHideAnimationFinished = useLatestCallback((finished: boolean) => { @@ -325,7 +337,7 @@ const Menu = ({ setMenuLayout({ width: 0, height: 0 }); setRendered(false); isShownRef.current = false; - focusFirstDOMNode(anchorRef.current); + focusFirstNode(anchorRef.current); }); const show = React.useCallback(async () => { @@ -673,7 +685,7 @@ const Menu = ({ > {isCoordinate(anchor) ? null : anchor} {rendered ? ( - +