suiss/ui ยท Interaction
OTP Autofill
A one-time code field that offers an incoming code, fills it digit by digit, and verifies it automatically.
OTP Autofill is an open-source interaction from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
OtpAutofill builds on the suiss/ui InputOTP component to recreate the iOS security code autofill flow. When the field is focused and empty, OtpAutofillSuggestion offers the received code; choosing it types the digits in with a short pop animation and calls onVerify once all slots are filled. A valid code turns the slots green and locks the field; an invalid one shakes the slots, marks them red, then clears the field and refocuses it.
The suggestion adapts to the device through useIsMobile. Below 48rem it docks above the on-screen keyboard like the QuickType bar, tracking the visual viewport; on wider screens it appears as an inline pill under the field. In supporting browsers, the WebOTP API also reads codes from SMS and fills them automatically.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
Usage
Examples
Wrong code
A rejected code shakes the field, shows an error, clears, and accepts a code typed by hand.
Accessibility
- Label the field with
aria-labelonOtpAutofillInput, which renders a single text input. - The input is read-only while filling, verifying, or verified, and sets
aria-invalidwhen the code is rejected. - The suggestion is a native button that is
inertwhile hidden, and it prevents focus from leaving the field on press. - After a rejected code, focus returns to the field. Pair the root with a
role="status"element to announce the result.
API Reference
OtpAutofill
The root that holds the code value and verification state.
| Prop | Type | Default |
|---|---|---|
maxLength | number | Required |
onVerify | (code: string) => Promise<boolean> | boolean | Required |
webOtp | boolean | true |
Also accepts the props of React.ComponentProps<"div">.
Place OtpAutofillInput and OtpAutofillSuggestion inside OtpAutofill. The value is managed internally. onVerify is called with the complete code once it reaches maxLength, whether it was filled, typed or pasted; return or resolve true to accept it or false to reject it. A paste of several characters fills with the same animation. Use useOtpAutofill to read state (idle, filling, verifying, verified, invalid) for status text or other UI. webOtp listens for SMS codes through the WebOTP API, where supported.
The root sets data-slot="otp-autofill" and data-state. The pop and shake animations are skipped under prefers-reduced-motion.
OtpAutofillInput
The InputOTP field wired to the autofill state.
| Prop | Type | Default |
|---|---|---|
containerClassName | string | - |
children | React.ReactNode | Required |
Also accepts the props of React.ComponentProps<"input">.
Pass InputOTPGroup, InputOTPSlot and InputOTPSeparator from input-otp as children. containerClassName styles the slot container and is merged with the state styles.
OtpAutofillSuggestion
The button that offers the received code and fills it when pressed.
| Prop | Type | Default |
|---|---|---|
code | string | null | undefined | Required |
source | React.ReactNode | "From Messages" |
Also accepts the props of React.ComponentProps<"button">.
Set code to null until a code arrives; the suggestion stays hidden without one. source names where the code came from. The suggestion sets data-slot="otp-autofill-suggestion", data-placement (keyboard or inline) and data-closed while hidden.
Web preview
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
OtpAutofill wraps an InputOTP and runs it through idle, filling, verifying, verified and invalid states. When the input is focused and empty, OtpAutofillSuggestion slides up above the keyboard with the incoming code; pressing it types the code one digit at a time.
When all digits are entered, onVerify runs and the slots dim while it is pending. A valid code turns the slots green and locks the input. An invalid code turns them red and shakes the row, then clears the input and refocuses it. Pasting or a system autofill of several digits plays the same digit-by-digit fill.
Pass maxLength and onVerify, a suspending function that returns whether the code is right. Put InputOTPGroup and InputOTPSlots inside OtpAutofillInput, and render an OtpAutofillSuggestion with the code your app received; it hides when code is empty.
Installation
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/otp-autofill.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/otp-autofill.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/otp-autofill.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
Examples
Wrong code
An incoming code that fails verification, showing the shake and reset.
Web preview
This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
Four digits
A four-digit code with its own suggestion.

This code uses Tailwind classes that need Uniwind or NativeWind. With StyleSheet, pass the same values through the style props.
Accessibility
accessibilityLabelonOtpAutofillInputis passed to the underlying text input.- The suggestion bar has the
buttonrole. - The input is read-only while filling, verifying and after a valid code.
API Reference
OtpAutofill
The root that holds the code and verification state.
| Prop | Type | Default |
|---|---|---|
maxLength | number | Required |
onVerify | (code: string) => Promise<boolean> | boolean | Required |
Also accepts the props of React.ComponentProps<typeof View>.
maxLength is the number of digits in the code. onVerify checks a complete code, returning a boolean or a promise of one; return true when it is valid. useOtpAutofill returns state, value, fill and inputRef for custom controls.
OtpAutofillInput
The InputOTP that shows each state and shakes on an invalid code.
| Prop | Type | Default |
|---|---|---|
containerClassName | string | - |
children | React.ReactNode | Required |
Also accepts the props of React.ComponentProps<typeof InputOTP>.
Put InputOTPGroup and InputOTPSlots inside it. containerClassName styles the animated container around the input. The input sets textContentType="oneTimeCode" and the platform's one-time code autoComplete, so the system can also offer the code. Slide and shake animations respect the system reduce motion setting.
OtpAutofillSuggestion
A bar above the keyboard that fills the suggested code on press.
| Prop | Type | Default |
|---|---|---|
code | string | null | undefined | Required |
source | React.ReactNode | "From Messages" |
portalHost | string | - |
className | string | - |
Also accepts the props of React.ComponentProps<typeof Pressable>.
Render it with the code your app received, or null to hide the bar. source is the label above the code. portalHost names a portal host to render into. The bar sits on the keyboard or above the bottom safe area.
OtpAutofillState
Values: .idle, .filling, .verifying, .verified, .invalid.
OtpAutofill
The root that holds the code and verification state.
| Parameter | Type | Default |
|---|---|---|
length | Int | - |
onVerify | (String) async -> Bool | - |
content | (OtpAutofillState) -> Content | - |
OtpAutofillInput
The InputOTP that shows each state and shakes on an invalid code.
| Parameter | Type | Default |
|---|---|---|
autoFocus | Bool | false |
content | () -> Content | - |
length | Int | - |
Overloads: init(autoFocus:content:), init(length:autoFocus:).
OtpAutofillSuggestion
A bar above the keyboard that fills the suggested code on press.
| Parameter | Type | Default |
|---|---|---|
code | String? | - |
source | LocalizedStringKey | "From Messages" |
OtpAutofillState
Values: Idle, Filling, Verifying, Verified, Invalid.
OtpAutofillScope
| Property | Type |
|---|---|
state | OtpAutofillState get() |
value | String get() |
maxLength | Int get() |
OtpAutofillScope.fill
| Parameter | Type | Default |
|---|---|---|
code | String | - |
OtpAutofill
The root that holds the code and verification state.
| Parameter | Type | Default |
|---|---|---|
maxLength | Int | - |
onVerify | suspend (String) -> Boolean | - |
modifier | Modifier | Modifier |
content | @Composable OtpAutofillScope.() -> Unit | - |
OtpAutofillInput
The InputOTP that shows each state and shakes on an invalid code.
Receiver: OtpAutofillScope.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
contentDescription | String? | null |
autoFocus | Boolean | false |
enabled | Boolean | true |
content | (@Composable RowScope.() -> Unit)? | null |
OtpAutofillSuggestion
A bar above the keyboard that fills the suggested code on press.
Receiver: OtpAutofillScope.
| Parameter | Type | Default |
|---|---|---|
code | String? | - |
modifier | Modifier | Modifier |
source | String | "From Messages" |
