Skip to content

Commit

Permalink
feat: textarea with adaptive height
Browse files Browse the repository at this point in the history
  • Loading branch information
leedom92 committed Apr 7, 2023
1 parent a8a8bec commit 3656c84
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 108 deletions.
104 changes: 0 additions & 104 deletions app/calcTextareaHeight.js

This file was deleted.

24 changes: 23 additions & 1 deletion app/components/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import chatStyle from "./chat.module.scss";

import { Input, Modal, showModal, showToast } from "./ui-lib";

import calcTextareaHeight from "../calcTextareaHeight";

const Markdown = dynamic(
async () => memo((await import("./markdown")).Markdown),
{
Expand Down Expand Up @@ -331,6 +333,10 @@ function useScrollToBottom() {
export function Chat(props: {
showSideBar?: () => void;
sideBarShowing?: boolean;
autoSize: {
minRows: number;
maxRows?: number;
};
}) {
type RenderMessage = Message & { preview?: boolean };

Expand All @@ -347,6 +353,7 @@ export function Chat(props: {
const { submitKey, shouldSubmit } = useSubmitHandler();
const { scrollRef, setAutoScroll } = useScrollToBottom();
const [hitBottom, setHitBottom] = useState(false);
const [textareaStyle, setTextareaStyle] = useState({});

const onChatBodyScroll = (e: HTMLElement) => {
const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 20;
Expand Down Expand Up @@ -380,6 +387,16 @@ export function Chat(props: {
dom.scrollTop = dom.scrollHeight - dom.offsetHeight + paddingBottomNum;
};

// textarea has an adaptive height
const resizeTextarea = () => {
const dom = inputRef.current;
if (!dom) return;
const { minRows, maxRows } = props.autoSize;
setTimeout(() => {
setTextareaStyle(calcTextareaHeight(dom, minRows, maxRows));
}, 50);
};

// only search prompts when user input is short
const SEARCH_TEXT_LIMIT = 30;
const onInput = (text: string) => {
Expand Down Expand Up @@ -504,6 +521,11 @@ export function Chat(props: {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// Textarea Adaptive height
useEffect(() => {
resizeTextarea();
});

return (
<div className={styles.chat} key={session.id}>
<div className={styles["window-header"]}>
Expand Down Expand Up @@ -659,8 +681,8 @@ export function Chat(props: {
<textarea
ref={inputRef}
className={styles["chat-input"]}
style={textareaStyle}
placeholder={Locale.Chat.Input(submitKey)}
rows={2}
onInput={(e) => onInput(e.currentTarget.value)}
value={userInput}
onKeyDown={onInputKeyDown}
Expand Down
2 changes: 0 additions & 2 deletions app/components/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import dynamic from "next/dynamic";
import { REPO_URL } from "../constant";
import { ErrorBoundary } from "./error";

import calcTextareaHeight from "../calcTextareaHeight";

export function Loading(props: { noLogo?: boolean }) {
return (
<div className={styles["loading-content"]}>
Expand Down
4 changes: 4 additions & 0 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ export function isMobileScreen() {
return window.innerWidth <= 600;
}

export function isFirefox() {
return /firefox/i.test(navigator.userAgent);
}

export function selectOrCopy(el: HTMLElement, content: string) {
const currentSelection = window.getSelection();

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "app/calcTextareaHeight.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 3656c84

Please sign in to comment.