# Auto-resize textarea with character count

> Auto-resize textarea with character count

A shadcn/ui block from the [@reui](https://reui.io) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/reui/c-textarea-6).

- Page, with a live preview: https://shadcn.exchange/r/reui/c-textarea-6
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/reui/c-textarea-6-light-aa4eadeec9f49511.png
- Registry item: https://reui.io/r/new-york-v4/c-textarea-6.json
- Listed: 2026-10-13, source last changed 2026-10-13

## Install

```bash
npx shadcn@latest add https://reui.io/r/new-york-v4/c-textarea-6.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- Registry items: `field`, `textarea`

## Files

### `c-textarea-6.tsx`

```tsx
"use client"

import { useCallback, useRef, useState } from "react"

import { Field, FieldLabel } from "@/components/ui/field"
import { Textarea } from "@/components/ui/textarea"

const MAX_CHARS = 280

export function Pattern() {
  const [value, setValue] = useState("")
  const textareaRef = useRef<HTMLTextAreaElement>(null)

  const handleChange = useCallback(
    (e: React.ChangeEvent<HTMLTextAreaElement>) => {
      const newValue = e.target.value
      if (newValue.length <= MAX_CHARS) {
        setValue(newValue)
      }

      const textarea = textareaRef.current
      if (textarea) {
        textarea.style.height = "auto"
        textarea.style.height = `${textarea.scrollHeight}px`
      }
    },
    []
  )

  const remaining = MAX_CHARS - value.length
  const isNearLimit = remaining <= 20
  const isAtLimit = remaining === 0

  return (
    <div className="mx-auto w-full max-w-xs">
      <Field className="w-full">
        <div className="flex items-center justify-between">
          <FieldLabel htmlFor="auto-resize-textarea">Bio</FieldLabel>
          <span
            className={`text-xs tabular-nums ${
              isAtLimit
                ? "text-destructive font-semibold"
                : isNearLimit
                  ? "text-warning"
                  : "text-muted-foreground"
            }`}
          >
            {value.length}/{MAX_CHARS}
          </span>
        </div>
        <Textarea
          ref={textareaRef}
          id="auto-resize-textarea"
          value={value}
          onChange={handleChange}
          placeholder="Tell us about yourself..."
          rows={2}
          className="resize-none overflow-hidden"
        />
      </Field>
    </div>
  )
}
```
