diff options
| author | Yuriy Andamasov <yuriy@vyos.io> | 2026-05-06 22:50:11 +0300 |
|---|---|---|
| committer | Yuriy Andamasov <yuriy@vyos.io> | 2026-05-07 15:51:18 +0300 |
| commit | b672cc06090bc5abf34e104fcbc5cc17014e38f2 (patch) | |
| tree | 2f1654b829bd2f8cc786f16bb30f63ebd2d9197d | |
| parent | ef9f63340de656d710cbe34dd401df9a99ac3d98 (diff) | |
| download | vyos-documentation-b672cc06090bc5abf34e104fcbc5cc17014e38f2.tar.gz vyos-documentation-b672cc06090bc5abf34e104fcbc5cc17014e38f2.zip | |
fix(codecopier): prevent state class crossover on rapid re-clicks
Reset both notifier classes and cancel any pending revert timeout at the
start of each click so a fast failure-then-success (or vice versa) can't
leave `copiedNotifier` and `copyFailedNotifier` applied simultaneously,
and so the 2-second auto-revert window restarts cleanly per click.
Addresses Copilot review feedback on PR #1890.
🤖 Generated by [robots](https://vyos.io)
| -rw-r--r-- | docs/_static/js/codecopier.js | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/docs/_static/js/codecopier.js b/docs/_static/js/codecopier.js index ae069ba0..02143b92 100644 --- a/docs/_static/js/codecopier.js +++ b/docs/_static/js/codecopier.js @@ -61,6 +61,18 @@ $(document).ready(async function () { // dropped, picking up the wrong snippet. const textToCopy = extractSnippetText(currentTarget.parentElement) + // Clear any state class left over from a previous click so a rapid + // success-after-failure (or vice versa) doesn't end up with both + // classes applied at once. + divWithNeededId.removeClass('copiedNotifier copyFailedNotifier') + + // Cancel a pending revert timeout from a previous click so the new + // click's 2-second window starts clean instead of being ended early. + const previousTimeout = divWithNeededId.data('revertTimeout') + if (previousTimeout) { + clearTimeout(previousTimeout) + } + try { await navigator.clipboard.writeText(textToCopy) // Only flip the button into the success state when the write actually @@ -73,11 +85,12 @@ $(document).ready(async function () { divWithNeededId.html('<span>Failed</span>') } - setTimeout(() => { + const revertTimeout = setTimeout(() => { divWithNeededId.html(innersOfCopyDiv) - divWithNeededId.removeClass('copiedNotifier') - divWithNeededId.removeClass('copyFailedNotifier') + divWithNeededId.removeClass('copiedNotifier copyFailedNotifier') + divWithNeededId.removeData('revertTimeout') }, 2000) + divWithNeededId.data('revertTimeout', revertTimeout) }) // we edit the button that is added by readthedocs portal |
