OTP Field
A one-time password input composed of individual character slots.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{OTPField, OTPFieldInput};
#[Component]
function OTPFieldHeroDemo(): Node
{
$slots = range(1, 6);
return (
<div className="flex w-full max-w-80 flex-col items-start gap-1">
<label htmlFor="otp-1" className="text-sm font-bold text-neutral-950 dark:text-white">Verification code</label>
<OTPField name="code" aria-describedby="otp-description">
{% for i in slots %}
<OTPFieldInput key={{ i }} id={{ i == 1 ? 'otp-1' : null }} />
{% endfor %}
</OTPField>
<p id="otp-description" className="m-0 text-sm text-neutral-600 dark:text-neutral-400">Enter the 6-character code we sent to your device.</p>
</div>
);
}
Examples#
Labeling an OTP field#
Give an id to the first OTPFieldInput and use a native <label> with a matching for. Let the first input use the field label: the other inputs get aria-label="Character 2 of 6" and so on, so assistive technology can announce which slot is focused.
Optionally, add aria-describedby when supporting text should be announced with the field.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{OTPField, OTPFieldInput};
#[Component]
function OTPFieldLabelingDemo(): Node
{
$slots = range(1, 6);
return (
<div className="flex w-full max-w-80 flex-col items-start gap-1">
<label htmlFor="labeled-1" className="text-sm font-bold text-neutral-950 dark:text-white">Verification code</label>
<OTPField aria-describedby="labeled-description">
{% for i in slots %}
<OTPFieldInput key={{ i }} id={{ i == 1 ? 'labeled-1' : null }} />
{% endfor %}
</OTPField>
<p id="labeled-description" className="m-0 text-sm text-neutral-600 dark:text-neutral-400">Enter the 6-character code we sent to your device.</p>
</div>
);
}
Form integration#
Use Field to handle label associations and form integration.
Pass autoSubmit to submit the owning form automatically when all slots are filled, or listen to phem-otp-field:complete to react to completion without submitting.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use Phem\Ui\Field;
use function Phem\Ui\{FieldDescription, FieldLabel, Form, OTPField, OTPFieldInput};
#[Component]
function OTPFieldFormDemo(): Node
{
$slots = range(1, 6);
return (
<Form>
<Field>
<FieldLabel>Verification code</FieldLabel>
<FieldDescription>Enter the 6-character code we sent to your device.</FieldDescription>
<OTPField name="verificationCode">
{% for i in slots %}<OTPFieldInput key={{ i }} />{% endfor %}
</OTPField>
</Field>
</Form>
);
}
Alphanumeric verification codes#
Use validationType="alphanumeric" for recovery, backup, or invite codes that mix letters and numbers.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{OTPField, OTPFieldInput};
#[Component]
function OTPFieldAlphanumericDemo(): Node
{
$slots = range(1, 6);
return (
<div className="flex w-full max-w-80 flex-col items-start gap-1">
<label htmlFor="recovery-1" className="text-sm font-bold text-neutral-950 dark:text-white">Recovery code</label>
<OTPField validationType="alphanumeric" aria-describedby="recovery-description">
{% for i in slots %}
<OTPFieldInput key={{ i }} id={{ i == 1 ? 'recovery-1' : null }} />
{% endfor %}
</OTPField>
<p id="recovery-description" className="m-0 text-sm text-neutral-600 dark:text-neutral-400">Accept letters and numbers for backup codes such as <code className="font-mono">A7C9XZ</code>.</p>
</div>
);
}
Grouped layouts#
Wrap subsets of inputs in your own layout elements and use OTPFieldSeparator when you want the code presented in smaller visual chunks such as 123-456.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{OTPField, OTPFieldInput, OTPFieldSeparator};
#[Component]
function OTPFieldGroupedDemo(): Node
{
return (
<div className="flex w-full max-w-80 flex-col items-start gap-1">
<label htmlFor="grouped-1" className="text-sm font-bold text-neutral-950 dark:text-white">Verification code</label>
<OTPField className="items-center">
<div className="flex gap-2">
<OTPFieldInput id="grouped-1" />
<OTPFieldInput />
<OTPFieldInput />
</div>
<OTPFieldSeparator className="h-px w-4 bg-current text-neutral-950 dark:text-white">{{ '' }}</OTPFieldSeparator>
<div className="flex gap-2">
<OTPFieldInput />
<OTPFieldInput />
<OTPFieldInput />
</div>
</OTPField>
</div>
);
}
Placeholder hints#
OTPFieldInput is a real input, so native placeholder props and CSS work as usual. This example keeps placeholder hints visible until the active slot receives focus.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{OTPField, OTPFieldInput};
#[Component]
function OTPFieldPlaceholderDemo(): Node
{
$slots = range(1, 6);
return (
<div className="flex w-full max-w-80 flex-col items-start gap-1">
<label htmlFor="hints-1" className="text-sm font-bold text-neutral-950 dark:text-white">Verification code</label>
<OTPField aria-describedby="hints-description">
{% for i in slots %}
<OTPFieldInput key={{ i }} id={{ i == 1 ? 'hints-1' : null }} placeholder="•" className="placeholder:text-neutral-500 focus:placeholder:text-transparent" />
{% endfor %}
</OTPField>
<p id="hints-description" className="m-0 text-sm text-neutral-600 dark:text-neutral-400">Placeholder hints can stay visible until the active slot is focused.</p>
</div>
);
}
Custom normalization#
Use normalize (uppercase or lowercase) to normalize accepted values before state updates, such as converting alphanumeric codes to uppercase. It runs after validationType filtering, and the result is filtered against validationType again.
A refused character sends phem-otp-field:invalid.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{OTPField, OTPFieldInput};
#[Component]
function OTPFieldNormalizeDemo(): Node
{
$slots = range(1, 6);
return (
<div className="flex w-full max-w-80 flex-col items-start gap-1">
<label htmlFor="normalized-1" className="text-sm font-bold text-neutral-950 dark:text-white">Recovery code</label>
<OTPField validationType="alphanumeric" normalize="uppercase" aria-describedby="normalized-description">
{% for i in slots %}
<OTPFieldInput key={{ i }} id={{ i == 1 ? 'normalized-1' : null }} />
{% endfor %}
</OTPField>
<p id="normalized-description" className="m-0 text-sm text-neutral-600 dark:text-neutral-400">Letters and digits only. Letters are converted to uppercase.</p>
</div>
);
}
Masked entry#
Use mask when the code should be obscured while it is being typed.
<?phem
use Phem\Attribute\Component;
use Phem\Node;
use function Phem\Ui\{OTPField, OTPFieldInput};
#[Component]
function OTPFieldMaskDemo(): Node
{
$slots = range(1, 6);
return (
<div className="flex w-full max-w-80 flex-col items-start gap-1">
<label htmlFor="access-1" className="text-sm font-bold text-neutral-950 dark:text-white">Access code</label>
<OTPField mask aria-describedby="access-description">
{% for i in slots %}
<OTPFieldInput key={{ i }} id={{ i == 1 ? 'access-1' : null }} />
{% endfor %}
</OTPField>
<p id="access-description" className="m-0 text-sm text-neutral-600 dark:text-neutral-400">Use <code className="font-mono">mask</code> to obscure the code on shared screens.</p>
</div>
);
}
API reference#
Import the components of the family:
use function Phem\Ui\{OTPField, OTPFieldInput, OTPFieldSeparator};OTPField#
One input for each character of a code: Base UI OTPField.Root, on the phem-otp-field controller. Put one OTPFieldInput in it for each character. With a name (or phem:bind), a hidden input keeps the full code for the form.
| Prop | Type | Default |
|---|---|---|
defaultValue | ?string | null |
validationType | string | 'numeric' |
normalize | ?string | null |
mask | bool | false |
autoSubmit | bool | false |
disabled | bool | false |
readOnly | bool | false |
required | bool | false |
name | ?string | null |
value | ?Bound | null |
children | ?Html | null |
...rest | mixed | — |
OTPFieldInput#
The input of one character. In a Field, each input is a control of the field: the label names the first one.
| Prop | Type | Default |
|---|---|---|
...rest | mixed | — |
OTPFieldSeparator#
A mark between two groups of the slots of the code, – by default.
| Prop | Type | Default |
|---|---|---|
children | ?Html | null |
...rest | mixed | — |
The source of the examples: examples/ui/OTPField.phem.