Skip to content

Add one-click copy button to code blocks in chat - #94337

Merged
rlinoz merged 5 commits into
mainfrom
claude-codeBlockCopyButton
Jun 26, 2026
Merged

rlinoz merged 5 commits into
mainfrom
claude-codeBlockCopyButton

Conversation

@MelvinBot

@MelvinBot MelvinBot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Adds a one-click Copy button to fenced (triple-backtick) code blocks rendered in chat.

  • The button is a small (28px) icon button rendered as an overlay on the code block, positioned 4px from the top and 4px from the right.
  • It only appears on hover, and only on hover-enabled devices (it is wrapped in Hoverable, which never reports hover on touch platforms, so it does not show on native).
  • Clicking it copies the raw text contents of the code block to the clipboard and the copy icon swaps to a checkmark to confirm the copy succeeded — reusing the existing ContextMenuItem mini-button pattern (useThrottledButtonState + successIcon).
  • This applies to all fenced code blocks in chat, not just AI/Concierge messages.

Implementation lives in PreRenderer.tsx, the renderer for <pre> blocks. A small recursive helper collects the raw text from the rendered tnode to copy.

Fixed Issues

$ #92562

PROPOSAL: #92562 (comment)

Tests

// TODO: The human co-author must fill out the tests you ran before marking this PR as "ready for review"

  1. Open any chat.
  2. Send a message containing a fenced code block using triple backticks, e.g.:
    ```
    const hello = "world";
    console.log(hello);
    ```
    
  3. Hover over the rendered code block and verify a copy icon button appears at the top-right (4px from top/right).
  4. Click the button and verify the contents are copied to your clipboard and the icon briefly changes to a checkmark.
  5. Verify the button does not appear when not hovering, and does not appear for inline (single-backtick) code.
  • Verify that no errors appear in the JS console

Offline tests

Same as Tests — the feature is entirely client-side (clipboard copy) and works offline.

QA Steps

// TODO: The human co-author must fill out the QA tests you ran before marking this PR as "ready for review".

  1. Open any chat.
  2. Send a message containing a fenced code block (triple backticks).
  3. Hover over the code block and verify a copy icon button appears at its top-right corner.
  4. Click it and verify the code is copied and the icon turns into a checkmark.
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native

N/A — the copy button is hover-only and does not render on touch (native) platforms.

Android: mWeb Chrome
iOS: Native

N/A — the copy button is hover-only and does not render on touch (native) platforms.

iOS: mWeb Safari
MacOS: Chrome / Safari

Copy button shown on hover over a code block:

Code block with copy button visible on hover

After clicking — "Copied!" state:

Code block showing Copied state after clicking the copy button

Co-authored-by: Shawn Borton <shawnborton@users.noreply.github.com>
@codecov

codecov Bot commented Jun 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
...c/components/HTMLEngineProvider/htmlEngineUtils.ts 75.43% <100.00%> (+4.01%) ⬆️
src/styles/index.ts 60.71% <ø> (+4.14%) ⬆️
...s/HTMLEngineProvider/HTMLRenderers/PreRenderer.tsx 0.00% <0.00%> (ø)
... and 519 files with indirect coverage changes

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot can you please use our icon button for this? It's a 28x28 button that has an icon in the middle and also has a hover state.

@MelvinBot

Copy link
Copy Markdown
Contributor Author

Quick clarification before I make the change, @shawnborton 🙂

The copy button already uses our standard 28×28 mini icon button — PreRenderer.tsx renders ContextMenuItem isMini, which is our BaseMiniContextMenuItem: a 28×28 button (reportActionContextMenuMiniButton) with a centered icon and a hover-background state.

My guess at what looks off in the screenshot: at rest (code block hovered, but cursor not yet on the button itself) it renders as a bare icon with no chrome — the button background only appears once you hover the button directly. So it doesn't read as a "button" until you're right on it.

How would you like me to proceed?

  1. Persistent button background — switch to our dedicated copy button CopyTextToClipboard with shouldUseButtonBackground (same 28×28 button used on the Verify Domain page), so it always reads as a button + keeps the hover state.
  2. Keep the current hover-only mini button (already 28×28 + hover) and just tweak a detail (icon size, etc.).
  3. A specific component you have in mind — point me at it and I'll swap to it.

Reply with @MelvinBot 1/2/3 (or the component name) and I'll push the change.

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot 1

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot please do option 1 from your previous comment

…ock copy

Co-authored-by: Shawn Borton <shawnborton@users.noreply.github.com>
@MelvinBot

Copy link
Copy Markdown
Contributor Author

Done — pushed option 1, @shawnborton 🙂

The code-block copy button now uses our dedicated CopyTextToClipboard with shouldHaveActiveBackground + shouldUseButtonBackground — the same 28×28 button styling used on the Verify Domain page. So instead of a bare icon at rest, it now renders with a persistent rounded button background as soon as the code block is hovered, and keeps the hover state + checkmark-on-copy feedback.

I also dropped the now-unused HTMLRenderer-CopyCode sentry label since CopyTextToClipboard carries its own.

Verification
  • Local checks pass: Prettier, ESLint, typecheck, and React Compiler compliance.
  • Verified in the web build via browser automation: hovering a fenced code block shows the copy button with a visible rounded button background (borderRadius: 100px, background: rgb(230, 225, 218)) at rest, and clicking swaps the icon to a checkmark with a "Copied!" tooltip.

Commit: 13a34f2

@github-actions

Copy link
Copy Markdown
Contributor

🚧 shawnborton has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

This comment has been minimized.

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot are you able to detect that if the code block has more than 1 line, let's place the button 16px from the top and 16px from the right? But keep the existing styles for single line code blocks?

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot It also seems that when I copy a code block, I lose the formatting of the code block - the line breaks, the spaces, etc. Can you please fix that?

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot please see my previous comment

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot are you okay? Can you respond to all of my previous comments please?

@MelvinBot

Copy link
Copy Markdown
Contributor Author

⏱️ Ran out of time before I could finish. This usually means the task was too complex or I got stuck exploring. Please try again with a more specific request or post in #melvinbot-ai.

Run details

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot are you able to detect that if the code block has more than 1 line, let's place the button 16px from the top and 16px from the right? But keep the existing styles for single line code blocks? It also seems that when I copy a code block, I lose the formatting of the code block - the line breaks, the spaces, etc. Can you please fix that?

…on copy

Co-authored-by: Shawn Borton <shawnborton@users.noreply.github.com>
@MelvinBot

Copy link
Copy Markdown
Contributor Author

Done — pushed both fixes, @shawnborton 🙂

1. Button position by line count. The copy button now sits 16px from the top/right on multi-line code blocks, and keeps the tight 4px corner placement for single-line blocks. Detection is codeText.trim().includes('\n') — trim() so a stray trailing newline doesn't flip a single-line block into "multi-line".

2. Copy now preserves formatting. Newlines inside a code block are emitted by ExpensiMark as void <br /> tags, which carry no text data — so the old recursive text collector silently dropped every line break. getCodeBlockText now returns \n for <br> nodes, so line breaks are preserved. Indentation/spaces were already intact (they're encoded as real spaces and <pre>'s white-space: pre keeps them).

Verification (web)

Verified in the web build via browser automation:

  • Multi-line block — copy button measured at top: 16px, right: 16px. Clipboard contents after clicking: "const hello = \"world\";\nif (hello) {\n console.log(hello);\n}\n" — line breaks and the 4-space indent before console.log are preserved.
  • Single-line block (const x = 1;) — copy button measured at top: 4px, right: 4px (unchanged tight corner).

Local checks pass: Prettier, ESLint, React Compiler compliance, and typecheck.

Commit: ce44ecc

@MelvinBot

Copy link
Copy Markdown
Contributor Author

I looked into the failing checks and there's nothing in the code to fix.

The only failing check is PR Author Checklist / checklist. That check fails only because the checklist boxes (and the Tests/QA sections) in the PR description aren't filled in — it has to be completed by you as the human author, so I can't (and shouldn't) resolve it on your behalf.

Every other engineering check passed: ✅ typecheck, ✅ ESLint, ✅ all 8 test jobs, ✅ all build variants, ✅ knip, ✅ Storybook, ✅ Prettier, ✅ React Compiler, ✅ Bun tests. The baseline-perf-tests / branch-perf-tests were still running when I checked — those are a performance comparison, not a fixable code failure.

To clear the remaining red check: fill in the Tests and QA Steps sections and tick the PR Author Checklist boxes, then re-run the checklist check.

@github-actions

Copy link
Copy Markdown
Contributor

🚧 shawnborton has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

This comment has been minimized.

@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot when the code block is more than 1 line, let's actually put the button 8px from the top and 8px from the right.

Co-authored-by: Shawn Borton <shawnborton@users.noreply.github.com>
@MelvinBot

Copy link
Copy Markdown
Contributor Author

Done — multi-line code blocks now place the copy button 8px from the top and 8px from the right (single-line blocks still use the tight 4px corner).

Commit: 7234256

@MelvinBot

Copy link
Copy Markdown
Contributor Author

🤖 I looked into the failing checks — there's nothing to fix in the code.

The only failing check is PR Author Checklist / checklist. This isn't a code or CI failure — it fails because the checklist boxes in the PR description aren't ticked yet. It'll go green once the author checklist is filled out; a code change can't resolve it.

Every real check has passed: ✅ typecheck, ✅ all 8 test jobs, ✅ ESLint, ✅ Prettier, ✅ Bun tests, ✅ Compare knip issues, ✅ Storybook tests, ✅ React Compiler Compliance, ✅ all build jobs.

The only remaining checks are baseline-perf-tests / branch-perf-tests, which were still running when I checked — these are comparative benchmarks and post results as a comment rather than hard-blocking.

So: no fix to push. Please complete the PR Author Checklist to clear that check.

@github-actions

Copy link
Copy Markdown
Contributor

🚧 shawnborton has triggered a test Expensify/App build. You can view the workflow run here.

@github-actions

This comment has been minimized.

@shawnborton

Copy link
Copy Markdown
Contributor

Ready for review, all good on my end. @ikevin127 since Melvin did the work here, I think I'll need your help coaching Melvin for the checklist as well as helping to get all of the platform screenshots. Thanks!

@ikevin127

ikevin127 commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

iOS: Native
ios.mov
iOS: mWeb Safari
ios-mweb.mov
MacOS: Chrome / Safari
web.mov

* Recursively collects the raw text contents of a code block so it can be copied to the clipboard,
* preserving the original line breaks and whitespace.
*/
function getCodeBlockText(tnode: TNode): string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Reusability & Testability: getCodeBlockText is a pure function living inside a component file — extract and test it

This is a textbook pure function (output depends only on input, no side effects) and the single most valuable thing in this PR to unit test, yet it's defined inline in the renderer where it can't be imported or tested in isolation.

There's already a natural home: @components/HTMLEngineProvider/htmlEngineUtils (you're importing isChildOfTaskTitle from it on lines 63–64). Move getCodeBlockText there.

Why it matters: the extraction logic has real edge cases (br handling, whitespace via &#32;, nested nodes, trailing newline) that are exactly the kind of thing that silently regresses. A unit test pins the behavior:

// htmlEngineUtils.test.ts
describe('getCodeBlockText', () => {
    it('joins text nodes and converts <br> to newlines', () => { /* ... */ });
    it('returns empty string for an empty block', () => { /* ... */ });
    it('preserves leading/trailing whitespace', () => { /* ... */ });
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 201decb. Moved getCodeBlockText out of the renderer into htmlEngineUtils.ts (the same util module PreRenderer already imports isChildOfTaskTitle from) and added getCodeBlockText.test.ts covering the edge cases you flagged — <br>→newline conversion, empty blocks, leading/trailing whitespace, nested nodes, and the trailing newline. All 7 cases pass.

@ikevin127 ikevin127 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 LGTM - Tests well and looks good

⚠️ Before merging though there are 3 NAB code-review comments that should be addressed and for this we need the assigned CME to ask MelvinBot to address the code-review comments:

and also check all the boxes on the PR Author Checklist to pass the workflow.

☝️ these are required to be done by somebody with access since contributors / reviewers can't ask Melvin for these changes directly.

cc @rlinoz

@melvin-bot

melvin-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

We did not find an internal engineer to review this PR, trying to assign a random engineer to #92562 as well as to this PR... Please reach out for help on Slack if no one gets assigned!

@melvin-bot
melvin-bot Bot requested a review from rlinoz June 25, 2026 23:13
@shawnborton

Copy link
Copy Markdown
Contributor

@MelvinBot can you please address the review comments above from @ikevin127 ?

…py button styles to shared styles

Co-authored-by: Shawn Borton <shawnborton@users.noreply.github.com>
@MelvinBot
MelvinBot requested review from a team as code owners June 25, 2026 23:55
@melvin-bot
melvin-bot Bot requested review from ikevin127 and removed request for a team June 25, 2026 23:55
@melvin-bot

melvin-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

@ikevin127 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@MelvinBot

Copy link
Copy Markdown
Contributor Author

⚠️ MelvinBot CI Fix Loop Detected

MelvinBot has made 5 consecutive commits trying to fix CI failures. Automatic CI fix dispatches are now paused for this PR.

Next steps: A human should review the recent commits and resolve the conflicting CI errors manually.

@shawnborton

Copy link
Copy Markdown
Contributor

Hmm @rlinoz any idea how to proceed here?

@ikevin127 ikevin127 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Previous code review comments mentioned here were addressed.

All that's left to do for CME is to check all the boxes on the PR Author Checklist since that's the only failing workflow - then we can merge if all good 🟢

@ikevin127

Copy link
Copy Markdown
Contributor

🟢 Here's the completed PR Author Checklist markdown, anyone with access can ask Melvin to update it with this:

<!-- If necessary, assign reviewers that know the area or changes well. Feel free to tag any additional reviewers you see fit. -->

### Explanation of Change

Adds a one-click **Copy** button to fenced (triple-backtick) code blocks rendered in chat.

- The button is a small (28px) icon button rendered as an overlay on the code block, positioned 4px from the top and 4px from the right.
- It only appears on hover, and only on hover-enabled devices (it is wrapped in `Hoverable`, which never reports hover on touch platforms, so it does not show on native).
- Clicking it copies the raw text contents of the code block to the clipboard and the copy icon swaps to a checkmark to confirm the copy succeeded — reusing the existing `ContextMenuItem` mini-button pattern (`useThrottledButtonState` + `successIcon`).
- This applies to all fenced code blocks in chat, not just AI/Concierge messages.

Implementation lives in [`PreRenderer.tsx`](https://github.com/Expensify/App/blob/claude-codeBlockCopyButton/src/components/HTMLEngineProvider/HTMLRenderers/PreRenderer.tsx), the renderer for `<pre>` blocks. A small recursive helper collects the raw text from the rendered `tnode` to copy.

### Fixed Issues

$ https://github.com/Expensify/App/issues/92562

PROPOSAL: https://github.com/Expensify/App/issues/92562#issuecomment-4779422101

### Tests
// TODO: **The human co-author** must fill out the tests you ran before marking this PR as "ready for review"

1. Open any chat.
2. Send a message containing a fenced code block using triple backticks, e.g.:
const hello = "world";
console.log(hello);
3. Hover over the rendered code block and verify a copy icon button appears at the top-right (4px from top/right).
4. Click the button and verify the contents are copied to your clipboard and the icon briefly changes to a checkmark.
5. Verify the button does not appear when not hovering, and does not appear for inline (single-backtick) code.

- [x] Verify that no errors appear in the JS console

### Offline tests

Same as Tests — the feature is entirely client-side (clipboard copy) and works offline.

### QA Steps
// TODO: **The human co-author** must fill out the QA tests you ran before marking this PR as "ready for review".

1. Open any chat.
2. Send a message containing a fenced code block (triple backticks).
3. Hover over the code block and verify a copy icon button appears at its top-right corner.
4. Click it and verify the code is copied and the icon turns into a checkmark.

- [x] Verify that no errors appear in the JS console

### PR Author Checklist

- [x] I linked the correct issue in the `### Fixed Issues` section above
- [x] I wrote clear testing steps that cover the changes made in this PR
 - [x] I added steps for local testing in the `Tests` section
 - [x] I added steps for the expected offline behavior in the `Offline steps` section
 - [x] I added steps for Staging and/or Production testing in the `QA steps` section
 - [x] I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
 - [x] I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
 - [x] I tested this PR with a [High Traffic account](https://github.com/Expensify/App/blob/main/contributingGuides/CONTRIBUTING.md#high-traffic-accounts) against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
- [x] I included screenshots or videos for tests on [all platforms](https://github.com/Expensify/App/blob/main/contributingGuides/CONTRIBUTING.md#make-sure-you-can-test-on-all-platforms)
- [x] I ran the tests on **all platforms** & verified they passed on:
 - [x] Android: Native
 - [x] Android: mWeb Chrome
 - [x] iOS: Native
 - [x] iOS: mWeb Safari
 - [x] MacOS: Chrome / Safari
- [x] I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
- [x] I followed proper code patterns (see [Reviewing the code](https://github.com/Expensify/App/blob/main/contributingGuides/PR_REVIEW_GUIDELINES.md#reviewing-the-code))
 - [x] I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. `toggleReport` and not `onIconClick`)
 - [x] I verified that comments were added to code that is not self explanatory
 - [x] I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
 - [x] I verified any copy / text shown in the product is localized by adding it to `src/languages/*` files and using the [translation method](https://github.com/Expensify/App/blob/4510fc76bbf5df699a2575bfb49a276af90f3ed7/src/components/LocaleContextProvider.tsx#L80)
 - [x] I verified all numbers, amounts, dates and phone numbers shown in the product are using the [localization methods](https://github.com/Expensify/App/blob/4510fc76bbf5df699a2575bfb49a276af90f3ed7/src/components/LocaleContextProvider.tsx#L116-L123)
 - [x] I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
 - [x] I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
 - [x] I verified the JSDocs style guidelines (in [`STYLE.md`](https://github.com/Expensify/App/blob/main/contributingGuides/STYLE.md#jsdocs)) were followed
- [x] If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
- [x] I followed the guidelines as stated in the [Review Guidelines](https://github.com/Expensify/App/blob/main/contributingGuides/PR_REVIEW_GUIDELINES.md)
- [x] I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like `Avatar`, I verified the components using `Avatar` are working as expected)
- [x] I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
- [x] I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
- [x] I verified that if a function's arguments changed that all usages have also been updated correctly
- [x] If any new file was added I verified that:
 - [x] The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
- [x] If a new CSS style is added I verified that:
 - [x] A similar style doesn't already exist
 - [x] The style can't be created with an existing [StyleUtils](https://github.com/Expensify/App/blob/main/src/styles/utils/index.ts) function (i.e. `StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)`)
- [x] If new assets were added or existing ones were modified, I verified that:
 - [x] The assets are optimized and compressed (for SVG files, run `npm run compress-svg`)
 - [x] The assets load correctly across all supported platforms.
- [x] If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
- [x] If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like `Avatar` is modified, I verified that `Avatar` is working as expected in all cases)
- [x] If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
- [x] If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
- [x] If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
 - [x] I verified that all the inputs inside a form are aligned with each other.
 - [x] I added `Design` label and/or tagged `@Expensify/design` so the design team can review the changes.
- [x] If a new page is added, I verified it's using the `ScrollView` component to make it scrollable when more elements are added to the page.
- [x] I added [unit tests](https://github.com/Expensify/App/blob/main/tests/README.md) for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
- [x] If the `main` branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the `Test` steps.

### Screenshots/Videos
<details>
<summary>Android: Native</summary>

N/A — the copy button is hover-only and does not render on touch (native) platforms.

</details>

<details>
<summary>Android: mWeb Chrome</summary>

<!-- add screenshots or videos here -->

</details>

<details>
<summary>iOS: Native</summary>

N/A — the copy button is hover-only and does not render on touch (native) platforms.

</details>

<details>
<summary>iOS: mWeb Safari</summary>

<!-- add screenshots or videos here -->

</details>

<details>
<summary>MacOS: Chrome / Safari</summary>

Copy button shown on hover over a code block:

![Code block with copy button visible on hover](https://melvinbot-assets.exops.io/04-code-block-hover-showing-copy-7d3aa7dc-3c17-4f33-98a8-cc5f3eb8d517.png)

After clicking — "Copied!" state:

![Code block showing Copied state after clicking the copy button](https://melvinbot-assets.exops.io/07-code-block-copied-immediate-044e4878-c3a8-4306-a708-95dc338963a5.png)

</details>
```

@rlinoz
rlinoz merged commit 4a79d68 into main Jun 26, 2026
41 of 43 checks passed
@rlinoz
rlinoz deleted the claude-codeBlockCopyButton branch June 26, 2026 12:54
@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@github-actions

Copy link
Copy Markdown
Contributor

🚧 rlinoz has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify OSBotify mentioned this pull request Jun 26, 2026
55 tasks done
@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/rlinoz in version: 9.4.21-2 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/mountiny in version: 9.4.21-9 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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.

6 participants