Skip to content

Commit 6feb62e

Browse files
committed
add "copy" button to every example code blocks
(This part of) the Clipboard API is supported (https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText) by Safari since 2009, by Chrome since 2011, and by Firefox since 2013, but just in case I included a detection code to make sure the "copy to clipboard" button is not shown if we can't copy to the clipboard :) `clone.svg` and `check.svg` from https://dazzleui.gumroad.com/l/dazzleiconsfree Picked because they seem to go well with the rest of the design, but I'm happy to replace them with anything else :)
1 parent 45dca87 commit 6feb62e

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

sass/pages/_docs.scss

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,30 @@
3232
p {
3333
margin-top: 5px;
3434
}
35+
36+
pre:has(code) {
37+
position: relative;
38+
39+
button.copy {
40+
position: absolute;
41+
top: 0;
42+
right: 0;
43+
margin: .4rem;
44+
width: 2rem;
45+
height: 2rem;
46+
border-color: transparent;
47+
background-color: transparent;
48+
opacity: 0;
49+
transition: opacity 0.25s ease-in-out;
50+
}
51+
52+
/* :hover for mouse & touchscreen,
53+
* :focus for keyboard navigation */
54+
&:hover button.copy,
55+
button.copy:focus {
56+
opacity: 1;
57+
}
58+
}
3559
}
3660

3761
.docs-whats-a-bevy {

static/clipboard.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
(() => {
2+
'use strict';
3+
document.addEventListener('DOMContentLoaded', () => {
4+
// Don't add the "copy to clipboard" button if we can't copy to clipboard :)
5+
if (!navigator.clipboard || !navigator.clipboard.writeText) {
6+
return;
7+
}
8+
9+
// Downloaded from https://dazzleui.gumroad.com/l/dazzleiconsfree
10+
// Author: Dazzle UI
11+
// License: CC-BY
12+
// Modifications:
13+
// - Removing `width` & `height` to adapt to the container size.
14+
// - Replacing `stroke="black"` with `stroke="currentColor"` to respect dark/light modes.
15+
const svg_clone = '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M8 8H7.2C6.0799 8 5.51984 8 5.09202 8.21799C4.71569 8.40973 4.40973 8.71569 4.21799 9.09202C4 9.51984 4 10.0799 4 11.2V16.8C4 17.9201 4 18.4802 4.21799 18.908C4.40973 19.2843 4.71569 19.5903 5.09202 19.782C5.51984 20 6.0799 20 7.2 20H12.8C13.9201 20 14.4802 20 14.908 19.782C15.2843 19.5903 15.5903 19.2843 15.782 18.908C16 18.4802 16 17.9201 16 16.8V16M11.2 16H16.8C17.9201 16 18.4802 16 18.908 15.782C19.2843 15.5903 19.5903 15.2843 19.782 14.908C20 14.4802 20 13.9201 20 12.8V7.2C20 6.0799 20 5.51984 19.782 5.09202C19.5903 4.71569 19.2843 4.40973 18.908 4.21799C18.4802 4 17.9201 4 16.8 4H11.2C10.0799 4 9.51984 4 9.09202 4.21799C8.71569 4.40973 8.40973 4.71569 8.21799 5.09202C8 5.51984 8 6.07989 8 7.2V12.8C8 13.9201 8 14.4802 8.21799 14.908C8.40973 15.2843 8.71569 15.5903 9.09202 15.782C9.51984 16 10.0799 16 11.2 16Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
16+
const svg_check = '<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 12.6111L8.92308 17.5L20 6.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
17+
18+
function ready(btn) {
19+
btn.ariaLabel = 'Copy code block to clipboard';
20+
btn.innerHTML = svg_clone;
21+
}
22+
23+
function success(btn) {
24+
btn.ariaLabel = 'Code block copied!';
25+
btn.innerHTML = svg_check;
26+
}
27+
28+
// Add button to every `<pre><code>`
29+
[].forEach.call(document.querySelectorAll('pre:has(code)'), function(pre) {
30+
const btn = document.createElement('button');
31+
btn.classList.add('copy');
32+
ready(btn);
33+
btn.onclick = () => navigator.clipboard.writeText(pre.firstChild.innerText).then(() => {
34+
// Temporarily show a check mark to visually indicate success
35+
success(btn);
36+
setTimeout(() => ready(btn), 2000);
37+
});
38+
pre.appendChild(btn);
39+
});
40+
});
41+
})();

templates/docs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<script src="/optional-helpers.js"></script>
1818
<script defer src="/highlight-intrapage-link.js"></script>
19+
<script defer src="/clipboard.js"></script>
1920
{% endblock head_extensions %}
2021

2122
{% block mobile_page_menu %}

0 commit comments

Comments
 (0)