Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto focus chat input #2998

Merged
merged 1 commit into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions website/src/components/Chat/ChatForm.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Box, CircularProgress, Flex, Textarea } from "@chakra-ui/react";
import { Box, CircularProgress, Flex, Textarea, useBreakpointValue } from "@chakra-ui/react";
import { Send } from "lucide-react";
import { useTranslation } from "next-i18next";
import { forwardRef, KeyboardEvent, SyntheticEvent, useCallback } from "react";
import { forwardRef, KeyboardEvent, SyntheticEvent, useCallback, useEffect } from "react";
import TextareaAutosize from "react-textarea-autosize";
import { useFallbackRef } from "src/hooks/ui/useFallbackRef";
import { QueueInfo } from "src/lib/chat_stream";

import { ChatConfigDrawer } from "./ChatConfigMobile";
Expand All @@ -15,7 +16,7 @@ type ChatFormProps = {
};

// eslint-disable-next-line react/display-name
export const ChatForm = forwardRef<HTMLTextAreaElement, ChatFormProps>((props, ref) => {
export const ChatForm = forwardRef<HTMLTextAreaElement, ChatFormProps>((props, forwardedRef) => {
const { isSending, onSubmit: onSubmit, queueInfo } = props;
const { t } = useTranslation("chat");
const handleSubmit = useCallback(
Expand All @@ -34,6 +35,16 @@ export const ChatForm = forwardRef<HTMLTextAreaElement, ChatFormProps>((props, r
},
[onSubmit]
);

const ref = useFallbackRef(forwardedRef);
const isDeskTop = useBreakpointValue({ base: false, md: true });

useEffect(() => {
if (isDeskTop) {
ref.current?.focus();
}
}, [isDeskTop, ref]);

return (
<Box as="form" maxWidth={{ base: "3xl", "2xl": "4xl" }} onSubmit={handleSubmit} className="py-2 w-full mx-auto">
<div className="relative">
Expand Down
7 changes: 7 additions & 0 deletions website/src/hooks/ui/useFallbackRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ForwardedRef, RefObject, useRef } from "react";

export const useFallbackRef = <T>(maybeRef?: ForwardedRef<T>) => {
const ref = useRef(null);

return (maybeRef || ref) as RefObject<T>;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand what this does?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AbdBarho you can read more about it here https://mtsknn.fi/blog/react-fallback-ref