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

guide-section.tsx

75 lines2.8 KB

1import type { LinkOptions } from "@tanstack/react-router";
2import { Check } from "lucide-react";
3import { useEffect, useRef } from "react";
4import type { ReactNode } from "react";
5​
6import { cn } from "@/lib/utils";
7​
8import { useActiveSection, useIsSectionDone } from "./guide-store";
9import { TryItButton } from "./try-it";
10​
11interface GuideSectionProps {
12 id: string;
13 /** Small label above the heading, e.g. "Step 2". */
14 eyebrow?: string;
15 title: string;
16 /** Where the section's hands-on step happens in the app, if it has one. */
17 tryIt?: LinkOptions;
18 children: ReactNode;
20​
21export function GuideSection({ id, eyebrow, title, tryIt, children }: GuideSectionProps) {
22 const isActive = useActiveSection() === id;
23 const isDone = useIsSectionDone(id);
24 const ref = useRef<HTMLElement>(null);
25​
26 useEffect(() => {
27 if (isActive) ref.current?.scrollIntoView({ behavior: "smooth", block: "nearest" });
28 }, [isActive]);
29​
30 return (
31 <section
32 ref={ref}
33 id={id}
34 data-scroll-anchor
35 data-active={isActive || undefined}
36 aria-current={isActive ? "step" : undefined}
37 className={cn(
38 "relative scroll-mt-6 rounded-2xl border border-transparent px-5 py-6 transition-[background-color,border-color,box-shadow] duration-300 sm:px-7",
39 "data-active:border-guide-accent/40 data-active:bg-guide-accent-soft data-active:shadow-[0_0_0_4px_color-mix(in_oklch,var(--guide-accent)_12%,transparent)]",
40 )}
41 >
42 <span
43 aria-hidden
44 className="absolute inset-y-6 left-0 w-[3px] rounded-full bg-guide-accent opacity-0 transition-opacity duration-300 group-data-active:opacity-100 [[data-active]>&]:opacity-100"
45 />
46 <header className="mb-3 flex items-center gap-2">
47 {eyebrow && (
48 <span className="text-xs font-semibold tracking-wide text-guide-accent uppercase">
49 {eyebrow}
50 </span>
51 )}
52 {isDone && (
53 <span
54 className="grid size-4 place-items-center rounded-full bg-emerald-600 text-white dark:bg-emerald-500"
55 title="Done"
56 >
57 <Check className="size-3" strokeWidth={3} aria-hidden />
58 <span className="sr-only">Done</span>
59 </span>
60 )}
61 {isActive && (
62 <span className="ml-auto inline-flex items-center gap-1.5 rounded-full bg-guide-accent px-2 py-0.5 text-[11px] font-semibold text-white">
63 <span className="size-1.5 animate-pulse rounded-full bg-white" />
64 You are here
65 </span>
66 )}
67 </header>
68 <h2 className="mb-3 text-[1.35rem] leading-tight font-semibold tracking-tight text-balance">
69 {title}
70 </h2>
71 <div className="guide-prose">{children}</div>
72 {tryIt && <TryItButton sectionId={id} target={tryIt} />}
73 </section>
74 );