4
4
Fragment ,
5
5
KeyboardEvent ,
6
6
useEffect ,
7
+ useRef ,
7
8
useState ,
8
9
} from "react" ;
9
10
import { twMerge } from "tailwind-merge" ;
@@ -24,6 +25,9 @@ const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
24
25
} ) => {
25
26
const [ input , setInput ] = useState ( "" ) ;
26
27
const [ inputFocus , setInputFocus ] = useState ( false ) ;
28
+ const [ shouldBlink , setShouldBlink ] = useState ( true ) ;
29
+ const shouldBlinkTimeoutRef = useRef < number > ( ) ;
30
+
27
31
const [ caretPosition , setCaretPosition ] = useState ( 0 ) ;
28
32
const [ currentHistoryIndex , setCurrentHistoryIndex ] = useState (
29
33
history . length
@@ -87,9 +91,17 @@ const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
87
91
onClick = { ( e ) => setCaretPosition ( e . currentTarget . selectionStart ?? 0 ) }
88
92
onKeyDown = { handleKeyDown }
89
93
onChange = { ( e ) => {
94
+ clearTimeout ( shouldBlinkTimeoutRef . current ) ;
95
+
90
96
setInput ( e . target . value ) ;
91
97
setCaretPosition ( e . target . selectionStart ?? 0 ) ;
92
98
setCurrentHistoryIndex ( history . length ) ;
99
+
100
+ setShouldBlink ( false ) ;
101
+ shouldBlinkTimeoutRef . current = setTimeout (
102
+ ( ) => setShouldBlink ( true ) ,
103
+ 500
104
+ ) ;
93
105
} }
94
106
className = "w-full cursor-default whitespace-nowrap bg-transparent p-0 text-kali-white text-transparent focus:outline-none"
95
107
/>
@@ -110,7 +122,10 @@ const TerminalPromptInput: FC<TerminalPromptInputProps> = ({
110
122
>
111
123
{ inputFocus && (
112
124
< span
113
- className = "content[''] absolute top-0 h-[20px] w-2 animate-blink bg-kali-gray/[.8]"
125
+ className = { twMerge (
126
+ "content[''] absolute top-0 h-[20px] w-2 bg-kali-gray/[.8]" ,
127
+ shouldBlink && "animate-blink"
128
+ ) }
114
129
style = { { left : `${ input . length === 0 ? 0 : caretPosition } ch` } }
115
130
/>
116
131
) }
0 commit comments