Input OTP Primitive
Input OTP is a Starwind Runtime primitive in the form-value-control contract family. Astro and React share one semantic component API. React coordinates reactive state through controlled and default props plus callbacks. Astro renders initial state and coordinates later changes through DOM events and Runtime methods. Raw HTML uses Runtime attributes, DOM events, and imperative methods.
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> );}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>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.ReactNode -
- Description
- Changes how the Slot part is rendered.
- Kind
- rendering
- Targets
- slot
- 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.0.0 3 releases
v1.0.0
- Promoted this Primitive's vendoring version to the stable 1.0.0 baseline. Its existing API and Runtime behavior carry forward from the previous release.
v0.1.1
- Marked generated React Primitive files as client modules so vendored installs preserve client boundaries in React server frameworks.
- Kept generated React state-change callbacks and DOM events cancelable until the Runtime accepts the proposed state, then synchronized rendered state.
v0.1.0
- Introduced segmented value state, keyboard and paste handling, form participation, and value-change events.