import React, { useState } from "https://esm.sh/react";
import { createRoot } from "https://esm.sh/react-dom/client";
function App() {
const [password, setPassword] = useState("");
const [length, setLength] = useState(12);
const [includeUppercase, setIncludeUppercase] = useState(true);
const [includeLowercase, setIncludeLowercase] = useState(true);
const [includeNumbers, setIncludeNumbers] = useState(true);
const [includeSymbols, setIncludeSymbols] = useState(true);
const generatePassword = () => {
let charset = "";
if (includeUppercase) charset += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (includeLowercase) charset += "abcdefghijklmnopqrstuvwxyz";
if (includeNumbers) charset += "0123456789";
if (includeSymbols) charset += "!@#$%^&*()_+~`|}{[]:;?><,./-=";
if (charset === "") {
setPassword("Please select at least one character type");
return;
}
const array = new Uint32Array(length);
crypto.getRandomValues(array);
const password = Array.from(array)
.map(x => charset[x % charset.length])
.join('');
setPassword(password);
};
return (
<div className="container mx-auto p-4">
<h1 className="text-2xl font-bold mb-4">Password Generator</h1>
<div className="mb-4">
<label className="block mb-2">Password Length: {length}</label>
<input
type="range"
min="8"
max="32"
value={length}
onChange={(e) => setLength(Number(e.target.value))}
className="w-full"
/>
</div>
<div className="mb-4">
<label className="inline-flex items-center">
<input
type="checkbox"
checked={includeUppercase}
onChange={() => setIncludeUppercase(!includeUppercase)}
className="form-checkbox"
/>
<span className="ml-2">Include Uppercase</span>
</label>
</div>
<div className="mb-4">
<label className="inline-flex items-center">
<input
type="checkbox"
checked={includeLowercase}
onChange={() => setIncludeLowercase(!includeLowercase)}
className="form-checkbox"
/>
<span className="ml-2">Include Lowercase</span>
</label>
</div>
<div className="mb-4">
<label className="inline-flex items-center">
<input
type="checkbox"
checked={includeNumbers}
onChange={() => setIncludeNumbers(!includeNumbers)}
className="form-checkbox"
/>
<span className="ml-2">Include Numbers</span>
</label>
</div>
<div className="mb-4">
<label className="inline-flex items-center">
<input
type="checkbox"
checked={includeSymbols}
onChange={() => setIncludeSymbols(!includeSymbols)}
className="form-checkbox"
/>
<span className="ml-2">Include Symbols</span>
</label>
</div>
<button
onClick={generatePassword}
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
Generate Password
</button>
{password && (
<div className="mt-4">
<h2 className="text-xl font-semibold">Generated Password:</h2>
<p className="bg-gray-100 p-2 rounded mt-2 font-mono">{password}</p>