Repository navigation
Add one-click copy button to code blocks in chat - #94337
Conversation
Co-authored-by: Shawn Borton <shawnborton@users.noreply.github.com>
Codecov Report✅ Changes either increased or maintained existing code coverage, great job!
|
|
@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. |
|
Quick clarification before I make the change, @shawnborton 🙂 The copy button already uses our standard 28×28 mini icon button — 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?
Reply with |
|
@MelvinBot please do option 1 from your previous comment |
…ock copy Co-authored-by: Shawn Borton <shawnborton@users.noreply.github.com>
|
Done — pushed option 1, @shawnborton 🙂 The code-block copy button now uses our dedicated I also dropped the now-unused Verification
Commit: 13a34f2 |
|
🚧 shawnborton has triggered a test Expensify/App build. You can view the workflow run here. |
This comment has been minimized.
This comment has been minimized.
|
@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? |
|
@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? |
|
@MelvinBot please see my previous comment |
|
@MelvinBot are you okay? Can you respond to all of my previous comments please? |
|
⏱️ 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. |
|
@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>
|
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 2. Copy now preserves formatting. Newlines inside a code block are emitted by ExpensiMark as void Verification (web)Verified in the web build via browser automation:
Local checks pass: Prettier, ESLint, React Compiler compliance, and Commit: ce44ecc |
|
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 Every other engineering check passed: ✅ To clear the remaining red check: fill in the |
|
🚧 shawnborton has triggered a test Expensify/App build. You can view the workflow run here. |
This comment has been minimized.
This comment has been minimized.
|
@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>
|
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 |
|
🤖 I looked into the failing checks — there's nothing to fix in the code. The only failing check is Every real check has passed: ✅ The only remaining checks are So: no fix to push. Please complete the PR Author Checklist to clear that check. |
|
🚧 shawnborton has triggered a test Expensify/App build. You can view the workflow run here. |
This comment has been minimized.
This comment has been minimized.
|
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! |
Reviewer Checklist
Screenshots/VideosiOS: Nativeios.moviOS: mWeb Safariios-mweb.movMacOS: Chrome / Safariweb.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 { |
There was a problem hiding this comment.
🟡 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  , 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', () => { /* ... */ });
});There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🟢 LGTM - Tests well and looks good
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
|
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! |
|
@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>
|
@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 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. |
|
Hmm @rlinoz any idea how to proceed here? |
|
🟢 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.: |
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
|
🚧 rlinoz has triggered a test Expensify/App build. You can view the workflow run here. |
|
🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
|
|
🚀 Deployed to staging by https://github.com/rlinoz in version: 9.4.21-2 🚀
|
|
🚀 Deployed to production by https://github.com/mountiny in version: 9.4.21-9 🚀
Bundle Size Analysis (Sentry): |
Explanation of Change
Adds a one-click Copy button to fenced (triple-backtick) code blocks rendered in chat.
Hoverable, which never reports hover on touch platforms, so it does not show on native).ContextMenuItemmini-button pattern (useThrottledButtonState+successIcon).Implementation lives in
PreRenderer.tsx, the renderer for<pre>blocks. A small recursive helper collects the raw text from the renderedtnodeto 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"
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".
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectiontoggleReportand notonIconClick)src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.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:
After clicking — "Copied!" state: