/* Tooltips */
document.querySelectorAll("[data-tooltip]").forEach((el) => {
tippy(el, { content: el.dataset.tooltip });
});
/* Copy button component */
async function initCopyButtons() {
const copyIcon = ``;
const checkIcon = ``;
const defaultButtonLabel = `Zkopírovat text do schránky`;
const copiedButtonLabel = `Text zkopírován`;
const revertTimeout = 5_000;
const accessibleLabel = (text) => `${text}`;
const defaultButtonContent = copyIcon + accessibleLabel(defaultButtonLabel);
const copiedButtonContent = checkIcon + accessibleLabel(copiedButtonLabel);
try {
const permission = await navigator.permissions.query({
name: "clipboard-write",
});
if (permission.state == "denied") {
return;
}
} catch (error) {
// This query throws on some browsers which don't know "clipboard-write" permission.
// We ignore this error and then try to access clipboard anyway.
}
document
.querySelectorAll(".prose pre code, .code-block")
.forEach((codeBlock) => {
const button = document.createElement("button");
button.innerHTML = defaultButtonContent;
button.className = "button button--opaque absolute top-3 right-3";
button.addEventListener("click", () => {
navigator.clipboard.writeText(codeBlock.innerHTML).then(() => {
button.innerHTML = copiedButtonContent;
setTimeout(() => {
button.innerHTML = defaultButtonContent;
}, revertTimeout);
});
});
codeBlock.parentElement.insertBefore(
button,
codeBlock.nextElementSibling
);
});
}
initCopyButtons();