Skip to content

fix(react-router): exclude same-page hash anchors from NavLink#5750

Open
vianmora wants to merge 1 commit into
webstudio-is:mainfrom
webstudio-community:fix/navlink-hash-anchor-upstream
Open

fix(react-router): exclude same-page hash anchors from NavLink#5750
vianmora wants to merge 1 commit into
webstudio-is:mainfrom
webstudio-community:fix/navlink-hash-anchor-upstream

Conversation

@vianmora
Copy link
Copy Markdown

@vianmora vianmora commented May 7, 2026

Problem

Links like /#section share pathname / with the root page. With end={true}, React Router's NavLink still considers / to match / regardless of the hash — so every /#section link on the homepage gets aria-current="page" in SSR.

This causes any CSS rule targeting [aria-current=page] (e.g. active nav indicator, underline, color) to apply permanently to all hash anchors on the home page, instead of only the current page link.

Fix

Exclude href values starting with /# from the NavLink branch and fall through to a plain <a>. Hash anchors don't navigate between pages so they should never receive aria-current="page".

Test

  • Nav bar on homepage: only the / link should have aria-current="page", not /#section links
  • On other pages (e.g. /about): no hash anchor links should be marked active

@kof if you want to review this one

Links like /#section share pathname "/" with the root page, so React
Router's NavLink marks them all as aria-current="page" in SSR. This
causes [aria-current=page] styles to apply to every hash anchor on the
homepage, making hover/active indicators always visible.

Plain <a> is correct here since hash anchors don't cross page boundaries.
@vianmora vianmora closed this May 7, 2026
@vianmora vianmora deleted the fix/navlink-hash-anchor-upstream branch May 7, 2026 18:50
@vianmora vianmora restored the fix/navlink-hash-anchor-upstream branch May 7, 2026 18:50
@vianmora vianmora reopened this May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant