Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion example/src/Examples/MenuExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ const MenuExample = () => {
visible={_getVisible('menu1')}
onDismiss={_toggleMenu('menu1')}
anchor={
<Appbar.Action icon={MORE_ICON} onPress={_toggleMenu('menu1')} />
<Appbar.Action
icon={MORE_ICON}
aria-label="More options"
onPress={_toggleMenu('menu1')}
/>
}
>
<Menu.Item onPress={() => {}} title="Undo" />
Expand Down Expand Up @@ -116,6 +120,7 @@ const MenuExample = () => {
visible={_getVisible('menu3')}
onDismiss={_toggleMenu('menu3')}
anchor={contextualMenuCoord}
aria-label="List item actions"
>
<Menu.Item onPress={() => {}} title="Item 1" />
<Menu.Item onPress={() => {}} title="Item 2" />
Expand Down
21 changes: 20 additions & 1 deletion src/components/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -677,6 +686,7 @@ const Menu = ({
anchorRef.current = ref;
}}
collapsable={false}
nativeID={isWeb && hasAnchorNode ? anchorId : undefined}
>
{isCoordinate(anchor) ? null : anchor}
{rendered ? (
Expand All @@ -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}
Expand Down
60 changes: 60 additions & 0 deletions src/components/__tests__/Menu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,66 @@ describe('web focus', () => {
});
});

describe('menu role', () => {
const renderMenu = (props: Partial<React.ComponentProps<typeof Menu>> = {}) =>
render(
<Portal.Host>
<Menu
visible
onDismiss={jest.fn()}
anchor={<Button>Edit</Button>}
{...props}
>
<Menu.Item onPress={jest.fn()} title="Undo" />
<Menu.Item onPress={jest.fn()} title="Redo" />
</Menu>
</Portal.Host>
);

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(
Expand Down
Loading