Last active
April 16, 2023 14:28
-
-
Save KevinBatdorf/807c807a2c9ee9e72d3ea4d5c29e6454 to your computer and use it in GitHub Desktop.
Example adding custom copy button code block pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const blocks = Array.from( | |
document.querySelectorAll('.wp-block-kevinbatdorf-code-block-pro'), | |
); | |
const copyText = 'Click anywhere to copy'; | |
const copiedText = 'Copied!'; | |
blocks && | |
blocks.forEach((block) => { | |
if (!block.querySelector('span[data-code]')) return | |
block.insertAdjacentHTML( | |
'beforeend', | |
`<span class="my-copy-button" style="position:absolute;top:0;right:0;color:white;padding:0 0.5rem;background:#2e2e2e;z-index:9999;height:36px;font-size:0.9em;display:flex;align-items:center;">${copyText}</span>`, | |
); | |
block.addEventListener('click', () => { | |
block.querySelector('span[data-code]').click(); | |
const copyButton = block.querySelector('.my-copy-button'); | |
copyButton.innerText = copiedText; | |
setTimeout(() => (copyButton.innerText = copyText), 1500); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires the copy button to be there. Will just overlay some text on top of it.