π₯π₯π₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.
TypeScript boardsbug-trackerdjangodockerganttissue-trackerjirajira-alternativekanbanlinearpostgresqlproduct-managementproject-managementproject-planningpythonreactredistypesciptvitework-management
[WEB-1024] fix: textarea component auto-resize (#4221)
Aaryan Khandelwalf0cb48006f80
diff --git a/packages/ui/src/form-fields/textarea.tsx b/packages/ui/src/form-fields/textarea.tsxindex 271b76d83a..de225d68f3 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";// helpersimport { cn } from "../../helpers";+// hooks+import { useAutoResizeTextArea } from "../hooks/use-auto-resize-textarea";export interface TextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {mode?: "primary" | "transparent";@@ -8,21 +10,6 @@ export interface TextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaclassName?: string;}-// Updates the height of a <textarea> 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<HTMLTextAreaElement, TextAreaProps>((props, ref) => {const {id,@@ -35,10 +22,10 @@ const TextArea = React.forwardRef<HTMLTextAreaElement, TextAreaProps>((props, reclassName = "",...rest} = props;-- const textAreaRef = React.useRef<any>(ref);-- useAutoSizeTextArea(textAreaRef?.current, value);+ // refs+ const textAreaRef = useRef<any>(ref);+ // auto re-size+ useAutoResizeTextArea(textAreaRef);return (<textareadiff --git a/packages/ui/src/hooks/use-auto-resize-textarea.ts b/packages/ui/src/hooks/use-auto-resize-textarea.tsnew file mode 100644index 0000000000..a1b907a608--- /dev/null+++ b/packages/ui/src/hooks/use-auto-resize-textarea.ts@@ -0,0 +1,24 @@+import { useEffect } from "react";++export const useAutoResizeTextArea = (textAreaRef: React.RefObject<HTMLTextAreaElement>) => {+ useEffect(() => {+ const textArea = textAreaRef.current;+ if (!textArea) return;++ const resizeTextArea = () => {+ textArea.style.height = "auto";+ const computedHeight = textArea.scrollHeight + "px";+ textArea.style.height = computedHeight;+ };++ const handleInput = () => resizeTextArea();++ // resize on mount+ resizeTextArea();++ textArea.addEventListener("input", handleInput);+ return () => {+ textArea.removeEventListener("input", handleInput);+ };+ }, [textAreaRef]);+};diff --git a/web/components/pages/editor/editor-body.tsx b/web/components/pages/editor/editor-body.tsxindex 260b90b6fd..395f32ac8a 100644--- a/web/components/pages/editor/editor-body.tsx+++ b/web/components/pages/editor/editor-body.tsx@@ -109,7 +109,7 @@ export const PageEditorBody: React.FC<Props> = observer((props) => {})}><div className="h-full w-full flex flex-col gap-y-7 overflow-y-auto overflow-x-hidden">- <div className="w-full flex-shrink-0 ml-5">+ <div className="relative w-full flex-shrink-0 pl-5"><PageEditorTitleeditorRef={editorRef}title={pageTitle}diff --git a/web/components/pages/editor/title.tsx b/web/components/pages/editor/title.tsxindex 8b329e53a4..b5b06069fa 100644--- a/web/components/pages/editor/title.tsx+++ b/web/components/pages/editor/title.tsx@@ -1,8 +1,11 @@+import { useState } from "react";import { observer } from "mobx-react";// editorimport { EditorRefApi } from "@plane/document-editor";// uiimport { TextArea } from "@plane/ui";+// helpers+import { cn } from "@/helpers/common.helper";type Props = {editorRef: React.RefObject<EditorRefApi>;@@ -13,27 +16,51 @@ type Props = {export const PageEditorTitle: React.FC<Props> = observer((props) => {const { editorRef, readOnly, title, updateTitle } = props;+ // states+ const [isLengthVisible, setIsLengthVisible] = useState(false);return (<>{readOnly ? (<h6 className="-mt-2 break-words bg-transparent text-4xl font-bold">{title}</h6>) : (- <TextArea- onChange={(e) => updateTitle(e.target.value)}- className="-mt-2 w-full bg-custom-background text-4xl font-bold outline-none p-0 border-none resize-none rounded-none"- style={{- lineHeight: "1.2",- }}- placeholder="Untitled Page"- onKeyDown={(e) => {- if (e.key === "Enter") {- e.preventDefault();- editorRef.current?.setFocusAtPosition(0);- }- }}- value={title}- />+ <>+ <TextArea+ onChange={(e) => updateTitle(e.target.value)}+ className="-mt-2 w-full bg-custom-background text-4xl font-bold outline-none p-0 border-none resize-none rounded-none"+ style={{+ lineHeight: "1.2",+ }}+ placeholder="Untitled Page"+ onKeyDown={(e) => {+ if (e.key === "Enter") {+ e.preventDefault();+ editorRef.current?.setFocusAtPosition(0);+ }+ }}+ value={title}+ maxLength={255}+ onFocus={() => setIsLengthVisible(true)}+ onBlur={() => setIsLengthVisible(false)}+ />+ <div+ className={cn(+ "pointer-events-none absolute bottom-1 right-1 z-[2] rounded bg-custom-background-100 p-0.5 text-xs text-custom-text-200 opacity-0 transition-opacity",+ {+ "opacity-100": isLengthVisible,+ }+ )}+ >+ <span+ className={cn({+ "text-red-500": title.length === 0 || title.length > 255,+ })}+ >+ {title.length}+ </span>+ /255+ </div>+ </>)}</>);