forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboardCopy.js
More file actions
47 lines (42 loc) · 1.23 KB
/
clipboardCopy.js
File metadata and controls
47 lines (42 loc) · 1.23 KB
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
export default function handleClipboardCopy() {
const copyInputs = document.querySelectorAll('.js-copy-input');
const copyButtons = document.querySelectorAll('.js-copy-button');
// Iterate over each input and button pair
copyInputs.forEach((copyInput, index) => {
let inputFocused = false;
// Ensure corresponding button exists
if (!copyButtons[index]) {
return;
}
const copyButton = copyButtons[index];
// Copy on focus and allow for partial selection
copyInput.addEventListener('click', () => {
if (!inputFocused) {
copyInput.select();
inputFocused = true;
// Reset inputFocused when input loses focus
copyInput.addEventListener(
'blur',
() => {
inputFocused = false;
},
{ once: true },
);
}
});
// Copy to clipboard on button click
copyButton.addEventListener('click', () => {
navigator.clipboard.writeText(copyInput.value).then(() => {
// Temporarily change button content
const buttonIcon = copyButton.innerHTML;
copyButton.innerHTML = 'Copied!';
copyButton.disabled = true;
// Revert button content after 2 seconds
setTimeout(() => {
copyButton.innerHTML = buttonIcon;
copyButton.disabled = false;
}, 2000);
});
});
});
}