Kokonut UI

useAutoResizeTextarea

Forms 24

About

A hook that automatically resizes a textarea based on its content

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds useAutoResizeTextarea to your project and fits it to your theme.

Code

How useAutoResizeTextarea is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

import { useAutoResizeTextarea } from "@/components/hooks/use-auto-resize-textarea"
import { ChangeEvent, useState} from 'react'


interface AutoResizeTextareaProps {
  value: string
  onChange: (value: string) => void
  placeholder?: string
  className?: string
}

function AutoResizeTextarea({ 
  value, 
  onChange, 
  placeholder,
  className 
}: AutoResizeTextareaProps) {
  const { textareaRef, adjustHeight } = useAutoResizeTextarea({
    minHeight: 100,  // minimum height in pixels
    maxHeight: 400   // optional maximum height in pixels
  })

  const handleChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
    onChange(e.target.value)
    adjustHeight()
  }

  return (
    <textarea
      ref={textareaRef}
      value={value}
      onChange={handleChange}
      placeholder={placeholder}
      className={className}
    />
  )
}


export function AutoResizeTextareaDemo() {
  const [text, setText] = useState('')

  return (
    <AutoResizeTextarea
      value={text}
      onChange={setText}
      placeholder="Type something..."
      className="w-full p-2 border rounded"
    />
  )
}

More by Kokonut UI

View all
  • Background Paths

    Background Paths

    5.7K
  • Shape Landing Hero

    Shape Landing Hero

    3.5K
  • Beams Background

    Beams Background

    2.8K
  • Bento Grid

    Bento Grid

    2.2K
  • v0 AI Chat

    v0 AI Chat

    2.1K
  • AI Voice Input

    AI Voice Input

    1.7K
  • Hand Writing Text

    Hand Writing Text

    1.3K
  • Action Search Bar

    Action Search Bar

    1.3K

More in Forms

View all
  • Premium Auth

    Premium Auth

    500
  • Login 2

    Login 2

    7
  • Authentication Card

    Authentication Card

    47
  • Login 1

    Login 1

    52
  • Contact Card

    Contact Card

    1K
  • Login Form

    Login Form

    6
  • Form

    Form

    16
  • Book A Demo 3

    Book A Demo 3

    83