|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { useI18n } from "../contexts/I18nContext"; |
| 3 | + |
| 4 | +/** |
| 5 | + * First-visit onboarding overlay (controlled). |
| 6 | + * |
| 7 | + * Parent owns the visibility + persistence state (see App.tsx). This component |
| 8 | + * only renders the modal and reports the user's intent via onDismiss: |
| 9 | + * - onDismiss(true) → "Skip" / Finish — parent should persist. |
| 10 | + * - onDismiss(false) → backdrop click / Escape — parent should close without persisting. |
| 11 | + * |
| 12 | + * Force-show is handled by the parent (see `shouldShowOnboarding` in App.tsx). |
| 13 | + */ |
| 14 | + |
| 15 | +interface Props { |
| 16 | + onDismiss: (remember: boolean) => void; |
| 17 | +} |
| 18 | + |
| 19 | +const TITLE_ID = "ua-onboarding-title"; |
| 20 | + |
| 21 | +export default function OnboardingOverlay({ onDismiss }: Props) { |
| 22 | + const { t } = useI18n(); |
| 23 | + const STEPS = t.onboarding.steps; |
| 24 | + const [stepIdx, setStepIdx] = useState(0); |
| 25 | + |
| 26 | + // Capture-phase Escape handler — runs before the global keydown chain so we |
| 27 | + // can stopPropagation() and prevent it from also firing. |
| 28 | + useEffect(() => { |
| 29 | + const handler = (e: KeyboardEvent) => { |
| 30 | + if (e.key === "Escape") { |
| 31 | + e.stopPropagation(); |
| 32 | + onDismiss(false); |
| 33 | + } |
| 34 | + }; |
| 35 | + document.addEventListener("keydown", handler, true); |
| 36 | + return () => document.removeEventListener("keydown", handler, true); |
| 37 | + }, [onDismiss]); |
| 38 | + |
| 39 | + const isFirst = stepIdx === 0; |
| 40 | + const isLast = stepIdx === STEPS.length - 1; |
| 41 | + const step = STEPS[stepIdx]; |
| 42 | + |
| 43 | + return ( |
| 44 | + <div |
| 45 | + style={overlayStyle} |
| 46 | + onClick={(e) => { |
| 47 | + if (e.target === e.currentTarget) onDismiss(false); |
| 48 | + }} |
| 49 | + > |
| 50 | + <style>{KEYFRAMES}</style> |
| 51 | + <div |
| 52 | + role="dialog" |
| 53 | + aria-modal="true" |
| 54 | + aria-labelledby={TITLE_ID} |
| 55 | + style={cardStyle} |
| 56 | + > |
| 57 | + <div style={tagStyle}> |
| 58 | + <span style={numStyle}>0{stepIdx + 1}</span> |
| 59 | + <span> / 0{STEPS.length}</span> |
| 60 | + <span style={dotStyle} /> |
| 61 | + <span>{t.onboarding.header}</span> |
| 62 | + </div> |
| 63 | + |
| 64 | + <h2 id={TITLE_ID} style={titleStyle}> |
| 65 | + {step.title} |
| 66 | + </h2> |
| 67 | + <p style={bodyStyle}>{step.body}</p> |
| 68 | + {step.hint && ( |
| 69 | + <blockquote style={hintStyle}> |
| 70 | + <span style={{ color: "var(--color-accent)", marginRight: 8 }}>·</span> |
| 71 | + {step.hint} |
| 72 | + </blockquote> |
| 73 | + )} |
| 74 | + |
| 75 | + <div style={progressTrackStyle}> |
| 76 | + {STEPS.map((_, i) => ( |
| 77 | + <div |
| 78 | + key={i} |
| 79 | + style={{ |
| 80 | + ...dotProgressStyle, |
| 81 | + background: |
| 82 | + i === stepIdx |
| 83 | + ? "var(--color-accent)" |
| 84 | + : "var(--color-border-medium)", |
| 85 | + width: i === stepIdx ? 28 : 6, |
| 86 | + }} |
| 87 | + /> |
| 88 | + ))} |
| 89 | + </div> |
| 90 | + |
| 91 | + <div style={btnRowStyle}> |
| 92 | + <button |
| 93 | + type="button" |
| 94 | + onClick={() => onDismiss(true)} |
| 95 | + style={{ ...btnStyle, ...btnGhostStyle }} |
| 96 | + > |
| 97 | + {t.onboarding.skipForever} |
| 98 | + </button> |
| 99 | + <div style={{ flex: 1 }} /> |
| 100 | + {!isFirst && ( |
| 101 | + <button |
| 102 | + type="button" |
| 103 | + onClick={() => setStepIdx(stepIdx - 1)} |
| 104 | + style={{ ...btnStyle, ...btnGhostStyle }} |
| 105 | + > |
| 106 | + {t.onboarding.prev} |
| 107 | + </button> |
| 108 | + )} |
| 109 | + {!isLast ? ( |
| 110 | + <button |
| 111 | + type="button" |
| 112 | + onClick={() => setStepIdx(stepIdx + 1)} |
| 113 | + style={{ ...btnStyle, ...btnPrimaryStyle }} |
| 114 | + > |
| 115 | + {t.onboarding.next} |
| 116 | + </button> |
| 117 | + ) : ( |
| 118 | + <button |
| 119 | + type="button" |
| 120 | + onClick={() => onDismiss(true)} |
| 121 | + style={{ ...btnStyle, ...btnPrimaryStyle }} |
| 122 | + > |
| 123 | + {t.onboarding.finish} |
| 124 | + </button> |
| 125 | + )} |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + </div> |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +const KEYFRAMES = `@keyframes ua-fade-in { from { opacity: 0 } to { opacity: 1 } }`; |
| 133 | + |
| 134 | +const overlayStyle: React.CSSProperties = { |
| 135 | + position: "fixed", |
| 136 | + inset: 0, |
| 137 | + background: "rgba(0, 0, 0, 0.78)", |
| 138 | + backdropFilter: "blur(6px)", |
| 139 | + zIndex: 9999, |
| 140 | + display: "flex", |
| 141 | + alignItems: "center", |
| 142 | + justifyContent: "center", |
| 143 | + padding: 16, |
| 144 | + fontFamily: "var(--font-sans)", |
| 145 | + animation: "ua-fade-in 0.4s cubic-bezier(0.22, 1, 0.36, 1)", |
| 146 | +}; |
| 147 | + |
| 148 | +const cardStyle: React.CSSProperties = { |
| 149 | + background: "var(--color-elevated)", |
| 150 | + color: "var(--color-text-primary)", |
| 151 | + maxWidth: 580, |
| 152 | + width: "100%", |
| 153 | + padding: "48px 48px 36px", |
| 154 | + border: "1px solid var(--color-border-subtle)", |
| 155 | + borderTop: "2px solid var(--color-accent)", |
| 156 | + position: "relative", |
| 157 | +}; |
| 158 | + |
| 159 | +const tagStyle: React.CSSProperties = { |
| 160 | + fontSize: "0.72rem", |
| 161 | + letterSpacing: "0.3em", |
| 162 | + color: "var(--color-text-muted)", |
| 163 | + textTransform: "uppercase", |
| 164 | + marginBottom: 24, |
| 165 | + display: "flex", |
| 166 | + alignItems: "center", |
| 167 | + flexWrap: "wrap", |
| 168 | + gap: 4, |
| 169 | +}; |
| 170 | + |
| 171 | +const numStyle: React.CSSProperties = { |
| 172 | + fontFamily: "var(--font-heading)", |
| 173 | + color: "var(--color-accent)", |
| 174 | + fontSize: "0.9rem", |
| 175 | + letterSpacing: "0.1em", |
| 176 | + marginRight: 4, |
| 177 | +}; |
| 178 | + |
| 179 | +const dotStyle: React.CSSProperties = { |
| 180 | + width: 4, |
| 181 | + height: 4, |
| 182 | + background: "var(--color-accent)", |
| 183 | + borderRadius: "50%", |
| 184 | + margin: "0 12px", |
| 185 | +}; |
| 186 | + |
| 187 | +const titleStyle: React.CSSProperties = { |
| 188 | + fontFamily: "var(--font-heading)", |
| 189 | + fontSize: "1.7rem", |
| 190 | + fontWeight: 400, |
| 191 | + letterSpacing: "0.02em", |
| 192 | + lineHeight: 1.3, |
| 193 | + marginBottom: 16, |
| 194 | + color: "var(--color-text-primary)", |
| 195 | +}; |
| 196 | + |
| 197 | +const bodyStyle: React.CSSProperties = { |
| 198 | + fontSize: "0.98rem", |
| 199 | + lineHeight: 1.7, |
| 200 | + color: "var(--color-text-secondary)", |
| 201 | + marginBottom: 0, |
| 202 | +}; |
| 203 | + |
| 204 | +const hintStyle: React.CSSProperties = { |
| 205 | + margin: "20px 0 0", |
| 206 | + padding: "12px 18px", |
| 207 | + borderLeft: "2px solid var(--color-border-medium)", |
| 208 | + background: "var(--color-accent-overlay-bg)", |
| 209 | + fontSize: "0.86rem", |
| 210 | + color: "var(--color-accent)", |
| 211 | + fontStyle: "italic", |
| 212 | +}; |
| 213 | + |
| 214 | +const progressTrackStyle: React.CSSProperties = { |
| 215 | + display: "flex", |
| 216 | + gap: 6, |
| 217 | + marginTop: 36, |
| 218 | + marginBottom: 28, |
| 219 | +}; |
| 220 | + |
| 221 | +const dotProgressStyle: React.CSSProperties = { |
| 222 | + height: 4, |
| 223 | + borderRadius: 2, |
| 224 | + transition: "width 0.5s cubic-bezier(0.22, 1, 0.36, 1), background 0.3s", |
| 225 | +}; |
| 226 | + |
| 227 | +const btnRowStyle: React.CSSProperties = { |
| 228 | + display: "flex", |
| 229 | + alignItems: "center", |
| 230 | + gap: 10, |
| 231 | +}; |
| 232 | + |
| 233 | +const btnStyle: React.CSSProperties = { |
| 234 | + padding: "10px 22px", |
| 235 | + fontSize: "0.82rem", |
| 236 | + letterSpacing: "0.12em", |
| 237 | + textTransform: "uppercase", |
| 238 | + border: "1px solid", |
| 239 | + cursor: "pointer", |
| 240 | + fontFamily: "inherit", |
| 241 | + transition: "all 0.3s cubic-bezier(0.22, 1, 0.36, 1)", |
| 242 | + fontWeight: 400, |
| 243 | +}; |
| 244 | + |
| 245 | +const btnGhostStyle: React.CSSProperties = { |
| 246 | + background: "transparent", |
| 247 | + borderColor: "var(--color-border-medium)", |
| 248 | + color: "var(--color-text-muted)", |
| 249 | +}; |
| 250 | + |
| 251 | +const btnPrimaryStyle: React.CSSProperties = { |
| 252 | + background: "var(--color-accent)", |
| 253 | + borderColor: "var(--color-accent)", |
| 254 | + color: "var(--color-root)", |
| 255 | + fontWeight: 500, |
| 256 | +}; |
0 commit comments