site-nav.tsx 11 KB

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