Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/aria-current-query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/router": patch
---

`<A>` 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).
16 changes: 15 additions & 1 deletion src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -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 = () => {
Expand Down
28 changes: 28 additions & 0 deletions test/anchor.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ describe("<A>", () => {
dispose();
});

test("sets aria-current only when the query matches too (#518)", () => {
const links = () => (
<>
<A href="/docs/intro?tag=a">a</A>
<A href="/docs/intro?tag=b">b</A>
<A href="/docs/intro?b=2&tag=b">b2</A>
<A href="/docs/intro">all</A>
</>
);
const history = createMemoryHistory();
history.set({ value: "/docs/intro?tag=b&b=2" });
const root = document.createElement("div");
const dispose = render(
() => (
<MemoryRouter history={history}>
<Route path="/docs/intro" component={links} />
</MemoryRouter>
),
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", () => <A href="/docs">parent</A>);
expect(parent.anchor().className).toBe("active");
Expand Down
Loading