OTP input
Visual digit cells backed by one native text input for paste and system one-time-code autofill.
npx @swipeui/cli add otp-inputWhen to use it
- Verifying a phone number or email with a 4- or 6-digit code.
- Two-factor sign-in steps.
- Any code that iOS or Android can autofill from a message.
- Codes people paste from another app.
When not to use it
- Passwords or PINs people choose. Use Password field.
- Long codes with letters, such as licence keys. Use Text field.
- Numbers with meaning, like amounts. Use Number field.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add otp-input- Adds
components/swipeui/ui/otp-input.tsxand 13 shared helpers. - Installs clsx, tailwind-merge, react-native-reanimated, react-native-worklets, uniwind, expo-haptics, class-variance-authority, react-native-svg. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import { OtpInput } from '@/components/swipeui/ui/otp-input';
<OtpInput label="Verification code" length={6} onComplete={verify} />;Examples
Use the variant and appearance controls in the live specimen to compare the documented states. The application owns validation, operation results and persistence.
API reference
OtpInputProps
| Prop | Type | Default | Description |
|---|---|---|---|
label? | | — | Visible label, or inherit an enclosing Field. |
helper? | | — | Instructions under the field; an error or success message replaces it. |
error? | | — | Validation message: a destructive ring, an alert glyph, one shake and an error haptic. |
success? | | — | Confirmation under the field, such as "Username available", in the success colour. |
status? | | |
|
required? | | | Required; "Required" is spoken, and |
requiredIndicator? | | | How a required label is marked: a muted asterisk, the word, or nothing. |
optionalIndicator? | | | Show a muted "Optional" after the label. |
disabled? | | | Disable editing and accessory actions. |
disabledStyle? | | |
|
appearance? | | | The surface: |
size? | | | Height: |
shape? | | |
|
labelPlacement? | | |
|
focusRing? | | | The focus indicator: a primary ring with a soft halo, the ring alone, or none. |
messageMorph? | | | How a changed message arrives: rises in, letter by letter, or at once. |
reserveMessageSpace? | | | Keep one line of room under the field so forms don't move as messages appear. |
displayValue? | | — | Decorative rendering for an unfocused value; the native text remains the accessible/editable source. |
leadingIcon? | | — | A leading icon: a node, or a function given the state colour and size. It turns primary while focused. |
trailingIcon? | | — | A trailing icon: a node, or a function given the state colour and size. |
clearable? | | | 1.x switch: |
clearButton? | | | When a "Clear text" button shows. |
undoClear? | | | After clearing, the button becomes "Undo clear" for a moment. Phones have no ⌘Z. |
undoClearWindow? | | | How long undo stays, in milliseconds. |
counter? | | | A rolling character counter under the field, using |
tintCaret? | | | Caret and selection in the primary colour. |
iconTint? | | | The leading icon turns primary while focused ( |
shakeOnError? | | | Shake once for each new error. Reduce Motion keeps it still. Shake the cells, with an error haptic, each time a new error arrives. |
messages? | | | Built-in strings, for translation. |
nextRef? | | — | Ref focused when Return is pressed; otherwise a single-line field dismisses. |
haptics? | | | All feedback, or each part: |
motion? | | | Ring, label and message motion. |
className? | | — | Classes on the outer field. |
controlClassName? | | — | Classes on the field surface. |
inputClassName? | | — | Classes on the editable text. |
style? | | — | Outer field style. |
inputStyle? | | — | Native editable text style. |
value? | | — | Digit string. |
defaultValue? | | | Initial digit string. |
onValueChange? | | — | Reports normalized digits. |
length? | | | Number of digits. |
onComplete? | | — | Called once per newly completed digit string, never on initial render. |
align? | | | Where the row of cells sits; the label and message follow. |
Native view/input props not listed here pass through where the exported TypeScript type allows them.
OTP input settings
| Prop | Does | Default |
|---|---|---|
| shakeOnError | Shake the cells once per new error | true |
| align | Cells, label and message at the start, center or end | center |
| haptics | false, or { error } | on |
Composition and behavior
- length sets the number of boxes (6 by default).
- value and onValueChange hold the digits.
- onComplete fires once per completed code.
- Uses one-time-code autofill hints on both platforms.
Behavior and gestures
- Digits appear in separate boxes, but there's one real input, so pasting a whole code fills every box.
- The keyboard offers the code from your messages on iOS and Android.
- When the last digit arrives, onComplete runs once, and the keyboard closes unless you pass nextRef.
- Pre-filled or disabled codes never trigger onComplete automatically.
- Non-digit characters are ignored.
Accessibility
- Screen readers see one labelled input instead of separate boxes.
- Helper and error text are read as the hint.
- The boxes are visual only and hidden from assistive tech.
Motion
- Static on its own; any movement comes from the parent that animates it.
- The active box highlights without animation.
Performance
- One native input; boxes re-render only when digits change.
Notes for AI
- Set length to match your code; verify in onComplete.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.