diff --git a/app/components/Index.tsx b/app/components/Index.tsx index 2dbba7d8..4a34dd49 100644 --- a/app/components/Index.tsx +++ b/app/components/Index.tsx @@ -40,7 +40,8 @@ export default function Index({ }: { groupedReferences: Record>; }) { - const pathname = usePathname(); + // trailingSlash: true makes this "/x/", but the hrefs compared below have no slash + const pathname = usePathname().replace(/\/+$/, ''); const activeItemRef = useRef(null); const containerRef = useRef(null); @@ -114,6 +115,7 @@ export default function Index({ ; render that one tag, not raw HTML. +function remarkBrTags() { + return (tree: any) => { + const visit = (node: any) => node.children?.forEach((child: any, i: number) => { + if (child.type === 'html' && /^$/i.test(child.value)) node.children[i] = { type: 'break' }; + else visit(child); + }); + visit(tree); + }; +} + +const remarkPlugins = [remarkGfm, remarkBrTags]; + export default function Markdown({ content, sourcePath }: MarkdownProps) { const processedContent = useMemo(() => { // Split content by H2 headings to group sections @@ -29,7 +42,7 @@ export default function Markdown({ content, sourcePath }: MarkdownProps) { elements.push(
{section} @@ -49,7 +62,7 @@ export default function Markdown({ content, sourcePath }: MarkdownProps) { {title} {sectionContent} diff --git a/app/components/Navbar.tsx b/app/components/Navbar.tsx index adda0c81..bcf2fb56 100644 --- a/app/components/Navbar.tsx +++ b/app/components/Navbar.tsx @@ -3,6 +3,7 @@ import { useEffect, useRef, useState, type ReactNode } from "react"; import Image from "next/image"; import Link from "next/link"; +import { usePathname } from "next/navigation"; // The Jekyll layout renders the same file, so the blog and announcement pages show this nav too import nav from "../../blogs/_data/nav.json"; import { withBasePath } from "../services/sitePath"; @@ -96,14 +97,45 @@ function ExternalIcon() { ); } +// The banner above the sticky nav varies in height, so fit the open menu to the screen space actually left +function fitToViewport(menu: HTMLDivElement | null) { + if (!menu) return; + const fit = () => { + menu.style.maxHeight = `${window.innerHeight - menu.getBoundingClientRect().top}px`; + }; + fit(); + window.addEventListener("resize", fit); + return () => window.removeEventListener("resize", fit); +} + export default function Navbar() { + const pathname = usePathname(); const [isMenuOpen, setIsMenuOpen] = useState(false); + const [menuPathname, setMenuPathname] = useState(pathname); const [openGroup, setOpenGroup] = useState(null); const desktopNav = useRef(null); + const menuButton = useRef(null); const closeMenu = () => setIsMenuOpen(false); const closeGroup = () => setOpenGroup(null); + // Close on any navigation, back/forward included; resetting during render avoids a frame of open menu on the new page + if (menuPathname !== pathname) { + setMenuPathname(pathname); + setIsMenuOpen(false); + } + + useEffect(() => { + if (!isMenuOpen) return; + const closeOnEscape = (event: KeyboardEvent) => { + if (event.key !== "Escape") return; + setIsMenuOpen(false); + menuButton.current?.focus(); + }; + document.addEventListener("keydown", closeOnEscape); + return () => document.removeEventListener("keydown", closeOnEscape); + }, [isMenuOpen]); + useEffect(() => { if (!openGroup) return; // Safari never focuses a clicked button, so blur alone cannot close the panel @@ -127,7 +159,7 @@ export default function Navbar() {