The nav was sticky inside a wrapper only as tall as itself, so it had nowhere to travel and scrolled away. The grid cell now sticks instead, and the mobile "Jump to" bar sits straight in the page for the same reason. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
5.3 KiB
TypeScript
142 lines
5.3 KiB
TypeScript
/* ═══════════════════════════════════════════════════════════════
|
|
EDITOR SECTION NAV
|
|
|
|
The record editor's table of contents. 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";
|
|
|
|
export type EditorSection = {
|
|
id: string;
|
|
label: string;
|
|
count?: number;
|
|
hasError?: boolean;
|
|
};
|
|
|
|
const ACTIVE_LINE = 140;
|
|
|
|
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: "smooth", block: "start" });
|
|
}
|
|
|
|
function Label({ section }: { section: EditorSection }) {
|
|
return (
|
|
<>
|
|
<span className="truncate">{section.label}</span>
|
|
{section.count != null && (
|
|
<span className="text-xs text-[#4a6b72]/70">({section.count})</span>
|
|
)}
|
|
{section.hasError && (
|
|
<span aria-label="needs attention" className="h-1.5 w-1.5 shrink-0 rounded-full bg-[#b3261e]" />
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
/* The desktop column. Place it in the grid's first track, in a cell
|
|
that is itself sticky (see EntityEdit). */
|
|
export function SectionNav({ sections }: { sections: EditorSection[] }) {
|
|
const active = useActiveSection(sections);
|
|
|
|
return (
|
|
<nav aria-label="Sections" className="hidden max-h-[calc(100vh-8rem)] overflow-y-auto lg:block">
|
|
<p className="mb-2 px-3 text-xs font-semibold uppercase tracking-wider text-[#4a6b72]/70">
|
|
On this page
|
|
</p>
|
|
<ul className="space-y-0.5 border-l border-[#138ba0]/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}
|
|
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 ${
|
|
lit
|
|
? "border-[#138ba0] font-semibold text-[#138ba0]"
|
|
: "border-transparent text-[#4a6b72] hover:border-[#138ba0]/40 hover:text-[#138ba0]"
|
|
}`}
|
|
>
|
|
<Label section={section} />
|
|
</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-[#138ba0]/15 bg-[#f6fbfc]/95 px-6 py-2 backdrop-blur lg:hidden">
|
|
<label className="flex items-center gap-3 text-sm text-[#4a6b72]">
|
|
<span className="shrink-0">Jump to</span>
|
|
<select
|
|
value={active ?? ""}
|
|
onChange={(e) => jump(e.target.value)}
|
|
className="w-full rounded-lg border border-[#4a6b72]/25 bg-white px-3 py-1.5 text-sm text-[#26454c] outline-none focus:border-[#138ba0]"
|
|
>
|
|
{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>
|
|
);
|
|
}
|