# SimpleTextField

Text input with an optional label, hint and validation state. It renders a native `<input>`: `type`, `disabled`, `readonly`, `required` and events (`oninput`, `onchange`, ...) work as in HTML. Icons can sit outside or inside the field, on either side, and every piece of content (label, icons, state icon, hint) can be replaced with a snippet. With `type="password"` an eye button inside the field shows and hides the password; `revealable={false}` removes it. Its state is exposed as `data-state`, `data-disabled`, `data-readonly` and `data-revealed` for app CSS.

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

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

## Examples

### Basic

A field with label, placeholder, icon and hint. `bind:value` keeps the value in sync. A password field has an eye button that shows and hides the password.

```svelte
<script lang="ts">
	import { mdiEmailOutline, mdiLockOutline } from '@mdi/js';
	import { SimpleTextField } from '@likable-hair/svelte';

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

<div class="form">
	<SimpleTextField
		label="Email"
		type="email"
		placeholder="name@salon.com"
		prependInnerIcon={mdiEmailOutline}
		hint="We never share your email."
		bind:value={email}
	/>
	<SimpleTextField
		label="Password"
		type="password"
		prependInnerIcon={mdiLockOutline}
		bind:value={password}
	/>
</div>

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

### States

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

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

<div class="form">
	<SimpleTextField
		label="Phone"
		type="tel"
		value="+39 33"
		prependInnerIcon={mdiPhoneOutline}
		state="error"
		hint="Invalid phone number"
	/>
	<SimpleTextField label="Promo code" value="AURORA25" state="success" hint="Code applied" />
	<SimpleTextField label="Customer ID" value="C-1024" readonly />
	<SimpleTextField label="Branch" value="Milan" disabled />
</div>

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

### Icons and snippets

Icons can sit outside (`prependIcon`, `appendIcon`) or inside the field (`prependInnerIcon`, `appendInnerIcon`). Snippets replace them, and the label, with any content.

```svelte
<script lang="ts">
	import { mdiHelpCircleOutline, mdiMagnify, mdiStoreOutline } from '@mdi/js';
	import { SimpleTextField } from '@likable-hair/svelte';
</script>

<div class="form">
	<SimpleTextField
		label="Salon"
		prependIcon={mdiStoreOutline}
		appendIcon={mdiHelpCircleOutline}
		placeholder="Outer icons"
	/>
	<SimpleTextField placeholder="Allergies, preferences…">
		{#snippet labelSnippet()}
			Notes <em>optional</em>
		{/snippet}
	</SimpleTextField>
	<SimpleTextField type="search" prependInnerIcon={mdiMagnify} placeholder="Search clients…">
		{#snippet appendInnerSnippet()}
			<kbd>⌘K</kbd>
		{/snippet}
	</SimpleTextField>
</div>

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

	em {
		font-style: normal;
		font-weight: 400;
		color: var(--global-color-text-3);
	}

	kbd {
		padding: 2px 6px;
		border-radius: var(--global-radius-xs);
		border: var(--global-border-width) solid var(--global-color-border);
		background: var(--global-color-surface-2);
		font-family: var(--global-font-family-mono);
		font-size: 11px;
	}
</style>
```

## Props

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (bindable) | `string \| number \| null` |  | Value of the input. |
| `type` | `'text' \| 'password' \| 'number' \| 'time' \| 'date' \| 'email' \| 'tel' \| 'search' \| 'url'` | `'text'` | Native input `type`. |
| `label` | `string` |  | Visible label above the field, linked to the input. |
| `hint` | `string` |  | Text below the field. With `state` it becomes the error or success message. |
| `state` | `'error' \| 'success'` |  | Validation state: colors the border, the hint and shows an icon. `error` also sets `aria-invalid`. |
| `placeholder` | `string` |  | Native `placeholder`. |
| `disabled` | `boolean` | `false` | Native `disabled`. |
| `readonly` | `boolean` | `false` | Native `readonly`. |
| `id` | `string` |  | `id` of the input. Generated when missing, so the label always points to the input. |
| `name` | `string` |  | Native `name`. |
| `prependIcon` | `string` |  | SVG path of an icon outside the field, on the left. |
| `prependInnerIcon` | `string` |  | SVG path of an icon inside the field, on the left. |
| `appendInnerIcon` | `string` |  | SVG path of an icon inside the field, on the right. |
| `appendIcon` | `string` |  | SVG path of an icon outside the field, on the right. |
| `revealable` | `boolean` | `true` | With `type="password"`, shows an eye button inside the field that shows and hides the password. On by default: `false` removes it. |
| `revealed` (bindable) | `boolean` |  | Whether the password is shown as plain text, with `revealable`. Bind it to read or set it. |
| `revealLabel` | `string` |  | Accessible name of the eye button, a toggle that is pressed while the password is shown. Defaults to "Show password", in the language of `locale`. |
| `locale` | `string` |  | BCP 47 locale of the texts: `it` and `en` are built in, other languages fall back to English. Defaults to the one set with `setDefaultLocale`, else `en`. |
| `input` (bindable) | `HTMLInputElement` |  | The native `<input>` element. |
| `class` | `{ container?: string; label?: string; row?: string; field?: string; input?: string; hint?: string; }` | `{}` | Extra classes for each part. |

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

## Snippets

| Name | Parameters | Description |
| --- | --- | --- |
| `prependSnippet` | `{ prependIcon: string \| undefined }` | Replaces the outer left icon. |
| `prependInnerSnippet` | `{ prependInnerIcon: string \| undefined }` | Replaces the inner left icon. |
| `appendInnerSnippet` | `{ appendInnerIcon: string \| undefined }` | Replaces the inner right icon. |
| `appendSnippet` | `{ appendIcon: string \| undefined }` | Replaces the outer right icon. |
| `hintSnippet` | `{ hint: string \| undefined }` | Replaces the hint below the field. |
| `labelSnippet` | `{ label: string \| undefined }` | Replaces the label content (for example to add an "optional" tag). It stays linked to the input. |
| `stateIconSnippet` | `{ state: 'error' \| 'success' }` | Replaces the icon shown for `state`. Render nothing to hide it. |

## CSS variables

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

| Per-instance variable | Default |
| --- | --- |
| `--simple-text-field-border-color` | `--simple-text-field-default-border-color`: `var(--global-input-border-color)` |
| `--simple-text-field-hover-border-color` | `--simple-text-field-default-hover-border-color`: `var(--global-input-hover-border-color)` |
| `--simple-text-field-focus-border-color` | `--simple-text-field-default-focus-border-color`: `var(--global-input-focus-border-color)` |
| `--simple-text-field-focus-ring-color` | `--simple-text-field-default-focus-ring-color`: `var(--global-input-focus-ring-color)` |
| `--simple-text-field-hint-color` | `--simple-text-field-default-hint-color`: `var(--global-input-hint-color)` |
| `--simple-text-field-icon-size` | `--simple-text-field-default-icon-size`: `18px` |
| `--simple-text-field-gap` | `--simple-text-field-default-gap`: `6px` |
| `--simple-text-field-width` | `--simple-text-field-default-width`: `100%` |
| `--simple-text-field-max-width` | `--simple-text-field-default-max-width`: `none` |
| `--simple-text-field-error-color` | `--simple-text-field-default-error-color`: `var(--global-input-error-color)` |
| `--simple-text-field-success-color` | `--simple-text-field-default-success-color`: `var(--global-input-success-color)` |
| `--simple-text-field-label-color` | `--simple-text-field-default-label-color`: `var(--global-input-label-color)` |
| `--simple-text-field-label-font-size` | `--simple-text-field-default-label-font-size`: `var(--global-input-label-font-size)` |
| `--simple-text-field-label-font-weight` | `--simple-text-field-default-label-font-weight`: `var(--global-input-label-font-weight)` |
| `--simple-text-field-outer-gap` | `--simple-text-field-default-outer-gap`: `8px` |
| `--simple-text-field-icon-color` | `--simple-text-field-default-icon-color`: `var(--global-input-icon-color)` |
| `--simple-text-field-inner-gap` | `--simple-text-field-default-inner-gap`: `var(--global-input-gap)` |
| `--simple-text-field-height` | `--simple-text-field-default-height`: `var(--global-input-height)` |
| `--simple-text-field-padding` | `--simple-text-field-default-padding`: `0 var(--global-input-padding-x)` |
| `--simple-text-field-background` | `--simple-text-field-default-background`: `var(--global-input-background)` |
| `--simple-text-field-border-width` | `--simple-text-field-default-border-width`: `var(--global-input-border-width)` |
| `--simple-text-field-border-radius` | `--simple-text-field-default-border-radius`: `var(--global-input-radius)` |
| `--simple-text-field-box-shadow` | `--simple-text-field-default-box-shadow`: `var(--global-input-shadow)` |
| `--simple-text-field-focus-ring-width` | `--simple-text-field-default-focus-ring-width`: `var(--global-focus-ring-width)` |
| `--simple-text-field-reveal-color` | `--simple-text-field-default-reveal-color`: `var(--global-input-icon-color)` |
| `--simple-text-field-reveal-hover-color` | `--simple-text-field-default-reveal-hover-color`: `var(--global-color-text)` |
| `--simple-text-field-color` | `--simple-text-field-default-color`: `var(--global-input-color)` |
| `--simple-text-field-font-size` | `--simple-text-field-default-font-size`: `var(--global-input-font-size)` |
| `--simple-text-field-font-weight` | `--simple-text-field-default-font-weight`: `400` |
| `--simple-text-field-placeholder-color` | `--simple-text-field-default-placeholder-color`: `var(--global-input-placeholder-color)` |
| `--simple-text-field-hint-font-size` | `--simple-text-field-default-hint-font-size`: `var(--global-input-hint-font-size)` |
| `--simple-text-field-disabled-background` | `--simple-text-field-default-disabled-background`: `var(--global-input-disabled-background)` |
| `--simple-text-field-disabled-opacity` | `--simple-text-field-default-disabled-opacity`: `var(--global-input-disabled-opacity)` |
