diff --git a/example/src/Examples/MenuExample.tsx b/example/src/Examples/MenuExample.tsx index 2a55069090..acd93ad83d 100644 --- a/example/src/Examples/MenuExample.tsx +++ b/example/src/Examples/MenuExample.tsx @@ -57,7 +57,11 @@ const MenuExample = () => { visible={_getVisible('menu1')} onDismiss={_toggleMenu('menu1')} anchor={ - + } > {}} title="Undo" /> @@ -116,6 +120,7 @@ const MenuExample = () => { visible={_getVisible('menu3')} onDismiss={_toggleMenu('menu3')} anchor={contextualMenuCoord} + aria-label="List item actions" > {}} title="Item 1" /> {}} title="Item 2" /> diff --git a/src/components/Menu/Menu.tsx b/src/components/Menu/Menu.tsx index 8beabdf571..d35095b667 100644 --- a/src/components/Menu/Menu.tsx +++ b/src/components/Menu/Menu.tsx @@ -70,6 +70,11 @@ export type Props = { * Accessibility label for the overlay. This is read by the screen reader when the user taps outside the menu. */ dismissAccessibilityLabel?: string; + /** + * Accessible name of the menu on web. Defaults to the anchor's content, so + * set it when `anchor` is an `{ x, y }` position. + */ + 'aria-label'?: string; /** * testID for the overlay that is displayed behind the menu. */ @@ -213,8 +218,12 @@ const Menu = ({ children, theme: themeOverrides, keyboardShouldPersistTaps, + 'aria-label': ariaLabel, }: Props) => { const theme = useInternalTheme(themeOverrides); + const isWeb = Platform.OS === 'web'; + const anchorId = `${React.useId()}-anchor`; + const hasAnchorNode = !isCoordinate(anchor); const { direction } = useLocale(); const insets = useSafeAreaInsets(); @@ -677,6 +686,7 @@ const Menu = ({ anchorRef.current = ref; }} collapsable={false} + nativeID={isWeb && hasAnchorNode ? anchorId : undefined} > {isCoordinate(anchor) ? null : anchor} {rendered ? ( @@ -694,7 +704,16 @@ const Menu = ({ menuRef.current = ref; }} collapsable={false} - aria-modal={visible} + // On the web the items are `menuitem`s, which need a `menu` around + // them. It is named after the anchor that opens it. `aria-modal` + // is only valid on a dialog, so it stays on iOS, where it keeps + // VoiceOver inside the menu. + role={isWeb ? 'menu' : undefined} + aria-label={isWeb ? ariaLabel : undefined} + aria-labelledby={ + isWeb && !ariaLabel && hasAnchorNode ? anchorId : undefined + } + aria-modal={isWeb ? undefined : visible} style={[styles.wrapper, positionStyle, style]} pointerEvents={pointerEvents} onAccessibilityEscape={onDismiss} diff --git a/src/components/__tests__/Menu.test.tsx b/src/components/__tests__/Menu.test.tsx index bf943621ef..7eefe38e46 100644 --- a/src/components/__tests__/Menu.test.tsx +++ b/src/components/__tests__/Menu.test.tsx @@ -130,6 +130,66 @@ describe('web focus', () => { }); }); +describe('menu role', () => { + const renderMenu = (props: Partial> = {}) => + render( + + Edit} + {...props} + > + + + + + ); + + describe('on web', () => { + beforeEach(() => { + jest.replaceProperty(Platform, 'OS', 'web'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + it('wraps the items in a menu named after the anchor', async () => { + await renderMenu(); + + const menu = screen.getByLabelText('Edit', { exact: true }); + expect(menu).toHaveProp('role', 'menu'); + expect(menu).not.toHaveProp('aria-modal'); + expect(screen.getAllByRole('menuitem')).toHaveLength(2); + }); + + it('uses aria-label over the anchor', async () => { + await renderMenu({ 'aria-label': 'Edit options' }); + + expect(screen.getByLabelText('Edit options')).toHaveProp('role', 'menu'); + expect( + screen.queryByLabelText('Edit', { exact: true }) + ).not.toBeOnTheScreen(); + }); + + it('is named by aria-label when the anchor is a position', async () => { + await renderMenu({ anchor: { x: 10, y: 10 }, 'aria-label': 'Actions' }); + + expect(screen.getByLabelText('Actions')).toHaveProp('role', 'menu'); + }); + }); + + it('leaves the native menu unnamed', async () => { + await renderMenu({ 'aria-label': 'Edit options' }); + + expect(screen.queryByLabelText('Edit options')).not.toBeOnTheScreen(); + expect( + screen.queryByLabelText('Edit', { exact: true }) + ).not.toBeOnTheScreen(); + }); +}); + it('renders visible menu', async () => { const tree = ( await render(