stylescape
Version:
Stylescape is a visual identity framework developed by Scape Agency.
44 lines • 1.6 kB
JavaScript
export class ClipboardHelper {
static copyCodeFromButton(button) {
const nextElement = button.nextElementSibling;
if (!(nextElement instanceof HTMLElement))
return;
const code = nextElement.innerText;
navigator.clipboard.writeText(code).then(() => {
button.textContent = 'Copied!';
setTimeout(() => {
button.textContent = 'Copy';
}, 1500);
});
}
static attachToButtons(selector = '.copy-button') {
const buttons = document.querySelectorAll(selector);
buttons.forEach((button) => {
button.addEventListener('click', () => ClipboardHelper.copyCodeFromButton(button));
});
}
static copyById(codeId) {
const codeElement = document.getElementById(codeId);
if (!(codeElement instanceof HTMLElement)) {
console.warn(`Code element with ID "${codeId}" not found.`);
return;
}
const text = codeElement.innerText.trim();
navigator.clipboard
.writeText(text)
.then(() => {
const button = document.querySelector(`button[onclick*="${codeId}"]`);
if (button) {
const original = button.textContent;
button.textContent = 'Copied!';
setTimeout(() => {
button.textContent = original;
}, 1500);
}
})
.catch((err) => {
console.error('Failed to copy text:', err);
});
}
}
//# sourceMappingURL=ClipboardHelper.js.map