Every record editor gets a sticky "On this page" column listing its field groups and collections (with row counts), lighting the one in view and marking any with a save error. Below lg it becomes a sticky "Jump to" menu. Editor pages widen to fit it through a small layout context; other admin pages keep their width. Each collection now sits in its own card, and repeater rows fold to their title line. Rows start folded, with Expand all and Collapse all; a new row, or one the server rejected, opens itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
5.3 KiB
TypeScript
141 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. */
|
|
export function SectionNav({ sections }: { sections: EditorSection[] }) {
|
|
const active = useActiveSection(sections);
|
|
|
|
return (
|
|
<nav aria-label="Sections" className="sticky top-6 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 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>
|
|
);
|
|
}
|