site-nav.tsx 10 KB

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