ExamCraftAI / Frontend /src /components /auth /AuthInput.jsx
Hkoradiya123
Initial commit
5463318
Raw History Blame Contribute Delete
8.09 kB
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 (
<div className={cn("relative mb-6 h-12 w-full rounded-lg bg-muted animate-pulse", className)}>
</div>
);
}
// Shake animation for error
const shakeVariants = {
initial: { x: 0 },
error: {
x: [0, -4, 4, -4, 4, 0],
transition: { duration: 0.4 },
},
};
return (
<motion.div
className={cn("relative mb-6", className)}
initial="initial"
animate={error ? "error" : "initial"}
variants={shakeVariants}
>
{/* Input Wrapper */}
<div
className={cn(
"relative flex items-center w-full rounded-xl border bg-white dark:bg-[#111827]/80 px-4 py-3 text-sm transition-all duration-300 backdrop-blur-md",
focused ? "border-[#4F7CFF] shadow-[0_0_15px_rgba(79,124,255,0.2)] ring-1 ring-[#4F7CFF]" : "border-slate-200 dark:border-white/10 hover:border-slate-300 dark:hover:border-white/20",
error ? "border-red-500 shadow-[0_0_15px_rgba(239,68,68,0.2)] ring-1 ring-red-500" : "",
isValid && !focused && !error ? "border-emerald-500 shadow-[0_0_10px_rgba(16,185,129,0.1)]" : "",
props.disabled ? "opacity-50 cursor-not-allowed" : ""
)}
>
{/* Optional Icon */}
{Icon && (
<div className="mr-3 text-slate-400 transition-colors duration-300">
<Icon size={18} className={cn(focused ? "text-[#4F7CFF]" : "", error ? "text-red-500" : "", isValid ? "text-emerald-500" : "")} />
</div>
)}
{/* Floating Label / Input area */}
<div className="relative flex-1">
{label && (
<label
className={cn(
"absolute left-0 transition-all duration-300 pointer-events-none bg-white dark:bg-[#111827] px-1 font-medium z-10 rounded",
focused || (props.value && props.value !== '') || props.defaultValue
? "-top-6 text-[11px] text-[#4F7CFF] -ml-1"
: "top-0 text-sm text-slate-400",
error && (focused || props.value) ? "text-red-500" : "",
isValid && !focused && !error ? "text-emerald-500" : ""
)}
>
{label}
</label>
)}
<input
type={inputType}
ref={ref}
onFocus={(e) => {
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}
/>
</div>
{/* Validation Feedback / Tools */}
<div className="flex items-center gap-2 ml-2">
{/* Checkmark */}
<AnimatePresence>
{isValid && !error && (
<motion.div
initial={{ scale: 0, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
exit={{ scale: 0, opacity: 0 }}
className="text-emerald-500"
>
<CheckCircle2 size={18} />
</motion.div>
)}
</AnimatePresence>
{/* Password Visibility Toggle */}
{isPassword && (
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="text-slate-400 hover:text-slate-700 dark:hover:text-slate-200 transition-colors focus:outline-none"
aria-label={showPassword ? "Hide password" : "Show password"}
>
{showPassword ? <EyeOff size={18} /> : <Eye size={18} />}
</button>
)}
</div>
</div>
{/* Caps Lock Warning */}
<AnimatePresence>
{isPassword && capsLock && focused && (
<motion.div
initial={{ opacity: 0, y: -5 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -5 }}
className="absolute -top-7 right-0 text-xs text-amber-500 flex items-center gap-1 bg-amber-50 dark:bg-amber-500/10 px-2 py-1 rounded border border-amber-200 dark:border-amber-500/20 shadow-sm"
>
<AlertTriangle size={12} /> Caps Lock is ON
</motion.div>
)}
</AnimatePresence>
{/* Error Validation Feedback */}
<AnimatePresence>
{error && (
<motion.div
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: "auto" }}
exit={{ opacity: 0, height: 0 }}
className="text-xs text-red-500 flex flex-row items-center gap-1 mt-2 font-medium"
>
<AlertCircle size={12} /> {error}
</motion.div>
)}
</AnimatePresence>
</motion.div>
);
}
);
AuthInput.displayName = "AuthInput";