[Bug] AppShell skip link changes the URL hash, so a hash router leaves the page
Description
AppShell's skip link is <a href="#astryx-app-shell-main">. Its click handler moves focus to the main region, but it doesn't call preventDefault(), so the browser also follows the link and sets location.hash to #astryx-app-shell-main.
In an app that keeps its routes in the hash (hash history in TanStack Router or React Router, with pages such as #/orders/46), the router reads that hash as a route. Using the skip link takes the user off the page they were on, to the not-found route or wherever the app sends unknown routes.
Expected: the skip link moves focus to the main region and leaves the URL alone.
The handler already moves focus itself, so following the link adds nothing. A possible fix in AppShell:
const handleSkipLinkClick = useCallback((event) => {
event.preventDefault();
document.getElementById(mainContentId)?.focus();
}, [mainContentId]);
The href can stay, so the link still works without JavaScript.
Reproduction
- Render an
AppShellin an app that uses TanStack Router withcreateHashHistory(), on a route such as#/orders/46. - Press Tab to show "Skip to content", then press Enter.
- Focus moves to the main region, but
location.hashbecomes#astryx-app-shell-mainand the router leaves/orders/46.
In a Vitest + Testing Library test, fireEvent.click(screen.getByTestId("skip-to-content")) returns true (the default action isn't prevented), and a hashchange event fires with #astryx-app-shell-main.
Astryx Version
@astryxdesign/[email protected] (the handler is the same in 0.7.17)
Environment
Reproduced in jsdom with Vitest. Not specific to a browser: the link's default action runs wherever the click isn't prevented. Found with TanStack Router 1.170 and createHashHistory().
🤖 (Posted by Cindy's agent.) Thanks for the clear write-up and repro, @inesgomas! Confirmed on main: AppShell's skip link moves focus to the main region in its click handler but doesn't prevent the link's default, so the browser also sets location.hash, and a hash router reads that as a route change. Your suggested fix (prevent the default, keep the href so the link still works without JavaScript) matches what the handler already intends. I've routed it as a fix and will post here when it's up.
I'd like to work on this. Following your suggested fix: preventDefault() in AppShell's skip-link click handler, keeping the href so the link still works without JavaScript, plus a regression test that starts on a hash route (#/orders/46), activates the skip link, and checks that focus moves to main and the hash stays. Could you assign it to me? I'll open a draft PR then.
Thanks @cixzhang, I posted before I saw your note. Happy to leave it with you.
🤖 (Posted by Cindy's agent.) A fix is up in #13835: the skip link now prevents the browser's default navigation, so it moves focus to the main region without changing location.hash, and the href stays so it still works without JavaScript. I'll post here when it lands. Thanks again, @inesgomas!
🤖 (Posted by Cindy's agent.) Fixed in #13835: AppShell's skip link now moves focus to the main region and leaves the URL alone, so a hash router stays on the current page. The link keeps its href, so it still works without JavaScript. It ships in the next release. Thanks again for the clear report and the suggested fix, @inesgomas!