import React, { useState, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import { cn } from "../../../src/utils/cn";
import { Eye, EyeOff, AlertCircle, CheckCircle2, AlertTriangle } from "lucide-react";
export const AuthInput = React.forwardRef(
({ className, type = "text", error, isValid, label, icon: Icon, isLoading, ...props }, ref) => {
const [focused, setFocused] = useState(false);
const [showPassword, setShowPassword] = useState(false);
const [capsLock, setCapsLock] = useState(false);
const isPassword = type === "password";
const inputType = isPassword ? (showPassword ? "text" : "password") : type;
useEffect(() => {
const handleKeyUp = (e) => {
if (e.getModifierState && e.getModifierState("CapsLock")) {
setCapsLock(true);
} else {
setCapsLock(false);
}
};
window.addEventListener("keyup", handleKeyUp);
window.addEventListener("keydown", handleKeyUp);
return () => {
window.removeEventListener("keyup", handleKeyUp);
window.removeEventListener("keydown", handleKeyUp);
};
}, []);
// Skeleton state
if (isLoading) {
return (
);
}
// Shake animation for error
const shakeVariants = {
initial: { x: 0 },
error: {
x: [0, -4, 4, -4, 4, 0],
transition: { duration: 0.4 },
},
};
return (
{/* Input Wrapper */}
{/* Optional Icon */}
{Icon && (
)}
{/* Floating Label / Input area */}
{label && (
)}
{
setFocused(true);
props.onFocus?.(e);
}}
onBlur={(e) => {
setFocused(false);
props.onBlur?.(e);
}}
className={cn(
"flex w-full bg-transparent placeholder:opacity-0 focus:outline-none focus:placeholder:opacity-100 disabled:cursor-not-allowed",
"text-slate-900 dark:text-white placeholder:text-slate-400 tracking-wide font-medium"
)}
{...props}
/>
{/* Validation Feedback / Tools */}
{/* Checkmark */}
{isValid && !error && (
)}
{/* Password Visibility Toggle */}
{isPassword && (
)}
{/* Caps Lock Warning */}
{isPassword && capsLock && focused && (
Caps Lock is ON
)}
{/* Error Validation Feedback */}
{error && (
{error}
)}
);
}
);
AuthInput.displayName = "AuthInput";