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