Skip to content

Commit 22250db

Browse files
himanshuramavatbenjaminkott
authored andcommitted
[FEATURE] Added a copy button to the SVG code snippet for easy copying
1 parent 0e74d5b commit 22250db

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

assets/js/main.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ document.addEventListener("DOMContentLoaded", () => {
1818
}
1919
}
2020
}
21+
22+
document.querySelectorAll('.code-copy-btn').forEach((button) => {
23+
button.addEventListener('click', () => {
24+
const codeBlock = button.nextElementSibling?.querySelector('code');
25+
26+
if (codeBlock) {
27+
const textToCopy = codeBlock.textContent.trim();
28+
29+
navigator.clipboard
30+
.writeText(textToCopy)
31+
.then(() => {
32+
button.textContent = 'Copied!';
33+
setTimeout(() => {
34+
button.textContent = 'Copy';
35+
}, 2000);
36+
})
37+
.catch((error) => {
38+
console.error('Failed to copy text:', error);
39+
});
40+
}
41+
});
42+
});
2143
});
2244

2345
// Theme

assets/scss/docs/_misc.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,19 @@
4848
font-family: monospace;
4949
}
5050
}
51+
52+
.code-container {
53+
position: relative;
54+
border-radius: var(--bs-border-radius);
55+
overflow: hidden;
56+
57+
.code-copy-btn {
58+
font-family: var(--bs-font-monospace);
59+
position: absolute;
60+
inset-block-start: 0;
61+
inset-inline-end: 0;
62+
border-start-start-radius: 0;
63+
border-start-end-radius: 0;
64+
border-end-end-radius: 0;
65+
}
66+
}

tmpl/html/docs/_makros.html.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{% macro codeblock(markup, language = 'html') %}
2-
<pre class="p-3 rounded bg-adapt-light"><code class="language-{{ language }}">{{ markup|trim|escape }}</code></pre>
2+
<div class="code-container position-relative">
3+
<button class="btn btn-default btn-sm code-copy-btn" aria-label="Copy code" title="Copy">Copy</button>
4+
<pre class="p-3 rounded bg-adapt-light code-block"><code class="language-{{ language }}">{{ markup|trim|escape }}</code></pre>
5+
</div>
36
{% endmacro %}

0 commit comments

Comments
 (0)