bb-plane-fork/packages/ui/src/hooks/use-auto-resize-textarea.ts
Aaryan Khandelwal ad27184a91
[WEB-1072] fix: pages UI improvements (#4294)
* fix: outline alignment

* fix: textarea auto-resize logic
2024-04-26 18:29:18 +05:30

16 lines
541 B
TypeScript

import { useLayoutEffect } from "react";
export const useAutoResizeTextArea = (
textAreaRef: React.RefObject<HTMLTextAreaElement>,
value: string | number | readonly string[]
) => {
useLayoutEffect(() => {
const textArea = textAreaRef.current;
if (!textArea) return;
// We need to reset the height momentarily to get the correct scrollHeight for the textarea
textArea.style.height = "0px";
const scrollHeight = textArea.scrollHeight;
textArea.style.height = scrollHeight + "px";
}, [textAreaRef, value]);
};