Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure specified trigger id is used for menu aria-labelledby #31339

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Bug fix for #18318 which ensures that a specified id attribute placed on the first trigger child is used for the menu aria-labelledby when provided",
"packageName": "@fluentui/react-menu",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,23 @@ describe('Menu', () => {
expect(container.querySelector('#second')?.getAttribute('aria-checked')).toBe('true');
expect(container.querySelector('#third')?.getAttribute('aria-checked')).toBe('false');
});

it('should render correct aria-labelledby attribute from corresponding trigger id', () => {
const customId = 'myTrigger';
const { getByRole } = render(
<Menu open>
<MenuTrigger disableButtonEnhancement>
<button id={customId}>Menu trigger</button>
</MenuTrigger>
<MenuPopover>
<MenuList>
<MenuItem>Item</MenuItem>
</MenuList>
</MenuPopover>
</Menu>,
);

// Assert
expect(getByRole('menu')).toHaveAttribute('aria-labelledby', customId);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const useMenu_unstable = (props: MenuProps): MenuState => {
defaultCheckedValues,
mountNode = null,
} = props;
const triggerId = useId('menu');
let triggerId = useId('menu');
const [contextTarget, setContextTarget] = usePositioningMouseTarget();

const positioningState = {
Expand Down Expand Up @@ -87,6 +87,9 @@ export const useMenu_unstable = (props: MenuProps): MenuState => {
} else if (children.length === 1) {
menuPopover = children[0];
}
if (menuTrigger?.props?.children?.props?.id) {
triggerId = menuTrigger.props.children.props.id;
}

const { targetRef: triggerRef, containerRef: menuPopoverRef } = usePositioning(positioningState);

Expand Down
Loading