Copy Code Block Using Tailwind UI
The Copy Code Block component, created with Tailwind CSS, offers a user-friendly way to display code snippets and enables effortless copying of code examples with a single click.
other
Loading component...
51 lines
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
<link
href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:ital,wght@0,200;0,300;0,400;0,500;0,600;0,700;0,800;1,200;1,300;1,400;1,500;1,600;1,700;1,800&display=swap"
rel="stylesheet">
<style>
body {
font-family: 'Plus Jakarta Sans', sans-serif;
}
</style>
<div class="bg-gray-100 mx-8 lg:mx-0 flex justify-center items-center h-screen">
<div class="w-full lg:max-w-2xl mx-auto bg-gray-900 px-4 rounded-md shadow-lg relative">
<div class="flex justify-between items-center px-4 py-4 rounded-t-lg">
<span class="text-xs font-semibold text-gray-400">html</span>
<button
id="copyButton"
class="text-xs font-semibold text-gray-400 btn-copy hover:text-gray-200"
data-clipboard-target="#codeBlock"
onclick="copyCodeToClipboard()">📋 Copy code</button>
</div>
<pre class="text-sm text-white overflow-x-auto">
<code id="codeBlock" class="block">
<a href="#" class="block max-w-sm p-6 bg-white border border-gray-200 rounded-lg shadow hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700">
<h5 class="mb-2 text-2xl font-bold tracking-tight text-gray-900 dark:text-white">Noteworthy technology acquisitions 2021</h5>
<p class="font-normal text-gray-700 dark:text-gray-400">Here are the biggest enterprise technology acquisitions of 2021 so far, in reverse chronological order.</p>
</a>
</code>
</pre>
</div>
</div>
<script>
function copyCodeToClipboard() {
const copyText = document.getElementById('codeBlock').innerText;
const textArea = document.createElement('textarea');
textArea.value = copyText;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
const copyButton = document.getElementById('copyButton');
copyButton.textContent = '✅ Copied!';
setTimeout(function () {
copyButton.textContent = '📋 Copy code';
}, 2000);
}
</script>