|
| 1 | +'use client' |
| 2 | +import React, { useEffect, useState } from 'react' |
| 3 | + |
| 4 | +export function TerminalAnimation({ |
| 5 | + text, |
| 6 | + logs, |
| 7 | + spinner = 'ora', |
| 8 | +}: { |
| 9 | + text: string |
| 10 | + logs: string |
| 11 | + spinner?: 'ora' | 'line' | 'dots' | 'blocks' |
| 12 | +}) { |
| 13 | + const command = text |
| 14 | + |
| 15 | + const [typedLength, setTypedLength] = useState(0) |
| 16 | + const [phase, setPhase] = useState< |
| 17 | + 'typing' | 'waitingEnter' | 'spinning' | 'showingLogs' |
| 18 | + >('typing') |
| 19 | + const [reveal, setReveal] = useState(false) |
| 20 | + const [revealedCount, setRevealedCount] = useState(0) |
| 21 | + const spinnerFramesMap: Record<string, string[]> = { |
| 22 | + ora: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'], |
| 23 | + line: ['|', '/', '-', '\\'], |
| 24 | + dots: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'], |
| 25 | + blocks: ['▖', '▘', '▝', '▗'], |
| 26 | + } |
| 27 | + const spinFrames = spinnerFramesMap[spinner] ?? spinnerFramesMap.ora |
| 28 | + const [spinIndex, setSpinIndex] = useState(0) |
| 29 | + |
| 30 | + const resetAnimation = () => { |
| 31 | + setTypedLength(0) |
| 32 | + setPhase('typing') |
| 33 | + setReveal(false) |
| 34 | + setRevealedCount(0) |
| 35 | + setSpinIndex(0) |
| 36 | + } |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + if (phase !== 'typing') return |
| 40 | + const id = setInterval(() => { |
| 41 | + setTypedLength((n) => { |
| 42 | + const next = n + 1 |
| 43 | + if (next >= command.length) { |
| 44 | + clearInterval(id) |
| 45 | + setPhase('waitingEnter') |
| 46 | + return command.length |
| 47 | + } |
| 48 | + return next |
| 49 | + }) |
| 50 | + }, 25) |
| 51 | + return () => clearInterval(id) |
| 52 | + }, [phase, command.length]) |
| 53 | + |
| 54 | + useEffect(() => { |
| 55 | + if (phase === 'waitingEnter') { |
| 56 | + const id = setTimeout(() => setPhase('spinning'), 500) |
| 57 | + return () => clearTimeout(id) |
| 58 | + } |
| 59 | + if (phase === 'spinning') { |
| 60 | + setRevealedCount(0) |
| 61 | + const timeoutId = setTimeout(() => setPhase('showingLogs'), 1200) |
| 62 | + const spinId = setInterval(() => { |
| 63 | + setSpinIndex((i) => (i + 1) % spinFrames.length) |
| 64 | + }, 80) |
| 65 | + return () => { |
| 66 | + clearTimeout(timeoutId) |
| 67 | + clearInterval(spinId) |
| 68 | + } |
| 69 | + } |
| 70 | + if (phase === 'showingLogs') { |
| 71 | + const logLines = logs.split('\n') |
| 72 | + const timer = setInterval(() => { |
| 73 | + setRevealedCount((n) => { |
| 74 | + const next = n + 1 |
| 75 | + if (next >= logLines.length) { |
| 76 | + clearInterval(timer) |
| 77 | + setReveal(true) |
| 78 | + return logLines.length |
| 79 | + } |
| 80 | + return next |
| 81 | + }) |
| 82 | + }, 180) |
| 83 | + return () => clearInterval(timer) |
| 84 | + } |
| 85 | + }, [phase, logs, spinFrames.length]) |
| 86 | + |
| 87 | + return ( |
| 88 | + <div> |
| 89 | + <Prompt caret> |
| 90 | + {command.slice(0, typedLength)} |
| 91 | + {phase === 'waitingEnter' && ( |
| 92 | + <span className="ml-2 text-xs text-black/50">⏎</span> |
| 93 | + )} |
| 94 | + </Prompt> |
| 95 | + <div |
| 96 | + className={`transition-all duration-300 ease-out ${ |
| 97 | + reveal |
| 98 | + ? 'opacity-100 translate-y-0 scale-100' |
| 99 | + : 'opacity-100 translate-y-0 scale-100' |
| 100 | + }`} |
| 101 | + onDoubleClick={resetAnimation} |
| 102 | + > |
| 103 | + <CodeBlock> |
| 104 | + {logs.split('\n').map((line, idx) => { |
| 105 | + const isVisible = |
| 106 | + phase === 'spinning' |
| 107 | + ? idx === 0 |
| 108 | + : idx < |
| 109 | + (phase === 'showingLogs' ? Math.max(1, revealedCount) : 0) |
| 110 | + const content = |
| 111 | + phase === 'spinning' && idx === 0 |
| 112 | + ? `Building ${spinFrames[spinIndex]}` |
| 113 | + : line |
| 114 | + return ( |
| 115 | + <div key={idx} className={isVisible ? '' : 'opacity-0'}> |
| 116 | + {content} |
| 117 | + </div> |
| 118 | + ) |
| 119 | + })} |
| 120 | + </CodeBlock> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +function Prompt({ |
| 127 | + children, |
| 128 | + className = '', |
| 129 | + caret = false, |
| 130 | +}: { |
| 131 | + children: React.ReactNode |
| 132 | + className?: string |
| 133 | + caret?: boolean |
| 134 | +}) { |
| 135 | + return ( |
| 136 | + <div className={`flex text-sm ${className}`}> |
| 137 | + <span className="text-black/40 mr-2">{`➜`}</span> |
| 138 | + <span className="text-[#000]">~/project</span> |
| 139 | + <span className="ml-2 text-black">$</span> |
| 140 | + <span className="ml-2 inline-flex items-center"> |
| 141 | + <span className="text-black/60">{children}</span> |
| 142 | + {caret && ( |
| 143 | + <span className="ml-1 inline-block h-4 w-2 translate-y-[1px] bg-[#000]/70 align-middle caret" /> |
| 144 | + )} |
| 145 | + </span> |
| 146 | + </div> |
| 147 | + ) |
| 148 | +} |
| 149 | + |
| 150 | +function CodeBlock({ children }: { children: React.ReactNode }) { |
| 151 | + return ( |
| 152 | + <pre className="mt-2 w-full block rounded-md bg-[#f5e6d4] text-[12px] leading-relaxed text-black/80"> |
| 153 | + <code className="px-3 py-2 block w-full select-none">{children}</code> |
| 154 | + </pre> |
| 155 | + ) |
| 156 | +} |
0 commit comments