sovereign-intelligence
Front door to Drake Stapleton's sovereign stack.
Brought by drakestapleton
sovereign-intelligence / docs / archive / drakestapleton.com-old / src / components / StickyChapterNav.tsx
| 1 | import { useEffect, useRef, useState } from "react"; |
| 2 | |
| 3 | export interface ChapterItem { |
| 4 | id: string; |
| 5 | number: string; |
| 6 | label: string; |
| 7 | summary: string; |
| 8 | } |
| 9 | |
| 10 | export const HOMEPAGE_CHAPTERS: ChapterItem[] = [ |
| 11 | { id: "formation", number: "01", label: "Formation", summary: "Early life and resilience" }, |
| 12 | { id: "drive", number: "02", label: "Drive", summary: "Endurance and recovery" }, |
| 13 | { id: "record", number: "03", label: "Life Record", summary: "Documented milestones" }, |
| 14 | { id: "people", number: "04", label: "People", summary: "Family and inheritances" }, |
| 15 | { id: "work", number: "05", label: "Work", summary: "Career ladder and impact" }, |
| 16 | { id: "software", number: "06", label: "Software and AI", summary: "Engineering and practice" }, |
| 17 | { id: "latest", number: "07", label: "Latest Work", summary: "Atlas and AIEN architecture" }, |
| 18 | { id: "identity", number: "08", label: "Identity", summary: "Freedom Fighter ethos" }, |
| 19 | ]; |
| 20 | |
| 21 | export function StickyChapterNav() { |
| 22 | const [activeId, setActiveId] = useState<string>("formation"); |
| 23 | const [drawerOpen, setDrawerOpen] = useState<boolean>(false); |
| 24 | const pillListRef = useRef<HTMLDivElement>(null); |
| 25 | |
| 26 | useEffect(() => { |
| 27 | const observer = new IntersectionObserver( |
| 28 | (entries) => { |
| 29 | entries.forEach((entry) => { |
| 30 | if (entry.isIntersecting) { |
| 31 | setActiveId(entry.target.id); |
| 32 | } |
| 33 | }); |
| 34 | }, |
| 35 | { |
| 36 | rootMargin: "-25% 0px -60% 0px", |
| 37 | threshold: 0, |
| 38 | } |
| 39 | ); |
| 40 | |
| 41 | HOMEPAGE_CHAPTERS.forEach((chapter) => { |
| 42 | const element = document.getElementById(chapter.id); |
| 43 | if (element) { |
| 44 | observer.observe(element); |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | return () => observer.disconnect(); |
| 49 | }, []); |
| 50 | |
| 51 | useEffect(() => { |
| 52 | const container = pillListRef.current; |
| 53 | if (container) { |
| 54 | const activePill = container.querySelector(`[data-id="${activeId}"]`) as HTMLElement | null; |
| 55 | if (activePill) { |
| 56 | const offsetLeft = activePill.offsetLeft - container.offsetLeft; |
| 57 | const targetScroll = offsetLeft - container.clientWidth / 2 + activePill.clientWidth / 2; |
| 58 | container.scrollTo({ left: targetScroll, behavior: "smooth" }); |
| 59 | } |
| 60 | } |
| 61 | }, [activeId]); |
| 62 | |
| 63 | const activeIndex = HOMEPAGE_CHAPTERS.findIndex((c) => c.id === activeId); |
| 64 | const safeIndex = activeIndex >= 0 ? activeIndex : 0; |
| 65 | const currentChapter = HOMEPAGE_CHAPTERS[safeIndex]; |
| 66 | |
| 67 | const hasPrev = safeIndex > 0; |
| 68 | const hasNext = safeIndex < HOMEPAGE_CHAPTERS.length - 1; |
| 69 | |
| 70 | const scrollToChapter = (id: string) => { |
| 71 | const target = document.getElementById(id); |
| 72 | if (target) { |
| 73 | target.scrollIntoView({ behavior: "smooth" }); |
| 74 | setDrawerOpen(false); |
| 75 | } |
| 76 | }; |
| 77 | |
| 78 | const progressPercent = Math.round(((safeIndex + 1) / HOMEPAGE_CHAPTERS.length) * 100); |
| 79 | |
| 80 | return ( |
| 81 | <aside className="sticky-chapter-nav" aria-label="Homepage Chapter Navigation"> |
| 82 | <div className="sticky-chapter-inner"> |
| 83 | <div className="chapter-stepper"> |
| 84 | <button |
| 85 | type="button" |
| 86 | className="stepper-btn" |
| 87 | onClick={() => hasPrev && scrollToChapter(HOMEPAGE_CHAPTERS[safeIndex - 1].id)} |
| 88 | disabled={hasPrev === false} |
| 89 | aria-label="Previous Chapter" |
| 90 | > |
| 91 | ← Prev |
| 92 | </button> |
| 93 | |
| 94 | <button |
| 95 | type="button" |
| 96 | className="chapter-indicator-btn" |
| 97 | onClick={() => setDrawerOpen(!drawerOpen)} |
| 98 | aria-expanded={drawerOpen} |
| 99 | aria-label="Toggle Chapter Drawer" |
| 100 | > |
| 101 | <span className="indicator-badge">{currentChapter.number}</span> |
| 102 | <span className="indicator-label">{currentChapter.label}</span> |
| 103 | <span className="indicator-chevron">{drawerOpen ? "▲" : "▼"}</span> |
| 104 | </button> |
| 105 | |
| 106 | <button |
| 107 | type="button" |
| 108 | className="stepper-btn" |
| 109 | onClick={() => hasNext && scrollToChapter(HOMEPAGE_CHAPTERS[safeIndex + 1].id)} |
| 110 | disabled={hasNext === false} |
| 111 | aria-label="Next Chapter" |
| 112 | > |
| 113 | Next → |
| 114 | </button> |
| 115 | </div> |
| 116 | |
| 117 | <nav className="chapter-pill-list" ref={pillListRef} aria-label="Chapter list"> |
| 118 | {HOMEPAGE_CHAPTERS.map((chapter) => { |
| 119 | const isCurrent = chapter.id === activeId; |
| 120 | return ( |
| 121 | <button |
| 122 | key={chapter.id} |
| 123 | type="button" |
| 124 | data-id={chapter.id} |
| 125 | className={`chapter-pill ${isCurrent ? "active" : ""}`} |
| 126 | onClick={() => scrollToChapter(chapter.id)} |
| 127 | aria-current={isCurrent ? "true" : undefined} |
| 128 | > |
| 129 | <span className="pill-num">{chapter.number}</span> |
| 130 | <span className="pill-label">{chapter.label}</span> |
| 131 | </button> |
| 132 | ); |
| 133 | })} |
| 134 | </nav> |
| 135 | </div> |
| 136 | |
| 137 | <div |
| 138 | className="chapter-progress-bar" |
| 139 | style={{ width: `${progressPercent}%` }} |
| 140 | role="progressbar" |
| 141 | aria-valuenow={progressPercent} |
| 142 | aria-valuemin={0} |
| 143 | aria-valuemax={100} |
| 144 | /> |
| 145 | |
| 146 | {drawerOpen && ( |
| 147 | <div className="chapter-drawer-overlay" onClick={() => setDrawerOpen(false)}> |
| 148 | <div |
| 149 | className="chapter-drawer-dialog" |
| 150 | onClick={(event) => event.stopPropagation()} |
| 151 | role="dialog" |
| 152 | aria-label="All Chapters" |
| 153 | > |
| 154 | <div className="drawer-header"> |
| 155 | <h3>Dossier Chapters</h3> |
| 156 | <button |
| 157 | type="button" |
| 158 | className="drawer-close-btn" |
| 159 | onClick={() => setDrawerOpen(false)} |
| 160 | aria-label="Close chapter menu" |
| 161 | > |
| 162 | ✕ |
| 163 | </button> |
| 164 | </div> |
| 165 | <div className="drawer-list"> |
| 166 | {HOMEPAGE_CHAPTERS.map((chapter) => { |
| 167 | const isCurrent = chapter.id === activeId; |
| 168 | return ( |
| 169 | <button |
| 170 | key={chapter.id} |
| 171 | type="button" |
| 172 | className={`drawer-item ${isCurrent ? "active" : ""}`} |
| 173 | onClick={() => scrollToChapter(chapter.id)} |
| 174 | > |
| 175 | <span className="drawer-item-num">{chapter.number}</span> |
| 176 | <div className="drawer-item-content"> |
| 177 | <strong>{chapter.label}</strong> |
| 178 | <small>{chapter.summary}</small> |
| 179 | </div> |
| 180 | {isCurrent && <span className="drawer-item-active-dot">●</span>} |
| 181 | </button> |
| 182 | ); |
| 183 | })} |
| 184 | </div> |
| 185 | </div> |
| 186 | </div> |
| 187 | )} |
| 188 | </aside> |
| 189 | ); |
| 190 | } |