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 (
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
type RouteObject,
|
||||
useLocation,
|
||||
useNavigate,
|
||||
useNavigationType,
|
||||
useParams,
|
||||
useSearchParams,
|
||||
} from "react-router-dom";
|
||||
@@ -165,6 +166,7 @@ function RouteLifecycle({
|
||||
buildId: string;
|
||||
}) {
|
||||
const { pathname, search } = useLocation();
|
||||
const navigationType = useNavigationType();
|
||||
const { message, resolve } = useLocale();
|
||||
const diagnostics = useApplicationDiagnostics();
|
||||
// A locale change re-renders the title only; it is not a route change.
|
||||
@@ -178,7 +180,12 @@ function RouteLifecycle({
|
||||
const main = document.getElementById("main-content");
|
||||
main?.focus({ preventScroll: true });
|
||||
try {
|
||||
if (!navigator.userAgent.toLowerCase().includes("jsdom")) {
|
||||
// 08-08. Back and forward leave the scroll to the browser, which
|
||||
// restores the position it saved for that entry.
|
||||
if (
|
||||
navigationType !== "POP" &&
|
||||
!navigator.userAgent.toLowerCase().includes("jsdom")
|
||||
) {
|
||||
window.scrollTo({ top: 0, left: 0, behavior: "auto" });
|
||||
}
|
||||
} catch {
|
||||
@@ -188,7 +195,7 @@ function RouteLifecycle({
|
||||
routeId: definition.routeId,
|
||||
buildId,
|
||||
});
|
||||
}, [buildId, definition, diagnostics, pathname, search]);
|
||||
}, [buildId, definition, diagnostics, navigationType, pathname, search]);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,11 @@
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
/* 08-08. Hash targets land below the sticky 4.5rem header. */
|
||||
html {
|
||||
scroll-padding-top: 4.5rem;
|
||||
}
|
||||
|
||||
.app-shell {
|
||||
display: grid;
|
||||
min-height: 100vh;
|
||||
|
||||
Reference in New Issue
Block a user