Skip to content

Commit 13291ad

Browse files
author
Nikos Katsikanis
committed
copy to clipboard button
1 parent 632d72e commit 13291ad

File tree

2 files changed

+60
-23
lines changed

2 files changed

+60
-23
lines changed

js/components/copy.js

+57-22
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,71 @@
11
// js/components/copy.js
22

33
export default (hostComponent) => {
4-
let editMode = false;
5-
6-
const handleCopy = () => {
7-
// Logic for handling the copy action
8-
console.log("Data copied!");
9-
};
10-
114
const renderButton = () => {
5+
// Read data attributes for dynamic content
6+
const textToCopy = hostComponent.getAttribute('data-text-to-copy') || '[email protected]';
7+
const initialText = hostComponent.getAttribute('data-initial-text') || 'Copy';
8+
const successText = hostComponent.getAttribute('data-success-text') || 'Copied!';
9+
10+
// Create the button HTML with dynamic values
1211
hostComponent.innerHTML = `
12+
<button id="copy-btn" class="outline small-button" data-copytext="${textToCopy}">
13+
<span id="copy-text">${initialText}</span>
14+
<svg id="copy-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
15+
<path d="M0 0h24v24H0V0z" fill="none"/>
16+
<path d="M13 3H6c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V8l-7-5zm0 2l5 5h-5V5zm-7 14V5h6v5h5v9H6z"/>
17+
</svg>
18+
</button>
19+
`;
20+
21+
const copyButton = hostComponent.querySelector('#copy-btn');
22+
const copyTextElement = copyButton.querySelector('#copy-text');
23+
const copyIcon = copyButton.querySelector('#copy-icon');
24+
25+
// Function to copy text to clipboard
26+
const handleCopy = (copyText) => {
27+
navigator.clipboard.writeText(copyText)
28+
.then(() => {
29+
console.log("Data copied:", copyText);
30+
})
31+
.catch((err) => {
32+
console.error("Failed to copy:", err);
33+
});
34+
};
35+
36+
copyButton.addEventListener('click', () => {
37+
const copyText = copyButton.getAttribute('data-copytext');
38+
39+
if (copyText) {
40+
// Handle clipboard copy
41+
handleCopy(copyText);
42+
43+
// Temporarily change the button text and icon
44+
copyTextElement.textContent = successText;
45+
46+
// Change icon to a tick (✔️)
47+
copyIcon.innerHTML = `
48+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
49+
<path d="M0 0h24v24H0z" fill="none"/>
50+
<path d="M9 16.17L4.83 12 3.41 13.41l5.59 5.59 12-12L19.17 5l-10 10z"/>
51+
</svg>
52+
`;
1353

54+
// Temporarily add the "success" class for visual feedback
55+
copyButton.classList.add('success');
1456

15-
<button id="copy-email-btn" class="outline small-button">
16-
<span id="copy-text">Copy Email</span>
17-
<svg id="copy-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
57+
// Revert the text, icon, and class after 2 seconds
58+
setTimeout(() => {
59+
copyTextElement.textContent = initialText; // Reset to initial text
60+
copyIcon.innerHTML = `
61+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
1862
<path d="M0 0h24v24H0V0z" fill="none"/>
1963
<path d="M13 3H6c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V8l-7-5zm0 2l5 5h-5V5zm-7 14V5h6v5h5v9H6z"/>
2064
</svg>
21-
</button>
22-
23-
`;
24-
25-
const copyButton = hostComponent.querySelector('#copy-button');
26-
copyButton.addEventListener('click', () => {
27-
if (editMode) {
28-
console.log("Copy cancelled");
29-
} else {
30-
handleCopy();
65+
`;
66+
copyButton.classList.remove('success'); // Remove the success class
67+
}, 2000); // Reset after 2 seconds
3168
}
32-
editMode = !editMode;
33-
renderButton();
3469
});
3570
};
3671

js/routes/button-badge.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ export default (hostComponent) => {
7474
<p>Special Cases</p>
7575
7676
<div>
77-
<div data-component="copy"></div>
77+
<div data-component="copy" data-text-to-copy="[email protected]"
78+
data-initial-text="Copy Email" data-success-text="Copied!"></div>
79+
</div>
7880
7981
</div>
8082
<h1>Badges</h1>

0 commit comments

Comments
 (0)