Textarea

A multi-line text field that grows to fit its content.

Put it in a Field for a label and a description, like any input. It sizes to its content with field-sizing: content, between its minimum height and whatever limit you set.

Preview
basic.tsx
import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Textarea } from "~/components/ui/textarea"

export default function Example() {
  return (
    <Field className="w-full max-w-sm">
      <FieldLabel>Notes for the editor</FieldLabel>
      <Textarea placeholder="Anything they should know before layout…" />
      <FieldDescription>It grows as you type.</FieldDescription>
    </Field>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesInputform itemTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/textarea.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/textarea.tsx
import { Textarea as TextareaPrimitive } from "@ui-sdk/react/textarea"
import type * as React from "react"

import { inputVariants } from "~/components/ui/input"
import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/** A multi-line text field, filled like the system's inputs, that grows to fit its content. */
function Textarea({ className, ...props }: React.ComponentProps<typeof TextareaPrimitive>) {
  return (
    <TextareaPrimitive
      className={cnState(
        [
          inputVariants(),
          textVariants({ size: "control" }),
          "flex field-sizing-content min-h-16 w-full resize-none px-md py-sm",
        ].join(" "),
        className,
      )}
      {...props}
    />
  )
}

export { Textarea }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Textarea

A multi-line text field, filled like the system's inputs, that grows to fit its content.

autoResize

Grows and shrinks with the text. Bound it with CSS min-height and max-height; past the maximum, it scrolls.

Type boolean · Default false

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: InputState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: InputState) => string)

controller

Reads and writes the field from anywhere. Create one with createInputController().

Type { getValue: () => string; setValue: (value: string, options?: ChangeOptions<InputValueChangeReason>) => boolean; clear: (options?: ChangeOptions<...>) => boolean; focus: () => void; }

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultValue

The value it starts with. A form reset returns here.

Type string

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

onSubmit

Runs on the submit shortcut, unless an input method is composing. Without it, the shortcut submits the form the textarea is in. With neither, the key is left alone, so Enter adds a line.

Type (value: string, details: TextareaSubmitDetails) => void

onValueChange

Called when the user changes the value: typing ("input-change"), a Clear part ("clear-press"), a form reset ("reset") or a controller ("imperative"). details.cancel() keeps the value as it was.

Type (value: string, details: ChangeDetails<InputValueChangeReason>) => void

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<InputState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: InputState) => CSSProperties)

submitShortcut

Which keys submit: "enter", with Shift+Enter for a new line, or "mod+enter", with Enter for a new line. Without it, Enter always starts a new line.

Type "enter" | "mod+enter"

value

The value, for a controlled field.

Type string