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

guide-section.tsx

58 lines2.1 KB

1import { useEffect, useRef } from "react";
2import type { ReactNode } from "react";
3​
4import { cn } from "@/lib/utils";
5​
6import { useActiveSection } from "./guide-store";
7​
8interface GuideSectionProps {
9 id: string;
10 /** Small label above the heading, e.g. "Step 2". */
11 eyebrow?: string;
12 title: string;
13 children: ReactNode;
15​
16export function GuideSection({ id, eyebrow, title, children }: GuideSectionProps) {
17 const isActive = useActiveSection() === id;
18 const ref = useRef<HTMLElement>(null);
19​
20 useEffect(() => {
21 if (isActive) ref.current?.scrollIntoView({ behavior: "smooth", block: "nearest" });
22 }, [isActive]);
23​
24 return (
25 <section
26 ref={ref}
27 id={id}
28 data-active={isActive || undefined}
29 aria-current={isActive ? "step" : undefined}
30 className={cn(
31 "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",
32 "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)]",
33 )}
34 >
35 <span
36 aria-hidden
37 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"
38 />
39 <header className="mb-3 flex items-center gap-2">
40 {eyebrow && (
41 <span className="text-xs font-semibold tracking-wide text-guide-accent uppercase">
42 {eyebrow}
43 </span>
44 )}
45 {isActive && (
46 <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">
47 <span className="size-1.5 animate-pulse rounded-full bg-white" />
48 You are here
49 </span>
50 )}
51 </header>
52 <h2 className="mb-3 text-[1.35rem] leading-tight font-semibold tracking-tight text-balance">
53 {title}
54 </h2>
55 <div className="guide-prose">{children}</div>
56 </section>
57 );