|
1 | 1 | // js/components/copy.js
|
2 | 2 |
|
3 | 3 | 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 |
| - |
11 | 4 | 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 |
12 | 11 | 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 | + `; |
13 | 53 |
|
| 54 | + // Temporarily add the "success" class for visual feedback |
| 55 | + copyButton.classList.add('success'); |
14 | 56 |
|
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"> |
18 | 62 | <path d="M0 0h24v24H0V0z" fill="none"/>
|
19 | 63 | <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"/>
|
20 | 64 | </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 |
31 | 68 | }
|
32 |
| - editMode = !editMode; |
33 |
| - renderButton(); |
34 | 69 | });
|
35 | 70 | };
|
36 | 71 |
|
|
0 commit comments