We're Launching Soon
We're building something extraordinary. Join our waitlist to get early access and exclusive updates when we go live.
Use this Template
Click "Copy Template" and paste the code into your BricksBuilder Website.
/*new*/
function updateClipboard(newClip, button) {
navigator.clipboard.writeText(newClip).then(() => {
console.log('Clipboard successfully set');
// Change button text
const originalText = button.textContent;
button.textContent = 'Copied!';
button.setAttribute('aria-label', 'Code copied to clipboard');
// Restore original text after 2s
setTimeout(() => {
button.textContent = originalText;
button.setAttribute('aria-label', 'Copy code');
}, 2000);
}).catch(err => console.error('Clipboard write failed:', err));
}
// Event delegation for dynamically generated buttons
document.addEventListener('click', (e) => {
const button = e.target.closest('.copy-btn'); // Ensure button click works even if child element (e.g., span) is clicked
if (button) {
e.preventDefault();
// Get dynamic code from data attribute
let codeText = button.dataset.code;
// If data-code is missing, get text from nearest code block
if (!codeText) {
const codeBlock = button.closest('.code-container')?.querySelector('code');
codeText = codeBlock ? codeBlock.innerText.trim() : '';
}
if (codeText) {
updateClipboard(codeText, button);
} else {
console.warn('No code found to copy for', button);
}
}
});