Skip to contents

Input OTP

A one-time password input that types into one input and draws each character in its own slot.

InputOTP ports input-otp 1.4.2, which Sveltery UI’s input OTP is built on. One real <input> sits over the slots, so typing, paste, selection and autofill are the browser’s own. For the Base UI part with one input per slot, see OTP Field.

Anatomy

Import the component and draw the slots from its state:

Anatomy

Examples

Slots as children

Without render, children renders. A slot component reads the state with getInputOTPContext():

Slot.svelte

Patterns

pattern drops input that doesn’t match. REGEXP_ONLY_DIGITS, REGEXP_ONLY_CHARS and REGEXP_ONLY_DIGITS_AND_CHARS are exported:

Letters and digits

Reading the input

Rest props go to the input, attachments included:

Focus on mount

API reference

Root

Holds the slots and the input that types into them. Rest props go to the input. Renders a <div> element.

defaultValue string ''
Description

The code an empty value starts from.

Type
string
Default
''
value string —
Name
Description

The code.

Type
string
Binding
bind:value
containerClass ClassValue | null —
Description

Class for the container that holds the slots and the input.

Type
ClassValue | null
maxlength* number —
Description

The number of slots.

Type
number
noScriptCSSFallback string | null —
Description

CSS that shows the plain input when JavaScript is off. null leaves it out.

Type
string | null
onComplete function —
Description

Called with the value when the last slot fills.

Type
(value: string) => void
pasteTransformer function —
Description

Rewrites pasted text before it is inserted.

Type
(pasted: string) => string
pattern string | RegExp —
Name
Description

Every value must match it, for example REGEXP_ONLY_DIGITS. Input that doesn't match is dropped.

Type
string | RegExp
placeholder string —
Description

Characters shown in the empty slots, one per slot, through placeholderChar.

Type
string
pushPasswordManagerStrategy 'increase-width' | 'none' 'increase-width'
Description

Widens the input so a password manager's badge sits outside the slots.

Type
'increase-width' | 'none'
Default
'increase-width'
textAlign 'left' | 'center' | 'right' 'left'
Description

The text alignment of the input.

Type
'left' | 'center' | 'right'
Default
'left'
render Snippet<[state: InputOTPRootState]> —
Name
Description

Renders the slots from the state. Without it, children renders and reads the state with getInputOTPContext().

Type
Snippet<[state: InputOTPRootState]>
slots InputOTPSlotState[]
Name
slots
Description

One entry per slot.

Type
InputOTPSlotState[]
isFocused boolean
Name
isFocused
Description

Whether the input is focused.

Type
boolean
isHovering boolean
Name
isHovering
Description

Whether the pointer is over the input and it is not disabled.

Type
boolean
data-input-otp
Present on the input.
data-input-otp-container
Present on the container that holds the slots and the input.
data-input-otp-mse
The end of the selection the slots mirror, set on the input.
data-input-otp-mss
The start of the selection the slots mirror, set on the input.
data-input-otp-placeholder-shown
Present on the input while the value is empty.
--root-height
The height of the input, set on the container.