Skip to content

Commit

Permalink
FIX: Make copybutton remain for a second during success (#176)
Browse files Browse the repository at this point in the history
closes #175
  • Loading branch information
choldgraf committed Nov 15, 2022
1 parent 403f289 commit e529aa0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sphinx_copybutton/_static/copybutton.css
Expand Up @@ -35,7 +35,8 @@ div.highlight {
position: relative;
}

.highlight:hover button.copybtn {
/* Show the copybutton */
.highlight:hover button.copybtn, button.copybtn.success {
opacity: 1;
}

Expand Down
15 changes: 11 additions & 4 deletions sphinx_copybutton/_static/copybutton.js_t
Expand Up @@ -102,18 +102,25 @@ const clearSelection = () => {
}
}

// Changes tooltip text for two seconds, then changes it back
// Changes tooltip text for a moment, then changes it back
// We want the timeout of our `success` class to be a bit shorter than the
// tooltip and icon change, so that we can hide the icon before changing back.
var timeoutIcon = 2000;
var timeoutSuccessClass = 1500;

const temporarilyChangeTooltip = (el, oldText, newText) => {
el.setAttribute('data-tooltip', newText)
el.classList.add('success')
setTimeout(() => el.setAttribute('data-tooltip', oldText), 2000)
setTimeout(() => el.classList.remove('success'), 2000)
// Remove success a little bit sooner than we change the tooltip
// So that we can use CSS to hide the copybutton first
setTimeout(() => el.classList.remove('success'), timeoutSuccessClass)
setTimeout(() => el.setAttribute('data-tooltip', oldText), timeoutIcon)
}

// Changes the copy button icon for two seconds, then changes it back
const temporarilyChangeIcon = (el) => {
el.innerHTML = iconCheck;
setTimeout(() => {el.innerHTML = iconCopy}, 2000)
setTimeout(() => {el.innerHTML = iconCopy}, timeoutIcon)
}

const addCopyButtonToCodeCells = () => {
Expand Down

0 comments on commit e529aa0

Please sign in to comment.