From b672cc06090bc5abf34e104fcbc5cc17014e38f2 Mon Sep 17 00:00:00 2001 From: Yuriy Andamasov Date: Wed, 6 May 2026 22:50:11 +0300 Subject: fix(codecopier): prevent state class crossover on rapid re-clicks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- docs/_static/js/codecopier.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'docs/_static') 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('Failed') } - 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 -- cgit v1.2.3