"use client"; import dynamic from "next/dynamic"; import { usePathname, useRouter } from "next/navigation"; import { useTheme } from "next-themes"; import { useCallback, useEffect, useRef, useState } from "react"; import { LiquidGlass, useGlassLight } from "@/components/glass/liquid-glass"; import { useShortcut } from "@/components/nav/shortcut"; import { springs } from "@/lib/motion"; import { switchTheme } from "@/lib/theme-transition"; import { cn } from "@/lib/utils"; import { setView, useView, type View } from "@/lib/view-store"; const CommandPalette = dynamic(() => import("./command-palette"), { ssr: false, }); type Tab = { id: View | "blog"; label: string }; const TABS: Tab[] = [ { id: "all", label: "All" }, { id: "about", label: "About" }, { id: "work", label: "Work" }, { id: "blog", label: "Blog" }, ]; export function SiteNav() { const pathname = usePathname(); const router = useRouter(); const view = useView(); const onHome = pathname === "/"; const activeId = pathname.startsWith("/blog") ? "blog" : onHome ? view : null; const activeIndex = TABS.findIndex((t) => t.id === activeId); const [paletteOpen, setPaletteOpen] = useState(false); const shortcut = useShortcut(); const select = useCallback( (id: Tab["id"]) => { if (id === "blog") { if (!pathname.startsWith("/blog") || pathname !== "/blog") router.push("/blog"); return; } if (onHome) { setView(id); window.scrollTo({ top: 0, behavior: "smooth" }); } else { setView(id, { updateUrl: false }); router.push(id === "all" ? "/" : `/#${id}`); } }, [onHome, pathname, router], ); useEffect(() => { const onKey = (e: KeyboardEvent) => { const typing = e.target instanceof HTMLElement && (e.target.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(e.target.tagName)); if ((e.key === "k" || e.key === "K") && (e.metaKey || e.ctrlKey)) { e.preventDefault(); setPaletteOpen((open) => !open); } else if (e.key === "/" && !typing) { e.preventDefault(); setPaletteOpen(true); } }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, []); return ( <>
setPaletteOpen(true)} />
{paletteOpen && ( setPaletteOpen(false)} onSelectTab={select} /> )} ); } function Tabs({ activeIndex, onSelect, }: { activeIndex: number; onSelect: (id: Tab["id"]) => void; }) { const listRef = useRef(null); const prevIndex = useRef(activeIndex); const drag = useRef<{ startX: number; lastX: number; lastT: number; v: number; moved: boolean; } | null>(null); // Nested backdrop filters are unreliable in Chromium, so the pill sits behind // the labels as lit glass; only the bar itself refracts the page. const dropletRef = useRef(null); useGlassLight(dropletRef); // Liquid hop between tabs: stretch along the path, squash, then settle with a spring. useEffect(() => { const el = dropletRef.current; const from = prevIndex.current; prevIndex.current = activeIndex; if (!el || activeIndex < 0) return; el.style.transform = `translateX(${activeIndex * 100}%)`; el.style.opacity = "1"; if (from < 0 || from === activeIndex) return; const dist = Math.abs(activeIndex - from); const stretch = 1 + Math.min(0.55, dist * 0.22); const s = springs.wobble(); el.animate( [ { transform: `translateX(${from * 100}%) scale(1, 1)` }, { transform: `translateX(${((from + activeIndex) / 2) * 100}%) scale(${stretch}, ${2 - stretch})`, offset: 0.45, }, { transform: `translateX(${activeIndex * 100}%) scale(1, 1)` }, ], { duration: Math.max(420, s.duration), easing: s.easing }, ); }, [activeIndex]); const indexAt = (clientX: number) => { const list = listRef.current; if (!list) return activeIndex; const r = list.getBoundingClientRect(); const w = r.width / TABS.length; return Math.max( 0, Math.min(TABS.length - 1, Math.floor((clientX - r.left) / w)), ); }; // Press and drag the droplet across the bar, like the iOS 26 tab bar. const onPointerDown = (e: React.PointerEvent) => { if (e.button !== 0) return; drag.current = { startX: e.clientX, lastX: e.clientX, lastT: performance.now(), v: 0, moved: false, }; dropletRef.current?.style.setProperty("--press", "1"); }; const onPointerMove = (e: React.PointerEvent) => { const d = drag.current; const el = dropletRef.current; const list = listRef.current; if (!d || !el || !list) return; if (!d.moved && Math.abs(e.clientX - d.startX) < 6) return; if (!d.moved) { d.moved = true; list.setPointerCapture(e.pointerId); el.getAnimations().forEach((a) => a.cancel()); } const now = performance.now(); d.v = 0.8 * d.v + 0.2 * ((e.clientX - d.lastX) / Math.max(1, now - d.lastT)); d.lastX = e.clientX; d.lastT = now; const r = list.getBoundingClientRect(); const w = r.width / TABS.length; const x = Math.max(0, Math.min(r.width - w, e.clientX - r.left - w / 2)); const stretch = 1 + Math.min(0.45, Math.abs(d.v) * 0.18); el.style.opacity = "1"; el.style.transform = `translateX(${(x / w) * 100}%) scale(${stretch * 1.06}, ${(2 - stretch) * 1.06})`; }; const onPointerUp = (e: React.PointerEvent) => { const d = drag.current; drag.current = null; dropletRef.current?.style.removeProperty("--press"); if (!d?.moved) return; const index = indexAt(e.clientX); const el = dropletRef.current; if (el) { const current = el.style.transform; const s = springs.wobble(); el.style.transform = `translateX(${index * 100}%)`; el.animate([{ transform: current }, { transform: el.style.transform }], { duration: s.duration, easing: s.easing, }); } prevIndex.current = index; onSelect(TABS[index].id); }; return (
{TABS.map((tab, i) => ( ))}
); } /** Opens the command palette; its shortcut lives in the hover label and the footer. */ function SearchButton({ shortcut, onOpen, }: { shortcut: string; onOpen: () => void; }) { const ref = useRef(null); useGlassLight(ref); return ( ); } function ThemeButton() { const { resolvedTheme, setTheme } = useTheme(); const ref = useRef(null); useGlassLight(ref); return ( ); }