-
Notifications
You must be signed in to change notification settings - Fork 20
feat: Copy/Clickboard button in code snippets #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,7 @@ | |
| body { | ||
| color: var(--text-color); | ||
| background: var(--background-color); | ||
| font-family: | ||
| "Helvetica Neue", | ||
| "Segoe UI", | ||
| "Noto Sans", | ||
| Helvetica, | ||
| Arial, | ||
| font-family: "Helvetica Neue", "Segoe UI", "Noto Sans", Helvetica, Arial, | ||
|
||
| sans-serif; | ||
| font-size: 20px; | ||
| line-height: 2em; | ||
|
|
@@ -22,18 +17,30 @@ a { | |
| color: var(--accent-color); | ||
| } | ||
|
|
||
| a:hover, a:active { | ||
| a:hover, | ||
| a:active { | ||
| text-decoration: underline; | ||
| } | ||
|
|
||
| pre, code { | ||
| pre, | ||
| code { | ||
| font-family: Monaco, "Ubuntu Mono", Inconsolata, Consolas, monospace; | ||
| } | ||
|
|
||
| pre { | ||
| line-height: 1.4em; | ||
|
|
||
| margin: 20px 0 !important; // remove ALL outside spacing | ||
|
||
| padding: 0 !important; // remove ALL inside spacing | ||
| } | ||
|
|
||
| pre > code { | ||
| margin: 0 !important; // remove internal margin | ||
| padding: 16px !important; // remove internal padding | ||
| display: block !important; | ||
| } | ||
|
|
||
|
|
||
| strong { | ||
| font-weight: bold; | ||
| } | ||
|
|
@@ -58,3 +65,24 @@ em { | |
| transform: translateY(0%); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| .copy-code-button { | ||
| position: absolute; | ||
| top: 6px; | ||
| right: 6px; | ||
| padding: 0; | ||
| font-size: 12px; | ||
|
|
||
| background: transparent !important; | ||
| border: none !important; | ||
| box-shadow: none !important; | ||
| outline: none !important; | ||
|
|
||
| cursor: pointer; | ||
| } | ||
|
|
||
| .copy-code-button span { | ||
| font-size: 16px; | ||
| background: transparent !important; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,11 @@ | |
| <meta name="twitter:card" content="summary" /> | ||
|
|
||
| <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
| <link | ||
| href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined" | ||
|
||
| rel="stylesheet" | ||
| /> | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: could you remove this newline? |
||
| </head> | ||
| <body> | ||
| <a class="skip-to-content-link" href="#main"> | ||
|
|
@@ -45,6 +50,10 @@ <h1><a href="/">Helmet.js</a></h1> | |
| <main id="main" class="container"> | ||
| {{ block "main" . }}{{ end }} | ||
| </div> | ||
| <link rel="stylesheet" href="/css/copy-code.css"> | ||
|
||
| <script src="/js/copy-code.js" defer></script> | ||
|
|
||
|
|
||
| </body> | ||
| </html> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| pre { | ||
| position: relative !important; | ||
| padding-top: 2em; | ||
| overflow-x: auto; | ||
| } | ||
|
|
||
| .copy-code-button { | ||
| position: absolute !important; | ||
| top: 12px; | ||
| right: 12px; | ||
| width: 16px; | ||
| height: 16px; | ||
| padding: 0; | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| font-size: 10px; | ||
| line-height: 1; | ||
| background: rgba(255, 255, 255, 0.06); | ||
| border: 1px solid rgba(255, 255, 255, 0.15); | ||
| border-radius: 4px; | ||
| color: #fff; | ||
| cursor: pointer; | ||
| z-index: 300; | ||
| transition: all 0.15s ease; | ||
| } | ||
|
|
||
| .copy-code-button:hover { | ||
| background: rgba(255, 255, 255, 0.18); | ||
| transform: scale(1.05); | ||
| } | ||
|
|
||
| .copy-code-button:active { | ||
| transform: scale(0.85); | ||
| } | ||
|
|
||
| pre code { | ||
| display: block; | ||
| padding: 1em; | ||
| padding-top: 0; | ||
| margin: 0; | ||
| background: none; | ||
| position: static !important; | ||
| overflow-x: auto; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| (function () { | ||
|
||
| document.addEventListener("DOMContentLoaded", () => { | ||
| const blocks = document.querySelectorAll( | ||
| "pre > code.language-js, pre > code.language-javascript" | ||
| ); | ||
|
|
||
| blocks.forEach((code) => { | ||
| const pre = code.parentElement; | ||
| pre.style.position = "relative"; | ||
|
||
|
|
||
| const btn = document.createElement("button"); | ||
| btn.className = "copy-code-button"; | ||
| btn.innerHTML = | ||
| '<span class="material-symbols-outlined">content_copy</span>'; | ||
|
||
|
|
||
| pre.appendChild(btn); | ||
|
|
||
| btn.addEventListener("click", async () => { | ||
|
||
| try { | ||
| await navigator.clipboard.writeText(code.innerText.trim()); | ||
| btn.innerHTML = | ||
| '<span class="material-symbols-outlined">check</span>'; | ||
| setTimeout(() => { | ||
| btn.innerHTML = | ||
| '<span class="material-symbols-outlined">content_copy</span>'; | ||
|
||
| }, 800); | ||
| } catch (err) { | ||
| btn.innerHTML = | ||
| '<span class="material-symbols-outlined">error</span>'; | ||
| setTimeout(() => { | ||
| btn.innerHTML = | ||
| '<span class="material-symbols-outlined">content_copy</span>'; | ||
| }, 800); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
| })(); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you revert the changes to this file?