Green and amber sections give it their palette's lightest shade, teal and deep teal the teal one (the darkest in dark mode), the shades the nav's text reads on, easing between them as the reader scrolls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
232 lines
9 KiB
TypeScript
232 lines
9 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
PAGE SHELL
|
|
Just the chrome that makes pages look alike: a page header, then
|
|
sections with a thin accent banner between them and a left-aligned
|
|
heading with a blurb and rule. Sections paint no background of
|
|
their own: the page's (PageBackdrop.tsx) runs behind all of them.
|
|
|
|
`sideNav` adds an "On this page" list in the left margin, the
|
|
admin editors' own (SectionNav), sticky under the header, listing
|
|
the sections with the one being read lit. It sits beside the
|
|
sections rather than taking a column from them, so they stay where
|
|
they'd be without it; it shows once the margin beside the 72rem
|
|
content is wide enough for it (88rem screens up, titles wrapping
|
|
onto a second line). Narrower, the header's dropdowns list the
|
|
sections instead.
|
|
|
|
Whatever goes inside a section is passed as `content`, the shell
|
|
doesn't care what it is.
|
|
|
|
No <main> and no footer here, Layout provides both, so a page
|
|
using this shell drops straight into the router's Outlet.
|
|
|
|
Usage:
|
|
<PageShell
|
|
title="Community"
|
|
intro="One line under the page title."
|
|
sections={[
|
|
{
|
|
id: "local", // matches the nav hash, #local
|
|
|
|
title: "Local Groups",
|
|
blurb: "Optional line under the section heading.",
|
|
accent: BRAND, // heading, banner, rule
|
|
actions: <SomeButton />, // optional, right of the heading
|
|
content: <YourStuff />,
|
|
},
|
|
]}
|
|
/>
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { useEffect, useRef, useState, type ReactNode } from "react";
|
|
|
|
import { documentTitle } from "../lib/site.ts";
|
|
import { accentText, ACCENTS, BRAND } from '../lib/color.ts'
|
|
import { SectionNav, useActiveSection } from "./admin/SectionNav.tsx";
|
|
import "./PageShell.css";
|
|
|
|
|
|
export type ShellSection = {
|
|
id: string;
|
|
title: string;
|
|
blurb?: string;
|
|
accent: string;
|
|
actions?: ReactNode;
|
|
content: ReactNode;
|
|
};
|
|
|
|
type PageShellProps = {
|
|
title: ReactNode;
|
|
intro?: ReactNode;
|
|
sections: ShellSection[];
|
|
/** An "On this page" column beside the sections, from lg up. */
|
|
sideNav?: boolean;
|
|
};
|
|
|
|
export function Section({ section }: { section: ShellSection }) {
|
|
const {
|
|
id,
|
|
title,
|
|
blurb,
|
|
accent,
|
|
// Optional action bar for this section's heading row, a view
|
|
// toggle, a filter, a link. Whatever the section needs; the
|
|
// shell just gives it a place to sit.
|
|
actions,
|
|
content,
|
|
} = section;
|
|
|
|
return (
|
|
<section
|
|
id={id}
|
|
className="py-20"
|
|
// Clear of the fixed header, whatever it holds today (Layout
|
|
// sets --header-h from its measured height).
|
|
style={{ scrollMarginTop: "calc(var(--header-h, 5rem) + 1rem)" }}
|
|
>
|
|
{/* Heading, left aligned, optional actions on the right */}
|
|
<div className="max-w-6xl mx-auto px-6 mb-10">
|
|
<div className="flex flex-wrap items-end justify-between gap-4">
|
|
<div>
|
|
<h2 className="text-3xl md:text-4xl font-bold" style={{ color: accentText(accent) }}>
|
|
{title}
|
|
</h2>
|
|
{blurb && <p className="mt-2 text-muted max-w-xl">{blurb}</p>}
|
|
</div>
|
|
{actions}
|
|
</div>
|
|
</div>
|
|
|
|
{content}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
/* The side nav's circle: half again the list's width (or a little
|
|
more than its height, if taller), at most 22rem. */
|
|
const circleSize = (box: { width: number; height: number }) =>
|
|
Math.min(Math.max(box.width * 1.5, box.height * 1.1), 22 * 16);
|
|
|
|
/* Where the side nav sticks, and how far its list sits in from the
|
|
column's top and left: the circle centers on that. The list sits
|
|
at least LIST_TOP down, and lower if its circle needs the room to
|
|
start CIRCLE_GAP below the first section line (h-2, 8px), rather
|
|
than over it. */
|
|
const STICK = "calc(var(--header-h, 5rem) + 1rem)";
|
|
const LIST_TOP = 32;
|
|
const LIST_LEFT = 16;
|
|
const CIRCLE_GAP = 8 + 4;
|
|
|
|
/* The circle's color family for a section's accent (PageShell.css):
|
|
green and amber their own, teal and deep teal (and any other) teal. */
|
|
const circleFamily = (accent?: string) =>
|
|
accent === ACCENTS.green ? "green" : accent === ACCENTS.amber ? "amber" : "teal";
|
|
/* How far left of the list's center the circle sits. */
|
|
const CIRCLE_SHIFT = 24;
|
|
|
|
export default function PageShell({ title, intro, sections, sideNav = false }: PageShellProps) {
|
|
// The list's measured box, so its circle can center on it from a
|
|
// layer of its own (below).
|
|
// The column's width too: the circle keeps its right edge inside
|
|
// it, sliding off the screen's left edge instead when the window
|
|
// narrows.
|
|
const listRef = useRef<HTMLDivElement>(null);
|
|
const columnRef = useRef<HTMLElement>(null);
|
|
const [listBox, setListBox] = useState<{ width: number; height: number } | null>(null);
|
|
const [columnWidth, setColumnWidth] = useState(0);
|
|
useEffect(() => {
|
|
const list = listRef.current;
|
|
const column = columnRef.current;
|
|
if (!list || !column) return;
|
|
const ro = new ResizeObserver(() => {
|
|
setListBox({ width: list.offsetWidth, height: list.offsetHeight });
|
|
setColumnWidth(column.offsetWidth);
|
|
});
|
|
ro.observe(list);
|
|
ro.observe(column);
|
|
return () => ro.disconnect();
|
|
}, [sideNav]);
|
|
const circle = listBox && circleSize(listBox);
|
|
const navSections = sideNav ? sections.map((s) => ({ id: s.id, label: s.title })) : [];
|
|
const active = useActiveSection(navSections);
|
|
const activeAccent = sections.find((s) => s.id === active)?.accent;
|
|
const listTop = listBox && circle ? Math.max(LIST_TOP, CIRCLE_GAP + circle / 2 - listBox.height / 2) : LIST_TOP;
|
|
|
|
// The tab's title as the visitor moves around. The first load's
|
|
// comes from the server (pageMeta.js), which also sets the page's
|
|
// description and preview for search and link sharing.
|
|
useEffect(() => {
|
|
if (typeof title === "string") document.title = documentTitle(title);
|
|
}, [title]);
|
|
|
|
// The thin solid banner between sections, the page's full width
|
|
// (behind the side nav).
|
|
const banner = (accent: string) => <div className="h-2" style={{ background: accent }} />;
|
|
const bands = sections.map((section, i) => (
|
|
<div key={section.id}>
|
|
{banner(section.accent)}
|
|
<Section section={section} />
|
|
{i === sections.length - 1 && banner(section.accent)}
|
|
</div>
|
|
));
|
|
|
|
return (
|
|
<>
|
|
{/* Page header */}
|
|
<div className="pt-24 pb-12 px-6">
|
|
<div className="max-w-6xl mx-auto">
|
|
<h1 className="text-4xl sm:text-5xl md:text-6xl font-bold [overflow-wrap:anywhere] hyphens-auto" style={{ color: accentText(BRAND) }}>
|
|
{title}
|
|
</h1>
|
|
{intro && <p className="mt-3 text-lg text-muted max-w-2xl">{intro}</p>}
|
|
</div>
|
|
</div>
|
|
|
|
{sideNav ? (
|
|
<div className="relative">
|
|
{/* The margin left of the content (72rem, its text 1.5rem in),
|
|
clipping sideways only, so nothing reaches the
|
|
content or the screen's edge. clip, not hidden: hidden
|
|
would make it a scroll container, and the sticky layers in
|
|
it would stop sticking. */}
|
|
<aside
|
|
ref={columnRef}
|
|
className="absolute inset-y-0 left-0 hidden overflow-x-clip min-[88rem]:block"
|
|
style={{ width: "calc((100% - 72rem) / 2 + 1rem)" }}
|
|
>
|
|
{/* A solid circle centered on the list, in the light shade
|
|
of the section being read (PageShell.css): a sticky layer of
|
|
its own, in front of the section lines like the list,
|
|
which comes after it and so draws over it. Height 0, so
|
|
the list's layer below starts, and sticks, at the same
|
|
place. */}
|
|
{listBox && circle ? (
|
|
<div aria-hidden="true" className="pointer-events-none sticky z-10 h-0" style={{ top: STICK }}>
|
|
<div
|
|
className="side-nav-circle absolute rounded-full"
|
|
data-family={circleFamily(activeAccent)}
|
|
style={{
|
|
width: circle,
|
|
height: circle,
|
|
left: Math.min(LIST_LEFT + listBox.width / 2 - CIRCLE_SHIFT - circle / 2, columnWidth - circle),
|
|
top: listTop + listBox.height / 2 - circle / 2,
|
|
}}
|
|
/>
|
|
</div>
|
|
) : null}
|
|
{/* The list, in front of the section lines and its circle. */}
|
|
<div className="sticky z-10 pl-4 pr-2" style={{ top: STICK, paddingTop: listTop }}>
|
|
<div ref={listRef} className="w-fit max-w-full">
|
|
<SectionNav sections={navSections} wrap />
|
|
</div>
|
|
</div>
|
|
</aside>
|
|
{bands}
|
|
</div>
|
|
) : (
|
|
bands
|
|
)}
|
|
|
|
</>
|
|
);
|
|
}
|