From 0ca1e96d6bf75b17988dd60d4e500d7a86a5985a Mon Sep 17 00:00:00 2001 From: Tarun Chauhan Date: Tue, 10 Jan 2023 21:35:00 +0530 Subject: [PATCH] feat: add copy to clipboard in code blocks --- website/siteConfig.js | 7 +++- website/static/css/code-block-buttons.css | 42 ++++++++++++++++++++ website/static/js/code-block-buttons.js | 47 +++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 website/static/css/code-block-buttons.css create mode 100644 website/static/js/code-block-buttons.js diff --git a/website/siteConfig.js b/website/siteConfig.js index b4d93ab11a45..9c20ded7e76e 100644 --- a/website/siteConfig.js +++ b/website/siteConfig.js @@ -56,9 +56,14 @@ const siteConfig = { }, usePrism: ["javascript", "jsx", "typescript", "ts", "js", "html", "css"], useEnglishUrl: true, - scripts: ["https://buttons.github.io/buttons.js"], + scripts: [ + "https://buttons.github.io/buttons.js", + "https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js", + "/js/code-block-buttons.js", + ], stylesheets: [ "//unpkg.com/@sandhose/prettier-animated-logo@1.0.3/dist/wide.css", + "/css/code-block-buttons.css", ], algolia: { apiKey: process.env.ALGOLIA_PRETTIER_API_KEY, diff --git a/website/static/css/code-block-buttons.css b/website/static/css/code-block-buttons.css new file mode 100644 index 000000000000..bf2c1246d55c --- /dev/null +++ b/website/static/css/code-block-buttons.css @@ -0,0 +1,42 @@ +/* "Copy" code block button */ +pre { + position: relative; +} + +pre .btnIcon { + position: absolute; + top: -5px; + right: 16px; + z-index: 2; + cursor: pointer; + border: 1px solid transparent; + padding: 0; + background-color: transparent; + height: 30px; + transition: all 0.25s ease-out; + display: none; +} + +pre .btnIcon:hover { + text-decoration: none; + display: block; +} + +pre code[class*="language-"]:hover + .btnIcon { + display: block; +} + +.btnIcon__body { + align-items: center; + display: flex; + color: #535b64; +} + +.btnIcon svg { + fill: currentColor; + margin-right: 0.4em; +} + +.btnIcon__label { + font-size: 11px; +} diff --git a/website/static/js/code-block-buttons.js b/website/static/js/code-block-buttons.js new file mode 100644 index 000000000000..68f1953d1ce9 --- /dev/null +++ b/website/static/js/code-block-buttons.js @@ -0,0 +1,47 @@ +"use strict"; + +window.addEventListener("load", () => { + function button(label, ariaLabel, icon, className) { + const btn = document.createElement("button"); + btn.classList.add("btnIcon", className); + btn.setAttribute("type", "button"); + btn.setAttribute("aria-label", ariaLabel); + btn.innerHTML = + '
' + + icon + + '' + + label + + "" + + "
"; + return btn; + } + + function addButtons(codeBlockSelector, btn) { + for (const code of document.querySelectorAll(codeBlockSelector)) { + code.parentNode.appendChild(btn.cloneNode(true)); + } + } + + const copyIcon = + ''; + + addButtons( + ".hljs", + button("Copy", "Copy code to clipboard", copyIcon, "btnClipboard") + ); + // eslint-disable-next-line no-undef + const clipboard = new ClipboardJS(".btnClipboard", { + target(trigger) { + return trigger.parentNode.querySelector("code"); + }, + }); + + clipboard.on("success", (event) => { + event.clearSelection(); + const textEl = event.trigger.querySelector(".btnIcon__label"); + textEl.textContent = "Copied"; + setTimeout(() => { + textEl.textContent = "Copy"; + }, 2000); + }); +});