|
1 | | -<!-- simple component only taking slot --> |
| 1 | +<script setup lang="ts"> |
| 2 | +const { copy } = useClipboard() |
| 3 | +
|
| 4 | +const handleCopy = async (e: MouseEvent) => { |
| 5 | + const target = (e.target as HTMLElement).closest('[data-copy]') |
| 6 | + if (!target) return |
| 7 | +
|
| 8 | + // Find the code block sibling |
| 9 | + const wrapper = target.closest('.readme-code-block') |
| 10 | + if (!wrapper) return |
| 11 | +
|
| 12 | + const pre = wrapper.querySelector('pre') |
| 13 | + if (!pre?.textContent) return |
| 14 | +
|
| 15 | + await copy(pre.textContent) |
| 16 | +
|
| 17 | + // Visual feedback |
| 18 | + const icon = target.querySelector('span') |
| 19 | + if (icon) { |
| 20 | + const originalIcon = 'i-carbon:copy' |
| 21 | + const successIcon = 'i-carbon:checkmark' |
| 22 | +
|
| 23 | + icon.classList.remove(originalIcon) |
| 24 | + icon.classList.add(successIcon) |
| 25 | +
|
| 26 | + setTimeout(() => { |
| 27 | + icon.classList.remove(successIcon) |
| 28 | + icon.classList.add(originalIcon) |
| 29 | + }, 2000) |
| 30 | + } |
| 31 | +} |
| 32 | +</script> |
| 33 | + |
2 | 34 | <template> |
3 | | - <article class="readme prose prose-invert max-w-[70ch]"> |
| 35 | + <article class="readme prose prose-invert max-w-[70ch]" @click="handleCopy"> |
| 36 | + <!-- Hidden element to safelist icons for dynamic usage --> |
| 37 | + <div class="hidden i-carbon:copy i-carbon:checkmark"></div> |
4 | 38 | <slot /> |
5 | 39 | </article> |
6 | 40 | </template> |
|
96 | 130 | box-sizing: border-box; |
97 | 131 | } |
98 | 132 |
|
| 133 | +.readme :deep(.readme-code-block) { |
| 134 | + display: block; |
| 135 | + width: 100%; |
| 136 | + position: relative; |
| 137 | +} |
| 138 | +
|
| 139 | +.readme :deep(.readme-copy-button) { |
| 140 | + position: absolute; |
| 141 | + top: 0.4rem; |
| 142 | + right: 0.4rem; |
| 143 | + left: auto; |
| 144 | + display: inline-flex; |
| 145 | + align-items: center; |
| 146 | + justify-content: center; |
| 147 | + padding: 0.25rem; |
| 148 | + border-radius: 6px; |
| 149 | + background: color-mix(in srgb, var(--bg-subtle) 80%, transparent); |
| 150 | + border: 1px solid var(--border); |
| 151 | + color: var(--fg-subtle); |
| 152 | + opacity: 0; |
| 153 | + transition: |
| 154 | + opacity 0.2s ease, |
| 155 | + color 0.2s ease, |
| 156 | + border-color 0.2s ease; |
| 157 | +} |
| 158 | +
|
| 159 | +.readme :deep(.readme-code-block:hover .readme-copy-button) { |
| 160 | + opacity: 1; |
| 161 | +} |
| 162 | +
|
| 163 | +.readme :deep(.readme-copy-button:hover) { |
| 164 | + color: var(--fg); |
| 165 | + border-color: var(--border-hover); |
| 166 | +} |
| 167 | +
|
| 168 | +.readme :deep(.readme-copy-button > span) { |
| 169 | + width: 1.05rem; |
| 170 | + height: 1.05rem; |
| 171 | + display: inline-block; |
| 172 | + pointer-events: none; |
| 173 | +} |
| 174 | +
|
99 | 175 | .readme :deep(pre code), |
100 | 176 | .readme :deep(.shiki code) { |
101 | 177 | background: transparent !important; |
|
0 commit comments