Topic: Both the account link and its child link are marked as the current page

briskbyte priority asked 6 days ago


Our sidebar has links for /account and /account/settings. On the settings screen both links receive the active appearance and both announce themselves as the current page to a screen reader.

The wrapper currently marks a link active when the current pathname starts with its href. We want the parent section to remain visually expanded, but only the actual page link should have aria-current.


Mateusz Lazaru staff answered 5 days ago


Use exact path matching for aria-current="page". A prefix match can still determine whether a parent section is expanded, but that is a separate state from identifying the current page link.

If your router supplies an exact-match option, use that for the leaf link. Normalize trailing slashes consistently before comparing paths, and leave aria-current undefined on links that are not the current page.

const isCurrentPage = pathname === href;
return (
  <a href={href} aria-current={isCurrentPage ? 'page' : undefined}>
    {label}
  </a>
);

https://mdbootstrap.com/docs/react/navigation/sidenav/



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB React
  • MDB Version: MDB5 9.0.0
  • Device: Desktop
  • Browser: Firefox
  • OS: macOS
  • Provided sample code: No
  • Provided link: No