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
5 changes: 5 additions & 0 deletions .changeset/calm-anchors-scroll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-iso': patch
---

Scroll cross-route URL fragments to matching IDs or named anchors after the destination route commits.
22 changes: 21 additions & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ function isInScope(href) {
);
}

/** @param {string} url */
function scrollToUrl(url) {
const hash = new URL(url, location.origin).hash;
if (hash) {
try {
const fragment = decodeURIComponent(hash.slice(1));
const target = document.getElementById(fragment) ||
Array.from(document.getElementsByName(fragment)).find(
element => element.localName == 'a'
);
Comment on lines +33 to +35

@rschristian rschristian Aug 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know that we really need to support this, as it's super niche, but this can be done simpler:

Suggested change
Array.from(document.getElementsByName(fragment)).find(
element => element.localName == 'a'
);
document.querySelector(`a[name="${fragment}"`);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid potential CSS injection issues mainly

if (target) {
target.scrollIntoView();
return;
}
} catch {}
}

scrollTo(0, 0);
}

/**
* @param {string} state
* @param {MouseEvent | PopStateEvent | { url: string, replace?: boolean }} action
Expand Down Expand Up @@ -274,7 +294,7 @@ export function Router(props) {

// The route is loaded and rendered.
if (prevRoute.current !== path) {
if (wasPush) scrollTo(0, 0);
if (wasPush) scrollToUrl(url);
if (props.onRouteChange) props.onRouteChange(url);

prevRoute.current = path;
Expand Down
7 changes: 6 additions & 1 deletion test/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,15 @@ describe('Router', () => {
scrollTo.restore();
});

it('should ignore clicks on document fragment links', async () => {
it('should handle document fragment links', async () => {
Comment thread
rschristian marked this conversation as resolved.
const pushState = sinon.spy(history, 'pushState');
const scrollIntoView = sinon.spy(Element.prototype, 'scrollIntoView');

const Route = sinon.fake(
() => <div>
<a href="#foo">just #foo</a>
<a href="/other#bar">other #bar</a>
<a name="bar">bar target</a>
</div>
);
render(
Expand Down Expand Up @@ -815,8 +817,11 @@ describe('Router', () => {
expect(loc).to.deep.include({ url: '/other#bar', path: '/other' });
expect(pushState).to.have.been.called;
expect(location.hash).to.equal('#bar');
expect(scrollIntoView).to.have.been.calledOnce;
expect(scrollIntoView).to.have.been.calledOn(scratch.querySelector('a[name="bar"]'));

pushState.restore();
scrollIntoView.restore();
});

it('should ignore clicks on download links', async () => {
Expand Down