use-textarea-resize
simple-ai/use-textarea-resizeFreeHook
simple-ai publishes no description for this item.
Install it
npx shadcn@latest add https://simple-ai.dev/r/use-textarea-resize.jsonSource
1"use client";23import type { ComponentProps } from "react";4import { useLayoutEffect, useRef } from "react";56export function useTextareaResize(7 value: ComponentProps<"textarea">["value"],8 rows = 1,9) {10 const textareaRef = useRef<HTMLTextAreaElement>(null);1112 // biome-ignore lint/correctness/useExhaustiveDependencies: Needed13 useLayoutEffect(() => {14 const textArea = textareaRef.current;1516 if (textArea) {17 // Get the line height to calculate minimum height based on rows18 const computedStyle = window.getComputedStyle(textArea);19 const lineHeight =20 Number.parseInt(computedStyle.lineHeight, 10) || 20;21 const padding =22 Number.parseInt(computedStyle.paddingTop, 10) +23 Number.parseInt(computedStyle.paddingBottom, 10);2425 // Calculate minimum height based on rows26 const minHeight = lineHeight * rows + padding;2728 // Reset height to auto first to get the correct scrollHeight29 textArea.style.height = "0px";30 const scrollHeight = Math.max(textArea.scrollHeight, minHeight);3132 // Set the final height33 textArea.style.height = `${scrollHeight + 2}px`;34 }35 }, [textareaRef, value, rows]);3637 return textareaRef;38}
