This commit is contained in:
Serreau Jovann
2026-03-23 15:32:43 +01:00
parent 6b009a4511
commit f0002ae7cb

View File

@@ -1,12 +1,29 @@
export function initShare() {
document.querySelectorAll('[data-share-copy]').forEach(btn => {
btn.addEventListener('click', () => {
const url = btn.dataset.shareCopy
globalThis.navigator.clipboard.writeText(url).then(() => {
const original = btn.innerHTML
btn.innerHTML = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>'
setTimeout(() => { btn.innerHTML = original }, 1500)
})
})
})
/**
* Handles the click event to copy text and toggle the button icon.
* @param {PointerEvent} event
*/
async function handleCopyClick(event) {
const btn = event.currentTarget;
const url = btn.dataset.shareCopy;
try {
await globalThis.navigator.clipboard.writeText(url);
// Swap UI state
const original = btn.innerHTML;
btn.innerHTML = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/></svg>';
setTimeout(() => {
btn.innerHTML = original;
}, 1500);
} catch (err) {
console.error('Failed to copy text: ', err);
}
}
export function initShare() {
const shareButtons = document.querySelectorAll('[data-share-copy]');
shareButtons.forEach(btn => {
btn.addEventListener('click', handleCopyClick);
});
}