Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Input OTP Primitive

Use Input OTP to enter a short code across visible slots. One input receives typing and paste so the slots represent a single value.

Enter the four digits from your email.

Anatomy

Use the Astro primitive adapter to render InputOtp anatomy with the Runtime wiring included.

---
import { InputOtp } from "@starwind-ui/astro/input-otp";
---
<InputOtp.Root>
<InputOtp.Group>
<InputOtp.Slot index={0} />
<InputOtp.Separator />
<InputOtp.Slot index={1} />
</InputOtp.Group>
</InputOtp.Root>

Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโ€™s types for the available props and events. Plain HTML uses DOM events and Runtime methods.

API Reference

Root

The main element that owns the Input OTP Runtime instance.

Default element
div
Discovery hook
data-sw-input-otp
Role
-

Props

Prop Type Default Toggle
value string -
Description
Controls the current Input OTP value.
Kind
control
Targets
-
Full type
string
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-value
  • starwind:value-change
  • setValue
defaultValue string -
Description
Sets the initial Input OTP value for uncontrolled usage.
Kind
control
Targets
-
Full type
string
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-value
  • starwind:value-change
  • setValue
disabled boolean false
Description
Disables the Root part.
Kind
option
Targets
-
Full type
boolean
form string -
Description
Associates the control with a form element.
Kind
option
Targets
-
Full type
string
id string -
Description
Sets the id used by the associated native control.
Kind
option
Targets
-
Full type
string
maxLength number 6
Description
Configures the max length option for the Root part.
Kind
option
Targets
-
Full type
number
name string -
Description
Sets the submitted form field name.
Kind
option
Targets
-
Full type
string
pattern RegExp | string -
Description
Configures the pattern option for the Root part.
Kind
option
Targets
-
Full type
RegExp | string
readOnly boolean false
Description
Marks the control as read-only.
Kind
option
Targets
-
Full type
boolean
required boolean false
Description
Marks the form control as required.
Kind
option
Targets
-
Full type
boolean
onValueChange (value: string, details: InputOtpValueChangeDetails) => void -
Description
Runs when the Input OTP value changes.
Kind
callback
Targets
-
Full type
(value: string, details: InputOtpValueChangeDetails) => void
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-value
  • starwind:value-change
  • setValue

Events

Event Callback Value Toggle
valueChange onValueChange value: string
Description
Fires when the value changes for Input OTP.
DOM event
starwind:value-change
Details type
InputOtpValueChangeDetails
Timing
before-state-commit
Cancelable
Yes
Cancellation sequence
  1. Check internal eligibility and intent.
  2. Create one details object for the proposal.
  3. Call the Runtime callback with the details object when the controller exposes one.
  4. Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
  5. Read details.isCanceled, including cancellation caused by preventDefault().
  6. Apply the accepted state.
  7. Notify Runtime subscribers and other accepted-only observers.

State

State Type Props Toggle
value string value / defaultValue
Description
Tracks the current Input OTP value.
Initial attribute
data-value
Runtime getter
getValue
Runtime setter
setValue
React

Use value for controlled state and defaultValue for default state, and onValueChange for change proposals.

  • value
  • defaultValue
  • onValueChange
Astro

Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.

  • value
  • defaultValue
  • starwind:value-change
  • setValue
Runtime / HTML

Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.

  • data-value
  • starwind:value-change
  • setValue

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-input-otp - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-value - Reflects the value state on the Root part.
Metadata
Attribute Value Description
data-default-value - Reflects the default value prop on the Root part.
data-disabled - Reflects the disabled prop on the Root part.
data-form - Reflects the form prop on the Root part.
data-id - Reflects the id prop on the Root part.
data-max-length - Reflects the max length prop on the Root part.
data-name - Reflects the name prop on the Root part.
data-pattern - Reflects the pattern prop on the Root part.
data-readonly - Reflects the readonly prop on the Root part.
data-required - Reflects the required prop on the Root part.

Input

The native input synchronized by Input OTP.

Default element
input
Discovery hook
data-sw-input-otp-input
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-input-otp-input - Marks the Input part so Starwind Runtime can find it.

Group

Groups related Input OTP items.

Default element
div
Discovery hook
data-sw-input-otp-group
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-input-otp-group - Marks the Group part so Starwind Runtime can find it.

Slot

A character slot inside Input OTP.

Default element
div
Discovery hook
data-sw-input-otp-slot
Role
-

Props

Prop Type Default Toggle
index number -
Description
Configures the index option for the Slot part.
Kind
option
Targets
slot
Full type
number
caret React, Vue, Svelte only React.ReactNode -
Description
Changes how the Slot part is rendered.
Kind
rendering
Targets
slot
Frameworks
React, Vue, Svelte
Full type
React.ReactNode

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-input-otp-slot - Marks the Slot part so Starwind Runtime can find it.
Metadata
Attribute Value Description
data-index - Reflects the index prop on the Slot part.

Slot Char

The visible character rendered in a Input OTP slot.

Default element
span
Discovery hook
data-sw-input-otp-char
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-input-otp-char - Marks the Slot Char part so Starwind Runtime can find it.

Slot Caret

The caret shown inside the active Input OTP slot.

Default element
div
Discovery hook
data-sw-input-otp-caret
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-input-otp-caret - Marks the Slot Caret part so Starwind Runtime can find it.

Separator

Separates groups of Input OTP items.

Default element
div
Discovery hook
data-sw-input-otp-separator
Role
separator

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-input-otp-separator - Marks the Separator part so Starwind Runtime can find it.

Runtime API

Factory
createInputOtp
Import
@starwind-ui/runtime/input-otp
Root hook
root data-sw-input-otp
Option props
defaultValue, disabled, form, id, maxLength, name, onValueChange, pattern, readOnly, required, value

Option Lifecycles

OptionLifecycle
defaultValueconstructor-only
disabledsetter-backed
formsetter-backed
idsetter-backed
maxLengthrefresh-required
namesetter-backed
onValueChangeconstructor-only
patternconstructor-only
readOnlyconstructor-only
requiredsetter-backed
valuesetter-backed

Runtime Setters

Method Target Description
setValue state: value Updates the current Input OTP value from Runtime code.
setDisabled prop: disabled Updates whether Input OTP is disabled from Runtime code.
setFormOptions props: form, id, name, required Updates Input OTP form-related options from Runtime code.

Form Participation

FactValue
Form propsform, id, name, required, value
Hidden inputinput (text)
Field integrationYes
Component Relationship
Input Otp Direct primitive

Changelog

View version history v1.2.0 6 releases

v1.2.0

  • Input OTP arrow keys follow visible slot positions in RTL. The stored value and paste order stay the same.
  • Input OTP now shows a `value` that the parent sets after the first render. This applies when `value` starts as `undefined` and gets a value later. Before, React and Vue ignored that value. A value that needs no correction does not fire a change callback, a change event, or a model update. If the Runtime corrects the value, Vue `v-model` and Svelte `bind:value` receive the corrected value, so your state matches what the user sees.
  • After that late value, a one-way parent that renders again with the same old value no longer undoes the user's last change.
  • In React, Input OTP with a parent-held `value` now goes back to the parent value when the parent refuses a change. Before, the refused text stayed in the input and in form data.
  • In Vue, this component now needs Vue 3.5.21 or newer.

v1.1.0

  • In Astro, an attribute that you set on an Input OTP part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
  • In Svelte, a defined `value` that you pass when InputOtp mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, InputOtp keeps its own state.
  • In React, Input OTP inside a `Field` with a `name` now sends its value when the form submits. Before, React removed the name from the hidden input each time it rendered.

v1.0.1

  • React and Vue Input OTP now restore the original default value after a form reset, including after `readOnly` changes.
  • The initial native input value in Vue now matches the value supplied by your app.
  • Reduced the JavaScript loaded by Input OTP when it is used without Field.
  • Reduced repeated JavaScript when Astro Input OTP is used with other Starwind components.

v1.0.0

  • This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.

v0.1.1

  • The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
  • In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.

v0.1.0

  • First release. Input OTP splits its value into slots and handles the keyboard and paste. It sends its value with the form and fires value-change events.