Skip to content
Open
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
13 changes: 11 additions & 2 deletions src/claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ export function setupLinkClaims(
const location = router.location;
const routing = plugin && plugin.track();
const url = managedUrl(a);
const target = url && url.pathname + url.search;
// Include hash so hashHistory's parsePath can recover `/applications`
// from an href like `#/applications` (URL pathname alone is `/`). Same
// spelling the click handler feeds parsePath (#746).
const target =
url &&
router.parsePath(url.pathname + url.search + (("hash" in url && url.hash) || ""));
// no per-anchor `end` opt-out like useLinkState has
const { active, current } = match(target);
const pending = !!routing && plugin!.pending(target);
Expand Down Expand Up @@ -257,7 +262,11 @@ export function setupServerLinkClaims(
basePath
);
if (!url) return "";
const { active, current } = match(url.pathname + url.search, url.comparable);
// Same parsePath path as the client claims / click handler (#746).
const target = router.parsePath(
url.pathname + url.search + (("hash" in url && url.hash) || "")
);
const { active, current } = match(target, url.comparable);
return current ? ' data-active aria-current="page"' : active ? " data-active" : "";
});
}
9 changes: 7 additions & 2 deletions src/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,15 @@ export const useLinkState = (
): LinkState => {
const router = useRouter();
const location = router.location;
// a paths node's brand is its logical path; toString is the display href
// a paths node's brand is its logical path; toString is the display href.
// Display strings under hashHistory start with `#` — map them back through
// parsePath the same way navigate() does, so `useLinkState("#/about")`
// matches location.pathname `/about` (#746).
const to = useResolvedPath(() => {
const h: any = href();
return h[HREF] || String(h);
let path: string = h[HREF] || String(h);
if (path[0] === "#") path = router.parsePath(path);
return path;
});
const base = router.base.path();
const state = createMemo(() => linkMatcher(location, base, options.end)(to()));
Expand Down
103 changes: 102 additions & 1 deletion test/claims.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "solid-js";
import { vi } from "vitest";
import { setupLinkClaims } from "../src/claims.js";
import { createRouter, memoryHistory, pendingLinks } from "../src/index.js";
import { createRouter, hashHistory, memoryHistory, pendingLinks } from "../src/index.js";
import type { LinkState, Navigator } from "../src/index.js";
import { useLinkState, useNavigate } from "../src/index.js";
import type { RouterContext } from "../src/types.js";
Expand Down Expand Up @@ -1163,3 +1163,104 @@ describe("link state parity: claimed anchors and useLinkState", () => {
}
});
});

describe("hashHistory link claims and useLinkState (#746)", () => {
const originalScrollTo = window.scrollTo;
beforeEach(() => {
window.scrollTo = vi.fn() as any;
window.history.replaceState(null, "", "/#/");
});
afterEach(() => {
window.history.replaceState(null, "", "/");
});
afterAll(() => {
window.scrollTo = originalScrollTo;
});

const routes = [
{ path: "/", component: () => <div data-route="home" /> },
{ path: "/applications", component: () => <div data-route="applications" /> }
] as const;

test("only the current hash link gets aria-current / data-active", async () => {
const div = mount();
let navigate!: Navigator;
const Router = createRouter({ routes, history: hashHistory() });
const dispose = render(
() => (
<Router>
{props => {
navigate = useNavigate();
return (
<nav>
<a data-testid="home" href="#/">
Home
</a>
<a data-testid="applications" href="#/applications">
Applications
</a>
{props.children}
</nav>
);
}}
</Router>
),
div
);
try {
await settle();
const home = div.querySelector('[data-testid="home"]')!;
const applications = div.querySelector('[data-testid="applications"]')!;

expect(home.getAttribute("aria-current")).toBe("page");
expect(home.hasAttribute("data-active")).toBe(true);
expect(applications.hasAttribute("aria-current")).toBe(false);
expect(applications.hasAttribute("data-active")).toBe(false);

navigate("/applications");
await settle();

expect(applications.getAttribute("aria-current")).toBe("page");
expect(applications.hasAttribute("data-active")).toBe(true);
expect(home.hasAttribute("aria-current")).toBe(false);
expect(home.hasAttribute("data-active")).toBe(false);
} finally {
dispose();
div.remove();
}
});

test("useLinkState understands display hash hrefs and paths nodes", async () => {
window.history.replaceState(null, "", "/#/applications");
const Router = createRouter({ routes, history: hashHistory() });
let hashHref!: LinkState;
let pathsNode!: LinkState;
const div = mount();
const dispose = render(
() => (
<Router>
{p => (
<>
{
((hashHref = useLinkState(() => "#/applications")),
(pathsNode = useLinkState(() => Router.paths.applications)),
null)
}
{p.children}
</>
)}
</Router>
),
div
);
try {
await settle();
expect(String(Router.paths.applications)).toBe("#/applications");
expect([hashHref.active(), hashHref.current()]).toEqual([true, true]);
expect([pathsNode.active(), pathsNode.current()]).toEqual([true, true]);
} finally {
dispose();
div.remove();
}
});
});