-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
105 lines (95 loc) · 3.06 KB
/
App.jsx
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
import { useState, useCallback, useEffect, useRef } from 'react'
function App() {
const [length, setLength] = useState(8)
const [numberallowed,setNumberallowed]= useState("False")
const [charallowed,setCharallowed] = useState("False")
const [password,setPassword] = useState("")
const passwordref = useRef(null)
const passwordgenerator = useCallback(() =>{
let pass =""
let string="qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM"
if(numberallowed){
string += "1234567890";
}
if (charallowed){
string +="!@#$%^&*(){}[]></,.;:+-*";
}
for (let i = 0; i <= length; i++) {
let char=Math.floor(Math.random()*string.length+1)
pass+= string.charAt(char)
}
setPassword(pass)
},[length,numberallowed,charallowed,setPassword])
const copytoclipboard = useCallback(() => {
passwordref.current.select();
//passwordref.current.setSelectionRange(0, 999);
const button=document.getElementById("copy")
button.style.backgroundColor = "orange";
// After 2 seconds, revert back to blue
setTimeout(() => {
button.style.backgroundColor = "blue";
}, 2000);
window.navigator.clipboard.writeText(password)
}, [password])
useEffect(() => {
passwordgenerator()
},[length,numberallowed,charallowed,passwordgenerator])
return (
<div className="w-full max-w-md mx-auto shadow-md rounded-lg px-4 my-8 bg-slate-600 text-orange-500">
<h1 className="text-white text-center my-4">password generator</h1>
<div className="flex shadow rounded-lg overflow-hidden md-4">
<input
type="text"
value={password}
className="outline-none w-full py-1 px-3"
placeholder="Password"
readOnly
ref={passwordref}
/>
<button
id="copy"
onClick={copytoclipboard}
className="outline-none bg-blue-700 text-white px-3 py-1 shrink-0 ${isClicked ? bg-orange-500 : bg-blue-700}"
>copy</button>
</div>
<div className="flex text-sm gap-x-2">
<div className="flex items-center gap-x-1">
<input
type ="range"
min={6}
max={100}
value={length}
className="cursor-pointer"
onChange={(e) => {
setLength(e.target.value)
}}
/>
<label>length:{length}</label>
</div>
<div className="flex items-center gap-x-1">
<input
type="checkbox"
defaultChecked={numberallowed}
id ="numberInput"
onChange={() =>{
setNumberallowed((prev) => !prev);
}}
/>
<label htmlFor="numberInput">numbers</label>
</div>
<div className="flex items-center gap-x-1">
<input
type="checkbox"
defaultChecked={charallowed}
id="charInput"
onChange={() => {
setCharallowed((prev) => !prev);
}}
/>
<label htmlFor="charInput">Characters</label>
</div>
</div>
</div>
)
}
export default App