Skip to content

Commit

Permalink
feat(Menu): pass aria-label to menu button instead of menu (#4177)
Browse files Browse the repository at this point in the history
* feat(Menu): pass `aria-label` to menu button instead of menu

When using `<Menu>` with only a icon and no text element, there needs to be a `aria-label` on the button.

* Create flat-bugs-sparkle.md
  • Loading branch information
sebald authored Sep 27, 2024
1 parent 503b53a commit 7ea3838
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-bugs-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marigold/components": patch
---

feat(Menu): pass `aria-label` to menu button instead of menu
24 changes: 23 additions & 1 deletion packages/components/src/Menu/Menu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cleanup, fireEvent, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import React, { SVGProps } from 'react';
import { Theme, cva } from '@marigold/system';
import { Button } from '../Button';
import { setup } from '../test.utils';
Expand Down Expand Up @@ -316,3 +316,25 @@ test('supports Menu with sections', () => {
expect(screen.getByText('Food')).toBeInTheDocument();
expect(screen.getByText('Fruits')).toBeInTheDocument();
});

test('pass "aria-label" to button (when you use a menu with only an icon)', () => {
const Icon = (props: SVGProps<SVGSVGElement>) => (
<svg {...props}>
<circle cx={12} cy={12} r={10} />
</svg>
);

render(
<Menu
data-testid="menu"
aria-label="Descriptive label for the button"
label={<Icon />}
>
<Menu.Item key="burger">Burger</Menu.Item>
<Menu.Item key="pizza">Pizza</Menu.Item>
</Menu>
);

const btn = screen.getByLabelText('Descriptive label for the button');
expect(btn).toBeInTheDocument();
});
3 changes: 2 additions & 1 deletion packages/components/src/Menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ const _Menu = ({
disabled,
open,
placement,
'aria-label': ariaLabel,
...props
}: MenuProps) => {
const classNames = useClassNames({ component: 'Menu', variant, size });

return (
<MenuTrigger {...props}>
<Button variant="menu" disabled={disabled}>
<Button variant="menu" disabled={disabled} aria-label={ariaLabel}>
{label}
</Button>
<Popover open={open} placement={placement}>
Expand Down

0 comments on commit 7ea3838

Please sign in to comment.