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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | 'use client' import NumberFlow from '@number-flow/react' import { useSpring } from '@react-spring/web' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { css } from '../../styled-system/css' import { TypstSoroban } from './TypstSoroban' interface InteractiveAbacusProps { initialValue?: number columns?: number className?: string onValueChange?: (value: number) => void showValue?: boolean showControls?: boolean showManualInput?: boolean compact?: boolean } export function InteractiveAbacus({ initialValue = 0, columns = 3, className, onValueChange, showValue = true, showControls = true, showManualInput = false, compact = false, }: InteractiveAbacusProps) { const [currentValue, setCurrentValue] = useState(initialValue) const [isChanging, setIsChanging] = useState(false) const [_previousValue, setPreviousValue] = useState(initialValue) const [isEditing, setIsEditing] = useState(false) const [editingValue, setEditingValue] = useState('') const [disableAnimation, setDisableAnimation] = useState(false) const svgRef = useRef<HTMLDivElement>(null) const numberRef = useRef<HTMLDivElement>(null) // Remove the old spring animation since we're using NumberFlow now // Container animation for feedback const containerSpring = useSpring({ scale: isChanging ? 1.02 : 1, borderColor: isChanging ? '#fbbf24' : '#d97706', // amber-400 vs amber-600 config: { tension: 400, friction: 25 }, }) // Crossfade animation between old and new SVG states const _crossfadeSpring = useSpring({ opacity: isChanging ? 0.7 : 1, transform: isChanging ? 'scale(0.98)' : 'scale(1)', config: { tension: 300, friction: 30 }, }) // Handle bead clicks to toggle values const handleBeadClick = useCallback( (event: Event) => { const target = event.target as Element // Find the closest element with bead data attributes const beadElement = target.closest('[data-bead-type]') if (!beadElement) return const beadType = beadElement.getAttribute('data-bead-type') const beadColumn = parseInt(beadElement.getAttribute('data-bead-column') || '0', 10) const beadPosition = beadElement.getAttribute('data-bead-position') const isActive = beadElement.getAttribute('data-bead-active') === '1' if (beadType === 'heaven') { // Toggle heaven bead (worth 5) // Now using place-value based column numbering: 0=ones, 1=tens, 2=hundreds const placeValue = beadColumn const columnPower = 10 ** placeValue const heavenValue = 5 * columnPower const maxValue = 10 ** columns - 1 if (isActive) { // Deactivate heaven bead - subtract 5 from this column setCurrentValue((prev) => Math.max(0, prev - heavenValue)) } else { // Activate heaven bead - add 5 to this column setCurrentValue((prev) => Math.min(prev + heavenValue, maxValue)) } } else if (beadType === 'earth' && beadPosition) { // Toggle earth bead (worth 1 each) const position = parseInt(beadPosition, 10) // 0-3 where 0 is top (closest to bar), 3 is bottom // Now using place-value based column numbering: 0=ones, 1=tens, 2=hundreds const placeValue = beadColumn const columnPower = 10 ** placeValue // Calculate current digit in this column const currentDigit = Math.floor(currentValue / columnPower) % 10 const heavenContribution = Math.floor(currentDigit / 5) * 5 const earthContribution = currentDigit % 5 let newEarthContribution: number // Earth beads are numbered 0-3 from top to bottom (0 is closest to bar) // In traditional abacus logic: // - earthContribution represents how many beads are active (0-4) // - Active beads are positions 0, 1, 2, ... up to (earthContribution - 1) // - When you click a bead: toggle that "level" of activation if (isActive) { // This bead is currently active, so we deactivate it and all beads below it // If position 2 is clicked and active, we want positions 0,1 to remain active // So earthContribution should be position (2) newEarthContribution = position } else { // This bead is currently inactive, so we activate it and all beads above it // If position 2 is clicked and inactive, we want positions 0,1,2 to be active // So earthContribution should be position + 1 (3) newEarthContribution = position + 1 } // Calculate the new digit for this column const newDigit = heavenContribution + newEarthContribution // Calculate the new total value const columnContribution = (Math.floor(currentValue / columnPower) % 10) * columnPower const newValue = currentValue - columnContribution + newDigit * columnPower // Ensure value doesn't exceed maximum for this number of columns const maxValue = 10 ** columns - 1 setCurrentValue(Math.max(0, Math.min(newValue, maxValue))) } // Visual feedback with extended timing for smoother transition setIsChanging(true) // Update previous value for crossfade effect setPreviousValue(currentValue) // Extended timing to allow for smoother crossfade setTimeout(() => setIsChanging(false), 300) }, [currentValue, columns] ) // Add click event listener for bead interactions useEffect(() => { const svgContainer = svgRef.current if (!svgContainer) return svgContainer.addEventListener('click', handleBeadClick) return () => { svgContainer.removeEventListener('click', handleBeadClick) } }, [handleBeadClick]) // Notify parent of value changes useMemo(() => { onValueChange?.(currentValue) }, [currentValue, onValueChange]) const handleReset = useCallback(() => { setIsChanging(true) setTimeout(() => setIsChanging(false), 150) setCurrentValue(0) }, []) const handleSetValue = useCallback((value: number) => { setIsChanging(true) setTimeout(() => setIsChanging(false), 150) setCurrentValue(value) }, []) const handleKeyDown = useCallback( (event: React.KeyboardEvent) => { if (!showManualInput) return // Handle number keys and editing if (event.key >= '0' && event.key <= '9') { event.preventDefault() let newEditingValue: string if (!isEditing) { setIsEditing(true) newEditingValue = event.key setEditingValue(newEditingValue) } else { newEditingValue = editingValue + event.key const numValue = parseInt(newEditingValue, 10) const maxValue = 10 ** columns - 1 if (numValue <= maxValue) { setEditingValue(newEditingValue) } else { return // Don't update if exceeds max } } // Disable animation for keyboard input changes setDisableAnimation(true) // Update abacus immediately const liveValue = parseInt(newEditingValue, 10) || 0 setCurrentValue(liveValue) // Re-enable animation after a brief delay setTimeout(() => setDisableAnimation(false), 100) } else if (event.key === 'Backspace') { event.preventDefault() if (isEditing) { let newEditingValue: string if (editingValue.length > 1) { newEditingValue = editingValue.slice(0, -1) } else { newEditingValue = '0' } setEditingValue(newEditingValue) // Disable animation for potentially jarring backspace changes setDisableAnimation(true) const liveValue = parseInt(newEditingValue, 10) || 0 setCurrentValue(liveValue) // Re-enable animation after a brief delay setTimeout(() => setDisableAnimation(false), 100) } } else if (event.key === 'Enter' || event.key === 'Escape') { event.preventDefault() if (isEditing) { setIsEditing(false) setEditingValue('') // Value is already set from live updates } } else if (event.key === 'Delete') { event.preventDefault() setEditingValue('0') setIsEditing(true) // Disable animation for potentially jarring delete changes setDisableAnimation(true) // Update abacus immediately setCurrentValue(0) // Re-enable animation after a brief delay setTimeout(() => setDisableAnimation(false), 100) } }, [showManualInput, isEditing, editingValue, columns] ) const handleNumberClick = useCallback(() => { if (showManualInput && numberRef.current) { numberRef.current.focus() if (!isEditing) { setIsEditing(true) setEditingValue(String(currentValue)) } } }, [showManualInput, isEditing, currentValue]) const handleNumberBlur = useCallback(() => { if (isEditing) { setIsEditing(false) setEditingValue('') // Value is already live-updated, no need to set again } }, [isEditing]) return ( <div className={className}> <div className={css({ display: 'flex', flexDirection: compact ? 'row' : 'column', gap: compact ? '4' : '6', alignItems: 'center', })} > {/* Interactive Abacus Container */} <div className={css({ position: 'relative', display: 'flex', flexDirection: 'column', alignItems: 'center', })} > {/* Abacus with integrated value display */} <div ref={svgRef} style={containerSpring as any} className={css({ width: compact ? '240px' : '300px', height: compact ? '320px' : '400px', border: '3px solid', borderRadius: '12px', bg: 'gradient-to-br', gradientFrom: 'amber.50', gradientTo: 'orange.100', padding: compact ? '16px' : '20px', boxShadow: '0 8px 24px rgba(0, 0, 0, 0.1)', position: 'relative', transition: 'all 0.2s ease', _hover: { boxShadow: '0 12px 32px rgba(0, 0, 0, 0.15)', }, })} > <div className={css({ width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', overflow: 'hidden', })} > <div className={css({ transform: 'scale(2.2)', transformOrigin: 'center', })} > <TypstSoroban number={currentValue} width={compact ? '144pt' : '180pt'} height={compact ? '192pt' : '240pt'} className={css({ transition: 'all 0.3s ease', cursor: 'pointer', '& [data-bead-type]': { cursor: 'pointer', transition: 'all 0.2s ease', _hover: { filter: 'brightness(1.2)', transform: 'scale(1.05)', }, }, })} /> </div> </div> </div> {/* Column-based Value Display */} {showValue && ( <div className={css({ position: 'absolute', bottom: compact ? '-45px' : '-55px', left: '50%', transform: 'translateX(-50%)', display: 'flex', alignItems: 'center', gap: '0', width: compact ? '240px' : '300px', justifyContent: 'center', })} > {/* Position digits above their respective columns */} {Array.from({ length: columns }, (_, colIndex) => { const placeValue = columns - 1 - colIndex // rightmost is 0 (ones), leftmost is highest const digit = Math.floor( (isEditing ? parseInt(editingValue, 10) || 0 : currentValue) / 10 ** placeValue ) % 10 const columnWidth = compact ? 240 / columns : 300 / columns return ( <div key={colIndex} className={css({ position: 'relative', width: `${columnWidth}px`, display: 'flex', justifyContent: 'center', alignItems: 'center', })} > <div ref={colIndex === 0 && showManualInput ? numberRef : undefined} tabIndex={showManualInput && colIndex === 0 ? 0 : -1} onClick={showManualInput ? handleNumberClick : undefined} onKeyDown={showManualInput && colIndex === 0 ? handleKeyDown : undefined} onBlur={showManualInput && colIndex === 0 ? handleNumberBlur : undefined} className={css({ bg: 'white', border: '2px solid', borderColor: 'blue.200', rounded: 'xl', boxShadow: '0 4px 12px rgba(59, 130, 246, 0.15)', px: '2', py: '1', minW: compact ? '32px' : '40px', position: 'relative', outline: 'none', cursor: showManualInput ? 'pointer' : 'default', display: 'flex', alignItems: 'center', justifyContent: 'center', })} title={ showManualInput && colIndex === 0 ? 'Click to edit, type numbers, Enter to confirm' : undefined } > <NumberFlow value={digit} animated={!disableAnimation && !isEditing} style={{ fontSize: compact ? '1.25rem' : '1.5rem', fontWeight: 'bold', color: isEditing ? '#1d4ed8' : '#2563eb', textAlign: 'center', minWidth: compact ? '20px' : '24px', }} /> {/* Visual editing indicator - only show on first column when editing */} {isEditing && colIndex === 0 && ( <div className={css({ position: 'absolute', bottom: '-2px', left: '50%', transform: 'translateX(-50%)', width: '80%', height: '2px', bg: 'blue.400', rounded: 'full', animation: 'pulse 1s infinite', })} /> )} </div> </div> ) })} </div> )} </div> {/* Compact Controls */} {showControls && ( <div className={css({ display: 'flex', flexDirection: compact ? 'column' : 'row', alignItems: 'center', gap: '2', flexWrap: compact ? 'nowrap' : 'wrap', justifyContent: 'center', mt: showValue ? (compact ? '0' : '16') : '0', })} > <button onClick={handleReset} className={css({ px: '3', py: '2', bg: 'gray.100', color: 'gray.700', border: '1px solid', borderColor: 'gray.300', rounded: 'md', fontSize: 'xs', fontWeight: 'medium', cursor: 'pointer', transition: 'all 0.2s ease', minW: compact ? '60px' : 'auto', _hover: { bg: 'gray.200', borderColor: 'gray.400', transform: 'translateY(-1px)', }, _active: { transform: 'scale(0.95)', }, })} > Clear </button> {/* Compact preset buttons */} <div className={css({ display: 'flex', flexDirection: compact ? 'column' : 'row', gap: '1', flexWrap: 'wrap', })} > {(compact ? [1, 5, 10, 25] : [1, 5, 10, 25, 50, 99]).map((preset) => ( <button key={preset} onClick={() => handleSetValue(preset)} className={css({ px: '2', py: '1', bg: 'blue.100', color: 'blue.700', border: '1px solid', borderColor: 'blue.300', rounded: 'md', fontSize: 'xs', fontWeight: 'medium', cursor: 'pointer', transition: 'all 0.2s ease', minW: compact ? '40px' : '32px', _hover: { bg: 'blue.200', borderColor: 'blue.400', transform: 'translateY(-1px)', }, _active: { transform: 'scale(0.95)', }, })} > {preset} </button> ))} </div> </div> )} {/* Compact Instructions */} {!compact && ( <div className={css({ fontSize: 'sm', color: 'gray.600', textAlign: 'center', maxW: '450px', lineHeight: 'relaxed', bg: 'gray.50', px: '4', py: '3', rounded: 'lg', border: '1px solid', borderColor: 'gray.200', mt: showValue ? '16' : '0', })} > <strong>How to use:</strong> Click on the beads to activate or deactivate them! Heaven beads (top) are worth 5 each, earth beads (bottom) are worth 1 each. {showManualInput && ' You can also click the number to type directly.'} </div> )} </div> </div> ) } |