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>Use the React primitive adapter when InputOtp state participates in React rendering.
import { InputOtp } from "@starwind-ui/react/input-otp";
export function Example() { return ( <InputOtp.Root> <InputOtp.Group> <InputOtp.Slot index={0} /> <InputOtp.Separator /> <InputOtp.Slot index={1} /> </InputOtp.Group> </InputOtp.Root> );}Use the Vue components to build InputOtp.
<script setup lang="ts">import InputOtp from "@starwind-ui/vue/input-otp";</script>
<template> <InputOtp.Root> <InputOtp.Group> <InputOtp.Slot :index="0" /> <InputOtp.Separator /> <InputOtp.Slot :index="1" /> </InputOtp.Group> </InputOtp.Root></template>Use the Svelte components to build InputOtp.
<script lang="ts"> import { InputOtp } from "@starwind-ui/svelte/input-otp";</script>
<InputOtp.Root> <InputOtp.Group> <InputOtp.Slot index={0} /> <InputOtp.Separator /> <InputOtp.Slot index={1} /> </InputOtp.Group></InputOtp.Root>Render the InputOtp data-sw-* contract yourself, then initialize createInputOtp.
<div data-sw-input-otp> <input data-sw-input-otp-input autocomplete="one-time-code" class="sr-only" tabindex="-1" /> <div data-sw-input-otp-group> <div data-sw-input-otp-slot data-index="0"> <span data-sw-input-otp-char></span> <div data-sw-input-otp-caret class="pointer-events-none absolute inset-0 hidden items-center justify-center" hidden></div> </div> <div data-sw-input-otp-separator role="separator" aria-hidden="true"></div> <div data-sw-input-otp-slot data-index="1"> <span data-sw-input-otp-char></span> <div data-sw-input-otp-caret class="pointer-events-none absolute inset-0 hidden items-center justify-center" hidden></div> </div> </div></div>
<script type="module"> import { createInputOtp } from "@starwind-ui/runtime/input-otp";
const root = document.querySelector("[data-sw-input-otp]"); if (root) { createInputOtp(root); }</script>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
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.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-valuestarwind:value-changesetValue
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.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-valuestarwind:value-changesetValue
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.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-valuestarwind:value-changesetValue
Events
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
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
State
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.
valuedefaultValueonValueChange
- Astro
Use value or defaultValue for initial state, listen for starwind:value-change, and call setValue for later updates.
valuedefaultValuestarwind:value-changesetValue
- Runtime / HTML
Use data-value for initial state, listen for starwind:value-change, and call setValue for later updates.
data-valuestarwind:value-changesetValue
Data Attributes
Runtime hooks
State
Metadata
Input
The native input synchronized by Input OTP.
- Default element
- input
- Discovery hook
- data-sw-input-otp-input
- Role
- -
Data Attributes
Runtime hooks
Group
Groups related Input OTP items.
- Default element
- div
- Discovery hook
- data-sw-input-otp-group
- Role
- -
Data Attributes
Runtime hooks
Slot
A character slot inside Input OTP.
- Default element
- div
- Discovery hook
- data-sw-input-otp-slot
- Role
- -
Props
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
Metadata
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
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
Separator
Separates groups of Input OTP items.
- Default element
- div
- Discovery hook
- data-sw-input-otp-separator
- Role
- separator
Data Attributes
Runtime hooks
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
| Option | Lifecycle |
|---|---|
| defaultValue | constructor-only |
| disabled | setter-backed |
| form | setter-backed |
| id | setter-backed |
| maxLength | refresh-required |
| name | setter-backed |
| onValueChange | constructor-only |
| pattern | constructor-only |
| readOnly | constructor-only |
| required | setter-backed |
| value | setter-backed |
Runtime Setters
Form Participation
| Fact | Value |
|---|---|
| Form props | form, id, name, required, value |
| Hidden input | input (text) |
| Field integration | Yes |
Related Styled Components
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.