|
| 1 | +import { useEffect, useRef, useState, useCallback } from 'react' |
| 2 | +import { useTutorial } from '@/lib/tutorial' |
| 3 | + |
| 4 | +interface HighlightRect { |
| 5 | + top: number |
| 6 | + left: number |
| 7 | + width: number |
| 8 | + height: number |
| 9 | +} |
| 10 | + |
| 11 | +const PADDING = 10 |
| 12 | +const CALLOUT_WIDTH = 340 |
| 13 | +const CALLOUT_EST_HEIGHT = 210 |
| 14 | + |
| 15 | +export function TutorialOverlay() { |
| 16 | + const { isActive, currentStep, steps, next, back, close, finish } = useTutorial() |
| 17 | + const step = steps[currentStep] |
| 18 | + const isFirst = currentStep === 0 |
| 19 | + const isLast = currentStep === steps.length - 1 |
| 20 | + |
| 21 | + const [rect, setRect] = useState<HighlightRect | null>(null) |
| 22 | + const dialogRef = useRef<HTMLDivElement>(null) |
| 23 | + const firstFocusableRef = useRef<HTMLButtonElement>(null) |
| 24 | + |
| 25 | + const prefersReducedMotion = |
| 26 | + typeof window !== 'undefined' && |
| 27 | + window.matchMedia('(prefers-reduced-motion: reduce)').matches |
| 28 | + |
| 29 | + const measureTarget = useCallback(() => { |
| 30 | + if (!step) return |
| 31 | + const el = document.querySelector(step.targetSelector) |
| 32 | + if (!el) { |
| 33 | + setRect(null) |
| 34 | + return |
| 35 | + } |
| 36 | + const r = el.getBoundingClientRect() |
| 37 | + setRect({ top: r.top, left: r.left, width: r.width, height: r.height }) |
| 38 | + }, [step]) |
| 39 | + |
| 40 | + // Scroll to element and measure on step change |
| 41 | + useEffect(() => { |
| 42 | + if (!isActive || !step) return |
| 43 | + const el = document.querySelector(step.targetSelector) |
| 44 | + if (el) { |
| 45 | + el.scrollIntoView({ |
| 46 | + behavior: prefersReducedMotion ? 'instant' : 'smooth', |
| 47 | + block: 'center', |
| 48 | + }) |
| 49 | + } |
| 50 | + // Measure after scroll settles |
| 51 | + const timeout = setTimeout(measureTarget, prefersReducedMotion ? 0 : 400) |
| 52 | + return () => clearTimeout(timeout) |
| 53 | + }, [isActive, currentStep, step, prefersReducedMotion, measureTarget]) |
| 54 | + |
| 55 | + // Re-measure on resize / scroll |
| 56 | + useEffect(() => { |
| 57 | + if (!isActive) return |
| 58 | + window.addEventListener('resize', measureTarget) |
| 59 | + window.addEventListener('scroll', measureTarget, { passive: true }) |
| 60 | + return () => { |
| 61 | + window.removeEventListener('resize', measureTarget) |
| 62 | + window.removeEventListener('scroll', measureTarget) |
| 63 | + } |
| 64 | + }, [isActive, measureTarget]) |
| 65 | + |
| 66 | + // Keyboard: Esc → close |
| 67 | + useEffect(() => { |
| 68 | + if (!isActive) return |
| 69 | + const handler = (e: KeyboardEvent) => { |
| 70 | + if (e.key === 'Escape') { |
| 71 | + e.preventDefault() |
| 72 | + close() |
| 73 | + } |
| 74 | + } |
| 75 | + document.addEventListener('keydown', handler) |
| 76 | + return () => document.removeEventListener('keydown', handler) |
| 77 | + }, [isActive, close]) |
| 78 | + |
| 79 | + // Focus trap within dialog |
| 80 | + useEffect(() => { |
| 81 | + if (!isActive || !dialogRef.current) return |
| 82 | + const el = dialogRef.current |
| 83 | + const focusables = Array.from( |
| 84 | + el.querySelectorAll<HTMLElement>('button, [href], input, [tabindex]:not([tabindex="-1"])') |
| 85 | + ) |
| 86 | + const first = focusables[0] |
| 87 | + const last = focusables[focusables.length - 1] |
| 88 | + |
| 89 | + const handleTab = (e: KeyboardEvent) => { |
| 90 | + if (e.key !== 'Tab') return |
| 91 | + if (focusables.length === 0) { |
| 92 | + e.preventDefault() |
| 93 | + return |
| 94 | + } |
| 95 | + if (e.shiftKey) { |
| 96 | + if (document.activeElement === first) { |
| 97 | + e.preventDefault() |
| 98 | + last?.focus() |
| 99 | + } |
| 100 | + } else { |
| 101 | + if (document.activeElement === last) { |
| 102 | + e.preventDefault() |
| 103 | + first?.focus() |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + document.addEventListener('keydown', handleTab) |
| 109 | + return () => document.removeEventListener('keydown', handleTab) |
| 110 | + }, [isActive, currentStep]) |
| 111 | + |
| 112 | + // Move focus into dialog when step changes |
| 113 | + useEffect(() => { |
| 114 | + if (isActive && firstFocusableRef.current) { |
| 115 | + firstFocusableRef.current.focus() |
| 116 | + } |
| 117 | + }, [isActive, currentStep]) |
| 118 | + |
| 119 | + if (!isActive || !step) return null |
| 120 | + |
| 121 | + // --- Spotlight geometry --- |
| 122 | + const hlTop = rect ? rect.top - PADDING : 0 |
| 123 | + const hlLeft = rect ? rect.left - PADDING : 0 |
| 124 | + const hlWidth = rect ? rect.width + PADDING * 2 : 0 |
| 125 | + const hlHeight = rect ? rect.height + PADDING * 2 : 0 |
| 126 | + |
| 127 | + // --- Callout position --- |
| 128 | + const vw = window.innerWidth |
| 129 | + const vh = window.innerHeight |
| 130 | + const cw = Math.min(CALLOUT_WIDTH, vw - 32) |
| 131 | + let calloutTop: number |
| 132 | + let calloutLeft: number |
| 133 | + |
| 134 | + if (rect) { |
| 135 | + calloutLeft = Math.max(16, Math.min(vw - cw - 16, hlLeft + hlWidth / 2 - cw / 2)) |
| 136 | + const spaceBelow = vh - (hlTop + hlHeight) |
| 137 | + if (spaceBelow >= CALLOUT_EST_HEIGHT + 16) { |
| 138 | + calloutTop = hlTop + hlHeight + 12 |
| 139 | + } else { |
| 140 | + calloutTop = Math.max(16, hlTop - CALLOUT_EST_HEIGHT - 12) |
| 141 | + } |
| 142 | + } else { |
| 143 | + calloutTop = vh / 2 - CALLOUT_EST_HEIGHT / 2 |
| 144 | + calloutLeft = vw / 2 - cw / 2 |
| 145 | + } |
| 146 | + |
| 147 | + return ( |
| 148 | + <> |
| 149 | + {/* Dim backdrop via box-shadow on spotlight box */} |
| 150 | + <div |
| 151 | + aria-hidden="true" |
| 152 | + style={{ |
| 153 | + position: 'fixed', |
| 154 | + top: rect ? hlTop : 0, |
| 155 | + left: rect ? hlLeft : 0, |
| 156 | + width: rect ? hlWidth : '100%', |
| 157 | + height: rect ? hlHeight : '100%', |
| 158 | + borderRadius: 8, |
| 159 | + boxShadow: '0 0 0 9999px rgba(0,0,0,0.78)', |
| 160 | + border: rect ? '2px solid #00ff41' : 'none', |
| 161 | + zIndex: 9998, |
| 162 | + pointerEvents: 'none', |
| 163 | + }} |
| 164 | + /> |
| 165 | + |
| 166 | + {/* Callout dialog */} |
| 167 | + <div |
| 168 | + ref={dialogRef} |
| 169 | + role="dialog" |
| 170 | + aria-modal="true" |
| 171 | + aria-label={`Tutorial step ${currentStep + 1} of ${steps.length}: ${step.title}`} |
| 172 | + style={{ |
| 173 | + position: 'fixed', |
| 174 | + top: calloutTop, |
| 175 | + left: calloutLeft, |
| 176 | + width: cw, |
| 177 | + zIndex: 9999, |
| 178 | + outline: 'none', |
| 179 | + }} |
| 180 | + className="bg-black border-2 border-green-400 rounded-lg p-4 font-mono shadow-2xl shadow-green-900/50" |
| 181 | + > |
| 182 | + {/* Step indicator */} |
| 183 | + <div className="text-green-500/60 text-xs mb-1 tracking-wider"> |
| 184 | + STEP {currentStep + 1} / {steps.length} |
| 185 | + </div> |
| 186 | + |
| 187 | + <h2 className="text-green-400 font-bold text-sm mb-2 leading-snug"> |
| 188 | + {step.title} |
| 189 | + </h2> |
| 190 | + |
| 191 | + <p className="text-green-300/80 text-xs mb-3 leading-relaxed"> |
| 192 | + {step.body} |
| 193 | + </p> |
| 194 | + |
| 195 | + {/* Progress dots */} |
| 196 | + <div className="flex gap-1.5 mb-3" aria-hidden="true"> |
| 197 | + {steps.map((_, i) => ( |
| 198 | + <div |
| 199 | + key={i} |
| 200 | + className={`h-1.5 rounded-full ${ |
| 201 | + i === currentStep |
| 202 | + ? 'bg-green-400 w-4' |
| 203 | + : i < currentStep |
| 204 | + ? 'bg-green-700 w-1.5' |
| 205 | + : 'bg-white/20 w-1.5' |
| 206 | + }`} |
| 207 | + /> |
| 208 | + ))} |
| 209 | + </div> |
| 210 | + |
| 211 | + {/* Controls */} |
| 212 | + <div className="flex items-center gap-2 flex-wrap"> |
| 213 | + {/* Skip / Close */} |
| 214 | + <button |
| 215 | + ref={firstFocusableRef} |
| 216 | + onClick={close} |
| 217 | + className="text-xs text-white/40 hover:text-white/70 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-white/50 px-1 py-1 rounded transition-colors" |
| 218 | + > |
| 219 | + [skip] |
| 220 | + </button> |
| 221 | + |
| 222 | + <div className="flex gap-2 ml-auto"> |
| 223 | + {!isFirst && ( |
| 224 | + <button |
| 225 | + onClick={back} |
| 226 | + className="text-xs px-3 py-1.5 rounded border border-white/20 text-white/70 hover:bg-white/10 hover:text-white focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-white/50 transition-colors" |
| 227 | + > |
| 228 | + ← Back |
| 229 | + </button> |
| 230 | + )} |
| 231 | + {!isLast ? ( |
| 232 | + <button |
| 233 | + onClick={next} |
| 234 | + className="text-xs px-3 py-1.5 rounded bg-green-700 hover:bg-green-600 text-white focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-green-400 transition-colors font-bold" |
| 235 | + > |
| 236 | + Next → |
| 237 | + </button> |
| 238 | + ) : ( |
| 239 | + <button |
| 240 | + onClick={finish} |
| 241 | + className="text-xs px-3 py-1.5 rounded bg-green-700 hover:bg-green-600 text-white focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-green-400 transition-colors font-bold" |
| 242 | + > |
| 243 | + Finish ✓ |
| 244 | + </button> |
| 245 | + )} |
| 246 | + </div> |
| 247 | + </div> |
| 248 | + </div> |
| 249 | + </> |
| 250 | + ) |
| 251 | +} |
0 commit comments