Hamburger menu in nav

merged…
osebo56d ago

Can we put the nav options in a menu instead of having them scroll horizontal on mobile. Put the hamburger menu to the left of the “threads ide” title

2 repliesoldestnewest
Osebotfable-5-156d ago

Current state on main: app/layout.tsx renders <nav> inline with the three links and header.top nav is overflow-x: auto, so on phones the links scroll horizontally (and "Workspaces" gets clipped).

Plan:

  1. New client component components/site-nav.tsx holding the LINKS array (Threads / New thread / Workspaces) and a useState open flag.
  2. On ≤560px: render a ☰ button left of the "Thread IDE" title (first child of header.top). Tapping opens a full-width dropdown panel under the header; closes on link click, Escape, outside click, or route change. aria-expanded/aria-controls for a11y.
  3. On wider screens: same inline links as today, hamburger hidden.
  4. Drop the overflow-x: auto scrolling nav CSS.
  5. Static HTML mockup + Playwright screenshots at 390px (closed + open) and 900px, since the app needs Postgres/OAuth to run.
Osebotfable-5-156d agothreadide@210d36b

Done — pushed to thread/869cf61e (threadide 210d36b). PR link: https://github.com/osebo/threadide/pull/new/thread/869cf61e

What changed

  • New components/site-nav.tsx (client component). Holds the LINKS array (Threads / New thread / Workspaces) and the open/closed state. Teammates: add future header links there, not in layout.tsx.
  • app/layout.tsx: the inline <nav> is gone; <SiteNav /> is rendered as the first child of header.top, i.e. left of the "Thread IDE" title as requested.
  • app/globals.css: removed the overflow-x: auto scrolling nav. Two modes, chosen purely by CSS so server and client render identical DOM (no hydration mismatch):
    • ≤560px — 36×36 ☰ button left of the title. Tapping it opens a full-width panel under the header (position:absolute; top:100% inside the now position:relative header) with 12px-padded rows (good thumb targets); current page shown bold in accent color. Icon flips to ✕ while open.
    • >560px — button hidden, .site-nav is display:contents, and the links are placed after the title via order:1, so the desktop header is visually unchanged.
  • Menu closes on link tap (via usePathname change), Escape, or a pointerdown outside the button/panel. aria-expanded / aria-controls / aria-current wired up.

Verification

  • tsc --noEmit passes.
  • The app can't run in the sandbox (needs Postgres + GitHub OAuth), so the attached screenshots are from a static mockup linking the real globals.css with the same markup. Measured: on 390px the button is at x=6 (w=36) and the title at x=52; open panel spans 0–390. On 900px the button is 0×0 and links sit inline at x=119.
  • Not tested on a real iOS device — worth a quick tap-through on your phone after merging, particularly that the outside-tap-to-close feels right.
390px phone, menu open: ☰→✕ left of the title, full-width panel under the header (current page highlighted)
390px phone, menu open: ☰→✕ left of the title, full-width panel under the header (current page highlighted)
390px phone, menu closed: hamburger sits left of "Thread IDE"
390px phone, menu closed: hamburger sits left of "Thread IDE"
900px desktop: unchanged inline links, no hamburger
900px desktop: unchanged inline links, no hamburger
Diff · 3 files
diff --git a/threadide/app/globals.css b/threadide/app/globals.css
--- a/threadide/app/globals.css
+++ b/threadide/app/globals.css
@@ -31,14 +31,37 @@ body {
 a { color: inherit; }
 main { max-width: 860px; margin: 0 auto; padding: 24px 16px 80px; }
 header.top {
-  display: flex; align-items: baseline; gap: 16px; padding: 16px;
+  position: relative; /* anchor for the phone nav panel */
+  display: flex; align-items: center; gap: 16px; padding: 16px;
   border-bottom: 1px solid var(--border); background: var(--surface);
 }
-header.top h1 { font-size: 16px; margin: 0; white-space: nowrap; }
-header.top nav { display: flex; gap: 12px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
-header.top nav a { color: var(--muted); text-decoration: none; white-space: nowrap; }
+header.top h1 { font-size: 16px; margin: 0; white-space: nowrap; line-height: 1.3; }
+/* Desktop: SiteNav is just its inline links. DOM order is [SiteNav, h1, user]; `order` puts the links after the title. */
+.site-nav { display: contents; }
+.nav-toggle { display: none; }
+.nav-links { display: flex; gap: 12px; order: 1; min-width: 0; }
+header.top > span:last-child { order: 2; }
+.nav-links a { color: var(--muted); text-decoration: none; white-space: nowrap; }
+.nav-links a[aria-current="page"] { color: var(--text); }
 @media (max-width: 560px) {
-  header.top { gap: 12px; padding: 12px; }
+  header.top { gap: 10px; padding: 10px 12px; }
+  /* Phone: ☰ button sits left of the title; the links become a panel pinned under the header. */
+  .site-nav { display: block; }
+  .nav-toggle {
+    display: inline-flex; align-items: center; justify-content: center;
+    width: 36px; height: 36px; padding: 0; margin-left: -6px;
+    background: none; color: var(--text); font-size: 20px; line-height: 1; border-radius: 8px;
+  }
+  .nav-toggle:hover, .nav-toggle:focus-visible { background: rgba(128,128,128,.18); }
+  .nav-links {
+    display: none; position: absolute; left: 0; right: 0; top: 100%; z-index: 20;
+    flex-direction: column; gap: 0; padding: 6px 0;
+    background: var(--surface); border-bottom: 1px solid var(--border); box-shadow: 0 8px 20px rgba(0,0,0,.08);
+  }
+  .site-nav[data-open] .nav-links { display: flex; }
+  .nav-links a { padding: 12px 16px; font-size: 15px; color: var(--text); }
+  .nav-links a:hover { background: rgba(128,128,128,.12); }
+  .nav-links a[aria-current="page"] { font-weight: 600; color: var(--accent); }
   .user-login { display: none; } /* avatar + sign out is enough on a phone */
   .user-menu { gap: 6px; }
   .user-menu button.link { white-space: nowrap; }
diff --git a/threadide/app/layout.tsx b/threadide/app/layout.tsx
--- a/threadide/app/layout.tsx
+++ b/threadide/app/layout.tsx
@@ -3,6 +3,7 @@ import Link from "next/link";
 import "./globals.css";
 import { currentUser } from "@/lib/session";
 import { UserMenu } from "@/components/user-menu";
+import { SiteNav } from "@/components/site-nav";
 
 export const metadata: Metadata = {
   title: "Thread IDE",
@@ -15,16 +16,15 @@ export default async function RootLayout({ children }: { children: React.ReactNo
     <html lang="en">
       <body>
         <header className="top">
+          {/* On phones SiteNav renders a ☰ button here, left of the title; the links themselves
+              are absolutely positioned under the header. On desktop the button is hidden and the
+              links display inline, so visually the order is title → links either way. */}
+          <SiteNav />
           <h1>
             <Link href="/" style={{ textDecoration: "none" }}>
               Thread IDE
             </Link>
           </h1>
-          <nav>
-            <Link href="/">Threads</Link>
-            <Link href="/threads/new">New thread</Link>
-            <Link href="/workspaces">Workspaces</Link>
-          </nav>
           <span style={{ marginLeft: "auto" }}>
             <UserMenu user={user} />
           </span>
diff --git a/threadide/components/site-nav.tsx b/threadide/components/site-nav.tsx
new file mode 100644
--- /dev/null
+++ b/threadide/components/site-nav.tsx
@@ -0,0 +1,65 @@
+"use client";
+
+import Link from "next/link";
+import { usePathname } from "next/navigation";
+import { useEffect, useId, useRef, useState } from "react";
+
+/** Add new header links here; both the inline nav (desktop) and the ☰ panel (phone) read from it. */
+const LINKS = [
+  { href: "/", label: "Threads" },
+  { href: "/threads/new", label: "New thread" },
+  { href: "/workspaces", label: "Workspaces" },
+];
+
+/**
+ * Header navigation. ≥561px: plain inline links. ≤560px: the links collapse behind a ☰ button
+ * that sits left of the "Thread IDE" title and opens a full-width panel under the header.
+ * Which mode is shown is decided purely in CSS (see `.nav-toggle` / `.nav-links` in globals.css),
+ * so there's no hydration mismatch between server and client.
+ */
+export function SiteNav() {
+  const [open, setOpen] = useState(false);
+  const pathname = usePathname();
+  const panelId = useId();
+  const rootRef = useRef<HTMLDivElement>(null);
+
+  // Close whenever navigation happens (tapping a link, back button, ...).
+  useEffect(() => setOpen(false), [pathname]);
+
+  // Close on Escape or a tap anywhere outside the button/panel.
+  useEffect(() => {
+    if (!open) return;
+    const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setOpen(false); };
+    const onDown = (e: PointerEvent) => {
+      if (rootRef.current && !rootRef.current.contains(e.target as Node)) setOpen(false);
+    };
+    document.addEventListener("keydown", onKey);
+    document.addEventListener("pointerdown", onDown);
+    return () => {
+      document.removeEventListener("keydown", onKey);
+      document.removeEventListener("pointerdown", onDown);
+    };
+  }, [open]);
+
+  return (
+    <div ref={rootRef} className="site-nav" data-open={open || undefined}>
+      <button
+        type="button"
+        className="nav-toggle"
+        aria-label={open ? "Close menu" : "Open menu"}
+        aria-expanded={open}
+        aria-controls={panelId}
+        onClick={() => setOpen((o) => !o)}
+      >
+        <span aria-hidden="true">{open ? "✕" : "☰"}</span>
+      </button>
+      <nav id={panelId} className="nav-links">
+        {LINKS.map((l) => (
+          <Link key={l.href} href={l.href} aria-current={pathname === l.href ? "page" : undefined}>
+            {l.label}
+          </Link>
+        ))}
+      </nav>
+    </div>
+  );
+}