site-nav.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. <button
  76. aria-label={`${shortcut} command palette`}
  77. className="hidden h-10 items-center gap-1 rounded-full px-3 text-[13px] font-medium text-muted transition-colors hover:text-ink md:flex"
  78. data-cursor="Command palette"
  79. type="button"
  80. onClick={() => setPaletteOpen(true)}
  81. >
  82. {shortcut}
  83. </button>
  84. <ThemeButton />
  85. </LiquidGlass>
  86. </header>
  87. {paletteOpen && (
  88. <CommandPalette
  89. onClose={() => setPaletteOpen(false)}
  90. onSelectTab={select}
  91. />
  92. )}
  93. </>
  94. );
  95. }
  96. function Tabs({
  97. activeIndex,
  98. onSelect,
  99. }: {
  100. activeIndex: number;
  101. onSelect: (id: Tab["id"]) => void;
  102. }) {
  103. const listRef = useRef<HTMLDivElement>(null);
  104. const prevIndex = useRef(activeIndex);
  105. const drag = useRef<{
  106. startX: number;
  107. lastX: number;
  108. lastT: number;
  109. v: number;
  110. moved: boolean;
  111. } | null>(null);
  112. // Nested backdrop filters are unreliable in Chromium, so the pill sits behind
  113. // the labels as lit glass; only the bar itself refracts the page.
  114. const dropletRef = useRef<HTMLSpanElement>(null);
  115. useGlassLight(dropletRef);
  116. // Liquid hop between tabs: stretch along the path, squash, then settle with a spring.
  117. useEffect(() => {
  118. const el = dropletRef.current;
  119. const from = prevIndex.current;
  120. prevIndex.current = activeIndex;
  121. if (!el || activeIndex < 0) return;
  122. el.style.transform = `translateX(${activeIndex * 100}%)`;
  123. el.style.opacity = "1";
  124. if (from < 0 || from === activeIndex) return;
  125. const dist = Math.abs(activeIndex - from);
  126. const stretch = 1 + Math.min(0.55, dist * 0.22);
  127. const s = springs.wobble();
  128. el.animate(
  129. [
  130. { transform: `translateX(${from * 100}%) scale(1, 1)` },
  131. {
  132. transform: `translateX(${((from + activeIndex) / 2) * 100}%) scale(${stretch}, ${2 - stretch})`,
  133. offset: 0.45,
  134. },
  135. { transform: `translateX(${activeIndex * 100}%) scale(1, 1)` },
  136. ],
  137. { duration: Math.max(420, s.duration), easing: s.easing },
  138. );
  139. }, [activeIndex]);
  140. const indexAt = (clientX: number) => {
  141. const list = listRef.current;
  142. if (!list) return activeIndex;
  143. const r = list.getBoundingClientRect();
  144. const w = r.width / TABS.length;
  145. return Math.max(
  146. 0,
  147. Math.min(TABS.length - 1, Math.floor((clientX - r.left) / w)),
  148. );
  149. };
  150. // Press and drag the droplet across the bar, like the iOS 26 tab bar.
  151. const onPointerDown = (e: React.PointerEvent) => {
  152. if (e.button !== 0) return;
  153. drag.current = {
  154. startX: e.clientX,
  155. lastX: e.clientX,
  156. lastT: performance.now(),
  157. v: 0,
  158. moved: false,
  159. };
  160. dropletRef.current?.style.setProperty("--press", "1");
  161. };
  162. const onPointerMove = (e: React.PointerEvent) => {
  163. const d = drag.current;
  164. const el = dropletRef.current;
  165. const list = listRef.current;
  166. if (!d || !el || !list) return;
  167. if (!d.moved && Math.abs(e.clientX - d.startX) < 6) return;
  168. if (!d.moved) {
  169. d.moved = true;
  170. list.setPointerCapture(e.pointerId);
  171. el.getAnimations().forEach((a) => a.cancel());
  172. }
  173. const now = performance.now();
  174. d.v =
  175. 0.8 * d.v + 0.2 * ((e.clientX - d.lastX) / Math.max(1, now - d.lastT));
  176. d.lastX = e.clientX;
  177. d.lastT = now;
  178. const r = list.getBoundingClientRect();
  179. const w = r.width / TABS.length;
  180. const x = Math.max(0, Math.min(r.width - w, e.clientX - r.left - w / 2));
  181. const stretch = 1 + Math.min(0.45, Math.abs(d.v) * 0.18);
  182. el.style.opacity = "1";
  183. el.style.transform = `translateX(${(x / w) * 100}%) scale(${stretch * 1.06}, ${(2 - stretch) * 1.06})`;
  184. };
  185. const onPointerUp = (e: React.PointerEvent) => {
  186. const d = drag.current;
  187. drag.current = null;
  188. dropletRef.current?.style.removeProperty("--press");
  189. if (!d?.moved) return;
  190. const index = indexAt(e.clientX);
  191. const el = dropletRef.current;
  192. if (el) {
  193. const current = el.style.transform;
  194. const s = springs.wobble();
  195. el.style.transform = `translateX(${index * 100}%)`;
  196. el.animate([{ transform: current }, { transform: el.style.transform }], {
  197. duration: s.duration,
  198. easing: s.easing,
  199. });
  200. }
  201. prevIndex.current = index;
  202. onSelect(TABS[index].id);
  203. };
  204. return (
  205. <div
  206. ref={listRef}
  207. aria-label="Sections"
  208. className="relative flex touch-pan-y select-none"
  209. role="tablist"
  210. onPointerCancel={onPointerUp}
  211. onPointerDown={onPointerDown}
  212. onPointerMove={onPointerMove}
  213. onPointerUp={onPointerUp}
  214. >
  215. {TABS.map((tab, i) => (
  216. <button
  217. key={tab.id}
  218. aria-selected={i === activeIndex}
  219. className={cn(
  220. "relative z-10 h-10 w-[58px] rounded-full text-[13px] font-medium transition-[color,scale] duration-300 sm:w-[76px] sm:text-[14px]",
  221. i === activeIndex
  222. ? "scale-[1.06] text-ink"
  223. : "text-muted hover:text-ink",
  224. )}
  225. role="tab"
  226. type="button"
  227. onClick={() => {
  228. if (drag.current?.moved) return;
  229. onSelect(tab.id);
  230. }}
  231. >
  232. {tab.label}
  233. </button>
  234. ))}
  235. <span
  236. ref={dropletRef}
  237. aria-hidden
  238. className="lg pointer-events-none absolute inset-y-0 left-0 z-0 w-[58px] rounded-full bg-white/75 shadow-[0_6px_16px_-8px_rgb(40_20_30/0.35)] transition-[scale] duration-300 [--lg-blur:0px] [backdrop-filter:none] [scale:calc(1+var(--press,0)*0.12)] sm:w-[76px] dark:bg-white/12"
  239. style={{
  240. transform: `translateX(${Math.max(0, activeIndex) * 100}%)`,
  241. opacity: activeIndex < 0 ? 0 : 1,
  242. }}
  243. >
  244. <span className="lg-caustic" />
  245. </span>
  246. </div>
  247. );
  248. }
  249. function ThemeButton() {
  250. const { resolvedTheme, setTheme } = useTheme();
  251. const ref = useRef<HTMLButtonElement>(null);
  252. useGlassLight(ref);
  253. return (
  254. <button
  255. ref={ref}
  256. aria-label="Toggle dark mode"
  257. className="lg grid size-10 place-items-center rounded-full"
  258. data-cursor="Lights"
  259. type="button"
  260. onClick={(e) => {
  261. const r = e.currentTarget.getBoundingClientRect();
  262. switchTheme(resolvedTheme === "dark" ? "light" : "dark", setTheme, {
  263. x: r.left + r.width / 2,
  264. y: r.top + r.height / 2,
  265. });
  266. }}
  267. >
  268. <span className="lg-caustic" />
  269. <svg aria-hidden className="size-[18px]" fill="none" viewBox="0 0 24 24">
  270. <mask id="theme-moon-mask">
  271. <rect fill="white" height="24" width="24" />
  272. <circle
  273. className="origin-center translate-x-[9px] -translate-y-[9px] transition-transform duration-500 ease-out dark:translate-x-0 dark:translate-y-0"
  274. cx="17"
  275. cy="7"
  276. fill="black"
  277. r="6"
  278. />
  279. </mask>
  280. <circle
  281. className="origin-center scale-[0.55] transition-transform duration-500 ease-out dark:scale-100"
  282. cx="12"
  283. cy="12"
  284. fill="currentColor"
  285. mask="url(#theme-moon-mask)"
  286. r="8"
  287. />
  288. <g
  289. className="origin-center stroke-current transition-[opacity,rotate] duration-500 dark:rotate-45 dark:opacity-0"
  290. strokeLinecap="round"
  291. strokeWidth="2"
  292. >
  293. <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" />
  294. </g>
  295. </svg>
  296. </button>
  297. );
  298. }