ComponentsTextarea

Textarea

Multi-line text field for capturing user input.

Documentation

Usage

import type { ComponentPropsWithoutRef } from "react";
 
import { Field, Textarea } from "@optiaxiom/react";
 
export function App({
  description,
  error,
  label = "Input label",
  required = false,
}: Pick<
  ComponentPropsWithoutRef<typeof Field>,
  "description" | "error" | "label" | "required"
>) {
  return (
    <Field
      description={description}
      error={error}
      label={label}
      required={required}
    >
      <Textarea placeholder="Enter text..." />
    </Field>
  );
}

Error state

Enable the error prop to display the error state of the text field.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return <Textarea defaultValue="Some invalid value" error />;
}

Disabled state

Enable the disabled prop to toggle the disabled state of the text field.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return <Textarea defaultValue="Some disabled value" disabled />;
}

Read-only state

Enable the readOnly prop to display the read-only state of the text field.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return <Textarea defaultValue="Some read-only value" readOnly />;
}

Sizing

Fixed size

By default Textarea does not allow resizing and you can use the rows prop to set the size.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return <Textarea placeholder="Enter text..." rows={2} />;
}

Auto sizing

Use resize=auto prop to automatically resize the textarea as the value changes.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return <Textarea placeholder="Enter text..." resize="auto" />;
}

You can use the maxRows and rows props to limit the textarea size.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return (
    <Textarea maxRows={3} placeholder="Enter text..." resize="auto" rows={1} />
  );
}

Manual sizing

Use the resize=vertical prop to allow users to manually resize the textarea.

You can use rows prop to set the initial size of the textarea.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return <Textarea placeholder="Enter text..." resize="vertical" />;
}

Reset sizing

Use the resize=none prop to disallow resizing the textarea.

You can use rows prop to set the size of the textarea.

import { Textarea } from "@optiaxiom/react";
 
export function App() {
  return <Textarea placeholder="Enter text..." resize="none" rows={2} />;
}

Addons

Top and bottom addons

Use the addonBefore and addonAfter prop to add icons, text, or any other element to the start or end of the text field.

import { Button, Flex, Textarea, Tooltip } from "@optiaxiom/react";
import {
  IconArrowUp,
  IconAt,
  IconMoodSmile,
  IconPhoto,
} from "@tabler/icons-react";
 
export function App() {
  return (
    <Textarea
      addonAfter={
        <Flex borderT="1" flexDirection="row" gap="4" p="4">
          <Tooltip content="Add emoji">
            <Button appearance="subtle" icon={<IconMoodSmile />} size="sm" />
          </Tooltip>
          <Tooltip content="Add mention">
            <Button appearance="subtle" icon={<IconAt />} size="sm" />
          </Tooltip>
          <Tooltip content="Upload images">
            <Button appearance="subtle" icon={<IconPhoto />} size="sm" />
          </Tooltip>
 
          <Tooltip content="Submit">
            <Button
              appearance="primary"
              icon={<IconArrowUp />}
              ml="auto"
              size="sm"
            />
          </Tooltip>
        </Flex>
      }
      placeholder="Add a comment"
      w="256"
    />
  );
}

Handling addon clicks

Use the addonPointerEvents prop to control whether clicking blank spaces in the addons focuses the input or not.

import type { ComponentPropsWithoutRef } from "react";
 
import { Button, Flex, Textarea, Tooltip } from "@optiaxiom/react";
import {
  IconArrowUp,
  IconAt,
  IconMoodSmile,
  IconPhoto,
} from "@tabler/icons-react";
 
export function App({
  addonPointerEvents = "none",
}: Pick<ComponentPropsWithoutRef<typeof Textarea>, "addonPointerEvents">) {
  return (
    <Textarea
      addonAfter={
        <Flex flexDirection="row" gap="4" p="4">
          <Tooltip content="Add emoji">
            <Button appearance="subtle" icon={<IconMoodSmile />} size="sm" />
          </Tooltip>
          <Tooltip content="Add mention">
            <Button appearance="subtle" icon={<IconAt />} size="sm" />
          </Tooltip>
          <Tooltip content="Upload images">
            <Button appearance="subtle" icon={<IconPhoto />} size="sm" />
          </Tooltip>
 
          <Tooltip content="Submit">
            <Button
              appearance="primary"
              icon={<IconArrowUp />}
              ml="auto"
              size="sm"
            />
          </Tooltip>
        </Flex>
      }
      addonPointerEvents={addonPointerEvents}
      placeholder="Add a comment"
      w="256"
    />
  );
}

Props

Textarea

Supports all InputBase props in addition to its own.

Name

Type

asChild

false | true

maxRows

1 | 2 | 3 | 5 | 4

Limits the height of the textarea when resize=auto is used.

resize

"none" | "auto" | "vertical"

InputBase

Supports all Box props in addition to its own.

Name

Type

addonAfter

ReactNode

addonBefore

ReactNode

addonPointerEvents

"none" | "auto" = "auto"

When this prop is set to none clicking empty space inside the addon will focus the input box.

asChild

false | true

disabled

false | true

error

false | true

Changelog

0.1.0

  • Added component

Copyright 2024 © Optimizely.