Skip to content

Commit

Permalink
Merge pull request langflow-ai#8 from ZenHunter9457/user_input_focus
Browse files Browse the repository at this point in the history
Add Ref and event listener to keep focus on User input
  • Loading branch information
anovazzi1 committed Nov 1, 2023
2 parents bfcc38d + eaaa3a9 commit 3c1d2b0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/build/static/js/bundle.min.js

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/chatWidget/chatWindow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default function ChatWindow({
const ref = useRef<HTMLDivElement>(null);
const lastMessage = useRef<HTMLDivElement>(null);
const [windowPosition, setWindowPosition] = useState({ left: "0", top: "0" });
const inputRef = useRef<HTMLInputElement>(null); /* User input Ref */
useEffect(() => {
if (triggerRef)
setWindowPosition(
Expand All @@ -83,6 +84,17 @@ export default function ChatWindow({
)
);
}, [triggerRef, width, height, position]);

/* Initial listener for loss of focus that refocuses User input after a small delay */

const handleBlur = () => {
setTimeout(() => {
inputRef.current?.focus();
}, 50);
};
const inputElem = inputRef.current;
inputElem?.addEventListener('blur', handleBlur);

const [sendingMessage, setSendingMessage] = useState(false);

function handleClick() {
Expand Down Expand Up @@ -155,6 +167,26 @@ export default function ChatWindow({
lastMessage.current.scrollIntoView({ behavior: "smooth" });
}, [messages]);

/* Refocus the User input whenever a new response is returned from the LLM */

useEffect(() => {
const handleBlur = () => {
// after a slight delay
setTimeout(() => {
inputRef.current?.focus();
}, 100);
};
const inputElem = inputRef.current;
inputElem?.addEventListener('blur', handleBlur);
inputRef.current?.focus();

// Clean up the listener when the component is unmounted

return () => {
inputElem?.removeEventListener('blur', handleBlur);
};
}, [messages]);

return (
<div
className={
Expand Down Expand Up @@ -210,6 +242,7 @@ export default function ChatWindow({
disabled={sendingMessage}
placeholder={sendingMessage ? (placeholder_sending || "Thinking...") : (placeholder || "Type your message...")}
style={input_style}
ref={inputRef}
className="cl-input-element"
/>
<button
Expand Down

0 comments on commit 3c1d2b0

Please sign in to comment.