Exercises
Hamburger navigation
Over the past few years, a common convention has emerged: the hamburger menu.
Clicking or tapping the hamburger icon will open a navigation menu. This pattern is especially common on mobile, but can be found on some desktop sites as well.
Below, you'll find an app with an incomplete hamburger menu. Your job is to finish implementing it, taking care to consider the usability and accessibility of this feature.
Acceptance Criteria:
- While the menu is open, focus should be trapped within the modal (you can use the provided
FocusLock
component). - When the menu is closed, focus should be restored to the last-focused element.
- While the menu is open, scrolling should be disabled (you can use the provided
RemoveScroll
component). - Hitting "Escape" should close the menu.
- Clicking the backdrop should close the menu.
- The markup should be structured correctly, for screen-reader users. Here are some links to help you out:
NOTE: We don't want to automatically focus the dismiss button when the menu opens, like we did in the previous “Modals” lesson. We'll discuss in the solution video below.
Code Playground
import React from 'react';
import { Menu } from 'react-feather';
import useToggle from './use-toggle';
import Drawer from './Drawer';
import styles from './Header.module.css';
function Header() {
const [isMenuOpen, toggleIsMenuOpen] = useToggle(false);
return (
<header>
<a href="">Kaboom</a>
<div>
<button
className={styles.hamburgerBtn}
onClick={toggleIsMenuOpen}
>
<Menu />
</button>
{isMenuOpen && (
<Drawer handleDismiss={toggleIsMenuOpen}>
<ul className={styles.navigationList}>
<li>
<a href="">Home</a>
</li>
<li>
<a href="">Gallery</a>
</li>
<li>
<a href="">Photographers</a>
</li>
<li>
<a href="">Submit Work</a>
</li>
</ul>
</Drawer>
)}
</div>
</header>
);
}
export default Header;
Solution: