From 18991b8ce32484aa67e475504a9224dfbc1614e6 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 00:03:56 +0000 Subject: [PATCH] fix(A): set aria-current only when the query matches too Filter links like /?tag=a and /?tag=b were all marked aria-current="page" because only the pathname was compared. The current-page state now also requires the link's search params to match the location's, ignoring order, the same rule 2.0 uses. The active class keeps matching on the path. Fixes #518 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0117BoWPn7eCrWhevqJzoZeX --- .changeset/aria-current-query.md | 5 +++++ src/components.tsx | 16 +++++++++++++++- test/anchor.spec.tsx | 28 ++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 .changeset/aria-current-query.md diff --git a/.changeset/aria-current-query.md b/.changeset/aria-current-query.md new file mode 100644 index 00000000..e005078d --- /dev/null +++ b/.changeset/aria-current-query.md @@ -0,0 +1,5 @@ +--- +"@solidjs/router": patch +--- + +`` sets `aria-current="page"` only when the link's query also matches the current location (parameter order aside), so filter links like `/?tag=a` and `/?tag=b` are no longer all marked as the current page. The active class still matches on the path only (#518). diff --git a/src/components.tsx b/src/components.tsx index fcf2bab8..ae57791d 100644 --- a/src/components.tsx +++ b/src/components.tsx @@ -6,6 +6,12 @@ import { useHref, useLocation, useNavigate, useResolvedPath } from "./routing.js import type { Location, Navigator } from "./types.js"; import { normalizePath } from "./utils.js"; +const comparableQuery = (search: string) => { + const params = new URLSearchParams(search); + params.sort(); + return params.toString(); +}; + declare module "solid-js" { namespace JSX { interface AnchorHTMLAttributes { @@ -85,7 +91,15 @@ export function A(props: AnchorProps) { // trailing slashes are ignored so `/route` and `/route/` share active state const path = normalizePath(to_.split(/[?#]/, 1)[0]).toLowerCase().replace(/\/$/, ""); const loc = normalizeLocationPath(location.pathname); - return [props.end ? path === loc : loc.startsWith(path + "/") || loc === path, path === loc]; + if (path !== loc) return [!props.end && loc.startsWith(path + "/"), false]; + // aria-current="page" also needs the query to match (parameter order aside), + // so filter links like `/?tag=a` and `/?tag=b` are not all the current page + const hashless = to_.split("#", 1)[0]; + const q = hashless.indexOf("?"); + return [ + true, + comparableQuery(location.search) === comparableQuery(q < 0 ? "" : hashless.slice(q)) + ]; }); // One read, so a getter backed `state` is not observed twice const state = () => { diff --git a/test/anchor.spec.tsx b/test/anchor.spec.tsx index 6c631dfd..c194f243 100644 --- a/test/anchor.spec.tsx +++ b/test/anchor.spec.tsx @@ -49,6 +49,34 @@ describe("", () => { dispose(); }); + test("sets aria-current only when the query matches too (#518)", () => { + const links = () => ( + <> + a + b + b2 + all + + ); + const history = createMemoryHistory(); + history.set({ value: "/docs/intro?tag=b&b=2" }); + const root = document.createElement("div"); + const dispose = render( + () => ( + + + + ), + root + ); + const anchors = [...root.querySelectorAll("a")]; + // every link is on the current path, so all keep the active class + expect(anchors.map(a => a.className)).toEqual(["active", "active", "active", "active"]); + // only the link whose query matches (in any order) is the current page + expect(anchors.map(a => a.getAttribute("aria-current"))).toEqual([null, null, "page", null]); + dispose(); + }); + test("marks itself active for a parent path unless end is set", () => { const parent = mount("/docs/intro", () => parent); expect(parent.anchor().className).toBe("active");