41 lines
1.0 KiB
TypeScript
41 lines
1.0 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
|
|
export type PageHeaderProps = Readonly<{
|
|
title: string;
|
|
description?: string;
|
|
eyebrow?: string;
|
|
}>;
|
|
|
|
export function PageHeader({
|
|
title,
|
|
description,
|
|
eyebrow,
|
|
}: PageHeaderProps) {
|
|
const headingRef = useRef<HTMLHeadingElement | null>(null);
|
|
|
|
useEffect(() => {
|
|
const activeElement = document.activeElement;
|
|
const main = document.getElementById("main-content");
|
|
const routeOwnsFocus =
|
|
activeElement === null ||
|
|
activeElement === document.body ||
|
|
activeElement === document.documentElement ||
|
|
activeElement === main;
|
|
if (routeOwnsFocus) {
|
|
headingRef.current?.focus();
|
|
}
|
|
}, [title]);
|
|
|
|
return (
|
|
<header className="page-header">
|
|
{eyebrow ? <p className="page-header__eyebrow">{eyebrow}</p> : null}
|
|
<h1 ref={headingRef} tabIndex={-1} data-route-heading>
|
|
{title}
|
|
</h1>
|
|
{description ? (
|
|
<p className="page-header__description">{description}</p>
|
|
) : null}
|
|
</header>
|
|
);
|
|
}
|