Components
OTP Field
A one-time password input composed of individual character slots.
Enter the 6-character code we sent to your device.
Usage guidelines#
- Form controls must have an accessible name. It can be created using a
<label>element or the Field component. See Labeling an OTP field.
Anatomy#
Import the component and assemble its parts:
import { OTPField } from '@egoist/solid-base/otp-field';
<OTPField.Root>
<OTPField.Input />
<OTPField.Separator />
</OTPField.Root>;Examples#
Labeling an OTP field#
Pass an id to <OTPField.Root> and use a native <label> with a matching for. Let the first
input use the field label, and add aria-label to the remaining inputs so assistive technology
can announce which slot is focused.
Optionally, add aria-describedby when supporting text should be announced with the field.
<div>
<label for="verification-code">Verification code</label>
<OTPField.Root
id="verification-code"
length={6}
aria-describedby="verification-code-description"
>
<OTPField.Input />
<OTPField.Input aria-label="Character 2 of 6" />
<OTPField.Input aria-label="Character 3 of 6" />
<OTPField.Input aria-label="Character 4 of 6" />
<OTPField.Input aria-label="Character 5 of 6" />
<OTPField.Input aria-label="Character 6 of 6" />
</OTPField.Root>
<p id="verification-code-description">Enter the 6-character code we sent to your device.</p>
</div>The demos on this page render the slots with <Repeat>, which passes each slot's index as a
plain number:
<Repeat count={6}>
{(index) => (
<OTPField.Input aria-label={index === 0 ? undefined : `Character ${index + 1} of 6`} />
)}
</Repeat>Submitting on completion#
Pass autoSubmit to submit the owning form automatically when all slots are filled, or use
onValueComplete to react to completion without submitting.
Alphanumeric verification codes#
Use validationType="alphanumeric" for recovery, backup, or invite codes that mix letters and
numbers.
Accept letters and numbers for backup codes such as A7C9XZ.
Grouped layouts#
Wrap subsets of inputs in your own layout elements and use <OTPField.Separator> when you want
the code presented in smaller visual chunks such as 123-456.
Custom normalization#
Use normalizeValue 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. Use validationType="none" when the normalizer should
provide the full validation rule.
Pair custom rules with inputmode for keyboard hints and onValueInvalid for rejected
characters.
Letters and digits only. Letters are converted to uppercase.
Masked entry#
Use mask when the code should be obscured while it is being typed.
Use mask to obscure the code on shared screens.
API reference#
<OTPField.Root>
Groups all OTP field parts and manages their state. Renders a <div> element.
aria-labelledbystring—The id(s) of the element(s) labelling the group. Not forwarded to the slot inputs.
aria-describedbystring—The id(s) of the element(s) describing the group.
idstring—The id of the first input element. Subsequent inputs derive their ids from it ({id}-2, {id}-3, and so on).
autocompletestring'one-time-code'The input autocomplete attribute. Applied to the first slot and hidden validation input.
formstring—A string specifying the form element with which the hidden input is associated. This string's value must match the id of a form element in the same document.
length*number—The number of OTP input slots. Required so the root can clamp values, detect completion, and generate consistent validation markup before all slots hydrate.
autoSubmitbooleanfalseWhether to submit the owning form when the OTP becomes complete.
maskbooleanfalseWhether the slot inputs should mask entered characters. Pass type directly to individual <OTPField.Input> parts to use a custom input type.
inputmodeJSX.HTMLAttributes<HTMLInputElement>['inputmode']—The virtual keyboard hint applied to the slot inputs and hidden validation input.
Built-in validation modes provide sensible defaults, but you can override them when needed.
validationTypeOTPFieldRoot.ValidationType'numeric'The type of input validation to apply to the OTP value.
normalizeValue((value: string) => string)—Function that normalizes the OTP value after whitespace and validationType filtering. It runs whenever OTP Field normalizes a value, including initial/default values, controlled values, and user edits.
The returned value is filtered by validationType again, then clamped to length. It should be idempotent because OTP Field may normalize the same value more than once while handling edits, storing state, and rendering controlled or uncontrolled values. Non-idempotent normalizers can compound across those normalization passes. Characters rejected while normalizing typed or pasted text are reported through onValueInvalid.
requiredbooleanfalseWhether the user must enter a value before submitting a form.
disabledbooleanfalseWhether the component should ignore user interaction.
readOnlybooleanfalseWhether the user should be unable to change the field value.
namestring—Identifies the field when a form is submitted.
valuestring—The OTP value.
defaultValuestring—The uncontrolled OTP value when the component is initially rendered.
onValueChange| ((value: string, eventDetails: OTPFieldRoot.ChangeEventDetails) => void)—Callback fired when the OTP value changes.
The eventDetails.reason indicates what triggered the change: - 'input-change' for typing or autofill - 'input-clear' when a character is removed by text input - 'input-paste' for paste interactions - 'keyboard' for keyboard interactions that change the value
onValueInvalid| ((value: string, eventDetails: OTPFieldRoot.InvalidEventDetails) => void)—Callback fired when entered text contains characters that are rejected by validation or normalization before the OTP value updates.
The value argument is the attempted user-entered string before normalization.
onValueComplete| ((value: string, eventDetails: OTPFieldRoot.CompleteEventDetails) => void)—Callback function that is fired when the OTP value becomes complete, or when a complete value is pasted while the OTP is already complete.
When the value changes, it runs later than onValueChange, after the internal value update is applied. If a complete pasted value matches the current value, onValueChange does not fire.
If autoSubmit is enabled, it runs immediately before the owning form is submitted.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-completePresent when all slots are filled.
data-disabledPresent when the OTP field is disabled.
data-readonlyPresent when the OTP field is readonly.
data-requiredPresent when the OTP field is required.
data-validPresent when the OTP field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the OTP field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the OTP field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the OTP field's value has changed (when wrapped in Field.Root).
data-filledPresent when the OTP field contains at least one character.
data-focusedPresent when one of the OTP field inputs is focused.
<OTPField.Input>
An individual OTP character input. Renders an <input> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-completePresent when all slots are filled.
data-filledPresent when the input contains a character.
data-disabledPresent when the OTP field is disabled.
data-readonlyPresent when the OTP field is readonly.
data-requiredPresent when the OTP field is required.
data-validPresent when the OTP field is in a valid state (when wrapped in Field.Root).
data-invalidPresent when the OTP field is in an invalid state (when wrapped in Field.Root).
data-touchedPresent when the OTP field has been touched (when wrapped in Field.Root).
data-dirtyPresent when the OTP field's value has changed (when wrapped in Field.Root).
data-focusedPresent when any OTP field input is focused.
<OTPField.Separator>
A separator element accessible to screen readers. Renders a <div> element.
orientationOrientation'horizontal'The orientation of the separator.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.