diff --git a/src/components/PageShell.tsx b/src/components/PageShell.tsx index 8e897a5..0da8629 100644 --- a/src/components/PageShell.tsx +++ b/src/components/PageShell.tsx @@ -10,9 +10,10 @@ 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. + content is wide enough for it and its circle to take in the + longest list a page may have, 10 sections of two-line titles (30 + characters, 15 a line): 2xl (96rem, 1536px) screens up. 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. @@ -102,11 +103,23 @@ export function Section({ section }: { section: ShellSection }) { ); } -/* The side nav's circle: half again the list's width (or a little - more than its height, if taller), at most 22rem. */ +/* The side nav's circle at rest: 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); +/* The height an ellipse `width` wide needs to take in a box reaching + `across` and `down` from its center (or a corner it's centered on): + (across / a)^2 + (down / b)^2 <= 1. Equal to the width when a circle + that wide would do; taller when the list is too tall for one. A + box as wide as the ellipse can't be taken in, so it stops at 95% + (about three times the box's height). */ +const coverHeight = (width: number, across: number, down: number) => { + const a = width / 2; + const ratio = Math.min(across / a, 0.95); + return Math.max(width, (2 * down) / Math.sqrt(1 - ratio * ratio)); +}; + /* How far the list sits in from the column's top and left: the circle centers on that. LIST_TOP puts "On this page" level with the first section's title: the line (h-2, 8px), the section's @@ -124,9 +137,10 @@ const STUCK_TOP = 16; const stickAt = (listTop: number) => `calc(var(--header-h, 5rem) + ${STUCK_TOP - listTop}px)`; const LIST_LEFT = 16; const CIRCLE_GAP = 8 + 4; -/* How far off the screen's left edge the stuck circle's center may - go to take in a long list on a narrow window. */ -const MAX_BACK = 1200; +/* Room the circle keeps: from the section's edge at rest, and around + the list it takes in. */ +const CIRCLE_EDGE = 16; +const CIRCLE_MARGIN = 12; /* 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. */ @@ -165,11 +179,39 @@ export default function PageShell({ title, intro, sections, sideNav = false }: P ro.observe(column); return () => ro.disconnect(); }, [sideNav]); - const circle = listBox && circleSize(listBox); const navSections = sideNav ? sections.map((s) => ({ id: s.id, label: s.title, accent: s.accent })) : []; 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 shape at rest: a circle centered on the list (a little left, + // never left of the heading's center), CIRCLE_EDGE clear of the + // section's edge, made an ellipse as tall as it takes when the list + // is too tall for the circle (10 sections of two-line titles). + // The shape at a given width: its center as near the list's as the + // section's edge allows (it may run off the screen's left edge), and + // the height it then takes. + const restAt = (width: number, box: { width: number; height: number; heading: number }) => { + const center = Math.min( + LIST_LEFT + Math.max(box.width / 2 - CIRCLE_SHIFT, box.heading), + columnWidth - CIRCLE_EDGE - width / 2, + ); + const across = Math.max(center - LIST_LEFT, LIST_LEFT + box.width - center) + CIRCLE_MARGIN; + return { width, height: coverHeight(width, across, box.height / 2 + CIRCLE_MARGIN), left: center - width / 2 }; + }; + const rest = (() => { + if (!listBox) return null; + const circle = restAt(circleSize(listBox), listBox); + if (circle.height <= circle.width) return circle; + // Too tall a list for the circle: the width, up to 22rem, that + // needs the least height. + let best = circle; + for (let width = listBox.width + 2 * CIRCLE_MARGIN; width <= 22 * 16; width += 8) { + const shape = restAt(width, listBox); + if (shape.height < best.height) best = shape; + } + return best; + })(); + const listTop = listBox && rest ? Math.max(LIST_TOP, CIRCLE_GAP + rest.height / 2 - listBox.height / 2) : LIST_TOP; // Stuck: the page has scrolled the list up to its place under the // header. The circle then becomes the corner sun (below). @@ -197,47 +239,23 @@ export default function PageShell({ title, intro, sections, sideNav = false }: P }, [sideNav, listTop]); // The circle's box, from its layer's top left (which sticks with - // the list). Before sticking: centered on the list. Stuck: a sun in - // the corner, centered on the header's bottom left so the header - // and the screen's edge hide three quarters of it, big enough to - // reach past the list's far corner, but never wider than the column - // (it'd show a cut edge at the content). When a narrow window holds - // it smaller than that, it moves to take the whole list in anyway: - // first its center down the screen's edge, then (if that isn't - // enough) off the screen to the left, growing by as much, so its - // right edge stays put while its arc flattens over the list. + // the list). At rest: `rest` above. Stuck: a sun in the corner, + // centered on the header's bottom left so the header and the + // screen's edge hide three quarters of it, reaching past the list's + // far corner. Never wider than the column (it'd show a cut edge at + // the content): held narrower by the window, it stays centered on + // the header's edge (so below it, it only ever curves inward) and + // becomes an ellipse, as tall as it takes to take the list in. const circleBox = (() => { - if (!listBox || !circle) return null; + if (!listBox || !rest) return null; if (stuck) { - const edge = columnWidth - 8; // the furthest right it may reach - const roomy = Math.hypot(LIST_LEFT + listBox.width + 24, STUCK_TOP + listBox.height + 24); - if (roomy <= edge) return { size: roomy * 2, left: -roomy, top: listTop - STUCK_TOP - roomy }; - - // From the header's bottom left, with a slim margin: how far - // right the list reaches, and its top and bottom. - const right = LIST_LEFT + listBox.width + 8; - const top = STUCK_TOP - 4; - const bottom = STUCK_TOP + listBox.height + 8; - const reach = Math.sqrt(Math.max(edge * edge - right * right, 0)); - const drop = Math.min(Math.max(0, bottom - reach), (top + bottom) / 2); - // Off screen by `back`, if the list's corners are still out; at - // most MAX_BACK, since a list as wide as its column can't be - // taken in without crossing into the content (it nearly can't - // happen: titles wrap first). - const half = (bottom - top) / 2; - const gap = Math.max(edge - right, 1); - const back = - Math.hypot(right, Math.max(bottom - drop, drop - top)) <= edge - ? 0 - : Math.min(MAX_BACK, Math.max(0, (half * half - gap * gap) / (2 * gap) - right)); - const radius = edge + back; - return { size: radius * 2, left: -back - radius, top: listTop - STUCK_TOP + drop - radius }; + const across = LIST_LEFT + listBox.width + CIRCLE_MARGIN; + const down = STUCK_TOP + listBox.height + CIRCLE_MARGIN; + const width = 2 * Math.min(Math.hypot(across, down), columnWidth - 8); + const height = coverHeight(width, across, down); + return { width, height, left: -width / 2, top: listTop - STUCK_TOP - height / 2 }; } - return { - size: circle, - left: Math.min(LIST_LEFT + Math.max(listBox.width / 2 - CIRCLE_SHIFT, listBox.heading) - circle / 2, columnWidth - circle), - top: listTop + listBox.height / 2 - circle / 2, - }; + return { width: rest.width, height: rest.height, left: rest.left, top: listTop + listBox.height / 2 - rest.height / 2 }; })(); // The tab's title as the visitor moves around. The first load's @@ -279,7 +297,7 @@ export default function PageShell({ title, intro, sections, sideNav = false }: P it would stop sticking. */}