Add success indicator for code block copy button

This commit is contained in:
iBug
2024-05-06 01:20:41 +08:00
parent dc965c5bbe
commit 14e3fa2b28
6 changed files with 45 additions and 4 deletions

View File

@ -1,5 +1,11 @@
# Changelog # Changelog
## Unreleased
### Enhancements
- Change the icon of a copy button into a tick for 1.5 seconds on successful copy.
## [4.26.0](https://github.com/mmistakes/minimal-mistakes/releases/tag/4.26.0) ## [4.26.0](https://github.com/mmistakes/minimal-mistakes/releases/tag/4.26.0)
### Bug Fixes ### Bug Fixes

View File

@ -613,6 +613,8 @@ pre {
position: absolute; position: absolute;
top: 0.6em; top: 0.6em;
right: 0.5em; right: 0.5em;
width: 1.8em;
height: 1.5em;
z-index: 1; z-index: 1;
background: none; background: none;
border: none; border: none;
@ -637,6 +639,22 @@ pre {
z-index: 2; z-index: 2;
} }
i {
position: absolute;
top: 0.25em;
right: 0.25em;
&.copied {
opacity: 0;
}
}
&.copied i {
opacity: 0;
&.copied {
opacity: 1;
}
}
@at-root { @at-root {
.no-copy & { .no-copy & {
display: none; display: none;

View File

@ -190,6 +190,17 @@ $(document).ready(function () {
var result = copyText(codeBlock.innerText); var result = copyText(codeBlock.innerText);
// Restore the focus to the button // Restore the focus to the button
thisButton.focus(); thisButton.focus();
if (result) {
if (thisButton.interval !== null) {
clearInterval(thisButton.interval);
}
thisButton.classList.add('copied');
thisButton.interval = setTimeout(function () {
thisButton.classList.remove('copied');
clearInterval(thisButton.interval);
thisButton.interval = null;
}, 1500);
}
return result; return result;
}; };
@ -206,7 +217,7 @@ $(document).ready(function () {
var copyButton = document.createElement("button"); var copyButton = document.createElement("button");
copyButton.title = "Copy to clipboard"; copyButton.title = "Copy to clipboard";
copyButton.className = "clipboard-copy-button"; copyButton.className = "clipboard-copy-button";
copyButton.innerHTML = '<span class="sr-only">Copy code</span><i class="far fa-copy"></i>'; copyButton.innerHTML = '<span class="sr-only">Copy code</span><i class="far fa-fw fa-copy"></i><i class="fas fa-fw fa-check copied"></i>';
copyButton.addEventListener("click", copyButtonEventListener); copyButton.addEventListener("click", copyButtonEventListener);
container.prepend(copyButton); container.prepend(copyButton);
}); });

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@ permalink: "/docs/history/"
excerpt: Change log of enhancements and bug fixes made to the theme. excerpt: Change log of enhancements and bug fixes made to the theme.
sidebar: sidebar:
nav: docs nav: docs
last_modified_at: '2024-05-05T19:50:49+08:00' last_modified_at: '2024-05-06T01:20:38+08:00'
toc: false toc: false
--- ---
@ -17,6 +17,12 @@ toc: false
{% raw %} {% raw %}
## Unreleased ## Unreleased
### Enhancements
- Change the icon of a copy button into a tick for 1.5 seconds on successful copy.
## [4.26.0](https://github.com/mmistakes/minimal-mistakes/releases/tag/4.26.0)
### Bug Fixes ### Bug Fixes
- Fix an unclosed `<p>` tag in `page__hero.html`, near `page.header.actions`. - Fix an unclosed `<p>` tag in `page__hero.html`, near `page.header.actions`.