# Textarea

Multi-line text field with an optional label, hint, character counter and validation state. It renders a native `<textarea>`: `rows`, `maxlength`, `disabled`, `readonly`, `required` and events (`oninput`, `onchange`, ...) work as in HTML. By default the user can drag it taller; with `autoGrow` it grows with its content instead, between `rows` lines and `--textarea-max-height`. Every piece of content (label, state icon, hint, counter) can be replaced with a snippet. Its state is exposed as `data-state`, `data-disabled` and `data-readonly` for app CSS.

```ts
import { Textarea } from '@likable-hair/svelte';
```

Category: simple. Source: `src/lib/components/simple/forms/Textarea.svelte`.

## Examples

### Basic

`bind:value` keeps the text in sync and `label` is linked to the textarea. `rows` sets the visible lines (3 by default); the user can drag the field taller.

```svelte
<script lang="ts">
	import { Textarea } from '@likable-hair/svelte';

	let notes = $state('');
</script>

<div class="form">
	<Textarea
		label="Customer notes"
		placeholder="Allergies, preferences, usual color…"
		hint="Visible to every stylist of the salon."
		bind:value={notes}
	/>
</div>

<style>
	.form {
		width: 100%;
		max-width: 340px;
	}
</style>
```

### Counter

`counter` shows the number of characters below the field, against `maxlength` when it is set. `labelSnippet` adds an "optional" tag to the label.

```svelte
<script lang="ts">
	import { Textarea } from '@likable-hair/svelte';

	let notes = $state('Prefers morning appointments. Color 6.3 dark golden blonde.');
</script>

<div class="form">
	<Textarea maxlength={200} counter bind:value={notes}>
		{#snippet labelSnippet()}
			Customer notes <em>optional</em>
		{/snippet}
	</Textarea>
	<Textarea label="Internal memo" counter placeholder="No limit, only the length is shown" />
</div>

<style>
	.form {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 340px;
	}

	em {
		font-style: normal;
		font-weight: 400;
		opacity: 0.7;
	}
</style>
```

### States

`state="error"` and `state="success"` color the border and the hint and show an icon; `error` also sets `aria-invalid`. `disabled` and `readonly` are the native attributes.

```svelte
<script lang="ts">
	import { Textarea } from '@likable-hair/svelte';
</script>

<div class="form">
	<Textarea label="Reason" value="ok" state="error" hint="Write at least 20 characters" />
	<Textarea label="Feedback" value="Great service, very kind staff." state="success" hint="Thanks!" />
	<Textarea label="Signed agreement" value="The customer accepts the price list." readonly rows={2} />
	<Textarea label="Archived notes" value="Moved to the new branch." disabled rows={2} />
</div>

<style>
	.form {
		display: grid;
		gap: 16px;
		width: 100%;
		max-width: 340px;
	}
</style>
```

### Auto grow

With `autoGrow` the field grows with its content instead of scrolling: it starts at `rows` lines and stops at `--textarea-max-height`, then scrolls. It uses CSS `field-sizing: content` where supported and measures the content elsewhere.

```svelte
<script lang="ts">
	import { Textarea } from '@likable-hair/svelte';
</script>

<div class="form">
	<Textarea
		label="Message"
		placeholder="Type a few lines…"
		rows={1}
		autoGrow
		--textarea-max-height="160px"
	/>
</div>

<style>
	.form {
		width: 100%;
		max-width: 340px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (bindable) | `string \| null` |  | Text of the field. |
| `label` | `string` |  | Visible label above the field, linked to the textarea. |
| `hint` | `string` |  | Text below the field. With `state` it becomes the error or success message. |
| `state` | `'error' \| 'success'` |  | Validation state: colors the border and the hint and shows an icon. `error` also sets `aria-invalid`. |
| `placeholder` | `string` |  | Native `placeholder`. |
| `rows` | `number` | `3` | Native `rows`: the visible lines, and the minimum height with `autoGrow`. |
| `maxlength` | `number` |  | Native `maxlength`. With `counter` it is shown as the limit. |
| `counter` | `boolean` | `false` | Shows the number of characters below the field, as `length / maxlength` when `maxlength` is set. |
| `autoGrow` | `boolean` | `false` | Grows with the content instead of scrolling, up to `--textarea-max-height`. It also turns off manual resizing. |
| `disabled` | `boolean` | `false` | Native `disabled`. |
| `readonly` | `boolean` | `false` | Native `readonly`. |
| `id` | `string` |  | `id` of the textarea. Generated when missing, so the label always points to the textarea. |
| `name` | `string` |  | Native `name`. |
| `textarea` (bindable) | `HTMLTextAreaElement` |  | The native `<textarea>` element. |
| `class` | `{ container?: string; label?: string; field?: string; textarea?: string; hint?: string; counter?: string; }` | `{}` | Extra classes for each part. |

It also accepts the native attributes and events of `<textarea>` (for example `id`, `title`, `aria-label`, `onkeydown`), which are forwarded to the element.

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label content (for example to add an "optional" tag). It stays linked to the textarea. |
| `stateIconSnippet` | `{ state: 'error' \| 'success' }` | Replaces the icon shown for `state`. Render nothing to hide it. |
| `hintSnippet` | `{ hint: string \| undefined }` | Replaces the hint below the field. |
| `counterSnippet` | `{ length: number; maxlength: number \| undefined }` | Replaces the counter content. |

## CSS variables

For a single instance, pass them as style props: `<Textarea --textarea-background="tomato">`. The `--textarea-default-*` defaults apply to the whole app: override them in `:root` or in a theme.

| Per-instance variable | Default |
| --- | --- |
| `--textarea-border-color` | `--textarea-default-border-color`: `var(--global-input-border-color)` |
| `--textarea-hover-border-color` | `--textarea-default-hover-border-color`: `var(--global-input-hover-border-color)` |
| `--textarea-focus-border-color` | `--textarea-default-focus-border-color`: `var(--global-input-focus-border-color)` |
| `--textarea-focus-ring-color` | `--textarea-default-focus-ring-color`: `var(--global-input-focus-ring-color)` |
| `--textarea-hint-color` | `--textarea-default-hint-color`: `var(--global-input-hint-color)` |
| `--textarea-resize` | `--textarea-default-resize`: `vertical` |
| `--textarea-icon-size` | `--textarea-default-icon-size`: `18px` |
| `--textarea-gap` | `--textarea-default-gap`: `6px` |
| `--textarea-width` | `--textarea-default-width`: `100%` |
| `--textarea-max-width` | `--textarea-default-max-width`: `none` |
| `--textarea-error-color` | `--textarea-default-error-color`: `var(--global-input-error-color)` |
| `--textarea-success-color` | `--textarea-default-success-color`: `var(--global-input-success-color)` |
| `--textarea-label-color` | `--textarea-default-label-color`: `var(--global-input-label-color)` |
| `--textarea-label-font-size` | `--textarea-default-label-font-size`: `var(--global-input-label-font-size)` |
| `--textarea-label-font-weight` | `--textarea-default-label-font-weight`: `var(--global-input-label-font-weight)` |
| `--textarea-inner-gap` | `--textarea-default-inner-gap`: `var(--global-input-gap)` |
| `--textarea-padding` | `--textarea-default-padding`: `9px var(--global-input-padding-x)` |
| `--textarea-background` | `--textarea-default-background`: `var(--global-input-background)` |
| `--textarea-border-width` | `--textarea-default-border-width`: `var(--global-input-border-width)` |
| `--textarea-border-radius` | `--textarea-default-border-radius`: `var(--global-input-radius)` |
| `--textarea-box-shadow` | `--textarea-default-box-shadow`: `var(--global-input-shadow)` |
| `--textarea-color` | `--textarea-default-color`: `var(--global-input-color)` |
| `--textarea-font-family` | `--textarea-default-font-family`: `inherit` |
| `--textarea-font-size` | `--textarea-default-font-size`: `var(--global-input-font-size)` |
| `--textarea-font-weight` | `--textarea-default-font-weight`: `400` |
| `--textarea-line-height` | `--textarea-default-line-height`: `1.55` |
| `--textarea-focus-ring-width` | `--textarea-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--textarea-height` | `--textarea-default-height`: `auto` |
| `--textarea-min-height` | `--textarea-default-min-height`: `1lh` |
| `--textarea-max-height` | `--textarea-default-max-height`: `none` |
| `--textarea-placeholder-color` | `--textarea-default-placeholder-color`: `var(--global-input-placeholder-color)` |
| `--textarea-hint-font-size` | `--textarea-default-hint-font-size`: `var(--global-input-hint-font-size)` |
| `--textarea-counter-color` | `--textarea-default-counter-color`: `var(--global-input-hint-color)` |
| `--textarea-counter-font-family` | `--textarea-default-counter-font-family`: `var(--global-font-family-mono)` |
| `--textarea-counter-font-size` | `--textarea-default-counter-font-size`: `11px` |
| `--textarea-disabled-background` | `--textarea-default-disabled-background`: `var(--global-input-disabled-background)` |
| `--textarea-disabled-opacity` | `--textarea-default-disabled-opacity`: `var(--global-input-disabled-opacity)` |
