|
| 1 | +import React, { useState } from "react"; |
| 2 | + |
| 3 | +export default function PasswordGenerator() { |
| 4 | + const [length, setLength] = useState(12); |
| 5 | + const [includeUpper, setIncludeUpper] = useState(true); |
| 6 | + const [includeLower, setIncludeLower] = useState(true); |
| 7 | + const [includeNumbers, setIncludeNumbers] = useState(true); |
| 8 | + const [includeSymbols, setIncludeSymbols] = useState(true); |
| 9 | + const [password, setPassword] = useState(""); |
| 10 | + |
| 11 | + const generatePassword = () => { |
| 12 | + let charset = ""; |
| 13 | + if (includeUpper) charset += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 14 | + if (includeLower) charset += "abcdefghijklmnopqrstuvwxyz"; |
| 15 | + if (includeNumbers) charset += "0123456789"; |
| 16 | + if (includeSymbols) charset += "!@#$%^&*()_+-=[]{}|;:,.<>?"; |
| 17 | + if (!charset) return setPassword("⚠️ Select at least one option."); |
| 18 | + |
| 19 | + let pass = ""; |
| 20 | + for (let i = 0; i < length; i++) { |
| 21 | + pass += charset.charAt(Math.floor(Math.random() * charset.length)); |
| 22 | + } |
| 23 | + setPassword(pass); |
| 24 | + }; |
| 25 | + |
| 26 | + const copyPassword = async () => { |
| 27 | + if (!password) return; |
| 28 | + await navigator.clipboard.writeText(password); |
| 29 | + }; |
| 30 | + |
| 31 | + return ( |
| 32 | + <div className="min-h-screen text-white p-8 font-sans"> |
| 33 | + <h2 className="text-2xl font-bold mb-6 text-center text-transparent bg-clip-text bg-gradient-to-r from-green-400 to-emerald-500 drop-shadow-[0_0_10px_rgba(52,211,153,0.6)]"> |
| 34 | + 🔐 Password Generator |
| 35 | + </h2> |
| 36 | + |
| 37 | + <div className="max-w-3xl mx-auto bg-[#111827]/70 border border-[#1e293b] rounded-2xl p-6 backdrop-blur-sm shadow-[0_0_20px_rgba(52,211,153,0.15)] space-y-4"> |
| 38 | + <div className="flex items-center justify-between"> |
| 39 | + <label className="text-sm text-slate-300">Password Length</label> |
| 40 | + <input |
| 41 | + type="number" |
| 42 | + min="4" |
| 43 | + max="64" |
| 44 | + value={length} |
| 45 | + onChange={(e) => setLength(Number(e.target.value))} |
| 46 | + className="w-20 text-center bg-[#0f172a] border border-slate-700 rounded-md p-1 text-sm text-slate-200 focus:border-emerald-500 outline-none" |
| 47 | + /> |
| 48 | + </div> |
| 49 | + |
| 50 | + <div className="grid grid-cols-2 gap-3 mt-2"> |
| 51 | + <label className="flex items-center gap-2 text-sm text-slate-300"> |
| 52 | + <input type="checkbox" checked={includeUpper} onChange={() => setIncludeUpper(!includeUpper)} /> |
| 53 | + Include Uppercase |
| 54 | + </label> |
| 55 | + <label className="flex items-center gap-2 text-sm text-slate-300"> |
| 56 | + <input type="checkbox" checked={includeLower} onChange={() => setIncludeLower(!includeLower)} /> |
| 57 | + Include Lowercase |
| 58 | + </label> |
| 59 | + <label className="flex items-center gap-2 text-sm text-slate-300"> |
| 60 | + <input type="checkbox" checked={includeNumbers} onChange={() => setIncludeNumbers(!includeNumbers)} /> |
| 61 | + Include Numbers |
| 62 | + </label> |
| 63 | + <label className="flex items-center gap-2 text-sm text-slate-300"> |
| 64 | + <input type="checkbox" checked={includeSymbols} onChange={() => setIncludeSymbols(!includeSymbols)} /> |
| 65 | + Include Symbols |
| 66 | + </label> |
| 67 | + </div> |
| 68 | + |
| 69 | + <div className="flex gap-3 justify-center mt-4"> |
| 70 | + <button |
| 71 | + onClick={generatePassword} |
| 72 | + className="px-5 py-2 rounded-lg bg-gradient-to-r from-emerald-500 to-green-400 hover:from-emerald-400 hover:to-green-300 shadow-[0_0_12px_rgba(52,211,153,0.4)] transition-all" |
| 73 | + > |
| 74 | + Generate |
| 75 | + </button> |
| 76 | + <button |
| 77 | + onClick={copyPassword} |
| 78 | + className={`px-5 py-2 rounded-lg border border-slate-600 hover:border-emerald-400 transition-all ${!password && "opacity-40 cursor-not-allowed" |
| 79 | + }`} |
| 80 | + disabled={!password} |
| 81 | + > |
| 82 | + Copy |
| 83 | + </button> |
| 84 | + </div> |
| 85 | + |
| 86 | + <div> |
| 87 | + <h3 className="font-semibold text-emerald-400 mb-2">Generated Password</h3> |
| 88 | + <textarea |
| 89 | + readOnly |
| 90 | + className="w-full p-3 rounded-md bg-[#0f172a] border border-slate-700 text-slate-200 text-sm" |
| 91 | + rows="2" |
| 92 | + value={password} |
| 93 | + placeholder="Your secure password will appear here..." |
| 94 | + /> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +} |
0 commit comments