Skip to content

NewMCP server for your coding agent. Set it up ›

Nexus UI
Get Pro

OTP Input, animated React component

A six-box one-time-code field: digits spring in as you type, the boxes ripple while the code is checked, shake on a wrong code, and fold into a Verified pill on success. Also includes OTP Code, a field with no confirm button whose digits roll in and whose boxes slide together into one bar while it checks, then turn green or shake apart.

  • Free
  • Inputs & Controls
  • Spring

Ticks quietly for each digit, chimes when the code verifies and bumps on a wrong code. Sound is off on this site until you switch it on.

Types with a key click for every character, in sync with the text. The click stays off until you switch it on.

OTP Input

OTP Autofill

Underline OTP

OTP Code

Like OTP Input? 301 more are in Pro.

Pro unlocks all 301 Pro components, every template and block, and everything we release next. OTP Input stays free.

Props

PropTypeDefaultWhat it does
length6Number of digits. The boxes split into two groups at the middle.
verify—Return true when the code is valid. Runs once every box is filled.
onVerified—Called after a code verifies.
onResend—Called when “Resend code” is pressed.
autoplay—Types this code by itself, on a loop, until someone clicks in.
accent"#0071e3"Focus ring, caret and success colour.
OtpAutofill.code"482913"The six digit code carried by the pretend text message and the only code that verifies. Anything that is not six digits falls back to the default.
OtpAutofill.autoplayfalsePlays the message, Fill, verify and start over on a loop until someone interacts. Stays off under reduced motion and pauses off screen or in a hidden tab.
OtpAutofill.onVerifiedundefinedCalled once the code verifies.
OtpAutofill.onResendundefinedCalled when Resend code is pressed.
OtpAutofill.className""Extra classes for the card.
OtpLine.code"905172"The six digit code that verifies. Anything that is not six digits falls back to the default.
OtpLine.autoplayfalseTypes a wrong code and then the right one on a loop until someone interacts. Stays off under reduced motion and pauses off screen or in a hidden tab.
OtpLine.onVerifiedundefinedCalled once the right code is entered.
OtpLine.className""Extra classes for the card.
OtpCode.length6Number of digits.
OtpCode.destination"+44 •••• ••2048"Where the code was sent, shown masked.
OtpCode.verify(c) => new Promise((r) => setTimeout(() => r(c === DEMO), 900))Checks a code; resolve true when it is right. The demo accepts 246810.
OtpCode.onVerified—Called with the code once it verifies.
OtpCode.resendAfter30Seconds before a new code can be sent.
OtpCode.enableSoundtrueSoft synthesised sounds as you use it. They also follow the site-wide sound switch.
OtpCode.volume0.6Sound level, 0 to 1.
OtpCode.autoplayfalsePlays a short demo by itself until the visitor touches it. Never runs under reduced motion.
OtpCode.className""Extra classes for the outer element.
enableSoundtruePlays interface sounds on interaction. They follow the sound switch (setSoundsEnabled, on by default) and stay silent on hover. Autoplay makes the same sounds once the visitor has used the page. Pass false to silence this component.

Add it to your project

  1. 1

    Set up your project once

    You need a Next.js project with Tailwind CSS and the @/ import alias. In its root folder, run this once. It creates components.json and the cn() helper in src/lib/utils.ts:

    npx shadcn@latest init -d

    Starting from nothing? The Quick Start walks through the whole thing.

  2. 2

    Connect the registry

    Run this once per project. It writes the Nexus UI entry into components.json for you:

    npx shadcn@latest registry add "@nexus-labs=https://www.nexus-ui.com/api/registry/{name}"

    Type it exactly as shown. {name} is filled in by the CLI with the component you ask for, so you never replace it.

  1. 3

    Add the component

    Run this from your project root. It adds the component, the helpers it imports and its npm packages.

    npx shadcn@latest add @nexus-labs/otp-input
  2. 4

    Check what was added

    With a project made as in the Quick Start (a src/ folder), these files land in it:

    components/
    otp-input/
    index.ts
    otp-autofill.tsx
    otp-code.tsx
    otp-input.tsx
    otp-line.tsx
  3. 5

    Copying by hand? Skip the registry

    Instead of steps 2 and 3 you can copy the files yourself, with no registry and no API key. Step 1 still applies, so that cn() exists. Install the packages:

    npm install framer-motion

    Then show the source and create each file at the path it gives:

  4. 6

    Use it in a page

    Installing only adds the files. Import the component in a page and render it. The Usage tab on the preview shows a fuller example with props:

    // src/app/page.tsx
    import { OtpInput } from "@/components/otp-input";
    
    export default function Page() {
      return (
        <main>
          <OtpInput />
        </main>
      );
    }

Like OTP Input? 301 more are in Pro.

Pro unlocks all 301 Pro components, every template and block, and everything we release next. OTP Input stays free.

OTP Input questions

How do I install the OTP Input and what does it export?

Run `npx shadcn@latest add @nexus-labs/otp-input`. It depends on framer-motion and exports three variants from the same folder: `OtpInput` (six boxes), `OtpAutofill` (a pretend text message with a Fill button) and `OtpLine` (underlines without boxes).

How do I verify the code against my backend?

Pass `verify` to `OtpInput` as a function that takes the code and returns a boolean or a promise of one; it runs once every box is filled, after a short "Checking…" state. By default `verify` only accepts the demo code "482913", so always replace it. `onVerified` fires on success and `onResend` fires when Resend code is pressed.

Does it support SMS code autofill and a custom length?

Yes. The boxes are drawn over one real input with `autoComplete="one-time-code"` and `inputMode="numeric"`, which is what browsers use to offer a code from a text message, and non-digit characters are stripped. `length` sets the number of digits (default 6) and `accent` sets the focus ring, caret and success color (default `#0071e3`); `OtpAutofill` and `OtpLine` are fixed at six digits.

What happens on a wrong code, and how does it handle accessibility and reduced motion?

A wrong code shakes the row, shows "That code didn’t match. Try again." and clears the boxes, while a right code folds the boxes into a Verified pill with `role="status"`. The input has a screen-reader label, "One-time code". `OtpAutofill` and `OtpLine` never run their autoplay demo under reduced motion (and `OtpLine` skips its shake), but the main `OtpInput` animations do not check that setting.

Does it make sound, and how do I turn it off?

Ticks quietly for each digit, chimes when the code verifies and bumps on a wrong code. Sounds are synthesised with the Web Audio API, so there are no audio files, and they only play after a click or key press. Pass `enableSound={false}` to silence this component, or call `setSoundsEnabled(false)` from `@/lib/ui-sounds` to switch every sound off at once. The CLI installs `@/lib/ui-sounds` with the component.