feat: add password manager compatible OTP input component

- Redesigned InputOTP component with modern visual boxes
- Added native password manager support (Dashlane, 1Password, etc.)
- Implemented automatic cursor movement on input/delete
- Removed legacy slot-based components (InputOTPGroup, InputOTPSlot, InputOTPSeparator)
- Updated login page and 2FA setup to use new component
- Enhanced UX with hover effects, focus states, and filled state styling

The new InputOTP component uses a hidden native input with autoComplete='one-time-code'
for password manager compatibility while displaying modern rounded boxes with smooth
animations and visual feedback.
This commit is contained in:
Léo FILMON
2025-11-04 01:43:09 +00:00
parent 1ce153371a
commit c362b2c558
3 changed files with 85 additions and 94 deletions
@@ -25,11 +25,7 @@ import {
FormMessage, FormMessage,
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { import { InputOTP } from "@/components/ui/input-otp";
InputOTP,
InputOTPGroup,
InputOTPSlot,
} from "@/components/ui/input-otp";
import { import {
Tooltip, Tooltip,
TooltipContent, TooltipContent,
@@ -423,23 +419,14 @@ export const Enable2FA = () => {
)} )}
</div> </div>
<div className="flex flex-col justify-center items-center"> <div className="flex flex-col gap-2">
<FormLabel>Verification Code</FormLabel> <FormLabel>Verification Code</FormLabel>
<InputOTP <InputOTP
maxLength={6} maxLength={6}
value={otpValue} value={otpValue}
onChange={setOtpValue} onChange={setOtpValue}
autoComplete="off" autoFocus
> />
<InputOTPGroup>
<InputOTPSlot index={0} />
<InputOTPSlot index={1} />
<InputOTPSlot index={2} />
<InputOTPSlot index={3} />
<InputOTPSlot index={4} />
<InputOTPSlot index={5} />
</InputOTPGroup>
</InputOTP>
<FormDescription> <FormDescription>
Enter the 6-digit code from your authenticator app Enter the 6-digit code from your authenticator app
</FormDescription> </FormDescription>
+74 -57
View File
@@ -1,70 +1,87 @@
import { OTPInput, OTPInputContext } from "input-otp";
import { Dot } from "lucide-react";
import * as React from "react"; import * as React from "react";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
const InputOTP = React.forwardRef< const InputOTP = React.forwardRef<
React.ElementRef<typeof OTPInput>, HTMLInputElement,
React.ComponentPropsWithoutRef<typeof OTPInput> Omit<React.InputHTMLAttributes<HTMLInputElement>, "onChange"> & {
>(({ className, containerClassName, ...props }, ref) => ( value: string;
<OTPInput onChange: (value: string) => void;
ref={ref} maxLength: number;
containerClassName={cn( }
"flex items-center gap-2 has-[:disabled]:opacity-50", >(({ className, value, onChange, maxLength, ...props }, ref) => {
containerClassName, const [focusedIndex, setFocusedIndex] = React.useState<number | null>(null);
)} const inputRef = React.useRef<HTMLInputElement>(null);
className={cn("disabled:cursor-not-allowed", className)} const previousValueRef = React.useRef<string>(value);
{...props}
/>
));
InputOTP.displayName = "InputOTP";
const InputOTPGroup = React.forwardRef< React.useImperativeHandle(ref, () => inputRef.current!);
React.ElementRef<"div">,
React.ComponentPropsWithoutRef<"div">
>(({ className, ...props }, ref) => (
<div ref={ref} className={cn("flex items-center", className)} {...props} />
));
InputOTPGroup.displayName = "InputOTPGroup";
const InputOTPSlot = React.forwardRef< React.useEffect(() => {
React.ElementRef<"div">, if (value !== previousValueRef.current) {
React.ComponentPropsWithoutRef<"div"> & { index: number } const newLength = value.length;
>(({ index, className, ...props }, ref) => { setFocusedIndex(newLength);
const inputOTPContext = React.useContext(OTPInputContext); previousValueRef.current = value;
// @ts-ignore }
const { char, hasFakeCaret, isActive } = inputOTPContext.slots[index]; }, [value]);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = e.target.value.replace(/\D/g, "").slice(0, maxLength);
onChange(newValue);
};
const handleBoxClick = (index: number) => {
inputRef.current?.focus();
setFocusedIndex(index);
};
const slots = Array.from({ length: maxLength }, (_, i) => {
const char = value[i] || "";
const isActive =
focusedIndex === i || (focusedIndex === null && i === value.length);
const isFilled = !!char;
return (
<div
key={i}
onClick={() => handleBoxClick(i)}
className={cn(
"relative flex h-11 w-11 items-center justify-center rounded-lg border-2 border-input bg-background text-base font-semibold transition-all cursor-text hover:border-ring/50",
isActive && "border-ring ring-2 ring-ring/20 ring-offset-1",
isFilled && "border-primary/50 bg-primary/5",
className,
)}
>
<span className="text-foreground">{char}</span>
{isActive && !char && (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<div className="h-5 w-0.5 animate-caret-blink bg-primary duration-1000" />
</div>
)}
</div>
);
});
return ( return (
<div <div className="relative">
ref={ref} <input
className={cn( ref={inputRef}
"relative flex h-10 w-10 items-center justify-center border-y border-r border-input text-sm transition-all first:rounded-l-md first:border-l last:rounded-r-md", type="text"
isActive && "z-10 ring-2 ring-ring ring-offset-background", value={value}
className, onChange={handleChange}
)} onFocus={() => setFocusedIndex(value.length)}
{...props} onBlur={() => setFocusedIndex(null)}
> autoComplete="one-time-code"
{char} inputMode="numeric"
{hasFakeCaret && ( pattern="[0-9]*"
<div className="pointer-events-none absolute inset-0 flex items-center justify-center"> maxLength={maxLength}
<div className="h-4 w-px animate-caret-blink bg-foreground duration-1000" /> className="absolute inset-0 w-full h-full opacity-0 cursor-default"
</div> style={{ caretColor: "transparent" }}
)} {...props}
/>
<div className="flex items-center gap-2">{slots}</div>
</div> </div>
); );
}); });
InputOTPSlot.displayName = "InputOTPSlot"; InputOTP.displayName = "InputOTP";
const InputOTPSeparator = React.forwardRef< export { InputOTP };
React.ElementRef<"div">,
React.ComponentPropsWithoutRef<"div">
>(({ ...props }, ref) => (
<div ref={ref} {...props}>
<Dot />
</div>
));
InputOTPSeparator.displayName = "InputOTPSeparator";
export { InputOTP, InputOTPGroup, InputOTPSlot, InputOTPSeparator };
+7 -20
View File
@@ -1,7 +1,6 @@
import { IS_CLOUD, isAdminPresent } from "@dokploy/server"; import { IS_CLOUD, isAdminPresent } from "@dokploy/server";
import { validateRequest } from "@dokploy/server/lib/auth"; import { validateRequest } from "@dokploy/server/lib/auth";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { REGEXP_ONLY_DIGITS } from "input-otp";
import type { GetServerSidePropsContext } from "next"; import type { GetServerSidePropsContext } from "next";
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
@@ -30,11 +29,7 @@ import {
FormMessage, FormMessage,
} from "@/components/ui/form"; } from "@/components/ui/form";
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { import { InputOTP } from "@/components/ui/input-otp";
InputOTP,
InputOTPGroup,
InputOTPSlot,
} from "@/components/ui/input-otp";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { authClient } from "@/lib/auth-client"; import { authClient } from "@/lib/auth-client";
@@ -319,27 +314,19 @@ export default function Home({ IS_CLOUD }: Props) {
onSubmit={onTwoFactorSubmit} onSubmit={onTwoFactorSubmit}
className="space-y-4" className="space-y-4"
id="two-factor-form" id="two-factor-form"
autoComplete="off" autoComplete="on"
> >
<div className="flex flex-col gap-2"> <div className="flex flex-col gap-2">
<Label>2FA Code</Label> <Label htmlFor="totp-code">2FA Code</Label>
<InputOTP <InputOTP
id="totp-code"
name="totp"
value={twoFactorCode} value={twoFactorCode}
onChange={setTwoFactorCode} onChange={setTwoFactorCode}
maxLength={6} maxLength={6}
pattern={REGEXP_ONLY_DIGITS} placeholder="••••••"
autoComplete="off"
autoFocus autoFocus
> />
<InputOTPGroup>
<InputOTPSlot index={0} className="border-border" />
<InputOTPSlot index={1} className="border-border" />
<InputOTPSlot index={2} className="border-border" />
<InputOTPSlot index={3} className="border-border" />
<InputOTPSlot index={4} className="border-border" />
<InputOTPSlot index={5} className="border-border" />
</InputOTPGroup>
</InputOTP>
<CardDescription> <CardDescription>
Enter the 6-digit code from your authenticator app Enter the 6-digit code from your authenticator app
</CardDescription> </CardDescription>