diff --git a/sass/pages/_docs.scss b/sass/pages/_docs.scss index e7daa43361..c4260f7528 100644 --- a/sass/pages/_docs.scss +++ b/sass/pages/_docs.scss @@ -32,6 +32,30 @@ p { margin-top: 5px; } + + pre:has(code) { + position: relative; + + button.copy { + position: absolute; + top: 0; + right: 0; + margin: 0.5rem; + width: 2rem; + height: 2rem; + border-color: transparent; + background-color: transparent; + opacity: 0; + transition: opacity 0.25s ease-in-out; + } + + /* :hover for mouse & touchscreen, + * :focus for keyboard navigation */ + &:hover button.copy, + button.copy:focus { + opacity: 1; + } + } } .docs-whats-a-bevy { diff --git a/static/clipboard.js b/static/clipboard.js new file mode 100644 index 0000000000..092bbeabdd --- /dev/null +++ b/static/clipboard.js @@ -0,0 +1,41 @@ +(() => { + 'use strict'; + document.addEventListener('DOMContentLoaded', () => { + // Don't add the "copy to clipboard" button if we can't copy to clipboard :) + if (!navigator.clipboard || !navigator.clipboard.writeText) { + return; + } + + // Downloaded from https://dazzleui.gumroad.com/l/dazzleiconsfree + // Author: Dazzle UI + // License: CC-BY + // Modifications: + // - Removing `width` & `height` to adapt to the container size. + // - Replacing `stroke="black"` with `stroke="currentColor"` to respect dark/light modes. + const svg_clone = ''; + const svg_check = ''; + + function ready(btn) { + btn.ariaLabel = 'Copy'; + btn.innerHTML = svg_clone; + } + + function success(btn) { + btn.ariaLabel = 'Copied!'; + btn.innerHTML = svg_check; + } + + // Add button to every `
`
+        [].forEach.call(document.querySelectorAll('pre:has(code)'), function(pre) {
+            const btn = document.createElement('button');
+            btn.classList.add('copy');
+            ready(btn);
+            btn.onclick = () => navigator.clipboard.writeText(pre.firstChild.innerText).then(() => {
+                // Temporarily show a check mark to visually indicate success
+                success(btn);
+                setTimeout(() => ready(btn), 2000);
+            });
+            pre.appendChild(btn);
+        });
+    });
+})();
diff --git a/templates/docs.html b/templates/docs.html
index a6e4ce1766..ef2ab71fb0 100644
--- a/templates/docs.html
+++ b/templates/docs.html
@@ -16,6 +16,7 @@
 
   
   
+  
 {% endblock head_extensions %}
 
 {% block mobile_page_menu %}