Skip to content

Commit 14fadea

Browse files
fix(password-generator): improve copy button UX and prevent layout shift (#1655)
Co-authored-by: Priyankar Pal <[email protected]>
1 parent 05f435f commit 14fadea

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/plays/password-generator/PasswordGenerator.jsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ function PasswordGenerator(props) {
7272
const onCopyClick = (e) => {
7373
e.preventDefault();
7474
navigator.clipboard.writeText(password.password);
75-
setPassword({ ...password, status: true });
75+
setPassword((prev) => ({ ...prev, status: true }));
76+
77+
setTimeout(() => {
78+
setPassword((prev) => ({ ...prev, status: false }));
79+
}, 1500);
7680
};
7781

7882
const ErrorBox = () => {
@@ -109,8 +113,14 @@ function PasswordGenerator(props) {
109113
<h1 className="title">Password Generator</h1>
110114
{error && <ErrorBox />}
111115
<div className="inputfield">
112-
<input disabled readOnly className="text" type="text" value={password.password} />
113-
<button className="copy copybtn" onClick={onCopyClick}>
116+
<input
117+
disabled
118+
readOnly
119+
className={`text ${password.status ? 'copied' : ''}`}
120+
type="text"
121+
value={password.password}
122+
/>
123+
<button className="copy copybtn" disabled={password.status} onClick={onCopyClick}>
114124
{password?.status ? 'Copied' : 'Copy'}
115125
</button>
116126
</div>

src/plays/password-generator/password-generator-style.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
padding: 0.6em 1em;
4949
border-radius: 0.7em;
5050
text-transform: uppercase;
51+
min-width: 100px;
52+
text-align: center;
53+
}
54+
55+
.password-generator .main .inputfield .text.copied {
56+
background-color: #b3daff;
57+
border-radius: 0.4em;
58+
transition: background-color 0.3s ease;
59+
}
60+
61+
.password-generator .main .inputfield .copybtn:disabled {
62+
opacity: 0.55;
63+
background-color: #18c0f4;
64+
cursor: not-allowed;
5165
}
5266

5367
.password-generator .main .block {

0 commit comments

Comments
 (0)