Skip to content

Commit dda2f86

Browse files
Add accessibility props to NavMenu
1 parent 9046c3f commit dda2f86

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

changelog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
- New _Progress_ component based on Diamond Light added.
88
- New _ProgressDelayed_ component so that the progress isn't shown at all when it's a small wait.
9-
- NavMenu component added for creating dropdown menus in the NavBar
10-
- NavMenuLink component extends NavLink to work in the NavMenu
9+
- _NavMenu_ component added for creating dropdown menus in the Navbar
10+
- _NavMenuLink_ component extends NavLink to work in the NavMenu
1111

1212
### Fixed
1313

src/components/navigation/NavMenu.test.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ describe("NavMenu", () => {
1818
<NavMenuLink href="#test2">Link 2</NavMenuLink>
1919
</NavMenu>,
2020
);
21-
21+
const menuButton = screen.getByRole("button");
2222
expect(screen.queryByText("Link 1")).not.toBeInTheDocument();
23-
await user.click(screen.getByRole("button"));
23+
expect(menuButton).toHaveAttribute("aria-expanded", "false");
24+
await user.click(menuButton);
2425
expect(screen.getByText("Link 1")).toBeVisible();
2526
expect(screen.getByText("Link 2")).toBeVisible();
27+
expect(menuButton).toHaveAttribute("aria-expanded", "true");
2628
});
2729

2830
it("should open when selected using keyboard", async () => {
@@ -52,6 +54,17 @@ describe("NavMenu", () => {
5254
const link2 = screen.getByRole("menuitem", { name: "Link 2" });
5355
expect(document.activeElement).toBe(link2);
5456
});
57+
58+
it("should render with accessibility props", async () => {
59+
renderWithProviders(<NavMenu label="Navmenu" />);
60+
61+
const menuButton = screen.getByRole("button");
62+
const buttonControlsId = menuButton.getAttribute("aria-controls");
63+
expect(menuButton).toHaveAttribute("aria-haspopup", "menu");
64+
await user.click(menuButton);
65+
const menuId = screen.getByRole("presentation").getAttribute("id");
66+
expect(buttonControlsId).toEqual(menuId);
67+
});
5568
});
5669

5770
describe("NavMenuLink", () => {

src/components/navigation/NavMenu.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
MenuItem,
88
type MenuItemProps,
99
} from "@mui/material";
10-
import React, { useState, forwardRef } from "react";
10+
import React, { useState, forwardRef, useId } from "react";
1111
import { ExpandMore } from "@mui/icons-material";
1212
import { NavLink, NavLinkProps } from "./Navbar";
1313

@@ -54,6 +54,7 @@ const NavMenu = ({ label, children }: NavMenuProps) => {
5454
const [anchorElement, setAnchorElement] = useState<null | HTMLElement>(null);
5555
const open = Boolean(anchorElement);
5656
const [menuWidth, setMenuWidth] = useState(0);
57+
const menuId = useId();
5758

5859
const openMenu = (e: React.MouseEvent<HTMLButtonElement>) => {
5960
if (!open) {
@@ -71,6 +72,9 @@ const NavMenu = ({ label, children }: NavMenuProps) => {
7172
return (
7273
<>
7374
<Button
75+
aria-controls={menuId}
76+
aria-expanded={open}
77+
aria-haspopup="menu"
7478
onClick={(e) => openMenu(e)}
7579
disableFocusRipple
7680
sx={{
@@ -108,6 +112,7 @@ const NavMenu = ({ label, children }: NavMenuProps) => {
108112
/>
109113
</Button>
110114
<Menu
115+
id={menuId}
111116
open={open}
112117
onClose={closeMenu}
113118
anchorEl={anchorElement}

0 commit comments

Comments
 (0)