A modern, responsive two-factor authentication (2FA) UI component built with Svelte and TailwindCSS. This project provides a clean, user-friendly interface for entering 2FA verification codes with animations, validation, and keyboard navigation support.
# Clone the repository
git clone https://github.com/XDevRabi/otp-input-ui-svelte
cd otp-input-ui-svelte
# Install dependencies
npm install
# Start the development server
npm run dev
├── src/
│ ├── lib/ # Component library
│ │ ├── otp-form.svelte # Main OTP form container
│ │ └── otp-input.svelte # OTP input fields implementation
│ ├── store/ # State management
│ │ └── otpStore.js # OTP state and validation logic
│ ├── App.svelte # Root application component
│ ├── app.css # Global styles and theme variables
│ └── main.js # Application entry point
├── public/ # Static assets
└── index.html # HTML entry point
The root component that renders the OTP form and provides a test code for demonstration purposes.
The main container component that displays:
Handles the input fields for the 6-digit code with features:
Manages the state using Svelte's store:
code
: Array of 6 digits entered by the userdigitsLeft
: Derived store calculating remaining digits to be enteredisCodeComplete
: Derived store indicating if all 6 digits are enteredisVerified
: Derived store validating if the entered code matches "628593"The OTP input component implements several user-friendly behaviors:
The application uses Svelte's built-in store for state management:
// otpStore.js
import { writable, derived } from "svelte/store";
export const code = writable(["", "", "", "", "", ""]);
export const digitsLeft = derived(code, ($code) => 6 - $code.filter(digit => digit !== "").length);
export const isCodeComplete = derived(digitsLeft, $digitsLeft => $digitsLeft === 0);
export const isVerified = derived(code, $code => $code.join("") === "628593");
The OTP input supports the following keyboard interactions:
The UI uses TailwindCSS for styling. You can customize the appearance by modifying the theme variables in src/app.css
:
@theme{
--color-primary-blue: #038aff;
--color-neutral-1: #e9ebef;
--color-neutral-2: #777777;
--color-success: #47ac67;
--color-error: #ff4259;
--font-poppins: "Poppins", sans-serif;
--font-outfit: "Outfit", sans-serif;
}
# Start development server
npm run dev
# Build for production
npm run build
# Preview production build
npm run preview