Skip to content

Commit cfe2218

Browse files
authored
Fix copy code button in the docs (#9)
1 parent d09f9b5 commit cfe2218

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

src/components/CopyCodeButton.astro

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ const { code } = Astro.props;
33
---
44

55
<button
6-
class="top-[0.9rem] right-2 absolute flex items-center gap-1 bg-gray-700 hover:bg-gray-600 px-2 copy-code py-1 rounded font-bold text-sm text-white"
6+
class="top-[0.9rem] right-2 absolute flex items-center gap-1 bg-gray-700 hover:bg-gray-600 px-2 copy-code py-1 rounded font-bold text-sm text-white transition-colors duration-300"
77
data-code={code}
88
>
9-
<span>
9+
<span class="icon">
1010
<svg
1111
xmlns="http://www.w3.org/2000/svg"
1212
width="16"
@@ -17,11 +17,12 @@ const { code } = Astro.props;
1717
stroke-width="2"
1818
stroke-linecap="round"
1919
stroke-linejoin="round"
20-
><rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect><path
21-
d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path></svg
2220
>
21+
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"></rect>
22+
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"></path>
23+
</svg>
2324
</span>
24-
Copy
25+
<span class="label">Copy</span>
2526
</button>
2627

2728
<script>
@@ -31,17 +32,32 @@ const { code } = Astro.props;
3132
});
3233
}
3334

34-
// Select all buttons with the copy-code class
35-
const buttons = document.querySelectorAll<HTMLButtonElement>(".copy-code");
35+
const buttons = document.querySelectorAll(".copy-code");
3636

3737
buttons.forEach((button) => {
38+
const originalHTML = button.innerHTML;
39+
3840
button.addEventListener("click", () => {
3941
const code = button.getAttribute("data-code");
4042
copyToClipboard(code);
41-
const oldContent = button.getHTML();
42-
button.textContent = "Copied!";
43+
44+
button.classList.remove("bg-gray-700", "hover:bg-gray-600");
45+
button.classList.add("bg-green-600", "hover:bg-green-500");
46+
47+
button.innerHTML = `
48+
<span class="icon">
49+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" stroke="currentColor"
50+
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" viewBox="0 0 24 24">
51+
<polyline points="20 6 9 17 4 12" />
52+
</svg>
53+
</span>
54+
<span class="label">Copied!</span>
55+
`;
56+
4357
setTimeout(() => {
44-
button.innerHTML = oldContent;
58+
button.innerHTML = originalHTML;
59+
button.classList.remove("bg-green-600", "hover:bg-green-500");
60+
button.classList.add("bg-gray-700", "hover:bg-gray-600");
4561
}, 3000);
4662
});
4763
});

0 commit comments

Comments
 (0)