Skip to content

Commit

Permalink
[FEATURE] Added a copy button to the SVG code snippet for easy copying
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshuramavat committed Nov 19, 2024
1 parent 0ed6f1b commit 5a17c5a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
22 changes: 22 additions & 0 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,28 @@ document.addEventListener("DOMContentLoaded", () => {
}
}
}

document.querySelectorAll('.copy-btn').forEach((button) => {
button.addEventListener('click', () => {
const codeBlock = button.nextElementSibling?.querySelector('code');

if (codeBlock) {
const textToCopy = codeBlock.textContent.trim();

navigator.clipboard
.writeText(textToCopy)
.then(() => {
button.textContent = 'Copied!';
setTimeout(() => {
button.textContent = 'Copy';
}, 2000);
})
.catch((error) => {
console.error('Failed to copy text:', error);
});
}
});
});
});

// Theme
Expand Down
44 changes: 44 additions & 0 deletions assets/scss/docs/_misc.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,47 @@
font-family: monospace;
}
}

/* Container for Code Block */
.code-container {
position: relative;
border-radius: 6px;
font-family: monospace;
overflow: hidden;

/* Code Block */
.code-block {
margin: 0;
padding: 16px;
overflow-x: auto;
font-size: 14px;
line-height: 1.5;
}

/* Copy Button */
.copy-btn {
position: absolute;
top: 8px;
right: 8px;
padding: 4px 8px;
font-size: 12px;
color: #24292f;
background-color: #f3f4f6;
border: 1px solid #d0d7de;
border-radius: 6px;
cursor: pointer;
box-shadow:
0 1px 0 rgba(27, 31, 35, 0.04),
inset 0 1px 0 rgba(255, 255, 255, 0.25);

&:hover {
background-color: #e1e4e8;
border-color: #c9d1d9;
}

&:active {
background-color: #d7dbe0;
box-shadow: inset 0 1px 0 rgba(27, 31, 35, 0.15);
}
}
}
5 changes: 4 additions & 1 deletion tmpl/html/docs/_makros.html.twig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{% macro codeblock(markup, language = 'html') %}
<pre class="p-3 rounded bg-adapt-light"><code class="language-{{ language }}">{{ markup|trim|escape }}</code></pre>
<div class="code-container position-relative">
<button class="copy-btn" aria-label="Copy code" title="Copy">Copy</button>
<pre class="p-3 rounded bg-adapt-light code-block"><code class="language-{{ language }}">{{ markup|trim|escape }}</code></pre>
</div>
{% endmacro %}

0 comments on commit 5a17c5a

Please sign in to comment.