|
| 1 | +import * as React from 'react'; |
| 2 | +import {keyframes, rule} from 'nano-theme'; |
| 3 | + |
| 4 | +const scoreAnimation = keyframes({ |
| 5 | + from: { |
| 6 | + op: 0.8, |
| 7 | + tr: 'scale(1.2)', |
| 8 | + }, |
| 9 | + to: { |
| 10 | + op: 0, |
| 11 | + tr: 'scale(.7)', |
| 12 | + vis: 'hidden', |
| 13 | + }, |
| 14 | +}); |
| 15 | + |
| 16 | +const shakingAnimation = keyframes({ |
| 17 | + '0%': {tr: 'translateX(0), scale(1.2)', op: 1}, |
| 18 | + '10%': {tr: 'translateX(-2px)'}, |
| 19 | + '20%': {tr: 'translateX(2px)'}, |
| 20 | + '30%': {tr: 'translateX(-1px)'}, |
| 21 | + '40%': {tr: 'translateX(1px)'}, |
| 22 | + '50%': {tr: 'translateX(0), scale(1)'}, |
| 23 | + '100%': {op: 0, vis: 'hidden'}, |
| 24 | +}); |
| 25 | + |
| 26 | +const scoreClass = rule({ |
| 27 | + ff: 'Inter, ui-sans-serif, system-ui, -apple-system, "system-ui", "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif', |
| 28 | + pos: 'absolute', |
| 29 | + b: '0.9em', |
| 30 | + l: '.75em', |
| 31 | + fz: '.4em', |
| 32 | + op: 0.5, |
| 33 | + an: scoreAnimation + ' .5s ease-out forwards', |
| 34 | + ws: 'nowrap', |
| 35 | + pe: 'none', |
| 36 | + us: 'none', |
| 37 | +}); |
| 38 | + |
| 39 | +const scoreDeltaClass = rule({ |
| 40 | + pos: 'absolute', |
| 41 | + b: '1.3em', |
| 42 | + l: '1.2em', |
| 43 | + fz: '.5em', |
| 44 | + fw: 'bold', |
| 45 | + op: 0.5, |
| 46 | + col: '#07f', |
| 47 | + an: scoreAnimation + ' .3s ease-out forwards', |
| 48 | + pe: 'none', |
| 49 | + us: 'none', |
| 50 | +}); |
| 51 | + |
| 52 | +export interface CaretScoreProps { |
| 53 | + score: number; |
| 54 | + delta: number; |
| 55 | + onRender?: () => void; |
| 56 | +} |
| 57 | + |
| 58 | +export const CaretScore: React.FC<CaretScoreProps> = React.memo(({score, delta, onRender}) => { |
| 59 | + // biome-ignore lint: lint/correctness/useExhaustiveDependencies |
| 60 | + React.useEffect(() => { |
| 61 | + onRender?.(); |
| 62 | + }, []); |
| 63 | + |
| 64 | + const scoreMsg = |
| 65 | + score > 100 && score <= 120 |
| 66 | + ? 'Typing Spree!' |
| 67 | + : score > 200 && score <= 208 |
| 68 | + ? 'Go, go, go!' |
| 69 | + : score > 300 && score <= 320 |
| 70 | + ? 'Rampage!' |
| 71 | + : score > 400 && score <= 408 |
| 72 | + ? "Let's go!" |
| 73 | + : score > 500 && score <= 520 |
| 74 | + ? 'Unstoppable!' |
| 75 | + : score > 600 && score <= 608 |
| 76 | + ? 'Good stuff!' |
| 77 | + : score > 700 && score <= 708 |
| 78 | + ? 'Alright, alright!' |
| 79 | + : score > 1000 && score <= 1030 |
| 80 | + ? 'Godlike!' |
| 81 | + : score > 1500 && score <= 1530 |
| 82 | + ? 'Bingo, bango, bongo!' |
| 83 | + : score > 2000 && score <= 2030 |
| 84 | + ? 'Legendary!' |
| 85 | + : score > 3000 && score <= 3040 |
| 86 | + ? 'Beyond Godlike!' |
| 87 | + : score > 5000 && score <= 5040 |
| 88 | + ? 'Wicked Sick!' |
| 89 | + : score > 10000 && score <= 10050 |
| 90 | + ? 'Monster Type!' |
| 91 | + : score > 20000 && score <= 20050 |
| 92 | + ? 'Ultra Type!' |
| 93 | + : score > 50000 && score <= 50100 |
| 94 | + ? 'M-M-M-Monster Type!' |
| 95 | + : score; |
| 96 | + |
| 97 | + return ( |
| 98 | + <> |
| 99 | + {score >= 24 && ( |
| 100 | + <span |
| 101 | + contentEditable={false} |
| 102 | + className={scoreClass} |
| 103 | + style={{animation: typeof scoreMsg === 'string' ? shakingAnimation + ' .7s ease-out forwards' : undefined}} |
| 104 | + > |
| 105 | + {scoreMsg} |
| 106 | + </span> |
| 107 | + )} |
| 108 | + {(typeof scoreMsg === 'string' || (score > 42 && delta > 1)) && ( |
| 109 | + <span contentEditable={false} className={scoreDeltaClass}> |
| 110 | + +{delta} |
| 111 | + </span> |
| 112 | + )} |
| 113 | + </> |
| 114 | + ); |
| 115 | +}); |
0 commit comments