|
1 | 1 | <!-- Code for copy to clipboard button --> |
2 | 2 | <script> |
3 | 3 | document.addEventListener("DOMContentLoaded", function () { |
4 | | - document.querySelectorAll('div.highlight').forEach(block => { |
5 | | - // Create a wrapper for the button and message |
6 | | - const wrapper = document.createElement('div'); |
7 | | - wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;'; |
8 | | - |
9 | | - // Create the copy button |
10 | | - const button = document.createElement('button'); |
11 | | - button.className = 'copy-button'; |
12 | | - button.innerHTML = ` |
13 | | - <svg viewBox="0 0 24 24" width="18" height="18" fill="none" xmlns="http://www.w3.org/2000/svg"> |
14 | | - <path fill-rule="evenodd" clip-rule="evenodd" d="M21 8C21 6.34315 19.6569 5 18 5H10C8.34315 5 7 6.34315 7 8V20C7 21.6569 8.34315 23 10 23H18C19.6569 23 21 21.6569 21 20V8ZM19 8C19 7.44772 18.5523 7 18 7H10C9.44772 7 9 7.44772 9 8V20C9 20.5523 9.44772 21 10 21H18C18.5523 21 19 20.5523 19 20V8Z" fill="#0F0F0F"/> |
15 | | - <path d="M6 3H16C16.5523 3 17 2.55228 17 2C17 1.44772 16.5523 1 16 1H6C4.34315 1 3 2.34315 3 4V18C3 18.5523 3.44772 19 4 19C4.55228 19 5 18.5523 5 18V4C5 3.44772 5.44772 3 6 3Z" fill="#0F0F0F"/> |
| 4 | + |
| 5 | + // don't run script for tabbed code examples |
| 6 | + if (!document.querySelector('.codetabs.cli.group.flex.justify-start.items-center.flex-wrap.box-border.rounded-lg.mt-0.mb-0.mx-auto.bg-slate-900')) { |
| 7 | + document.querySelectorAll('div.highlight').forEach(block => { |
| 8 | + // Create a wrapper for the button and message |
| 9 | + const wrapper = document.createElement('div'); |
| 10 | + wrapper.style = 'position:absolute;top:10px;right:10px;z-index:1;display:flex;align-items:center;gap:6px;'; |
| 11 | + |
| 12 | + // Create the copy button |
| 13 | + const button = document.createElement('button'); |
| 14 | + button.innerHTML = ` |
| 15 | + <button class="clipboard-button text-neutral-400 hover:text-slate-100 bg-slate-600 relative h-7 w-7 p-1 rounded rounded-mx" title="Copy to clipboard"> |
| 16 | + <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="currentColor"> |
| 17 | + <path d="M9 43.95q-1.2 0-2.1-.9-.9-.9-.9-2.1V10.8h3v30.15h23.7v3Zm6-6q-1.2 0-2.1-.9-.9-.9-.9-2.1v-28q0-1.2.9-2.1.9-.9 2.1-.9h22q1.2 0 2.1.9.9.9.9 2.1v28q0 1.2-.9 2.1-.9.9-2.1.9Zm0-3h22v-28H15v28Zm0 0v-28 28Z"></path> |
16 | 18 | </svg> |
17 | 19 | `; |
18 | 20 |
|
19 | | - // Create the "Copied!" message span |
20 | | - const message = document.createElement('span'); |
21 | | - message.className = 'copied-message'; |
22 | | - message.innerText = 'Copied to clipboard!'; |
23 | | - message.style = 'opacity:0;transition:opacity 0.3s ease;font-size:0.75em;color:white;'; |
24 | | - |
25 | | - // Handle copy logic |
26 | | - button.addEventListener('click', () => { |
27 | | - const lines = block.querySelectorAll('span.cl'); |
28 | | - const text = Array.from(lines).map(line => line.textContent).join(''); |
29 | | - |
30 | | - navigator.clipboard.writeText(text).then(() => { |
31 | | - message.style.opacity = '1'; |
32 | | - setTimeout(() => { |
33 | | - message.style.opacity = '0'; |
34 | | - }, 2000); |
35 | | - }).catch(err => { |
36 | | - console.error("Copy failed", err); |
| 21 | + // Create the tooltip container |
| 22 | + const tooltipContainer = document.createElement('div'); |
| 23 | + tooltipContainer.className = 'tooltip relative inline-block'; |
| 24 | + |
| 25 | + // Create the tooltip span |
| 26 | + const tooltip = document.createElement('span'); |
| 27 | + tooltip.className = 'tooltiptext hidden bg-slate-200 rounded rounded-mx text-slate-800 text-center text-xs p-1 absolute'; |
| 28 | + tooltip.style.bottom = '-0.6rem'; |
| 29 | + tooltip.style.left = '-5.5rem'; |
| 30 | + tooltip.textContent = 'Copied!'; |
| 31 | + |
| 32 | + tooltipContainer.appendChild(tooltip); |
| 33 | + button.appendChild(tooltipContainer); |
| 34 | + |
| 35 | + // Handle copy logic |
| 36 | + button.addEventListener('click', () => { |
| 37 | + const lines = block.querySelectorAll('span.cl'); |
| 38 | + const text = Array.from(lines).map(line => line.textContent).join(''); |
| 39 | + |
| 40 | + navigator.clipboard.writeText(text).then(() => { |
| 41 | + tooltip.style.display = 'block'; |
| 42 | + setTimeout(() => { |
| 43 | + tooltip.style.display = 'none'; |
| 44 | + }, 2000); |
| 45 | + }).catch(err => { |
| 46 | + console.error("Copy failed", err); |
| 47 | + }); |
37 | 48 | }); |
38 | | - }); |
39 | 49 |
|
40 | | - // Append button and message |
41 | | - wrapper.appendChild(message); |
42 | | - wrapper.appendChild(button); |
43 | | - block.style.position = 'relative'; |
44 | | - block.appendChild(wrapper); |
45 | | - }); |
| 50 | + // Append button and message |
| 51 | + wrapper.appendChild(button); |
| 52 | + wrapper.appendChild(tooltipContainer); |
| 53 | + block.style.position = 'relative'; |
| 54 | + block.appendChild(wrapper); |
| 55 | + }); |
| 56 | + } |
46 | 57 | }); |
47 | 58 | </script> |
48 | 59 |
|
|
0 commit comments