Skip to content

Commit 162830b

Browse files
committed
Improve NavBar accessibility
1 parent 9ce1c12 commit 162830b

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org).
66

7+
## [0.9.13] - 2025-06-16
8+
### Added
9+
* *Nothing*
10+
11+
### Changed
12+
* *Nothing*
13+
14+
### Deprecated
15+
* *Nothing*
16+
17+
### Removed
18+
* *Nothing*
19+
20+
### Fixed
21+
* Improve accessibility in `NavBar` component.
22+
23+
724
## [0.9.12] - 2025-06-16
825
### Added
926
* Add tailwind-based `NavBar` component.

src/tailwind/navigation/NavBar.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { faChevronDown, faChevronUp } from '@fortawesome/free-solid-svg-icons';
22
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
33
import { clsx } from 'clsx';
44
import type { FC, HTMLProps } from 'react';
5-
import { useEffect } from 'react';
5+
import { useEffect, useId } from 'react';
66
import type { LinkProps } from 'react-router';
77
import { Link,useLocation } from 'react-router';
88
import { useToggle } from '../../hooks';
@@ -16,7 +16,7 @@ type ItemProps = {
1616
};
1717

1818
const MenuItem: FC<LinkProps & ItemProps> = ({ className, active, ...props }) => (
19-
<li role="menuitem" className="tw:w-full tw:flex">
19+
<li role="menuitem" className="tw:w-full tw:flex" data-active={active}>
2020
<Link
2121
className={clsx(
2222
'tw:px-2 tw:py-3',
@@ -37,7 +37,7 @@ const Dropdown: FC<Omit<DropdownProps, 'menuAlignment' | 'buttonVariant' | 'menu
3737
{ containerClassName, buttonClassName, menuClassName, active, ...props },
3838
) => {
3939
return (
40-
<li role="menuitem" className="tw:w-full tw:flex">
40+
<li role="menuitem" aria-haspopup className="tw:w-full tw:flex" data-active={active}>
4141
<BaseDropdown
4242
containerClassName={clsx('tw:max-md:w-full', containerClassName)}
4343
buttonVariant="text"
@@ -66,9 +66,11 @@ export type NavBarProps = HTMLProps<HTMLElement> & {
6666

6767
export const BaseNavBar: FC<NavBarProps> = ({ className, brand, children }) => {
6868
const { flag: menuOpen, toggle: toggleMenu, setToFalse: closeMenu } = useToggle(false, true);
69-
const { pathname } = useLocation();
69+
const menuId = useId();
70+
const toggleButtonId = useId();
7071

7172
// In mobile devices, collapse the navbar when the pathname changes
73+
const { pathname } = useLocation();
7274
useEffect(() => closeMenu(), [pathname, closeMenu]);
7375

7476
return (
@@ -90,6 +92,7 @@ export const BaseNavBar: FC<NavBarProps> = ({ className, brand, children }) => {
9092
{brand}
9193
</h4>
9294
<Button
95+
id={toggleButtonId}
9396
variant="secondary"
9497
className={clsx(
9598
'tw:absolute tw:right-0 tw:top-[50%] tw:translate-y-[-50%]',
@@ -99,11 +102,14 @@ export const BaseNavBar: FC<NavBarProps> = ({ className, brand, children }) => {
99102
)}
100103
onClick={toggleMenu}
101104
aria-label={`${menuOpen ? 'Hide' : 'Show'} menu`}
105+
aria-controls={menuId}
102106
>
103107
<FontAwesomeIcon icon={menuOpen ? faChevronUp : faChevronDown} />
104108
</Button>
105109
</div>
106110
<ul
111+
id={menuId}
112+
aria-labelledby={toggleButtonId}
107113
role="menu"
108114
className={clsx(
109115
'tw:m-0 tw:p-0',

0 commit comments

Comments
 (0)