Skip to content
Mosaic
On this page

Otp

A fixed-length verification code field: one box per character. Focus moves on as the user types, and a pasted code fills every box. The ref points at the first box.

Use for

  • a one-time code input
  • an email or SMS verification code
  • a TOTP code from an authenticator app

Practices

GuidancePractice
DoRender it inside Field.Root so the label targets the first box and errors mark it invalid.
DoPass name when it sits in a plain <form>. A hidden input submits the whole code.
Don’tBuild a verification step from it by hand. Use the steps in src/blocks/verification/, such as VerificationCode.

API reference

Otp

A role="group" of inputs. Carries data-status and data-disabled; each box is a cl-otp-slot.

Slot: cl-otp

PropTypeDescription
lengthnumberThe number of boxes.Default: 6
valuestringThe code, when controlled.
defaultValuestringThe code on mount, when uncontrolled.Default: ''
onValueChange(value: string) => voidCalled with the whole code on every change.
onComplete(value: string) => voidCalled with the code once every box is filled.
pattern'numeric' | 'alpha' | 'alphanumeric'numeric, alpha or alphanumeric. Other characters are dropped.Default: 'numeric'
maskbooleanMasks each box like a password.Default: false
statusOtpStatusColors every box for the outcome: neutral, success or error.Default: `error` when the enclosing `Field` is invalid, else `neutral`
namestringSubmits the code under this name through a hidden input.

Examples

A code field

TSX
<Field.Root>
  <Field.Label>Verification code</Field.Label>
  <Otp onComplete={onSubmitCode} />
</Field.Root>