site-nav.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. "use client";
  2. import dynamic from "next/dynamic";
  3. import { usePathname, useRouter } from "next/navigation";
  4. import { useTheme } from "next-themes";
  5. import { useCallback, useEffect, useRef, useState } from "react";
  6. import { LiquidGlass, useGlassLight } from "@/components/glass/liquid-glass";
  7. import { useShortcut } from "@/components/nav/shortcut";
  8. import { springs } from "@/lib/motion";
  9. import { switchTheme } from "@/lib/theme-transition";
  10. import { cn } from "@/lib/utils";
  11. import { setView, useView, type View } from "@/lib/view-store";
  12. const CommandPalette = dynamic(() => import("./command-palette"), {
  13. ssr: false,
  14. });
  15. type Tab = { id: View | "blog"; label: string };
  16. const TABS: Tab[] = [
  17. { id: "all", label: "All" },
  18. { id: "about", label: "About" },
  19. { id: "work", label: "Work" },
  20. { id: "projects", label: "Projects" },
  21. { id: "blog", label: "Blog" },
  22. ];
  23. export function SiteNav() {
  24. const pathname = usePathname();
  25. const router = useRouter();
  26. const view = useView();
  27. const onHome = pathname === "/";
  28. const activeId = pathname.startsWith("/blog") ? "blog" : onHome ? view : null;
  29. const activeIndex = TABS.findIndex((t) => t.id === activeId);
  30. const [paletteOpen, setPaletteOpen] = useState(false);
  31. const shortcut = useShortcut();
  32. const select = useCallback(
  33. (id: Tab["id"]) => {
  34. if (id === "blog") {
  35. if (!pathname.startsWith("/blog") || pathname !== "/blog")
  36. router.push("/blog");
  37. return;
  38. }
  39. if (onHome) {
  40. setView(id);
  41. window.scrollTo({ top: 0, behavior: "smooth" });
  42. } else {
  43. setView(id, { updateUrl: false });
  44. router.push(id === "all" ? "/" : `/#${id}`);
  45. }
  46. },
  47. [onHome, pathname, router],
  48. );
  49. useEffect(() => {
  50. const onKey = (e: KeyboardEvent) => {
  51. const typing =
  52. e.target instanceof HTMLElement &&
  53. (e.target.isContentEditable ||
  54. ["INPUT", "TEXTAREA", "SELECT"].includes(e.target.tagName));
  55. if ((e.key === "k" || e.key === "K") && (e.metaKey || e.ctrlKey)) {
  56. e.preventDefault();
  57. setPaletteOpen((open) => !open);
  58. } else if (e.key === "/" && !typing) {
  59. e.preventDefault();
  60. setPaletteOpen(true);
  61. }
  62. };
  63. window.addEventListener("keydown", onKey);
  64. return () => window.removeEventListener("keydown", onKey);
  65. }, []);
  66. return (
  67. <>
  68. <header className="pointer-events-none fixed inset-x-0 bottom-[max(14px,env(safe-area-inset-bottom))] z-50 flex justify-center px-3 sm:top-4 sm:bottom-auto">
  69. <LiquidGlass
  70. data-steady-light
  71. className="pointer-events-auto flex items-center gap-1 rounded-full p-1.5 [--glass-tint:rgb(255_255_255/0.5)] dark:[--glass-tint:rgb(16_20_26/0.55)]"
  72. glass={{ chroma: 0.1, blur: 3, saturate: 1.8 }}
  73. >
  74. <Tabs activeIndex={activeIndex} onSelect={select} />
  75. <SearchButton
  76. shortcut={shortcut}
  77. onOpen={() => setPaletteOpen(true)}
  78. />
  79. <ThemeButton />
  80. </LiquidGlass>
  81. </header>
  82. {paletteOpen && (
  83. <CommandPalette
  84. onClose={() => setPaletteOpen(false)}
  85. onSelectTab={select}
  86. />
  87. )}
  88. </>
  89. );
  90. }
  91. function Tabs({
  92. activeIndex,
  93. onSelect,
  94. }: {
  95. activeIndex: number;
  96. onSelect: (id: Tab["id"]) => void;
  97. }) {
  98. const listRef = useRef<HTMLDivElement>(null);
  99. const prevIndex = useRef(activeIndex);
  100. const drag = useRef<{
  101. startX: number;
  102. lastX: number;
  103. lastT: number;
  104. v: number;
  105. moved: boolean;
  106. } | null>(null);
  107. // Nested backdrop filters are unreliable in Chromium, so the pill sits behind
  108. // the labels as lit glass; only the bar itself refracts the page.
  109. const dropletRef = useRef<HTMLSpanElement>(null);
  110. useGlassLight(dropletRef);
  111. // Liquid hop between tabs: stretch along the path, squash, then settle with a spring.
  112. useEffect(() => {
  113. const el = dropletRef.current;
  114. const from = prevIndex.current;
  115. prevIndex.current = activeIndex;
  116. if (!el || activeIndex < 0) return;
  117. el.style.transform = `translateX(${activeIndex * 100}%)`;
  118. el.style.opacity = "1";
  119. if (from < 0 || from === activeIndex) return;
  120. const dist = Math.abs(activeIndex - from);
  121. const stretch = 1 + Math.min(0.55, dist * 0.22);
  122. const s = springs.wobble();
  123. el.animate(
  124. [
  125. { transform: `translateX(${from * 100}%) scale(1, 1)` },
  126. {
  127. transform: `translateX(${((from + activeIndex) / 2) * 100}%) scale(${stretch}, ${2 - stretch})`,
  128. offset: 0.45,
  129. },
  130. { transform: `translateX(${activeIndex * 100}%) scale(1, 1)` },
  131. ],
  132. { duration: Math.max(420, s.duration), easing: s.easing },
  133. );
  134. }, [activeIndex]);
  135. const indexAt = (clientX: number) => {
  136. const list = listRef.current;
  137. if (!list) return activeIndex;
  138. const r = list.getBoundingClientRect();
  139. const w = r.width / TABS.length;
  140. return Math.max(
  141. 0,
  142. Math.min(TABS.length - 1, Math.floor((clientX - r.left) / w)),
  143. );
  144. };
  145. // Press and drag the droplet across the bar, like the iOS 26 tab bar.
  146. const onPointerDown = (e: React.PointerEvent) => {
  147. if (e.button !== 0) return;
  148. drag.current = {
  149. startX: e.clientX,
  150. lastX: e.clientX,
  151. lastT: performance.now(),
  152. v: 0,
  153. moved: false,
  154. };
  155. dropletRef.current?.style.setProperty("--press", "1");
  156. };
  157. const onPointerMove = (e: React.PointerEvent) => {
  158. const d = drag.current;
  159. const el = dropletRef.current;
  160. const list = listRef.current;
  161. if (!d || !el || !list) return;
  162. if (!d.moved && Math.abs(e.clientX - d.startX) < 6) return;
  163. if (!d.moved) {
  164. d.moved = true;
  165. list.setPointerCapture(e.pointerId);
  166. el.getAnimations().forEach((a) => a.cancel());
  167. }
  168. const now = performance.now();
  169. d.v =
  170. 0.8 * d.v + 0.2 * ((e.clientX - d.lastX) / Math.max(1, now - d.lastT));
  171. d.lastX = e.clientX;
  172. d.lastT = now;
  173. const r = list.getBoundingClientRect();
  174. const w = r.width / TABS.length;
  175. const x = Math.max(0, Math.min(r.width - w, e.clientX - r.left - w / 2));
  176. const stretch = 1 + Math.min(0.45, Math.abs(d.v) * 0.18);
  177. el.style.opacity = "1";
  178. el.style.transform = `translateX(${(x / w) * 100}%) scale(${stretch * 1.06}, ${(2 - stretch) * 1.06})`;
  179. };
  180. const onPointerUp = (e: React.PointerEvent) => {
  181. const d = drag.current;
  182. drag.current = null;
  183. dropletRef.current?.style.removeProperty("--press");
  184. if (!d?.moved) return;
  185. const index = indexAt(e.clientX);
  186. const el = dropletRef.current;
  187. if (el) {
  188. const current = el.style.transform;
  189. const s = springs.wobble();
  190. el.style.transform = `translateX(${index * 100}%)`;
  191. el.animate([{ transform: current }, { transform: el.style.transform }], {
  192. duration: s.duration,
  193. easing: s.easing,
  194. });
  195. }
  196. prevIndex.current = index;
  197. onSelect(TABS[index].id);
  198. };
  199. return (
  200. <div
  201. ref={listRef}
  202. aria-label="Sections"
  203. className="relative flex touch-pan-y select-none"
  204. role="tablist"
  205. onPointerCancel={onPointerUp}
  206. onPointerDown={onPointerDown}
  207. onPointerMove={onPointerMove}
  208. onPointerUp={onPointerUp}
  209. >
  210. {TABS.map((tab, i) => (
  211. <button
  212. key={tab.id}
  213. aria-selected={i === activeIndex}
  214. className={cn(
  215. "relative z-10 h-10 w-[58px] rounded-full text-[13px] font-medium transition-[color,scale] duration-300 sm:w-[76px] sm:text-[14px]",
  216. i === activeIndex
  217. ? "scale-[1.06] text-ink"
  218. : "text-muted hover:text-ink",
  219. )}
  220. role="tab"
  221. type="button"
  222. onClick={() => {
  223. if (drag.current?.moved) return;
  224. onSelect(tab.id);
  225. }}
  226. >
  227. {tab.label}
  228. </button>
  229. ))}
  230. <span
  231. ref={dropletRef}
  232. aria-hidden
  233. className="lg pointer-events-none absolute inset-y-0 left-0 z-0 w-[58px] rounded-full shadow-[0_6px_16px_-8px_rgb(40_20_30/0.35)] [--glass-tint:rgb(255_255_255/0.75)] transition-[scale] duration-300 [--lg-blur:0px] [backdrop-filter:none] [scale:calc(1+var(--press,0)*0.12)] sm:w-[76px] dark:[--glass-tint:rgb(255_255_255/0.12)]"
  234. style={{
  235. transform: `translateX(${Math.max(0, activeIndex) * 100}%)`,
  236. opacity: activeIndex < 0 ? 0 : 1,
  237. }}
  238. >
  239. <span className="lg-caustic" />
  240. </span>
  241. </div>
  242. );
  243. }
  244. /** Opens the command palette; its shortcut lives in the hover label and the footer. */
  245. function SearchButton({
  246. shortcut,
  247. onOpen,
  248. }: {
  249. shortcut: string;
  250. onOpen: () => void;
  251. }) {
  252. const ref = useRef<HTMLButtonElement>(null);
  253. useGlassLight(ref);
  254. return (
  255. <button
  256. ref={ref}
  257. aria-keyshortcuts="Meta+K Control+K"
  258. aria-label="Search"
  259. className="lg hidden size-10 place-items-center rounded-full md:grid"
  260. data-cursor={`Search ${shortcut}`}
  261. type="button"
  262. onClick={onOpen}
  263. >
  264. <span className="lg-caustic" />
  265. <svg
  266. aria-hidden
  267. className="size-[17px]"
  268. fill="none"
  269. stroke="currentColor"
  270. strokeLinecap="round"
  271. strokeWidth="2"
  272. viewBox="0 0 24 24"
  273. >
  274. <circle cx="10.5" cy="10.5" r="6.5" />
  275. <path d="m15.5 15.5 4.5 4.5" />
  276. </svg>
  277. </button>
  278. );
  279. }
  280. function ThemeButton() {
  281. const { resolvedTheme, setTheme } = useTheme();
  282. const ref = useRef<HTMLButtonElement>(null);
  283. useGlassLight(ref);
  284. return (
  285. <button
  286. ref={ref}
  287. aria-label="Toggle dark mode"
  288. className="lg grid size-10 place-items-center rounded-full"
  289. data-cursor="Lights"
  290. type="button"
  291. onClick={(e) => {
  292. const r = e.currentTarget.getBoundingClientRect();
  293. switchTheme(resolvedTheme === "dark" ? "light" : "dark", setTheme, {
  294. x: r.left + r.width / 2,
  295. y: r.top + r.height / 2,
  296. });
  297. }}
  298. >
  299. <span className="lg-caustic" />
  300. <svg aria-hidden className="size-[18px]" fill="none" viewBox="0 0 24 24">
  301. <mask id="theme-moon-mask">
  302. <rect fill="white" height="24" width="24" />
  303. <circle
  304. className="origin-center translate-x-[9px] -translate-y-[9px] transition-transform duration-500 ease-out dark:translate-x-0 dark:translate-y-0"
  305. cx="17"
  306. cy="7"
  307. fill="black"
  308. r="6"
  309. />
  310. </mask>
  311. <circle
  312. className="origin-center scale-[0.55] transition-transform duration-500 ease-out dark:scale-100"
  313. cx="12"
  314. cy="12"
  315. fill="currentColor"
  316. mask="url(#theme-moon-mask)"
  317. r="8"
  318. />
  319. <g
  320. className="origin-center stroke-current transition-[opacity,rotate] duration-500 dark:rotate-45 dark:opacity-0"
  321. strokeLinecap="round"
  322. strokeWidth="2"
  323. >
  324. <path d="M12 1.5v2.2M12 20.3v2.2M1.5 12h2.2M20.3 12h2.2M4.6 4.6l1.5 1.5M17.9 17.9l1.5 1.5M4.6 19.4l1.5-1.5M17.9 6.1l1.5-1.5" />
  325. </g>
  326. </svg>
  327. </button>
  328. );
  329. }