erictsai83

OTP Input

Component

About

A fixed-slot one-time passcode input with paste, autofill, an error shake, and a staggered success wave.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds OTP Input to your project and fits it to your theme.

Code

How OTP Input is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

"use client";

import { useEffect, useRef, useState } from "react";

import { Button } from "@/components/ui/button";
import { OTPInput, type OTPStatus } from "@/components/ui/ericts-otp-input";

const demoValidOtp = "248917";
const demoExpiredOtp = "123456";
const maxOtpAttempts = 3;

export default function Preview() {
  const [value, setValue] = useState("");
  const [status, setStatus] = useState<OTPStatus>("idle");
  const [attemptsLeft, setAttemptsLeft] = useState(maxOtpAttempts);
  const [isVerifying, setIsVerifying] = useState(false);
  const verificationTimer = useRef<number | null>(null);
  const isLocked = attemptsLeft === 0;

  useEffect(() => {
    return () => {
      if (verificationTimer.current) {
        window.clearTimeout(verificationTimer.current);
      }
    };
  }, []);

  const clearVerificationTimer = () => {
    if (!verificationTimer.current) return;

    window.clearTimeout(verificationTimer.current);
    verificationTimer.current = null;
  };

  const verify = (code: string) => {
    if (isVerifying || isLocked || status === "success") return;

    clearVerificationTimer();
    setIsVerifying(true);
    setStatus("idle");

    verificationTimer.current = window.setTimeout(() => {
      verificationTimer.current = null;
      setIsVerifying(false);

      if (code === demoValidOtp) {
        setStatus("success");
        return;
      }

      setValue("");
      setAttemptsLeft((current) => Math.max(current - 1, 0));
      setStatus("error");
    }, 650);
  };

  const pasteCode = (code: string) => {
    if (isVerifying || isLocked || status === "success") return;

    setValue(code);
    verify(code);
  };

  const reset = () => {
    clearVerificationTimer();
    setValue("");
    setStatus("idle");
    setAttemptsLeft(maxOtpAttempts);
    setIsVerifying(false);
  };

  const attemptsMessage =
    attemptsLeft === 1
      ? "1 attempt remaining."
      : `${attemptsLeft} attempts remaining.`;
  const errorMessage =
    attemptsLeft > 0
      ? `Code expired. ${attemptsMessage}`
      : "Code expired. No attempts remaining.";

  return (
    <div className="mx-auto flex w-full max-w-md flex-col items-center gap-4">
      <OTPInput
        label="Security code"
        value={value}
        onChange={(nextValue) => {
          setValue(nextValue);
          if (status === "error") {
            setStatus("idle");
          }
        }}
        onComplete={verify}
        status={status}
        disabled={isVerifying || isLocked}
        errorMessage={errorMessage}
        // Success is a colour change plus the wave, and reduced motion removes
        // the wave. This line is what carries it in that case, and it is what
        // the live region announces.
        successMessage="Code verified."
      />

      <div
        role="group"
        aria-label="OTP demo actions"
        className="flex flex-wrap justify-center gap-2"
      >
        <Button
          type="button"
          variant="outline"
          size="sm"
          disabled={isVerifying || isLocked || status === "success"}
          onClick={() => pasteCode(demoValidOtp)}
        >
          Paste valid code
        </Button>
        <Button
          type="button"
          variant="outline"
          size="sm"
          disabled={isVerifying || isLocked || status === "success"}
          onClick={() => pasteCode(demoExpiredOtp)}
        >
          Paste expired code
        </Button>
        {/* Mounting Reset only once there is something to reset grew this row
            by a line on a phone and slid the whole demo up by 23px. It stays
            mounted and reserves its slot instead. */}
        <Button
          type="button"
          variant="ghost"
          size="sm"
          disabled={status !== "success" && !isLocked}
          onClick={reset}
        >
          Reset
        </Button>
      </div>
    </div>
  );
}

Dependencies

More by erictsai83

View all
  • Adaptive Drawer

    Adaptive Drawer

  • Status Button

    Status Button

  • Floating Shortcut Button

    Floating Shortcut Button

  • Filter Tags

    Filter Tags

  • Feedback Popover

    Feedback Popover

  • Expandable Toolbar

    Expandable Toolbar

  • Expandable Toggle Button

    Expandable Toggle Button

  • Arc Menu

    Arc Menu