|
| 1 | +"use client"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Component: StepConnector / StepComponent |
| 5 | + * |
| 6 | + * Visually connects sequential headings with a vertical rail and circles. |
| 7 | + * Supports optional numbering and a user‑checkable “completed” state. |
| 8 | + * |
| 9 | + * Props overview |
| 10 | + * - startAt: number — first step number (default 1) |
| 11 | + * - selector: string — heading selector (default 'h2') |
| 12 | + * - showNumbers: boolean — show numbers inside circles (default true) |
| 13 | + * - checkable: boolean — allow toggling completion (default false) |
| 14 | + * - persistence: 'session' | 'none' — completion storage (default 'session') |
| 15 | + * - showReset: boolean — show a small “Reset steps” action (default true) |
| 16 | + * |
| 17 | + * Usage |
| 18 | + * <StepConnector checkable /> |
| 19 | + * <StepConnector showNumbers={false} checkable persistence="none" /> |
| 20 | + * |
| 21 | + * Accessibility |
| 22 | + * - Each circle becomes a button when `checkable` is enabled (keyboard + aria‑pressed). |
| 23 | + * - When `showNumbers` is false, a subtle dot is shown; numbering remains implicit |
| 24 | + * via the DOM order, and buttons include descriptive aria‑labels. |
| 25 | + * |
| 26 | + * Theming / CSS variables (in style.module.scss) |
| 27 | + * - --rail-x, --circle, --gap control rail position and circle size/spacing. |
| 28 | + */ |
| 29 | + |
| 30 | +import {useEffect, useMemo, useRef, useState} from "react"; |
| 31 | +import styles from "./style.module.scss"; |
| 32 | + |
| 33 | +type Persistence = "session" | "none"; |
| 34 | + |
| 35 | +type Props = { |
| 36 | + children: React.ReactNode; |
| 37 | + /** Start numbering from this value. @defaultValue 1 */ |
| 38 | + startAt?: number; |
| 39 | + /** Which heading level to connect (CSS selector). @defaultValue 'h2' */ |
| 40 | + selector?: string; |
| 41 | + /** Show numeric labels inside circles. Set false for blank circles. @defaultValue true */ |
| 42 | + showNumbers?: boolean; |
| 43 | + /** Allow users to check off steps (circle becomes a button). @defaultValue false */ |
| 44 | + checkable?: boolean; |
| 45 | + /** Completion storage: 'session' | 'none'. @defaultValue 'session' */ |
| 46 | + persistence?: Persistence; |
| 47 | + /** Show a small "Reset steps" action when checkable. @defaultValue true */ |
| 48 | + showReset?: boolean; |
| 49 | +}; |
| 50 | + |
| 51 | +export function StepComponent({ |
| 52 | + children, |
| 53 | + startAt = 1, |
| 54 | + selector = "h2", |
| 55 | + showNumbers = true, |
| 56 | + checkable = false, |
| 57 | + persistence = "session", |
| 58 | + showReset = true, |
| 59 | +}: Props) { |
| 60 | + const containerRef = useRef<HTMLDivElement | null>(null); |
| 61 | + const [completed, setCompleted] = useState<Set<string>>(new Set()); |
| 62 | + |
| 63 | + const storageKey = useMemo(() => { |
| 64 | + if (typeof window === "undefined" || persistence !== "session") return null; |
| 65 | + try { |
| 66 | + const path = window.location?.pathname ?? ""; |
| 67 | + return `stepConnector:${path}:${selector}:${startAt}`; |
| 68 | + } catch { |
| 69 | + return null; |
| 70 | + } |
| 71 | + }, [persistence, selector, startAt]); |
| 72 | + |
| 73 | + useEffect(() => { |
| 74 | + const container = containerRef.current; |
| 75 | + if (!container) { |
| 76 | + // Return empty cleanup function for consistent return |
| 77 | + return () => {}; |
| 78 | + } |
| 79 | + |
| 80 | + const headings = Array.from( |
| 81 | + container.querySelectorAll<HTMLElement>(`:scope ${selector}`) |
| 82 | + ); |
| 83 | + |
| 84 | + headings.forEach(h => { |
| 85 | + h.classList.remove(styles.stepHeading); |
| 86 | + h.removeAttribute("data-step"); |
| 87 | + h.removeAttribute("data-completed"); |
| 88 | + const existingToggle = h.querySelector(`.${styles.stepToggle}`); |
| 89 | + if (existingToggle) existingToggle.remove(); |
| 90 | + }); |
| 91 | + |
| 92 | + headings.forEach((h, idx) => { |
| 93 | + const stepNumber = startAt + idx; |
| 94 | + h.setAttribute("data-step", String(stepNumber)); |
| 95 | + h.classList.add(styles.stepHeading); |
| 96 | + |
| 97 | + if (checkable) { |
| 98 | + const btn = document.createElement("button"); |
| 99 | + btn.type = "button"; |
| 100 | + btn.className = styles.stepToggle; |
| 101 | + btn.setAttribute("aria-label", `Toggle completion for step ${stepNumber}`); |
| 102 | + btn.setAttribute("aria-pressed", completed.has(h.id) ? "true" : "false"); |
| 103 | + btn.addEventListener("click", () => { |
| 104 | + setCompleted(prev => { |
| 105 | + const next = new Set(prev); |
| 106 | + if (next.has(h.id)) next.delete(h.id); |
| 107 | + else next.add(h.id); |
| 108 | + return next; |
| 109 | + }); |
| 110 | + }); |
| 111 | + h.insertBefore(btn, h.firstChild); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + // Cleanup function |
| 116 | + return () => { |
| 117 | + headings.forEach(h => { |
| 118 | + h.classList.remove(styles.stepHeading); |
| 119 | + h.removeAttribute("data-step"); |
| 120 | + h.removeAttribute("data-completed"); |
| 121 | + const existingToggle = h.querySelector(`.${styles.stepToggle}`); |
| 122 | + if (existingToggle) existingToggle.remove(); |
| 123 | + }); |
| 124 | + }; |
| 125 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 126 | + }, [startAt, selector, checkable]); |
| 127 | + |
| 128 | + useEffect(() => { |
| 129 | + if (!storageKey || !checkable) return; |
| 130 | + try { |
| 131 | + const raw = sessionStorage.getItem(storageKey); |
| 132 | + if (raw) setCompleted(new Set(JSON.parse(raw) as string[])); |
| 133 | + } catch { |
| 134 | + // Ignore storage errors |
| 135 | + } |
| 136 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 137 | + }, [storageKey, checkable]); |
| 138 | + |
| 139 | + useEffect(() => { |
| 140 | + const container = containerRef.current; |
| 141 | + if (!container) return; |
| 142 | + const headings = Array.from( |
| 143 | + container.querySelectorAll<HTMLElement>(`:scope ${selector}`) |
| 144 | + ); |
| 145 | + headings.forEach(h => { |
| 146 | + const isDone = completed.has(h.id); |
| 147 | + if (isDone) h.setAttribute("data-completed", "true"); |
| 148 | + else h.removeAttribute("data-completed"); |
| 149 | + const btn = h.querySelector(`.${styles.stepToggle}`) as HTMLButtonElement | null; |
| 150 | + if (btn) btn.setAttribute("aria-pressed", isDone ? "true" : "false"); |
| 151 | + }); |
| 152 | + |
| 153 | + if (storageKey && checkable) { |
| 154 | + try { |
| 155 | + sessionStorage.setItem(storageKey, JSON.stringify(Array.from(completed))); |
| 156 | + } catch { |
| 157 | + // Ignore storage errors |
| 158 | + } |
| 159 | + } |
| 160 | + }, [completed, selector, storageKey, checkable]); |
| 161 | + |
| 162 | + const handleReset = () => { |
| 163 | + setCompleted(new Set()); |
| 164 | + if (storageKey) { |
| 165 | + try { |
| 166 | + sessionStorage.removeItem(storageKey); |
| 167 | + } catch { |
| 168 | + // Ignore storage errors |
| 169 | + } |
| 170 | + } |
| 171 | + }; |
| 172 | + |
| 173 | + return ( |
| 174 | + <div |
| 175 | + ref={containerRef} |
| 176 | + className={styles.stepContainer} |
| 177 | + data-shownumbers={showNumbers ? "true" : "false"} |
| 178 | + > |
| 179 | + {checkable && showReset && ( |
| 180 | + <div className={styles.resetRow}> |
| 181 | + <button type="button" className={styles.resetBtn} onClick={handleReset}> |
| 182 | + Reset steps |
| 183 | + </button> |
| 184 | + </div> |
| 185 | + )} |
| 186 | + {children} |
| 187 | + </div> |
| 188 | + ); |
| 189 | +} |
| 190 | + |
| 191 | +// Alias to match usage <StepConnector>...</StepConnector> |
| 192 | +export function StepConnector(props: Props) { |
| 193 | + return <StepComponent {...props} />; |
| 194 | +} |
| 195 | + |
0 commit comments