"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { useHydrated } from "@/components/ui/use-hydrated"; import type { Crumb } from "./AppHeader"; import { PAGE_SHEET } from "./PageActions"; import { openSheet, useSheetOpen } from "./Sheet"; import { Mark } from "./Wordmark"; // --------------------------------------------------------------------------- // THE PLACE BAR -- the top of a phone page: where am I, and nothing else to // press but the way back and the page's ⋯. 44 px: // // ‹ articles / huck ⋯ // // back (the trail's last link, or the browser's back when you came from inside // umtool), the trail in mono, and ⋯ for the page's actions (PageActions.tsx). // A place with no trail shows the mark, home. Once the page's title has // scrolled away the bar says the title instead of the trail. // // It hides on a scroll down and comes back on a scroll up -- a transform, so // nothing reflows -- and says so on (`data-place-hidden`), which moves // a sticky tab strip up into its room. It never hides while a sheet is open. // // The desk never draws it (md:hidden): the head is the desk's place bar. // --------------------------------------------------------------------------- /** How far a scroll must go, in one direction, before the bar moves. */ const SLACK = 8; export default function PlaceBar({ crumbs, actions }: { crumbs: Crumb[]; actions: boolean }) { const router = useRouter(); const hydrated = useHydrated(); const trail = crumbs.slice(0, -1); const here = crumbs[crumbs.length - 1]; const back = [...trail].reverse().find((c) => c.href)?.href ?? null; const pageOpen = useSheetOpen(PAGE_SHEET); const [titled, setTitled] = useState(false); // Hide on the way down, return on the way up. useEffect(() => { const root = document.documentElement; let last = window.scrollY; let frame = 0; const read = () => { frame = 0; const y = window.scrollY; if (root.hasAttribute("data-sheet-open")) { last = y; return; } if (y <= 44) root.removeAttribute("data-place-hidden"); else if (y > last + SLACK) root.setAttribute("data-place-hidden", ""); else if (y < last - SLACK) root.removeAttribute("data-place-hidden"); else return; last = y; }; const on = () => { if (!frame) frame = requestAnimationFrame(read); }; window.addEventListener("scroll", on, { passive: true }); return () => { window.removeEventListener("scroll", on); if (frame) cancelAnimationFrame(frame); root.removeAttribute("data-place-hidden"); }; }, []); // The title in the bar once the page's own has scrolled away. useEffect(() => { const h1 = document.querySelector("[data-app-header=page] h1"); if (!h1 || typeof IntersectionObserver === "undefined") return; const io = new IntersectionObserver(([e]) => setTitled(!e.isIntersecting), { rootMargin: "-44px 0px 0px 0px" }); io.observe(h1); return () => io.disconnect(); }, []); // A tab strip that scrolls sideways shows the tab you are on. useEffect(() => { for (const strip of document.querySelectorAll(".tab-strip")) { const cur = strip.querySelector("[aria-current=page]"); // Only when it would be cut off, and with the tab before it still in view. if (cur && cur.offsetLeft + cur.offsetWidth > strip.clientWidth - 24) strip.scrollLeft = Math.max(0, cur.offsetLeft - 64); } }, []); return (
{back ? ( { // Came from inside umtool: back is where you were, not the trail's parent. if (e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0) return; if (window.history.length > 1 && document.referrer.startsWith(window.location.origin)) { e.preventDefault(); router.back(); } }} > ‹ ) : ( )} {titled || trail.length === 0 ? ( {titled ? here?.label : ""} ) : ( trail.map((c, i) => ( {i > 0 && / } {c.href ? ( {c.label} ) : ( c.label )} )) )} {actions && ( )}
); }