Skip to content

fix(link-dialog): hide the copy button when the Clipboard API is unavailable - #992

Merged
petyosi merged 2 commits into
mdx-editor:mainfrom
Misyuk-T:fix/link-dialog-clipboard-insecure-context
Oct 13, 2026
Merged

petyosi merged 2 commits into
mdx-editor:mainfrom
Misyuk-T:fix/link-dialog-clipboard-insecure-context

Conversation

@Misyuk-T

@Misyuk-T Misyuk-T commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

fixes #990

  • navigator.clipboard is undefined outside secure contexts, so the copy button in the link preview threw a TypeError over plain http (non-localhost origin)
  • LinkDialog now renders the copy button only when navigator.clipboard.writeText exists. everything else in the preview (open, edit, remove) is untouched
  • went with hiding over an execCommand('copy') fallback: it's deprecated and the issue itself suggests making the action optional
  • added src/test/link-dialog.test.tsx: no button without the Clipboard API, writeText called with the url when it's there. the first test fails without the fix
  • npm run lint, npm run typecheck, npm run test:once, npm run build all pass

Misyuk-T and others added 2 commits October 9, 2026 15:17
…ailable

navigator.clipboard is undefined outside secure contexts, so clicking the
copy button in the link preview over plain http threw a TypeError.

Fixes mdx-editor#990
Hiding the copy button removed copying for exactly the apps that hit mdx-editor#990: intranet deployments served over plain http, where navigator.clipboard is undefined. execCommand('copy') still works there, so fall back to it instead of hiding the button.

The fallback also covers a rejected writeText call, for example when a permissions policy blocks clipboard-write, which previously surfaced as an unhandled rejection. "Copied!" now appears only when a copy actually succeeded, and the button is hidden only when neither path can copy.
@petyosi

petyosi commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Thanks for this! I pushed a follow-up commit (ca2a284) on top of yours that falls back to document.execCommand('copy') instead of hiding the button. Hiding it removed copying for exactly the setup in #990, an intranet app served over plain http.

What changed:

  • If navigator.clipboard.writeText is missing, the URL is copied from a temporary off-screen textarea. Focus and the previous selection are restored afterwards.
  • If writeText rejects (for example, when a permissions policy blocks clipboard-write), the same fallback runs. This also gets rid of the unhandled rejection.
  • "Copied!" appears only when the copy actually succeeded.
  • The button is hidden only when neither path can copy.

I checked it in Chromium on a non-localhost http origin (isSecureContext === false): the URL reached the system clipboard. The tests cover the fallback, the rejection path and the failure case.

Let me know if anything looks off to you.

@petyosi
petyosi merged commit 4d9499a into mdx-editor:main Oct 13, 2026
4 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 4.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] window.navigator.clipboard is undefined

2 participants