Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | import * as HoverCard from '@radix-ui/react-hover-card' import type { Meta, StoryObj } from '@storybook/react' import type React from 'react' import { useState } from 'react' import { TutorialUIProvider, useTutorialUI } from './TutorialUIContext' // Demo tooltip component that uses the focus gate function DemoTooltip({ children, content, hintType, }: { children: React.ReactNode content: string hintType: 'term' | 'bead' }) { const [isOpen, setIsOpen] = useState(false) const ui = useTutorialUI() const handleOpenChange = (open: boolean) => { if (open) { const granted = ui.requestFocus(hintType) if (granted) { setIsOpen(true) } } else { ui.releaseFocus(hintType) setIsOpen(false) } } const isActive = ui.hintFocus === hintType const bgColor = hintType === 'term' ? '#3b82f6' : '#10b981' const activeBg = hintType === 'term' ? '#1d4ed8' : '#047857' return ( <HoverCard.Root open={isOpen} onOpenChange={handleOpenChange} openDelay={100} closeDelay={200}> <HoverCard.Trigger asChild> <button style={{ padding: '8px 16px', margin: '4px', backgroundColor: isActive ? activeBg : bgColor, color: 'white', border: 'none', borderRadius: '6px', cursor: 'pointer', fontWeight: isActive ? 'bold' : 'normal', opacity: ui.hintFocus === 'none' || ui.hintFocus === hintType ? 1 : 0.5, transition: 'all 0.2s ease', }} > {children} </button> </HoverCard.Trigger> <HoverCard.Portal> <HoverCard.Content style={{ backgroundColor: 'white', border: '1px solid #e5e7eb', borderRadius: '8px', padding: '12px', boxShadow: '0 4px 12px rgba(0, 0, 0, 0.1)', maxWidth: '300px', zIndex: 50, }} sideOffset={8} > <div> <strong>{hintType === 'term' ? '๐ Term Tooltip' : '๐ข Bead Tooltip'}</strong> <p style={{ margin: '8px 0 0', fontSize: '14px', color: '#6b7280' }}>{content}</p> <p style={{ margin: '8px 0 0', fontSize: '12px', color: '#9ca3af' }}> Focus owner: <code>{ui.hintFocus}</code> </p> </div> <HoverCard.Arrow /> </HoverCard.Content> </HoverCard.Portal> </HoverCard.Root> ) } function FocusStatus() { const ui = useTutorialUI() return ( <div style={{ padding: '12px', backgroundColor: '#f3f4f6', borderRadius: '6px', marginBottom: '20px', fontFamily: 'monospace', fontSize: '14px', }} > <strong>Focus Status:</strong> <code>{ui.hintFocus}</code> <br /> <small style={{ color: '#6b7280' }}>Only one tooltip type can have focus at a time</small> </div> ) } function TooltipGateDemo() { return ( <div style={{ padding: '20px' }}> <h3>Single-Owner Tooltip Gate Demo</h3> <p>Try hovering over these buttons. Only one tooltip can be open at a time:</p> <FocusStatus /> <div style={{ marginBottom: '30px' }}> <h4>Term Tooltips (Blue)</h4> <DemoTooltip hintType="term" content="This explains what the mathematical term means."> Hover: Term A </DemoTooltip> <DemoTooltip hintType="term" content="This shows reasoning for this part of the equation."> Hover: Term B </DemoTooltip> <DemoTooltip hintType="term" content="This demonstrates the calculation step."> Hover: Term C </DemoTooltip> </div> <div> <h4>Bead Tooltips (Green)</h4> <DemoTooltip hintType="bead" content="This shows which bead to move and how."> Hover: Bead 1 </DemoTooltip> <DemoTooltip hintType="bead" content="This explains the physical movement required."> Hover: Bead 2 </DemoTooltip> <DemoTooltip hintType="bead" content="This guides the hand position for this action."> Hover: Bead 3 </DemoTooltip> </div> <div style={{ marginTop: '30px', padding: '16px', backgroundColor: '#fff3cd', borderRadius: '6px', border: '1px solid #ffeaa7', }} > <h4>๐งช Test the Gate:</h4> <ol> <li>Hover over a blue "Term" button โ tooltip opens</li> <li> While keeping mouse on blue button, try hovering green "Bead" button โ green tooltip blocked </li> <li>Move mouse away from blue button โ blue tooltip closes</li> <li>Now hover green "Bead" button โ green tooltip opens</li> <li>Try hovering another green button โ focus transfers between green tooltips</li> </ol> </div> </div> ) } const meta: Meta<typeof TutorialUIProvider> = { title: 'Tutorial/TutorialUIContext', component: TutorialUIProvider, parameters: { layout: 'fullscreen', docs: { description: { component: ` The TutorialUIContext provides a single-owner tooltip gate system for tutorial interfaces. It ensures only one type of tooltip (term or bead) can be active at a time. **Key Features:** - **Single-owner focus**: Only 'term' OR 'bead' tooltips can be open at once - **Focus ownership**: \`requestFocus()\` returns true if granted, false if denied - **Active segment tracking**: Manages current pedagogical segment for Coach Bar - **Debug logging**: Console logs focus conflicts in development mode - **Graceful fallbacks**: Safe operation outside tutorial context **Focus States:** - \`'none'\`: No tooltips have focus (initial state) - \`'term'\`: Term tooltips own focus (bead tooltips blocked) - \`'bead'\`: Bead tooltips own focus (term tooltips blocked) `, }, }, }, } export default meta type Story = StoryObj<typeof meta> export const TooltipGate: Story = { render: () => ( <TutorialUIProvider> <TooltipGateDemo /> </TutorialUIProvider> ), } export const MultipleProviders: Story = { render: () => ( <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '20px', padding: '20px', }} > <div style={{ border: '2px solid #3b82f6', borderRadius: '8px', padding: '16px', }} > <h3 style={{ color: '#3b82f6', marginTop: 0 }}>Tutorial A</h3> <TutorialUIProvider> <FocusStatus /> <DemoTooltip hintType="term" content="Term tooltip in Tutorial A"> Tutorial A - Term </DemoTooltip> <DemoTooltip hintType="bead" content="Bead tooltip in Tutorial A"> Tutorial A - Bead </DemoTooltip> </TutorialUIProvider> </div> <div style={{ border: '2px solid #10b981', borderRadius: '8px', padding: '16px', }} > <h3 style={{ color: '#10b981', marginTop: 0 }}>Tutorial B</h3> <TutorialUIProvider> <FocusStatus /> <DemoTooltip hintType="term" content="Term tooltip in Tutorial B"> Tutorial B - Term </DemoTooltip> <DemoTooltip hintType="bead" content="Bead tooltip in Tutorial B"> Tutorial B - Bead </DemoTooltip> </TutorialUIProvider> </div> </div> ), parameters: { docs: { description: { story: 'Each TutorialUIProvider creates an independent tooltip gate. Tooltips in different tutorials can be open simultaneously.', }, }, }, } export const FocusDebugger: Story = { render: () => { function DebugPanel() { const ui = useTutorialUI() const [logs, setLogs] = useState<string[]>([]) // Mock requesting focus programmatically const testRequest = (type: 'term' | 'bead') => { const granted = ui.requestFocus(type) const timestamp = new Date().toLocaleTimeString() setLogs((prev) => [ ...prev.slice(-10), `${timestamp}: Request ${type} โ ${granted ? 'GRANTED' : 'DENIED'}`, ]) } const testRelease = (type: 'term' | 'bead') => { ui.releaseFocus(type) const timestamp = new Date().toLocaleTimeString() setLogs((prev) => [...prev.slice(-10), `${timestamp}: Released ${type}`]) } return ( <div style={{ padding: '20px' }}> <h3>Focus Debugger</h3> <FocusStatus /> <div style={{ marginBottom: '20px' }}> <h4>Manual Focus Control</h4> <div style={{ display: 'flex', gap: '8px', flexWrap: 'wrap' }}> <button onClick={() => testRequest('term')} style={{ padding: '6px 12px', backgroundColor: '#3b82f6', color: 'white', border: 'none', borderRadius: '4px', }} > Request Term Focus </button> <button onClick={() => testRequest('bead')} style={{ padding: '6px 12px', backgroundColor: '#10b981', color: 'white', border: 'none', borderRadius: '4px', }} > Request Bead Focus </button> <button onClick={() => testRelease('term')} style={{ padding: '6px 12px', backgroundColor: '#6b7280', color: 'white', border: 'none', borderRadius: '4px', }} > Release Term </button> <button onClick={() => testRelease('bead')} style={{ padding: '6px 12px', backgroundColor: '#6b7280', color: 'white', border: 'none', borderRadius: '4px', }} > Release Bead </button> </div> </div> <div style={{ marginBottom: '20px' }}> <h4>Interactive Tooltips</h4> <DemoTooltip hintType="term" content="Debug term tooltip"> Term Tooltip </DemoTooltip> <DemoTooltip hintType="bead" content="Debug bead tooltip"> Bead Tooltip </DemoTooltip> </div> <div> <h4>Focus Event Log</h4> <div style={{ backgroundColor: '#1f2937', color: '#f9fafb', padding: '12px', borderRadius: '6px', fontFamily: 'monospace', fontSize: '12px', height: '150px', overflowY: 'auto', }} > {logs.length === 0 ? ( <em style={{ color: '#9ca3af' }}>No events yet...</em> ) : ( logs.map((log, i) => <div key={i}>{log}</div>) )} </div> </div> </div> ) } return ( <TutorialUIProvider> <DebugPanel /> </TutorialUIProvider> ) }, parameters: { docs: { description: { story: 'Debug panel for testing focus management programmatically. Use the buttons to manually request/release focus and observe the behavior.', }, }, }, } |