Repository navigation
feat(ui): add ghost copy button for single-line code blocks (#9085) #9297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hrithiksaini99
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
hrithiksaini99:feat-one-line-codebox-ghost-copy-button
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+156
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@node-core/ui-components': minor | ||
| --- | ||
|
|
||
| Add ghost copy button visible on hover and focus for single-line code blocks with \`no-footer\` |
85 changes: 85 additions & 0 deletions
85
packages/ui-components/src/Common/BaseCodeBox/__tests__/index.test.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { afterEach, beforeEach, describe, it } from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
|
|
||
| import { render, fireEvent, screen } from '@testing-library/react'; | ||
|
|
||
| import BaseCodeBox from '../index'; | ||
|
|
||
| describe('BaseCodeBox', () => { | ||
| let originalClipboard; | ||
|
|
||
| beforeEach(() => { | ||
| originalClipboard = navigator.clipboard; | ||
| Object.defineProperty(navigator, 'clipboard', { | ||
| value: { writeText: () => Promise.resolve() }, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(navigator, 'clipboard', { | ||
| value: originalClipboard, | ||
| writable: true, | ||
| configurable: true, | ||
| }); | ||
| }); | ||
|
|
||
| it('renders code box with language and footer copy button by default', () => { | ||
| render( | ||
| <BaseCodeBox | ||
| language="JavaScript" | ||
| copyButtonLabel="Copy code" | ||
| copiedButtonLabel="Copied!" | ||
| > | ||
| <code>const x = 1;</code> | ||
| </BaseCodeBox> | ||
| ); | ||
|
|
||
| assert.ok(screen.getByText('JavaScript')); | ||
| assert.ok(screen.getByText('Copy code')); | ||
| }); | ||
|
|
||
| it('renders ghost copy button when no-footer is specified', () => { | ||
| render( | ||
| <BaseCodeBox | ||
| language="bash" | ||
| className="no-footer" | ||
| copyButtonLabel="Copy code" | ||
| copiedButtonLabel="Copied!" | ||
| > | ||
| <code>node --no-deprecation app.js</code> | ||
| </BaseCodeBox> | ||
| ); | ||
|
|
||
| // Language in footer toolbar should NOT be rendered | ||
| assert.strictEqual(screen.queryByText('bash'), null); | ||
|
|
||
| // Ghost copy button should be rendered with accessible label and no visible text | ||
| const button = screen.getByRole('button', { name: 'Copy code' }); | ||
| assert.ok(button); | ||
| assert.strictEqual(screen.queryByText('Copy code'), null); | ||
| }); | ||
|
|
||
| it('copies code content when ghost copy button is clicked', async t => { | ||
| t.mock.method(navigator.clipboard, 'writeText', () => Promise.resolve()); | ||
|
|
||
| render( | ||
| <BaseCodeBox | ||
| language="bash" | ||
| className="no-footer" | ||
| copyButtonLabel="Copy code" | ||
| copiedButtonLabel="Copied!" | ||
| > | ||
| <code>node --no-deprecation app.js</code> | ||
| </BaseCodeBox> | ||
| ); | ||
|
|
||
| const button = screen.getByRole('button', { name: 'Copy code' }); | ||
| fireEvent.click(button); | ||
|
|
||
| const copiedButton = await screen.findByRole('button', { name: 'Copied!' }); | ||
| assert.ok(copiedButton); | ||
| assert.strictEqual(navigator.clipboard.writeText.mock.callCount(), 1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add screenshots on how it looks on the PR description for dark/light theme 🙇
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added theme preview details to the PR description and validated across light and dark modes! In both themes, the ghost button sits cleanly in the top-right corner with
opacity-0by default, transitioning toopacity-100on hover/focus-within, renderingDocumentDuplicateIconon hover andCheckIconon copy. You can also test it live directly in the Vercel deploy preview.