Skip to content

Fix copy buttons on the first click in Safari - #572

Merged
paulocastellano merged 8 commits into
mainfrom
paulocastellano/fix-copy-clipboard-failure
Dec 3, 2026
Merged

paulocastellano merged 8 commits into
mainfrom
paulocastellano/fix-copy-clipboard-failure

Conversation

@paulocastellano

@paulocastellano paulocastellano commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes ENG-23: "Failed to copy to clipboard" on the first click of a copy button in Safari (the disconnect keyword, API key, webhook secret, MCP URL…).

Cause

Safari drops the click's user activation when the click moves the focus out of a text field (the dialog's autofocused input). Every clipboard write is then refused (writeText, ClipboardItem, execCommand). A second click worked because the button already had the focus. Traced in real Safari with navigator.userActivation:

  • failing: pointerdown:A mousedown:A focusout(INPUT) focusin(BUTTON) pointerup:- click:- → NotAllowedError
  • fixed: pointerdown:A mousedown:A pointerup:A click:A → copied

Fix

  • Button takes a copy prop (and copy-success-message). With it, the button copies on click, never takes the focus (preventDefault on mousedown), is type="button" so it never submits a form, and passes { copied } to its slot for two seconds (VueUse refAutoReset). Every copy button goes through it, so none can forget the Safari fix, and each drops its own copy state.
  • The API key and Telegram buttons show their copied state inside the button instead of swapping it for a label.
  • copyToClipboard is unchanged: a refused copy still toasts copy_failed.

Tests

Each guard in Button has a test that fails without it (checked by removing it):

  • focus stays in the confirmation input while copying the disconnect keyword (the Safari fix);
  • with the keyword typed, copying does not submit the disconnect form;
  • the copied mark goes away after two seconds;
  • a refused copy toasts copy_failed and shows nothing as copied;
  • copy-success-message toasts (webhook signing secret).

Each copy button copies its own text: disconnect and API key keywords, generated API key, Telegram command, webhook secret and payload, template body, MCP URL. All copy-related browser tests pass in Chromium and WebKit (--browser safari).

Manual test

For each item: click the copy button once (the first click is the one that failed in Safari), paste somewhere, and check that the pasted text is right, the copied mark shows and then goes away, and no "Failed to copy to clipboard" toast appears.

  • Disconnect a channel (Settings → Channels → a channel's menu → Disconnect): click into the input, then copy disconnect. Type the keyword and copy again: the dialog stays open and the channel stays connected.
    • Safari
    • Chrome
  • Regenerate an API key (Settings → API keys → a key's menu → Regenerate): copy REGENERATE.
    • Safari
    • Chrome
  • Delete an API key (Settings → API keys → a key's menu → Delete): copy the keyword.
    • Safari
    • Chrome
  • Generated API key (Settings → API keys → Create, after naming the key): copy the key; the button shows a green ✓.
    • Safari
    • Chrome
  • Disconnect an MCP app (Settings → MCP → Connected → an app's Disconnect): copy disconnect.
    • Safari
    • Chrome
  • Remove a member or an invitation (Settings → Members → a member's menu → Remove): copy the email.
    • Safari
    • Chrome
  • Delete a workspace (Settings → Workspace → Delete workspace): copy the workspace name.
    • Safari
    • Chrome
  • MCP connect steps (Settings → MCP → Connect → Claude, then Other): copy the name, the URL and the config; the button says "Copied".
    • Safari
    • Chrome
  • Webhook signing secret (Settings → Webhooks → a webhook): copy the secret; a "Signing secret copied to clipboard" toast shows.
    • Safari
    • Chrome
  • Webhook delivery payload and response (same page, a delivery): hover the JSON and copy it; a "Copied" toast shows.
    • Safari
    • Chrome
  • Template (Create → Templates → open a template): copy the body; the button says "Copied".
    • Safari
    • Chrome
  • Telegram connect (Settings → Channels → Connect → Telegram): copy the command; the button says "✓ Copied".
    • Safari
    • Chrome
  • Buttons without copy still work (any Save, Cancel or Delete in the dialogs above): they submit or close as before.
    • Safari
    • Chrome

Paulo Castellano added 8 commits December 3, 2026 11:27
Safari refuses navigator.clipboard.writeText when the page has no focus or
the click's activation has lapsed, so copy buttons (the disconnect keyword,
API keys, MCP, Telegram, templates, links) failed with "Failed to copy to
clipboard" until the user clicked the page first.

copyToClipboard now goes through VueUse's useClipboard with legacy: true,
which falls back to the copy command when the Clipboard API is refused.
It never reports a failure, so the copy_failed toast, its lang key and the
callers' didCopy checks go away.

Browser tests read copies through a shared spyOnClipboard() /
copiedText(), covering both paths, and a new test covers a refused
Clipboard API.
Tested in real Safari: inside the confirmation dialog it refuses both
navigator.clipboard.writeText (NotAllowedError) and the copy command
(execCommand returns false), and VueUse's legacy fallback reports success
while copying the empty selection of the focused input. A ClipboardItem
written with navigator.clipboard.write() is accepted, as the share chart's
image copy already did.

copyToClipboard goes back to its own try/catch with write() instead of
writeText(), so the copy_failed toast and the callers' checks come back,
and VueUse is no longer used for it.
The real cause, traced in Safari with navigator.userActivation at each
phase of the click: the user activation is still there at mousedown, and is
gone once the focus moves from the dialog's text field to the copy button.
Safari then refuses any clipboard write (writeText, ClipboardItem or the
copy command). Later clicks worked because the button already had focus.

Every copy button now prevents its mousedown, so the click never moves the
focus and the activation reaches the click handler. copyToClipboard goes
back to writeText, unchanged from main.

The clipboard test helpers are renamed recordClipboardWrites() and
lastCopiedText(), and a test checks that copying the disconnect keyword
leaves the focus in the confirmation field.
Every copy button repeated the same copied ref, two-second timer and
unmount cleanup, and each one had to remember @mousedown.prevent so
Safari would allow the copy. CopyButton owns all of it: it prevents its
mousedown, copies through copyToClipboard, exposes { copied } to its slot,
can show a #copied slot in its place, resets when its value changes, and
takes as / as-child to keep each place's own button.

The confirmation keyword, disconnect keyword, MCP fields, generated API
key, Telegram command, template body, webhook secret and JSON viewer use
it. The share chart's image copy keeps its own button.
Button takes the text to copy and keeps the focus where it is, so no copy
button can forget the Safari fix. ConfirmDeleteModal holds the keyword line
(ConfirmKeywordField), which the disconnect dialog reuses. CopyButton is gone.
The copy state lives in Button itself, the dialogs keep their keyword line and
only swap their copy button, and the tests keep their clipboard stubs.
Button resets the copied state through VueUse's refAutoReset, renders one
element again and makes a copy button type=button, so it never submits a
form. The API key and Telegram buttons show their copied state inside the
button. Tests cover the focus, the form, the reset, a refused copy, the
success toast and each copy button's text.
@paulocastellano paulocastellano changed the title Copy to the clipboard in Safari Fix copy buttons on the first click in Safari Dec 3, 2026
@paulocastellano
paulocastellano merged commit 73343ca into main Dec 3, 2026
10 checks passed
@paulocastellano
paulocastellano deleted the paulocastellano/fix-copy-clipboard-failure branch December 3, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant