Skip to content
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
22 changes: 17 additions & 5 deletions src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as React from 'react';
import {
AccessibilityInfo,
Dimensions,
findNodeHandle,
Keyboard,
Platform,
Pressable,
Expand Down Expand Up @@ -122,10 +124,13 @@ const getKeyboardHeight = (fallback: number) =>
const DEFAULT_ELEVATION: Elevation = 2;
const DEFAULT_MODE = 'elevated';

const focusFirstDOMNode = (
const focusFirstNode = (
el: React.ComponentRef<typeof View> | 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
Expand All @@ -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);
}
};

Expand Down Expand Up @@ -314,7 +326,7 @@ const Menu = ({
}

isShownRef.current = true;
focusFirstDOMNode(menuRef.current);
focusFirstNode(menuRef.current);
});

const handleHideAnimationFinished = useLatestCallback((finished: boolean) => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -673,7 +685,7 @@ const Menu = ({
>
{isCoordinate(anchor) ? null : anchor}
{rendered ? (
<Portal>
<Portal modal={visible}>
<Pressable
aria-label={dismissAccessibilityLabel}
role="button"
Expand Down
Loading