-
Notifications
You must be signed in to change notification settings - Fork 6.5k
fix(ui): make mobile nav toggle keyboard accessible #8801
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||||||||||||||||||||||||||||||
| import { describe, it } from 'node:test'; | ||||||||||||||||||||||||||||||||||
| import assert from 'node:assert/strict'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import { render, screen } from '@testing-library/react'; | ||||||||||||||||||||||||||||||||||
| import userEvent from '@testing-library/user-event'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import NavBar from '..'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const Logo = () => <span>Node.js</span>; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| describe('NavBar', () => { | ||||||||||||||||||||||||||||||||||
| it('uses a keyboard-accessible button to toggle the mobile navigation', async () => { | ||||||||||||||||||||||||||||||||||
| const user = userEvent.setup(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| render( | ||||||||||||||||||||||||||||||||||
| <NavBar | ||||||||||||||||||||||||||||||||||
| as="a" | ||||||||||||||||||||||||||||||||||
| Logo={Logo} | ||||||||||||||||||||||||||||||||||
| pathname="/" | ||||||||||||||||||||||||||||||||||
| sidebarItemTogglerAriaLabel="Toggle navigation menu" | ||||||||||||||||||||||||||||||||||
| navItems={[ | ||||||||||||||||||||||||||||||||||
| { text: 'Learn', link: '/learn' }, | ||||||||||||||||||||||||||||||||||
| { text: 'About', link: '/about' }, | ||||||||||||||||||||||||||||||||||
| ]} | ||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||
| <a href="/search">Search</a> | ||||||||||||||||||||||||||||||||||
| </NavBar> | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const menuButton = screen.getByRole('button', { | ||||||||||||||||||||||||||||||||||
| name: 'Toggle navigation menu', | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| const navigation = screen | ||||||||||||||||||||||||||||||||||
| .getByRole('navigation') | ||||||||||||||||||||||||||||||||||
| .querySelector('#navbar-navigation'); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| assert.ok(menuButton); | ||||||||||||||||||||||||||||||||||
| assert.ok(navigation); | ||||||||||||||||||||||||||||||||||
| assert.equal(menuButton.tagName, 'BUTTON'); | ||||||||||||||||||||||||||||||||||
| assert.equal(menuButton.getAttribute('tabindex'), null); | ||||||||||||||||||||||||||||||||||
| assert.equal(menuButton.getAttribute('aria-expanded'), 'false'); | ||||||||||||||||||||||||||||||||||
| assert.match(navigation.className, /\bhidden\b/); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| await user.click(menuButton); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| assert.equal(menuButton.getAttribute('aria-expanded'), 'true'); | ||||||||||||||||||||||||||||||||||
| assert.match(navigation.className, /\bflex\b/); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| await user.click(menuButton); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+49
|
||||||||||||||||||||||||||||||||||
| await user.click(menuButton); | |
| assert.equal(menuButton.getAttribute('aria-expanded'), 'true'); | |
| assert.match(navigation.className, /\bflex\b/); | |
| await user.click(menuButton); | |
| await user.tab(); | |
| assert.equal(document.activeElement, menuButton); | |
| await user.keyboard('{Enter}'); | |
| assert.equal(menuButton.getAttribute('aria-expanded'), 'true'); | |
| assert.match(navigation.className, /\bflex\b/); | |
| await user.keyboard(' '); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| import Hamburger from '@heroicons/react/24/solid/Bars3Icon'; | ||
| import XMark from '@heroicons/react/24/solid/XMarkIcon'; | ||
| import * as Label from '@radix-ui/react-label'; | ||
| import classNames from 'classnames'; | ||
| import { useState } from 'react'; | ||
|
|
||
|
|
@@ -55,25 +54,22 @@ const NavBar: FC<PropsWithChildren<NavbarProps>> = ({ | |
| <Logo /> | ||
| </Component> | ||
|
|
||
| <Label.Root | ||
| <button | ||
| className={styles.sidebarItemTogglerLabel} | ||
| htmlFor="sidebarItemToggler" | ||
| role="button" | ||
| type="button" | ||
| aria-label={sidebarItemTogglerAriaLabel} | ||
| aria-controls="navbar-navigation" | ||
| aria-expanded={isMenuOpen} | ||
| onClick={() => setIsMenuOpen(previousState => !previousState)} | ||
| > | ||
| {navInteractionIcons[isMenuOpen ? 'close' : 'show']} | ||
| </Label.Root> | ||
| </button> | ||
|
Comment on lines
+57
to
+66
|
||
| </div> | ||
|
|
||
| <input | ||
| className={classNames('peer', styles.sidebarItemToggler)} | ||
| id="sidebarItemToggler" | ||
| type="checkbox" | ||
| onChange={e => setIsMenuOpen(() => e.target.checked)} | ||
| aria-label={sidebarItemTogglerAriaLabel} | ||
| tabIndex={-1} | ||
| /> | ||
| <div className={classNames(styles.main, `hidden peer-checked:flex`)}> | ||
| <div | ||
| id="navbar-navigation" | ||
| className={classNames(styles.main, isMenuOpen ? 'flex' : 'hidden')} | ||
| > | ||
|
Comment on lines
+57
to
+72
|
||
| {navItems && navItems.length > 0 && ( | ||
| <div className={styles.navItems}> | ||
| {navItems.map(({ text, link, target }) => ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test seems unnecessary