File size: 8,091 Bytes
5463318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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";