A reusable OTP (One Time Password) input component built with Next.js, Tailwind CSS, and NextUI.
🚨🚨 Project under construction 🚨🚨
OTP NextJs Library
A reusable OTP (One Time Password) input component built with Next.js, Tailwind CSS, and NextUI.
Table of Contents
Installation
You can install the
next-otpnpm
If you are using npm as your package manager, you can install
next-otpnpm i next-otp
yarn
If you are using yarn as your package manager, you can install next-otp by running the following command in your terminal:
yarn add next-otp
pnpm
If you are using pnpm as your package manager, you can install next-otp by running the following command in your terminal:
pnpm add next-otp
Usage
React
Here's how you can use the OTP input component in your React project:
import React, { useState } from "react"; import OTP from "otp-next"; import "otp-next/styles.css"; const OTPExample = () => { const [otp, setOtp] = useState(""); const handleComplete = (otp) => { console.log("OTP complete:", otp); setOtp(otp); }; return ( <div className="flex flex-col items-center justify-center min-h-screen py-2"> <h1 className="text-2xl font-bold">OTP Input Example</h1> <OTP onComplete={handleComplete} /> <p className="mt-4">Entered OTP: {otp}</p> </div> ); }; export default OTPExample;
Next.js
Here's how you can use the OTP input component in your Next.js project:
"use client"; // NOTE - Client-side rendered component import React, { useState } from "react"; import OTP from "otp-next"; import "otp-next/styles.css"; const OTPExample = () => { const [otp, setOtp] = useState(""); const handleComplete = (otp: string) => { console.log("OTP complete:", otp); setOtp(otp); }; return ( <div className="flex flex-col items-center justify-center min-h-screen py-2"> <h1 className="text-2xl font-bold">OTP Input Example</h1> <OTP onComplete={handleComplete} /> <p className="mt-4">Entered OTP: {otp}</p> </div> ); }; export default OTPExample;
Props
| Prop | Type | Default | Description | | ------------- | ---------------------------------------------------------- | --------- | ------------------------------------------------------------------------------------------------------- | |
lengthnumberonComplete(otp: string) => voidnullacceptstringnumbersinputHeightinputWidthinputPropsspacingTypes
AcceptType
| Type key | Description | | -------------- | ------------------------------- | |
numberslettersalphanumericHeightClass
| Enum key | CSS Property Value Height | | ---------- | ------------------------- | |
h-0h-1h-2h-3h-4h-5h-6h-7h-8h-9h-10h-11h-12h-14h-16h-20h-24h-28h-32h-36h-40h-44h-48h-52h-56h-60h-64h-72h-80h-96h-autoh-pxh-0.5h-1.5h-2.5h-3.5h-1/2h-1/3h-2/3h-1/4h-2/4h-3/4h-1/5h-2/5h-3/5h-4/5h-1/6h-2/6h-3/6h-4/6h-5/6h-fullh-screenh-svhh-lvhh-dvhh-minh-maxh-fitWidthClass
| Enum key | CSS Property Value Width | | ---------- | ------------------------ | |
w-0w-1w-2w-3w-4w-5w-6w-7w-8w-9w-10w-11w-12w-14w-16w-20w-24w-28w-32w-36w-40w-44w-48w-52w-56w-60w-64w-72w-80w-96w-autow-pxw-0.5w-1.5w-2.5w-3.5w-1/2w-1/3w-2/3w-1/4w-2/4w-3/4w-1/5w-2/5w-3/5w-4/5w-1/6w-2/6w-3/6w-4/6w-5/6w-1/12w-2/12w-3/12w-4/12w-5/12w-6/12w-7/12w-8/12w-9/12w-10/12w-11/12w-fullw-screenw-svww-lvww-dvww-minw-maxw-fitSpacingClass
| Enum key | CSS Property Value Gap | | --------- | ---------------------- | |
gap-0gap-1gap-2gap-3gap-4gap-5gap-6gap-7gap-8gap-9gap-10gap-11gap-12gap-14gap-16gap-20gap-24gap-28gap-32gap-36gap-40gap-44gap-48gap-52gap-56gap-60gap-64gap-72gap-80gap-96gap-pxgap-0.5gap-1.5gap-2.5gap-3.5