added js for code blocks

This commit is contained in:
Samuel Breznjak 2025-01-24 12:49:33 +01:00
parent b3fcced804
commit 30d135b352

View file

@ -1 +1,27 @@
import "./cs16.css";
document.querySelectorAll(".toggle-vision").forEach((button) => {
button.addEventListener("click", function () {
const codeBlock = this.closest(".code-block").querySelector(".inner");
codeBlock.classList.toggle("hidden");
this.textContent = codeBlock.classList.contains("hidden")
? "Show code"
: "Hide code";
});
});
document.querySelectorAll(".inner .cs-btn").forEach((button) => {
button.addEventListener("click", function () {
const codeElement = this.closest(".inner").querySelector("code");
const codeToCopy = codeElement.textContent;
navigator.clipboard
.writeText(codeToCopy)
.then(() => {
this.textContent = "Copied!";
setTimeout(() => {
this.textContent = "Copy";
}, 2000);
})
.catch((err) => {
console.error("Failed to copy code: ", err);
});
});
});