Hovered and lit entries use their section's accent (as heading text), the editors' nav keeping the link teal. The circle's leftward shift stops at the heading's center, so short titles no longer pull it off the heading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
155 lines
6.2 KiB
TypeScript
155 lines
6.2 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
EDITOR SECTION NAV
|
|
|
|
The record editor's table of contents, and the public pages'
|
|
"On this page" column too (PageShell's sideNav). On a wide screen it's a
|
|
sticky column left of the form; below lg it's a slim sticky bar
|
|
with a "Jump to" menu. Both list the same sections, which
|
|
EntityEdit derives from the manifest (field groups, then child
|
|
collections with their row counts), so every editor gets one
|
|
without declaring anything.
|
|
|
|
The lit entry is the last section whose top has scrolled past
|
|
the line ACTIVE_LINE px below the viewport's top: the one you're
|
|
reading, not merely the one peeking in at the bottom. A section
|
|
with a save error carries a red dot, so a rejected save points
|
|
at where to look.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { scrollBehavior } from "../../lib/motion.ts";
|
|
import { accentText } from "../../lib/color.ts";
|
|
|
|
export type EditorSection = {
|
|
id: string;
|
|
label: string;
|
|
/** The public pages' section color: its entry takes it when hovered
|
|
or lit. Without one (the editors), the link teal. */
|
|
accent?: string;
|
|
count?: number;
|
|
hasError?: boolean;
|
|
};
|
|
|
|
const ACTIVE_LINE = 140;
|
|
|
|
export function useActiveSection(sections: EditorSection[]) {
|
|
const [active, setActive] = useState<string | null>(sections[0]?.id ?? null);
|
|
const ids = sections.map((section) => section.id).join("|");
|
|
|
|
useEffect(() => {
|
|
let frame = 0;
|
|
const measure = () => {
|
|
frame = 0;
|
|
let current: string | null = null;
|
|
for (const id of ids.split("|")) {
|
|
const el = document.getElementById(id);
|
|
if (el && el.getBoundingClientRect().top <= ACTIVE_LINE) current = id;
|
|
}
|
|
// At the very bottom the last short section may never reach
|
|
// the line; it's the one being read.
|
|
if (window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 4) {
|
|
current = ids.split("|").pop() ?? current;
|
|
}
|
|
setActive(current ?? ids.split("|")[0] ?? null);
|
|
};
|
|
const onScroll = () => {
|
|
if (!frame) frame = requestAnimationFrame(measure);
|
|
};
|
|
measure();
|
|
window.addEventListener("scroll", onScroll, { passive: true });
|
|
window.addEventListener("resize", onScroll);
|
|
return () => {
|
|
window.removeEventListener("scroll", onScroll);
|
|
window.removeEventListener("resize", onScroll);
|
|
if (frame) cancelAnimationFrame(frame);
|
|
};
|
|
}, [ids]);
|
|
|
|
return active;
|
|
}
|
|
|
|
function jump(id: string) {
|
|
document.getElementById(id)?.scrollIntoView({ behavior: scrollBehavior(), block: "start" });
|
|
}
|
|
|
|
function Label({ section, wrap }: { section: EditorSection; wrap?: boolean }) {
|
|
return (
|
|
<>
|
|
<span className={wrap ? "" : "truncate"}>{section.label}</span>
|
|
{section.count != null && (
|
|
<span className="text-xs text-muted/70">({section.count})</span>
|
|
)}
|
|
{section.hasError && (
|
|
<span aria-label="needs attention" className="h-1.5 w-1.5 shrink-0 rounded-full bg-danger" />
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
/* The desktop column. Place it in the grid's first track, in a cell
|
|
that is itself sticky (see EntityEdit). */
|
|
/* `wrap` lets a long label run onto a second line instead of being
|
|
cut short (the public pages' section titles, up to 30 characters). */
|
|
export function SectionNav({ sections, wrap = false }: { sections: EditorSection[]; wrap?: boolean }) {
|
|
const active = useActiveSection(sections);
|
|
|
|
return (
|
|
<nav aria-label="Sections" className="hidden max-h-[calc(100vh-8rem)] overflow-y-auto lg:block">
|
|
<p data-nav-heading className="mb-2 w-fit px-3 text-xs font-semibold uppercase tracking-wider text-muted/70">
|
|
On this page
|
|
</p>
|
|
<ul className="space-y-0.5 border-l border-brand/15">
|
|
{sections.map((section) => {
|
|
const lit = section.id === active;
|
|
return (
|
|
<li key={section.id}>
|
|
<button
|
|
type="button"
|
|
onClick={() => jump(section.id)}
|
|
aria-current={lit ? "location" : undefined}
|
|
style={section.accent ? ({ "--nav-accent": accentText(section.accent) } as React.CSSProperties) : undefined}
|
|
className={`-ml-px flex w-full items-center gap-1.5 border-l-2 px-3 py-1.5 text-left text-sm transition-colors ${
|
|
section.accent
|
|
? lit
|
|
? "border-[var(--nav-accent)] font-semibold text-[var(--nav-accent)]"
|
|
: "border-transparent text-muted hover:border-[var(--nav-accent)]/40 hover:text-[var(--nav-accent)]"
|
|
: lit
|
|
? "border-brand font-semibold text-link"
|
|
: "border-transparent text-muted hover:border-brand/40 hover:text-link"
|
|
}`}
|
|
>
|
|
<Label section={section} wrap={wrap} />
|
|
</button>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</nav>
|
|
);
|
|
}
|
|
|
|
/* The phone and tablet bar. Sits above the form, sticks to the top. */
|
|
export function SectionJump({ sections }: { sections: EditorSection[] }) {
|
|
const active = useActiveSection(sections);
|
|
|
|
return (
|
|
<div className="sticky top-0 z-20 -mx-6 mt-4 mb-2 border-b border-brand/15 bg-tint-soft/95 px-6 py-2 backdrop-blur lg:hidden">
|
|
<label className="flex items-center gap-3 text-sm text-muted">
|
|
<span className="shrink-0">Jump to</span>
|
|
<select
|
|
value={active ?? ""}
|
|
onChange={(e) => jump(e.target.value)}
|
|
className="w-full border border-muted/25 bg-surface px-3 py-1.5 text-sm text-body outline-none focus:border-brand"
|
|
>
|
|
{sections.map((section) => (
|
|
<option key={section.id} value={section.id}>
|
|
{section.label}
|
|
{section.count != null ? ` (${section.count})` : ""}
|
|
{section.hasError ? ", needs attention" : ""}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
</div>
|
|
);
|
|
}
|