Skip to content

Commit 2b153d0

Browse files
committed
Merge branch 'mayconblopes-enhance' into source
2 parents b8bb9d3 + 0f1d0a9 commit 2b153d0

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/templates/ContentTemplate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import { partColors } from './partColors';
2323
import path from 'path';
2424
import snakeCase from 'lodash/fp/snakeCase';
2525
import getPartTranslationPath from '../utils/getPartTranslationPath';
26+
import { createCopyButton } from './copy-code-button/create-copy-buttons';
27+
28+
2629

2730
export default class ContentTemplate extends Component {
2831
constructor(props) {
@@ -69,6 +72,7 @@ export default class ContentTemplate extends Component {
6972
});
7073

7174
window.addEventListener('scroll', this.handleScroll);
75+
createCopyButton();
7276
}
7377

7478
componentWillUnmount() {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.copy-code-button:host {
2+
display: inline-flex;
3+
}
4+
5+
.copy-code-button {
6+
display: inline-flex;
7+
align-items: center;
8+
justify-content: center;
9+
width: fit-content;
10+
margin: 0;
11+
padding: 0.5rem;
12+
background: var(--color-pre-background);
13+
border: none;
14+
border-radius: 0.25rem;
15+
box-shadow: 0 0.25rem 0.5rem rgba(0, 0, 0, 0.25);
16+
color: #fff;
17+
cursor: pointer;
18+
font-weight: 600;
19+
opacity: 0.2;
20+
transition: 0.2s ease-in-out;
21+
}
22+
23+
.copy-code-button:hover {
24+
opacity: 1;
25+
}
26+
27+
.copy-code-button:active{
28+
opacity: 0;
29+
color: red;
30+
}
31+
32+
pre {
33+
overflow: auto;
34+
padding: 1rem;
35+
border-radius: 0.5rem;
36+
position: relative;
37+
}
38+
39+
pre .copy-code-button {
40+
position: absolute;
41+
top: 0.5rem;
42+
right: 0.5rem;
43+
}
44+
45+
46+
code {
47+
white-space : pre-wrap !important;
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*This code exports a function named createCopyButton which adds a "copy" button to all pre elements
2+
containing code snippets. The function checks if it's being run in a browser before executing
3+
the code, because SSR from Gatsby. In the copyCode function, it copies the text content of the code element to
4+
the clipboard using the Clipboard API.*/
5+
6+
import './copy-code-button-style.css';
7+
8+
//Declaring a variable to check if the code is running on a browser or not.
9+
const isBrowser = typeof document !== undefined;
10+
11+
//Exporting a function that creates a copy button for pre elements containing code.
12+
export function createCopyButton() {
13+
//Checking if the code is running on a browser.
14+
if (isBrowser) {
15+
//Selecting all the pre elements containing code on the page.
16+
let blocks = document.querySelectorAll('pre');
17+
18+
//Function to copy the text content of the code element to clipboard.
19+
function copyCode() {
20+
const pre = this.parentElement;
21+
let code = pre.querySelectorAll('code')[0];
22+
navigator.clipboard.writeText(code.innerText);
23+
}
24+
25+
//Adding a copy button to each pre block.
26+
blocks.forEach(block => {
27+
let button = document.createElement('button');
28+
//Adding the CSS class for the copy button style.
29+
button.classList.add('copy-code-button');
30+
button.innerText = 'copy';
31+
button.addEventListener('click', copyCode);
32+
block.appendChild(button);
33+
});
34+
}
35+
}

0 commit comments

Comments
 (0)