Skip to content

Instantly share code, notes, and snippets.

@KevinBatdorf
Last active April 16, 2023 14:28
Show Gist options
  • Save KevinBatdorf/807c807a2c9ee9e72d3ea4d5c29e6454 to your computer and use it in GitHub Desktop.
Save KevinBatdorf/807c807a2c9ee9e72d3ea4d5c29e6454 to your computer and use it in GitHub Desktop.
Example adding custom copy button code block pro
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);
});
});
@KevinBatdorf
Copy link
Author

Requires the copy button to be there. Will just overlay some text on top of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment