input-otp.tsx
Overview
input-otp.tsx is a React component module designed to provide a customizable, accessible, and visually appealing One-Time Password (OTP) input interface. It builds upon an existing OTPInput component imported from the external 'input-otp' package, enhancing it with styling, context integration, and modular subcomponents to create a flexible OTP input UI.
The file exports four main components:
InputOTP: The main OTP input container component that wraps the baseOTPInputcomponent with additional styling and forwarding refs.InputOTPGroup: A styled container component to group OTP input slots horizontally.InputOTPSlot: A single slot component representing each character input in the OTP, handling active state and caret display.InputOTPSeparator: A visual separator between OTP slots, rendered as a dot icon.
These components can be composed together to build an interactive OTP input field with clear user focus, disabled states, and visual separation for enhanced usability.
Components
1. InputOTP
A wrapper component around the base OTPInput component from the 'input-otp' package. It forwards its ref and props, applies flexible container and input styling, and manages disabled states.
Signature
const InputOTP: React.ForwardRefExoticComponent<
React.PropsWithoutRef<React.ComponentPropsWithoutRef<typeof OTPInput>> & React.RefAttributes<React.ElementRef<typeof OTPInput>>
>
Props
Inherits all props from the original
OTPInputcomponent.className(optional): Additional CSS classes to apply to the input elements.containerClassName(optional): Additional CSS classes for the container wrapping the input.
Usage Example
import { InputOTP } from './input-otp';
function Example() {
const otpRef = React.useRef(null);
return (
<InputOTP
ref={otpRef}
length={6}
containerClassName="my-otp-container"
className="my-otp-input"
disabled={false}
onChange={(value) => console.log('OTP changed:', value)}
/>
);
}
Implementation Details
Uses the utility
cn(classNames) to merge default and custom classes.Applies a flex container with spacing and opacity for disabled state.
Forwards ref to the underlying
OTPInputcomponent.Ensures cursor style changes when disabled.
2. InputOTPGroup
A simple styled container component intended to wrap multiple InputOTPSlot components horizontally.
Signature
const InputOTPGroup: React.ForwardRefExoticComponent<
React.PropsWithoutRef<React.ComponentPropsWithoutRef<'div'>> & React.RefAttributes<HTMLDivElement>
>
Props
Inherits all standard
divelement props.className(optional): Additional CSS classes for styling the group container.
Usage Example
import { InputOTPGroup, InputOTPSlot, InputOTPSeparator } from './input-otp';
function OTPInputWithSeparators() {
return (
<InputOTPGroup className="custom-group">
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSeparator />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
</InputOTPGroup>
);
}
Implementation Details
Uses flexbox to arrange children horizontally.
Supports ref forwarding to the div element.
Default styling ensures items are centered vertically.
3. InputOTPSlot
Represents a single character input slot within the OTP input. It displays the character, highlights active slot, and shows a blinking caret when the slot is focused.
Signature
const InputOTPSlot: React.ForwardRefExoticComponent<
React.PropsWithoutRef<React.ComponentPropsWithoutRef<'div'> & { index: number }> & React.RefAttributes<HTMLDivElement>
>
Props
index(number, required): The index of this slot within the OTP input.className(optional): Additional CSS classes for this slot.Inherits other standard
divprops.
Usage Example
import { InputOTPSlot } from './input-otp';
function SingleSlotExample() {
return <InputOTPSlot index={0} className="my-slot" />;
}
Implementation Details
Uses
React.useContextto accessOTPInputContextfrom the'input-otp'package.Reads slot state (
char,hasFakeCaret,isActive) from the context using the providedindex.Applies custom styles for borders, rounded corners, and active state ring.
Shows a blinking caret (using CSS animation) if
hasFakeCaretis true.Supports ref forwarding.
4. InputOTPSeparator
A small visual separator component displayed between OTP slots, rendered as a centered dot icon.
Signature
const InputOTPSeparator: React.ForwardRefExoticComponent<
React.PropsWithoutRef<React.ComponentPropsWithoutRef<'div'>> & React.RefAttributes<HTMLDivElement>
>
Props
Inherits all standard
divelement props.
Usage Example
import { InputOTPSeparator } from './input-otp';
function SeparatorExample() {
return <InputOTPSeparator className="my-separator" />;
}
Implementation Details
Uses the
Doticon from thelucide-reacticon library.Sets
role="separator"for accessibility.Forwards ref to the wrapping div.
Important Implementation Details
Context Usage:
InputOTPSlotrelies onOTPInputContextto get per-slot state such as the displayed character, whether this slot has the blinking caret, and if it is active (focused).Styling: The
cnutility is used extensively to combine multiple CSS classes conditionally, supporting disabled states, active highlighting, and consistent layout.Accessibility: The separator has an ARIA role of
separatorto improve screen reader semantics.Forwarding Refs: All components use
React.forwardRefto allow parent components or higher-level forms to access underlying DOM elements or component instances.Component Composition: The components are designed to be composed, e.g., placing multiple
InputOTPSlotcomponents inside anInputOTPGroupwith optionalInputOTPSeparatorcomponents to customize the OTP input UI.
Interaction with Other System Parts
Dependency on
'input-otp'Package: The core OTP input logic and context (OTPInputandOTPInputContext) come from this external package. This file only wraps and styles those components.Utility
cn: A class names utility imported from@/lib/utilsused internally for conditional styling.Icon Library: Uses
Dotfrom thelucide-reactpackage for separators.Parent Usage: Typically, these components are used in UI forms requiring OTP verification, integrating with logic that handles OTP validation, submission, and error handling.
Visual Diagram
componentDiagram
component InputOTP {
+ref
+props
- containerClassName
- className
}
component InputOTPGroup {
+ref
+props
}
component InputOTPSlot {
+index
+ref
+props
- uses OTPInputContext
- displays char, caret, active state
}
component InputOTPSeparator {
+ref
+props
- renders Dot icon
}
InputOTPGroup <.. InputOTPSlot : contains >
InputOTPGroup <.. InputOTPSeparator : contains >
InputOTPSlot ..> OTPInputContext : consumes
Summary
The input-otp.tsx file provides a set of composable React components that enable flexible, styled, and accessible OTP input interfaces. By leveraging context from the underlying 'input-otp' package and augmenting it with custom styling and modular subcomponents, it delivers a polished user experience suitable for security-sensitive forms requiring OTP verification.
This design allows developers to easily customize the OTP input layout (e.g., grouping slots, adding separators) while maintaining consistent behavior such as caret blinking, active highlighting, and disabled states. The file's exported components can be integrated seamlessly into larger React applications for authentication workflows.