Skip to content

docs: copy-file buttons bind duplicate click handlers; error dialog is unstyled and leaks its Escape listener #78586

Done

Description

@angelica-moreira

Suggested label: bug

Problem

CopyEntireFileButton.astro has three bugs:

  1. Duplicate handlers. The component's inline script is emitted once per button, and each copy binds a click handler to every button. On a page with N buttons, each button gets N handlers, so one click fetches the file N times.
  2. Unstyled error dialog. The error modal is created by script, so it never gets Astro's scoped class. None of the .error-modal* styles apply.
  3. Leaked Escape listener. The dialog adds a keydown listener to document but only removes it when the dialog is closed with Escape. Closing it with the × button or an outside click leaves the listener attached.

How to reproduce

  1. Open a docs page that has more than one "Copy entire file" button and open the browser Network tab.
  2. Click one button. The file is requested once for each button on the page.
  3. Block that request (for example, with DevTools request blocking) and click again. The error dialog shows with no styling.
  4. Close the dialog with ×. A keydown listener stays on document. Repeat this and the listeners pile up.

Plan

  1. In the inline script, skip buttons that are already bound, for example with a data-copy-handler-attached flag.
  2. Change the .error-modal* selectors to :global(...).
  3. Declare handleEscape before closeModal, and make closeModal remove the listener, so every close path cleans up. handleEscape then only calls closeModal.
  4. Tests:
    • Count document keydown listeners before the dialog opens, while it is open, and after it closes, for each close path (×, outside click, Escape). The count must return to its starting value.
    • Check that one click causes one fetch.
    • Check that the dialog has styling.

Done when

  • One click causes one fetch.
  • The error dialog is styled.
  • The listener count returns to its starting value for all three close paths.
  • The new tests fail on main and pass with the fix.

Reference fix

Commit: angelica-moreira@2d2cefc

The same commit changes the copy-button tests to fetch from a small real local server (docs/tests/helpers/local-server.ts) instead of mocked routes, and to expect CRLF clipboard text on Windows. It relies on the clipboard Playwright project from the commit in issue 6.


A working version of this fix is on my fork, in branch angelica/docs-playwright-fixes (all changes vs main). With all 10 commits applied, the full docs Playwright suite passes (239/239). Feel free to reuse the commit linked above, or to implement the plan differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions