"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 { useLocale, useT } from "@/components/locale-provider"; import { useShortcut } from "@/components/nav/shortcut"; import { htmlLang, localePath, stripLocale, type Locale } from "@/lib/i18n"; 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 TabId = View | "blog"; // In order; the labels live in messages/ (nav.tabs). const TABS: TabId[] = ["all", "about", "work", "blog"]; export function SiteNav() { const lang = useLocale(); const t = useT(); // The path without its language, so both languages share the logic below. const pathname = stripLocale(usePathname()); const router = useRouter(); const view = useView(); const onHome = pathname === "/"; const activeId = pathname.startsWith("/blog") ? "blog" : onHome ? view : null; const activeIndex = TABS.findIndex((id) => id === activeId); const [paletteOpen, setPaletteOpen] = useState(false); const shortcut = useShortcut(); const select = useCallback( (id: TabId) => { if (id === "blog") { if (pathname !== "/blog") router.push(localePath(lang, "/blog")); return; } if (onHome) { setView(id); window.scrollTo({ top: 0, behavior: "smooth" }); } else { setView(id, { updateUrl: false }); const home = localePath(lang, "/"); router.push(id === "all" ? home : `${home}#${id}`); } }, [lang, 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 ( <>
t.nav.tabs[id])} sections={t.nav.sections} onSelect={select} /> setPaletteOpen(true)} />
{paletteOpen && ( setPaletteOpen(false)} onSelectTab={select} /> )} ); } function Tabs({ activeIndex, labels, sections, onSelect, }: { activeIndex: number; labels: string[]; sections: string; onSelect: (id: TabId) => 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]); }; return (
{TABS.map((tab, i) => ( ))}
); } /** Opens the command palette; its shortcut lives in the hover label and the footer. */ function SearchButton({ label, shortcut, onOpen, }: { label: string; shortcut: string; onOpen: () => void; }) { const ref = useRef(null); useGlassLight(ref); return ( ); } /** Opens this page in the other language; its label is written in that language. */ function LanguageButton({ lang, path }: { lang: Locale; path: string }) { const { nav } = useT(); const ref = useRef(null); const other: Locale = lang === "en" ? "zh" : "en"; useGlassLight(ref); return ( { // Keep the home grid's view (its hash) across the switch. e.currentTarget.href = localePath(other, path) + window.location.hash; }} > {nav.switchShort} ); } function ThemeButton() { const { nav } = useT(); const { resolvedTheme, setTheme } = useTheme(); const ref = useRef(null); useGlassLight(ref); return ( ); }