Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

mobile-guide-header.tsx

73 lines2.8 KB

1import { Check } from "lucide-react";
2import type { RefObject } from "react";
3​
4import { ThemeMenu } from "@/components/theme/theme-menu";
5import { GUIDE_SECTIONS } from "@/guide/content";
6import { useAnchorInView } from "@/hooks/use-anchor-in-view";
7import { useMediaQuery } from "@/hooks/use-media-query";
8import { cn } from "@/lib/utils";
9​
10import { DevGuideMenu } from "./dev-guide-menu";
11import { useActiveSection, useIsSectionDone } from "./guide-store";
12import { TanStackDbMark } from "./tanstack-db-mark";
13​
14/**
15 * Compact header for the full-screen guide: the logo, a chip naming the
16 * current section, and one theme button. The chip shows the active section
17 * (the one matching what you did in the app), otherwise the one you're
18 * reading; tapping it scrolls there.
19 */
20export function MobileGuideHeader({ scrollRef }: { scrollRef: RefObject<HTMLElement | null> }) {
21 const activeSectionId = useActiveSection();
22 const sectionInView = useAnchorInView(scrollRef);
23 const chipSectionId = activeSectionId ?? sectionInView;
24​
25 return (
26 <header className="box-content flex h-14 shrink-0 items-center gap-2.5 border-b bg-background px-4 pt-[env(safe-area-inset-top)]">
27 <TanStackDbMark />
28 {chipSectionId ? (
29 <SectionChip sectionId={chipSectionId} active={chipSectionId === activeSectionId} />
30 ) : (
31 <span className="text-sm font-semibold tracking-tight whitespace-nowrap">
32 Try TanStack DB
33 </span>
34 )}
35 <div className="ml-auto flex shrink-0 items-center gap-2">
36 {import.meta.env.DEV && <DevGuideMenu />}
37 <ThemeMenu />
38 </div>
39 </header>
40 );
42​
43function SectionChip({ sectionId, active }: { sectionId: string; active: boolean }) {
44 const section = GUIDE_SECTIONS.find((section) => section.id === sectionId);
45 const done = useIsSectionDone(sectionId);
46 const reduceMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
47 if (!section) return null;
48​
49 return (
50 <button
51 type="button"
52 onClick={() =>
53 document
54 .getElementById(sectionId)
55 ?.scrollIntoView({ behavior: reduceMotion ? "auto" : "smooth", block: "start" })
56 }
57 aria-label={`Current section: ${section.title}. Scroll to it.`}
58 className={cn(
59 "inline-flex h-8 shrink-0 items-center gap-1.5 rounded-full border px-3 text-[13px] font-medium whitespace-nowrap transition-colors",
60 active
61 ? "border-guide-accent/40 bg-guide-accent-soft text-foreground"
62 : "bg-muted/50 text-foreground",
63 )}
64 >
65 {done ? (
66 <Check className="size-3.5 text-emerald-600 dark:text-emerald-400" strokeWidth={3} />
67 ) : active ? (
68 <span className="size-1.5 animate-pulse rounded-full bg-guide-accent" />
69 ) : null}
70 {section.shortTitle}
71 </button>
72 );