fix(presentation): keep back-navigation scroll and open hash targets (08-08)
A direct link to /page#target stayed at the top: the target renders with the lazy page, after the router scrolled up. PageHeader now scrolls to it below the sticky header, focus never scrolls, and POP no longer scrolls to top. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b7fc7e53ce
commit
dbe6b769bf
@@ -6,6 +6,15 @@ export type PageHeaderProps = Readonly<{
|
||||
eyebrow?: string;
|
||||
}>;
|
||||
|
||||
function hashTarget(): HTMLElement | null {
|
||||
try {
|
||||
const id = decodeURIComponent(window.location.hash.slice(1));
|
||||
return id ? document.getElementById(id) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function PageHeader({
|
||||
title,
|
||||
description,
|
||||
@@ -21,9 +30,15 @@ export function PageHeader({
|
||||
activeElement === document.body ||
|
||||
activeElement === document.documentElement ||
|
||||
activeElement === main;
|
||||
if (routeOwnsFocus) {
|
||||
headingRef.current?.focus();
|
||||
}
|
||||
if (!routeOwnsFocus) return;
|
||||
// 08-08. A hash names a target in this page, which exists only once the
|
||||
// page has rendered. Focus never scrolls: back navigation keeps the
|
||||
// position the browser restored.
|
||||
const target = hashTarget();
|
||||
target?.scrollIntoView?.();
|
||||
(target && target.tabIndex >= 0 ? target : headingRef.current)?.focus({
|
||||
preventScroll: true,
|
||||
});
|
||||
}, [title]);
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user