# Otp

One-time passcode entry over a single native input.

Source: https://www.zazz.sh/api/primitives/otp/

## Installation

```bash
pnpm dlx zazz-ui add otp
```

Also adds `fields`, `input`, which it depends on.

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<form data-ui="field" style="--max-w: max-content">
  <label data-field-slot="label" for="f-otp">Verification code</label>
  <ui-otp data-otp-groups="3-3">
    <input
      data-ui="input"
      data-otp-slot="input"
      id="f-otp"
      name="otp"
      type="text"
      autocomplete="one-time-code"
      inputmode="numeric"
      pattern="\d{6}"
      maxlength="6"
      required
      spellcheck="false"
      autocapitalize="off"
    />
  </ui-otp>
  <div data-field-slot="description">
    <span data-field-slot="hint">Enter the 6-digit code we sent you.</span>
    <span data-field-slot="error">Enter all 6 digits.</span>
  </div>
</form>
```

## Behavior

`primitives/otp/otp.js`

`<ui-otp>`: a one-time password field with character slots.
Light-DOM custom element that augments a single
`<input data-ui="input" data-otp-slot="input">` (the standard OTP shape with
`autocomplete="one-time-code"`, `inputmode`, `pattern`, and `maxlength`)
with a presentational slot rail. The input stays the source of truth:
autofill, paste, IME, undo, form submission, and `:user-invalid` are all
native. The script stamps an `aria-hidden` rail of character cells behind
the input and stretches the input invisibly over it; each cell mirrors one
character and the caret position. Without JavaScript the markup is a plain,
fully functional OTP input.

The code length comes from the input `maxlength`.

Attributes on `<ui-otp>`:
- `data-otp-groups`: separator layout, e.g. `"3-3"` or `"2-2-2"`. The
  groups must sum to `maxlength`; otherwise one ungrouped run renders.
- `data-otp-type`: sanitization charset: `numeric` (default), `alpha`,
  `alphanumeric`, `none`. Keep it in agreement with the input `pattern`
  and `inputmode`, which carry the no-JS validation.
- `data-otp-mask`: render `•` in the cells instead of characters.
- `data-otp-auto-submit`: request the owning form submission once when
  the code is complete.

Stamped state: cells are `[data-otp-slot~="cell"]` with a `data-otp-state`
token list (`filled`, `active`); separators are `[data-otp-slot~="separator"]`; the root
gains `data-otp-ready` when enhanced and `data-otp-complete` when full.

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-ui="otp"` | identity | free value |
| `<ui-otp>` | tag | free value |
| `data-otp-slot` | slot | `cell` `input` `rail` `separator` |
| `data-otp-state` | state | `active` |
| `data-otp-auto-submit` | config | free value |
| `data-otp-complete` | config | free value |
| `data-otp-groups` | config | free value |
| `data-otp-index` | config | free value |
| `data-otp-mask` | config | free value |
| `data-otp-ready` | config | free value |
| `data-otp-type` | config | free value |

Requires: fields, input.

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-otp-caret-color` | `var(--ui-field-border-color--focus-within)` | — |
| `--ui-otp-cell-bg` | `var(--ui-field-bg)` | — |
| `--ui-otp-cell-border-color` | `var(--ui-field-border-color)` | — |
| `--ui-otp-cell-border-style` | `var(--ui-field-border-style)` | — |
| `--ui-otp-cell-border-width` | `var(--ui-field-border-width)` | — |
| `--ui-otp-cell-corner-shape` | `var(--ui-field-corner-shape)` | — |
| `--ui-otp-cell-font-size` | `var(--font-size-md)` | — |
| `--ui-otp-cell-h` | `var(--ui-field-h)` | — |
| `--ui-otp-cell-py` | `calc(var(--ui-field-py) / 2)` | — |
| `--ui-otp-cell-radius` | `var(--ui-field-radius)` | — |
| `--ui-otp-cell-w` | `var(--ui-field-h)` | — |
| `--ui-otp-gap` | `calc(var(--spacing) * 1.5)` | — |
| `--ui-otp-separator-text` | `var(--color-muted-foreground)` | — |
