@@ -72,6 +72,7 @@ export default function ChatWindow({
7272 const ref = useRef < HTMLDivElement > ( null ) ;
7373 const lastMessage = useRef < HTMLDivElement > ( null ) ;
7474 const [ windowPosition , setWindowPosition ] = useState ( { left : "0" , top : "0" } ) ;
75+ const inputRef = useRef < HTMLInputElement > ( null ) ; /* User input Ref */
7576 useEffect ( ( ) => {
7677 if ( triggerRef )
7778 setWindowPosition (
@@ -83,6 +84,17 @@ export default function ChatWindow({
8384 )
8485 ) ;
8586 } , [ triggerRef , width , height , position ] ) ;
87+
88+ /* Initial listener for loss of focus that refocuses User input after a small delay */
89+
90+ const handleBlur = ( ) => {
91+ setTimeout ( ( ) => {
92+ inputRef . current ?. focus ( ) ;
93+ } , 50 ) ;
94+ } ;
95+ const inputElem = inputRef . current ;
96+ inputElem ?. addEventListener ( 'blur' , handleBlur ) ;
97+
8698 const [ sendingMessage , setSendingMessage ] = useState ( false ) ;
8799
88100 function handleClick ( ) {
@@ -155,6 +167,26 @@ export default function ChatWindow({
155167 lastMessage . current . scrollIntoView ( { behavior : "smooth" } ) ;
156168 } , [ messages ] ) ;
157169
170+ /* Refocus the User input whenever a new response is returned from the LLM */
171+
172+ useEffect ( ( ) => {
173+ const handleBlur = ( ) => {
174+ // after a slight delay
175+ setTimeout ( ( ) => {
176+ inputRef . current ?. focus ( ) ;
177+ } , 100 ) ;
178+ } ;
179+ const inputElem = inputRef . current ;
180+ inputElem ?. addEventListener ( 'blur' , handleBlur ) ;
181+ inputRef . current ?. focus ( ) ;
182+
183+ // Clean up the listener when the component is unmounted
184+
185+ return ( ) => {
186+ inputElem ?. removeEventListener ( 'blur' , handleBlur ) ;
187+ } ;
188+ } , [ messages ] ) ;
189+
158190 return (
159191 < div
160192 className = {
@@ -210,6 +242,7 @@ export default function ChatWindow({
210242 disabled = { sendingMessage }
211243 placeholder = { sendingMessage ? ( placeholder_sending || "Thinking..." ) : ( placeholder || "Type your message..." ) }
212244 style = { input_style }
245+ ref = { inputRef }
213246 className = "cl-input-element"
214247 />
215248 < button
0 commit comments