Adding a CopyButton feature#537
Conversation
There was a problem hiding this comment.
Let's avoid adding the .idea folder. We can .idea to the main .gitignore file.
There was a problem hiding this comment.
Thanks for sharing @tormath1
With a couple more changes, will stash them before opening the PR for review.
|
Linking it for visibility 😊 flatcar/Flatcar#2022 |
|
Thanks a lot for the PR! The implementation needs to change a bit, currently the copy button is rendered outside of the codeblock - which does not look good in UI terms. It would be better to use a font-awesome icon and place it within. Other thing is we use pygments, but you can switch over to use Hugo's highlight which give much more better themes for the code block. |
6ba5054 to
63efd51
Compare
|
Could you please remove the |
75b28f1 to
9d34850
Compare
d1ed56f to
5daf7f0
Compare
|
Hello @yufi16, @sayanchowdhury! Sorry for the late response. Please use the existing SCSS variables. E.g. $flatcar-blue for #08a2af, $gray-100 for #f8f9fa, etc. These are exact matches already defined in _custom.scss and Bootstraps variables. Can you please check if "Fira Code" is properly loading? That is a custom font that is not installed on most of devices. |
| height: 2px; | ||
| position: absolute; | ||
| top: 0%; | ||
| top: 0; |
There was a problem hiding this comment.
Can you please use measurement units? It may still be correct without them, but it is a good practice to indicate them.
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| width: 100; |
| } | ||
|
|
||
| &.copied { | ||
| background: $flatcar-blue; /* cyan-blue feedback */ |
| border-radius: 6px; | ||
| display: block; | ||
| overflow-x: auto; | ||
| font-family: "Fira Code", monospace; |
There was a problem hiding this comment.
I just noticed we already load a monospace font family. Could we use $font-family-monospace instead (- that should include it)? Perhaps the difference doesn't make worth loading a new font family.
There was a problem hiding this comment.
what we have already is called overpass mono
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
| <link href="https://fonts.googleapis.com/css2?family=Overpass+Mono:wght@300;400;600;700&family=Overpass:ital,wght@0,100;0,200;0,300;0,400;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet" media="print" onload="this.media='all'"/> | ||
|
|
||
| <link href="https://fonts.googleapis.com/css2?family=Fira+Code:wght@300..700&display=swap" rel="stylesheet"> |
There was a problem hiding this comment.
If overpass mono is similar enough to fira code, I would suggest to drop fira code
|
Sure thing, On it! |
b09ff59 to
237c01a
Compare
d9f6989 to
6a903ae
Compare
|
This is still a Work in progress, and I will update it shortly. |
Signed-off-by: yussaffi <yufi.alam@outlook.com>
|
This seems to be ready for review, @sayanchowdhury @ervcz Please feel free to have a look! |
| closeMenu(this); | ||
| }); | ||
| }) | ||
| document.querySelectorAll(className).forEach(function(item) { |
There was a problem hiding this comment.
Can you please remove the identation here?
| function (item) { | ||
| if (getCookie("cookieconsent_status") !== "allow") { | ||
| item.classList.remove("d-none"); | ||
| function (item) { |
There was a problem hiding this comment.
I think here as well there is identation that needs to be dropped. We use 2-spaces for the js files.
| } | ||
| ); No newline at end of file | ||
| ); | ||
| document.addEventListener("DOMContentLoaded", () => { |
There was a problem hiding this comment.
Instead of the dynamically loading the button - it would be better to use the Render Hooks from hugo
| <a class="visually-hidden visually-hidden-focusable" href="#content">Skip to content</a> | ||
| {{- partial "header.html" . -}} | ||
| {{- partial "head.html" . -}} | ||
| <body> |
There was a problem hiding this comment.
This change strip the required indentation.
| overflow-x: auto; | ||
| font-family: "overpass mono", monospace; | ||
|
|
||
| &.copied { |
There was a problem hiding this comment.
I don't see this code being used.
| width: 100%; | ||
| top: 0px; | ||
| left: 0px; | ||
| width: 100; |
There was a problem hiding this comment.
This issue is still there.
| <link rel="preconnect" href="https://fonts.googleapis.com"> | ||
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
| <link href="https://fonts.googleapis.com/css2?family=Overpass+Mono:wght@300;400;600;700&family=Overpass:ital,wght@0,100;0,200;0,300;0,400;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet" media="print" onload="this.media='all'"/> | ||
|
|
There was a problem hiding this comment.
These lines make it easier to skim through
|
Please also update the commit messsage. |
Sure thing, will iterate as per the feedback asap! |
|
WIP! |
There was a problem hiding this comment.
Pull request overview
This PR introduces a “Copy” button for code snippets to make copying fenced code blocks faster, alongside some related styling updates to improve code-block readability.
Changes:
- Add client-side “copy to clipboard” buttons to rendered code blocks.
- Add SCSS styling for copy buttons and code block backgrounds.
- Remove the legacy
static/js/common.jsand apply minor formatting tweaks in templates/styles.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| themes/flatcar/layouts/partials/head.html | Minor edit in the head partial; file content impacts overall document structure. |
| themes/flatcar/layouts/_default/baseof.html | Formatting/indentation changes to the base layout wrapper. |
| themes/flatcar/assets/scss/_styles.scss | CSS adjustments plus new styles for code blocks and the copy button UI. |
| themes/flatcar/assets/js/common.js | Adds DOM logic to inject and handle copy-to-clipboard buttons for code snippets. |
| static/js/common.js | Removes the previously committed static version of the common JS. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| top: 0px; | ||
| left: 0px; | ||
| width: 100; |
| document.addEventListener("DOMContentLoaded", () => { | ||
| document.querySelectorAll("pre code").forEach(block => { | ||
| const wrapper = block.parentNode; | ||
| wrapper.classList.add("code-block"); | ||
|
|
||
| const button = document.createElement("button"); | ||
| button.className = "copy-btn"; | ||
| button.setAttribute("aria-label", "Copy to clipboard"); | ||
| button.innerHTML = ` | ||
| <i class="fa-regular fa-copy"></i>`; | ||
| wrapper.appendChild(button); | ||
|
|
||
| button.addEventListener("click", () => { | ||
| navigator.clipboard.writeText(block.innerText.trim()).then(() => { | ||
| button.classList.add("copied"); | ||
| setTimeout(() => button.classList.remove("copied"), 1200); | ||
| }); | ||
| }); | ||
| }); | ||
| }); No newline at end of file |
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> | ||
| <link href="https://fonts.googleapis.com/css2?family=Overpass+Mono:wght@300;400;600;700&family=Overpass:ital,wght@0,100;0,200;0,300;0,400;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet" media="print" onload="this.media='all'"/> | ||
|
|
||
| <!-- Search meta tags --> | ||
| {{ if .Params.noindex }} | ||
| <meta name="robots" content="noindex, follow" /> |
If you know that a PR is Work In Progress please set it as "Draft" |



What this PR does / why we need it:
Introducing a Copy button feature to quickly copy code snippets, reducing friction and saving time.
Which issue(s) this PR fixes:
#2022 -> flatcar/Flatcar#2022
Does this PR introduce a user-facing change?
Enhancing code blocks with distinct background, improving readability and visual clarity.
Special Notes for your Reviewer: