# Textarea

Displays a form textarea or a component that looks like a textarea.

Source: https://webjs.dev/ui/docs/components/textarea

```ts
import { html } from '@webjsdev/core';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaDemo() {
  return html`
    <textarea data-slot="textarea" placeholder="Type your message here." aria-label="Message" class=${textareaClass()}></textarea>
  `;
}
```

## Installation

```bash
npx @webjsdev/ui add textarea
```

### Copy and paste the following code into your project.

````ts title="components/ui/textarea.ts"
/**
 * Textarea: styled native `<textarea>`. Tier-1 class helper. Real
 * multi-line input: form submission, autosize via `field-sizing:
 * content`, and native validation all work.
 *
 * shadcn parity:
 *   Textarea  → textareaClass()
 *
 * Pair with `<label class=${labelClass()} for="...">` and wrap in
 * `<div class=${fieldClass()}>` for the canonical field rhythm.
 *
 * Design tokens used: --input, --background, --muted-foreground, --ring,
 * --destructive.
 *
 * A11y (required for accessible output):
 *   LABEL IT. A `<label class=${labelClass()} for="<the textarea's id">` linked
 *   by the `for` / `id` pair is the whole requirement. The `placeholder` in the
 *   old example was doing that job and cannot: it vanishes as soon as the user
 *   types, and it is not reliably exposed as a name.
 *   Point `aria-describedby` at a hint or a character counter that EXISTS on the
 *   page, and on a validation failure set `aria-invalid="true"` plus an
 *   `aria-describedby` error element so the reason is announced with the field.
 *   If you show a live character count, put it in an `aria-live="polite"`
 *   region, otherwise the number updates silently for a screen reader user.
 *   Leave the textarea RESIZABLE. The class autosizes via `field-sizing:
 *   content`, which is fine, but do not add `resize-none` on top: a user who
 *   needs more room to read their own text has no other way to get it (WCAG
 *   1.4.4-adjacent).
 *
 * @example
 * ```html
 * <div class=${fieldClass()}>
 *   <label class=${labelClass()} for="message">Message</label>
 *   <textarea class=${textareaClass()} name="message" id="message" rows="4"
 *             aria-describedby="message-hint"></textarea>
 *   <p class=${hintClass()} id="message-hint">Markdown is supported.</p>
 * </div>
 * ```
 */
import { cn } from '../lib/utils.ts';

const TEXTAREA_BASE =
  'rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-xs transition-[color,box-shadow] focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:aria-invalid:ring-destructive/40 flex field-sizing-content min-h-16 w-full outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50';

/** Compose Tailwind classes for a native `<textarea>`. */
export function textareaClass(): string {
  return cn(TEXTAREA_BASE);
}
````

### Update the import paths to match your project setup.

## Usage

```ts
import { textareaClass } from '#components/ui/textarea.ts';
```

```ts
<textarea class=${textareaClass()}></textarea>
```

## Field

Use `fieldClass()`, `fieldLabelClass()`, and `fieldDescriptionClass()` to create a textarea with a label and description.

```ts
import { html } from '@webjsdev/core';
import { fieldClass, fieldLabelClass, fieldDescriptionClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaField() {
  return html`
    <div role="group" data-slot="field" data-orientation="vertical" class=${fieldClass()}>
      <label data-slot="field-label" for="textarea-message" class=${fieldLabelClass()}>Message</label>
      <p data-slot="field-description" class=${fieldDescriptionClass()}>Enter your message below.</p>
      <textarea data-slot="textarea" id="textarea-message" placeholder="Type your message here." class=${textareaClass()}></textarea>
    </div>
  `;
}
```

## Disabled

Use the `disabled` attribute to disable the textarea. To style the disabled state, add the `data-disabled` attribute to the field.

```ts
import { html } from '@webjsdev/core';
import { fieldClass, fieldLabelClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaDisabled() {
  return html`
    <div role="group" data-slot="field" data-orientation="vertical" data-disabled="true" class=${fieldClass()}>
      <label data-slot="field-label" for="textarea-disabled" class=${fieldLabelClass()}>Message</label>
      <textarea data-slot="textarea" id="textarea-disabled" placeholder="Type your message here." disabled class=${textareaClass()}></textarea>
    </div>
  `;
}
```

## Invalid

Use the `aria-invalid` attribute to mark the textarea as invalid. To style the invalid state, add the `data-invalid` attribute to the field.

```ts
import { html } from '@webjsdev/core';
import { fieldClass, fieldLabelClass, fieldDescriptionClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaInvalid() {
  return html`
    <div role="group" data-slot="field" data-orientation="vertical" data-invalid="true" class=${fieldClass()}>
      <label data-slot="field-label" for="textarea-invalid" class=${fieldLabelClass()}>Message</label>
      <textarea data-slot="textarea" id="textarea-invalid" placeholder="Type your message here." aria-invalid="true" class=${textareaClass()}></textarea>
      <p data-slot="field-description" class=${fieldDescriptionClass()}>Please enter a valid message.</p>
    </div>
  `;
}
```

## Button

Pair with `buttonClass()` to create a textarea with a submit button.

```ts
import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaButton() {
  return html`
    <div class="grid w-full gap-2">
      <textarea data-slot="textarea" placeholder="Type your message here." aria-label="Message" class=${textareaClass()}></textarea>
      <button type="button" data-slot="button" data-variant="default" data-size="default" class=${buttonClass()}>Send message</button>
    </div>
  `;
}
```

## RTL

To enable RTL support, see the [RTL configuration guide](https://webjs.dev/ui/docs/rtl).

```ts
import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { fieldClass, fieldLabelClass, fieldDescriptionClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

const translations = {
  en: {
    dir: 'ltr',
    values: {
      label: 'Feedback',
      placeholder: 'Your feedback helps us improve...',
      description: 'Share your thoughts about our service.',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      label: 'التعليقات',
      placeholder: 'تعليقاتك تساعدنا على التحسين...',
      description: 'شاركنا أفكارك حول خدمتنا.',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      label: 'משוב',
      placeholder: 'המשוב שלך עוזר לנו להשתפר...',
      description: 'שתף את מחשבותיך על השירות שלנו.',
    },
  },
};

// The provider is the dir attribute: the field styles itself with logical
// properties, so dir="rtl" on it flips the whole layout.
export default function TextareaRtl() {
  const lang = 'ar';
  const { dir, values: t } = translations[lang];

  return html`
    <div role="group" data-slot="field" data-orientation="vertical" dir=${dir} lang=${lang} class=${cn(fieldClass(), 'w-full max-w-xs')}>
      <label data-slot="field-label" for="feedback" class=${fieldLabelClass()}>${t.label}</label>
      <textarea data-slot="textarea" id="feedback" placeholder=${t.placeholder} rows="4" class=${textareaClass()}></textarea>
      <p data-slot="field-description" class=${fieldDescriptionClass()}>${t.description}</p>
    </div>
  `;
}
```
