From f0cb48006f806f66a4f2b3927cd1333bb1e55d94 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Wed, 17 Apr 2024 19:41:34 +0530 Subject: [PATCH] [WEB-1024] fix: textarea component auto-resize (#4221) * chore: updated resize hook logic * fix: page title overflow issue * chore: add length validation to page title --- packages/ui/src/form-fields/textarea.tsx | 27 +++------ .../ui/src/hooks/use-auto-resize-textarea.ts | 24 ++++++++ web/components/pages/editor/editor-body.tsx | 2 +- web/components/pages/editor/title.tsx | 57 ++++++++++++++----- 4 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 packages/ui/src/hooks/use-auto-resize-textarea.ts diff --git a/packages/ui/src/form-fields/textarea.tsx b/packages/ui/src/form-fields/textarea.tsx index 271b76d83..de225d68f 100644 --- a/packages/ui/src/form-fields/textarea.tsx +++ b/packages/ui/src/form-fields/textarea.tsx @@ -1,6 +1,8 @@ -import * as React from "react"; +import React, { useRef } from "react"; // helpers import { cn } from "../../helpers"; +// hooks +import { useAutoResizeTextArea } from "../hooks/use-auto-resize-textarea"; export interface TextAreaProps extends React.TextareaHTMLAttributes { mode?: "primary" | "transparent"; @@ -8,21 +10,6 @@ export interface TextAreaProps extends React.TextareaHTMLAttributes when the value changes. -const useAutoSizeTextArea = (textAreaRef: HTMLTextAreaElement | null, value: any) => { - React.useEffect(() => { - if (textAreaRef) { - // We need to reset the height momentarily to get the correct scrollHeight for the textarea - textAreaRef.style.height = "0px"; - const scrollHeight = textAreaRef.scrollHeight; - - // We then set the height directly, outside of the render loop - // Trying to set this with state or a ref will product an incorrect value. - textAreaRef.style.height = scrollHeight + "px"; - } - }, [textAreaRef, value]); -}; - const TextArea = React.forwardRef((props, ref) => { const { id, @@ -35,10 +22,10 @@ const TextArea = React.forwardRef((props, re className = "", ...rest } = props; - - const textAreaRef = React.useRef(ref); - - useAutoSizeTextArea(textAreaRef?.current, value); + // refs + const textAreaRef = useRef(ref); + // auto re-size + useAutoResizeTextArea(textAreaRef); return (