Skip to content

Commit

Permalink
Fix copy code button position (alshedivat#1348)
Browse files Browse the repository at this point in the history
  • Loading branch information
YifanJiang233 authored and adityarauniyar committed Jan 17, 2024
1 parent e99afbc commit 8e3f288
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
20 changes: 6 additions & 14 deletions _sass/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -826,31 +826,23 @@ progress::-moz-progress-bar {
height: inherit;
}

pre {
padding: 8px 12px;
/* Copy button */
.code-display-wrapper {
position: relative;

/* Copy code to clipboard button */
.copy {
background: var(--global-card-bg-color);
border-color: var(--global-bg-color);
border-radius: .3rem;
border-style: solid;
border-style: none;
color: var(--global-text-color);
font-size: medium;
opacity: 0;
position: absolute;
right: .25rem;
top: .25rem;

&:active,
&:focus,
&:hover {
color: var(--global-hover-color);
opacity: 1;
}
right: .15rem;
top: .15rem;
}

&:active .copy,
&:focus .copy,
&:hover .copy {
Expand Down
12 changes: 11 additions & 1 deletion assets/js/copy_code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
var codeBlocks = document.querySelectorAll('pre');
codeBlocks.forEach(function (codeBlock) {
if (codeBlock.querySelector('pre:not(.lineno)') || codeBlock.querySelector('code')) {
// create copy button
var copyButton = document.createElement('button');
copyButton.className = 'copy';
copyButton.type = 'button';
copyButton.ariaLabel = 'Copy code to clipboard';
copyButton.innerText = 'Copy';
copyButton.innerHTML = '<i class="fas fa-clipboard"></i>';
codeBlock.append(copyButton);

// get code from code block and copy to clipboard
copyButton.addEventListener('click', function () {
Expand All @@ -32,5 +32,15 @@ codeBlocks.forEach(function (codeBlock) {
copyButton.innerHTML = '<i class="fas fa-clipboard"></i>';
}, waitFor);
});

// create wrapper div
var wrapper = document.createElement('div');
wrapper.className = 'code-display-wrapper';

// add copy button and code block to wrapper div
const parent = codeBlock.parentElement;
parent.insertBefore(wrapper, codeBlock);
wrapper.append(codeBlock);
wrapper.append(copyButton);
}
});

0 comments on commit 8e3f288

Please sign in to comment.