Repository navigation
fix(core): copy works in a frame that does not allow the async Clipboard API - #12035
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
AI review status for this pull request.
|
PR Analysis Report🔎 Preview ·
|
| Component | Size | LoC |
|---|---|---|
| richtext/RichTextView | 282.5 kB (+342 B) · 2.3 kB CSS (0) | — |
| CodeBlock | 80.5 kB (+333 B) · 12.1 kB CSS (0) | — |
| lab/CommentThread | 363.4 kB (+312 B) · 2.4 kB CSS (0) | — |
4 more components
| Component | Size | LoC |
|---|---|---|
| richtext/RichTextEditor | 305.9 kB (+307 B) · 2.6 kB CSS (0) | — |
| Markdown | 203.0 kB (+306 B) · 21.9 kB CSS (0) | — |
| lab/Comments | 370.2 kB (+291 B) · 2.5 kB CSS (0) | — |
| Timestamp | 27.0 kB (+1 B) · 11.0 kB CSS (0) | — |
Package size: @astryxdesign/core 7.61 MB packed (+1.8 kB); no new dependencies.
Not run on this PR: add ci:run-visual for Visual, ci:run-a11y for Accessibility, ci:run-rtl for RTL.
Generated by PR Enrichment workflow | View full report
There was a problem hiding this comment.
Changes requested: the fallback copies in a frame that refuses clipboard-write, but inside an open modal it reports a copy that did not happen, and the release note misstates one case.
copyThroughSelectionmounts its field ondocument.body(useClipboard.ts:75), so under an open Dialog, Drawer, AlertDialog or scrim BottomSheet the field is inert whileexecCommand('copy')still returnstrue, and a CodeBlock in an open Dialog inside a cross-origin frame shows "Copied" with the clipboard unchanged (onmainit shows the failure); mount the field in the open modal that holds the control and add a browser test that reads the clipboard.- The changeset and description say the Markdown heading-link copy showed a failed copy, but heading links never show a failure (they copied nothing), so reword that sentence.
Outside modals the fix works as described in Chromium: CodeBlock, heading links and the useClipboard story copy in a cross-origin frame, focus stays on the control without a focus ring, and the policy violation is gone.
Claims and files checked
Review mode: full; reason: no-earlier-review
- Claim: copy buttons fail in a frame that refuses clipboard-write and log a policy violation: verified on
mainin a cross-origin frame in Chromium. - Claim: the hook checks the policy and copies through a selected field where it is refused: verified (
useClipboard.ts:38-47,:103-109; Chromium). - Claim: the browser logs nothing on that path: verified (no console error on the head).
- Claim: focus and the selection come back: focus verified (test and Chromium); selection restore read in code (
useClipboard.ts:87-92). - Claim: copy works inside such a frame: false inside an open modal (first finding).
- Claim: heading links showed a failed copy: false (second finding).
- Claim: the normal path is unchanged and 323 existing tests pass: verified (21 files, 323 tests).
- Claim: the regression test copies through the selection, restores focus and reports a failed copy: verified; two of the three new tests fail on
main. - Claim: no new prop, option, export or visual change: verified (the API surface comparison has no rows).
- File
.changeset/clipboard-sandboxed-frame.md: second finding. - File
packages/core/src/hooks/useClipboard.ts: first finding; non-blocking comment notes. - File
packages/core/src/hooks/useClipboard.test.tsx: clear, but no test covers a control inside an open modal (part of the first finding). - File
packages/core/src/hooks/useClipboard.doc.mjs: clear.
| /** | ||
| * Write text to the clipboard: through the async Clipboard API where the | ||
| * document may use it, else through a selection and the copy command. | ||
| */ |
There was a problem hiding this comment.
Non-blocking: this JSDoc restates the two branches below, which the name and body already show. Consider dropping it.
c4cf53a to
86ae660
Compare
There was a problem hiding this comment.
Needs an engineering owner's decision, then changes: the modal fix works, but the fallback now copies past a frame's clipboard-write refusal, which the docsite's restricted playground preview relies on.
- Miss: no current record decides whether
useClipboardmay copy past a frame'sclipboard-writerefusal, and the docsite's playground preview (sandbox="allow-scripts") pins the opposite inapps/docsite/e2e/playground-preview-isolation.spec.ts:523, which now fails indocsite-browser, so an engineering owner needs to settle it (recorded by commenting/approve-specwith the full head SHA, which the author may do as an engineering owner) and either the fallback or that test then changes to match. - Miss: in a refusing frame, a keyboard copy from Timestamp's hover card shows "Copied", then the card closes about 200 ms later and focus drops to the page, because
copyThroughSelectionfocuses a field outside the card and removes it while it still has focus (packages/core/src/hooks/useClipboard.ts:88-99); writing the text from a one-timecopylistener duringexecCommand('copy')moves no focus and still passes your Chromium spec. - The new
Copy in a dialogstory passesheadertoDialog, which has no such prop, sotypecheckfails atapps/storybook/stories/CodeBlock.stories.tsx:538and the dialog renders without its title; compose it withLayoutandDialogHeaderas the Dialog stories do.
In Chromium, Copy inside an open Dialog in a refusing frame now copies the exact code, and your spec fails if the field goes back to the body. Comment suggestions are inline and non-blocking.
Claims and files checked
Review mode: full; reason: history
- Claim: on main, copy buttons in a refusing frame fail or copy nothing and log a policy violation: verified in Chromium (CodeBlock and Timestamp show the failure; the console logs the violation).
- Claim: the hook checks the policy and copies through a selected field where it is refused: verified (
useClipboard.ts:38-47,:68-122). - Claim: the refused API is not called there, so the browser logs nothing: verified (no console error on the head).
- Claim: inside an open modal the field goes into the dialog that holds focus, and the copy lands: verified (Chromium spec passes; it fails with the field on the body).
- Claim: focus and the selection come back: focus comes back, but Timestamp's hover card then closes after a keyboard copy (second finding); selection restore read in code (
:100-105). - Claim: CodeBlock and Timestamp showed a failed copy and heading links copied nothing before: verified.
- Claim: no new prop, option, export or visual change: verified (the API surface comparison has no rows).
- Claim: the existing useClipboard, CodeBlock and Markdown plugin tests pass: verified (594 tests with Timestamp).
- Claim: the regression tests cover the selection path, focus return, a failed copy and the modal case: verified by reading and running them.
- Claim: the Core package typechecks: verified by CI; the Storybook project does not (third finding).
- File
.changeset/clipboard-sandboxed-frame.md: clear; its claims hold, subject to the owner decision. - File
apps/storybook/stories/CodeBlock.stories.tsx: third finding. - File
packages/core/src/hooks/__tests__/useClipboardFrame.a11y.chromium.spec.ts: clear. - File
packages/core/src/hooks/useClipboard.doc.mjs: clear. - File
packages/core/src/hooks/useClipboard.test.tsx: clear; no test covers a copy control inside a surface that closes on focus loss. - File
packages/core/src/hooks/useClipboard.ts: first and second findings; non-blocking comment notes.
… focus move; the docsite test pins the async API refusal
|
/approve-spec 957379a |
|
/approve-spec 169a026 |
There was a problem hiding this comment.
Changes requested for one documentation fix. In Chromium, Copy now works in a frame that refuses clipboard-write (outside and inside an open Dialog, and from Timestamp's hover card by keyboard without closing it), the changed docsite isolation test passes, and the decision to copy in such a frame is approved for this head.
- The public JSDoc on
UseClipboardReturn.copy(packages/core/src/hooks/useClipboard.ts:137-139) and the file header (:7-8) still say the fallback copies through a selected hidden field, which the copy-event rewrite removed, and the JSDoc leaves out the fallback after aNotAllowedError; describe the copy command and itscopyevent there and in the Chromium spec's header (useClipboardFrame.a11y.chromium.spec.ts:9-10), as the inline suggestions show.
The earlier hover-card focus loss and story typecheck error are fixed. CI had not finished on this head when this review ended, and the head with the fix needs its own /approve-spec.
Claims and files checked
Review mode: incremental; reason: none
- Claim: on
main, copy buttons in a frame that refusesclipboard-writefail (CodeBlock, Timestamp) or copy nothing, and the browser logs a policy violation: verified in Chromium. - Claim: where the frame may use the async Clipboard API, the hook writes through it as before: verified (
useClipboard.ts:92-97; the Chromium spec withclipboard-writeallowed counts no copy command). - Claim: where the policy refuses, or the write is refused with
NotAllowedError, the hook runs the copy command inside the click and sets the text from thecopyevent: verified (:56-84,:92-103). - Claim: no element takes focus or the selection, so a control in a hover card or an open modal dialog keeps both: verified (unit test; Chromium spec in a Dialog; Timestamp hover card by keyboard).
- Claim: the copy command never runs outside user activation: verified where the browser exposes user activation (
:57-62, unit test); elsewhere the browser itself refuses it. - Claim:
copy()resolves true only when thecopyevent fired with the text set: verified (:81-83, unit tests). - Claim: where the policy refuses, the refused API is never called and nothing is logged: verified (no violation in Chromium).
- Claim: the decision that Core copies through the copy command in such a frame is taken: verified as an engineering owner's approval for this head; no spec record states it yet.
- Claim: the docsite isolation test now pins the async API refusal and expects Core's copy to succeed: verified (the test passes against a production docsite build).
- Claim: no new prop, option, export or visual change: verified (the API surface comparison has no rows).
- Claim:
useClipboard.doc.mjsdescribes the fallback: verified. - Claim: the listed unit and Chromium regression tests cover the refused and allowed frames: verified by reading and running them (all pass).
- Changeset: copy works in such frames for CodeBlock, Timestamp and Markdown heading links, also in a modal dialog or hover card, without moving focus: verified (CodeBlock and Timestamp in Chromium; heading links use the same hook).
- Changeset: before, those buttons copied nothing there, CodeBlock and Timestamp showed a failed copy, and a violation was logged per press: verified.
- Changeset: where the async Clipboard API is allowed, nothing changes: verified.
- File
.changeset/clipboard-sandboxed-frame.md: clear. - File
apps/docsite/e2e/playground-preview-isolation.spec.ts: clear. - File
apps/storybook/stories/CodeBlock.stories.tsx: clear. - File
packages/core/src/hooks/__tests__/useClipboardFrame.a11y.chromium.spec.ts: stale header (the finding). - File
packages/core/src/hooks/useClipboard.doc.mjs: clear. - File
packages/core/src/hooks/useClipboard.test.tsx: clear. - File
packages/core/src/hooks/useClipboard.ts: stale JSDoc and header (the finding); one non-blocking comment note.
… and its copy event
|
/approve-spec 57fb037 |
There was a problem hiding this comment.
Approved pending CI: CI has not finished on this head yet. Copy now works in a frame that refuses clipboard-write, outside and inside an open Dialog and from Timestamp's hover card by keyboard, the changed docsite isolation test passes against a production build, and the hook's header and copy JSDoc now describe the copy command and its copy event. An engineering owner approved copying in such a frame for this head.
- Optional: the JSDoc on the private
writeClipboardText(packages/core/src/hooks/useClipboard.ts:86-90) could keep only the reason the policy is read before a try.
Claims and files checked
Review mode: incremental; reason: none
- Changed rules (0): none.
- Claim: on the base, copy buttons in a frame that refuses
clipboard-writefail (CodeBlock and Timestamp show the failure, heading links copy nothing) and the browser logs a policy violation per press: verified in Chromium for CodeBlock and Timestamp; heading links fail silently by design. - Claim: the base hook writes only through
navigator.clipboard.writeText: verified. - Claim: where the frame may use the async Clipboard API, the hook writes through it as before: verified (
useClipboard.ts:96-97; the Chromium spec's allowed frame runs no copy command). - Claim: where the policy refuses, or the write is refused with
NotAllowedError, the hook runs the copy command inside the click and sets the text from thecopyevent: verified (:56-109, unit tests). - Claim: no element takes focus or the selection, so a control in a hover card or an open modal dialog keeps both: verified (unit test; Chromium spec in a Dialog; Timestamp hover card by keyboard).
- Claim: the copy command never runs outside user activation: verified where the browser exposes user activation (
:57-62, unit test). - Claim:
copy()resolves true only when thecopyevent fired with the text set: verified (:81-83, unit tests). - Claim: where the policy refuses, the refused API is never called and nothing is logged: verified (unit test; no console violation in Chromium).
- Claim: the decision that Core copies through the copy command in such a frame is taken: verified as an engineering owner's approval for this head; no spec record states it yet.
- Claim: the docsite isolation test now asserts the async API refusal and a successful Core copy on a click: verified (passes against a production docsite build).
- Claim: a write that fails for any other reason still reports a failure: verified (
:99-101, unit test). - Claim: no new prop, option, export, default or visual change: verified (the API surface comparison has no rows).
- Claim:
useClipboard.doc.mjsdescribes the fallback: verified. - Claim: the listed unit and Chromium regression tests cover the refused and allowed frames: verified by reading and running them (all pass).
- Claim: the listed testing commands pass: verified for the unit suites, the Storybook build with the Chromium spec, and the changeset check; typecheck and lint are green in CI.
- Changeset: copy works in such frames for CodeBlock, Timestamp and Markdown heading links, also in an open modal dialog or a hover card, without moving focus: verified (CodeBlock and Timestamp in Chromium; heading links use the same hook).
- Changeset: before, those buttons copied nothing there, CodeBlock and Timestamp showed a failed copy, and a violation was logged per press: verified.
- Changeset: where the async Clipboard API is allowed, nothing changes: verified.
- File
.changeset/clipboard-sandboxed-frame.md: clear. - File
apps/docsite/e2e/playground-preview-isolation.spec.ts: clear. - File
apps/storybook/stories/CodeBlock.stories.tsx: clear. - File
packages/core/src/hooks/__tests__/useClipboardFrame.a11y.chromium.spec.ts: clear. - File
packages/core/src/hooks/useClipboard.doc.mjs: clear. - File
packages/core/src/hooks/useClipboard.test.tsx: clear. - File
packages/core/src/hooks/useClipboard.ts: one optional comment cleanup.
User impact
A copy button in a page shown inside a sandboxed frame did nothing useful. Artifact viewers, embeds and previews often show HTML in an
<iframe sandbox="allow-scripts">whose permissions policy does not allowclipboard-write. There the async Clipboard API is refused:CodeBlock's Copy andTimestamp's copy showed the failed-copy state, Markdown's heading-link copy copied nothing, and the browser logged "Permissions policy violation: The Clipboard API has been blocked" for each press.Problem and solution fit
useClipboard(and the internaluseClipboardCopythe components use) writes only throughnavigator.clipboard.writeText, which a frame withoutclipboard-writerefuses.The browser's copy command (
document.execCommand('copy')) still runs in such a frame inside a click. The hook keeps the async API first: where the document may use it, it writes through it as before. Where the frame's permissions policy refusesclipboard-write, or the browser refuses the write (NotAllowedError), the hook runs the copy command inside the reader's click or key press, and puts the text in through thecopyevent the command fires (clipboardData.setData). No element takes focus or the selection, so a control in a hover card or an open modal dialog keeps both. It never runs the copy command outside user activation, andcopy()resolves true only when thatcopyevent fired with the text set. Where the policy refuses, the refused API is never called, so the browser logs nothing.Expected behavior and authority
component:CodeBlockFR2: a copy writes the exactcodestring and reports success; a failed write must not report success.useClipboard's documented contract:copy()writes the text and resolvestrueon success.Current specification: the maintainers decided that Core copies through the browser's copy command when a frame refuses the async Clipboard API. The reader asked for the copy, and the refusal covers only the async API (any code in the frame can run the copy command on a click), so a failing Core copy is not a boundary. The docsite playground's isolation test now pins the real boundary: its preview's async Clipboard API stays refused.
Smallest restoration
packages/core/src/hooks/useClipboard.ts: onewriteClipboardTextused by the hook: the policy check, the async write, and the copy-command fallback through a one-timecopylistener. No new prop, option, export or visual change; the success and failure states are the same.useClipboard.doc.mjsdescribes the fallback.Evidence
sandbox="allow-scripts"frame:navigator.clipboard.writeTextis refused and logs the permissions-policy violation (every template with a copy button, rendered into such a frame).Dialogputs the exact code on the clipboard (read back from the clipboard), runs the copy command, keeps focus on the control, and logs no violation; in the same frame withallow="clipboard-write", Copy writes through the async API and runs no copy command.clipboard-writeis allowed, the hook callsnavigator.clipboard.writeTextexactly as before, and a write that fails for any other reason still reports a failure.useClipboard.test.tsx: in a refusing frame the copy command carries the exact text, the refused API is not tried, the copy is announced and reported, no focus moves and no element is added, the page's owncopyhandlers cannot change the text, nothing runs outside user activation, and the copy fails when the command copies nothing or fires nocopyevent; after aNotAllowedErrorthe copy command copies, after any other failure it does not.useClipboardFrame.a11y.chromium.spec.ts: both paths in a sandboxed frame, as above.NotAllowedError) and that Core's copy on a click succeeds.Scope
Components: useClipboard (CodeBlock, Timestamp, Markdown heading links through it); the docsite playground isolation test
Testing
pnpm exec vitest run --project ui packages/core/src/hooks/useClipboard packages/core/src/CodeBlock packages/core/src/Timestamp packages/core/src/Markdownpnpm storybook:build, thenpnpm exec playwright test useClipboardFrame CodeBlockCopyClearance(Chromium)tsc --noEmitfor Core, Storybook and the docsite; ESLint on the changed files;node scripts/check-changesets.mjs