Added copy code button functionality in code blocks #7701
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.
Summary
This PR adds a "Copy to Clipboard" button feature to all code blocks (
<pre>tags) throughout the/conceptssection and all its child routes on the webpack.js.org documentation site. The implementation enhances user experience by allowing visitors to quickly copy code examples to their clipboard with a single click.Motivation:
Related Issues:
What kind of change does this PR introduce?
This PR introduces a feature enhancement that adds interactive UI elements to documentation pages without modifying any content files or breaking changes.
Changes Include:
CopyCodeButtonReact component with clipboard functionalityPagecomponent for automatic code block enhancementReactDOM.render)Did you add tests for your changes?
Yes, comprehensive tests have been added:
Test Results:
Does this PR introduce a breaking change?
No, this PR introduces zero breaking changes:
If relevant, what needs to be documented once your changes are merged or what have you already documented?
No additional documentation needed:
/concepts/*pagesImplementation Details
Files Added:
src/components/CopyCodeButton/CopyCodeButton.jsx(33 lines)src/components/CopyCodeButton/CopyCodeButton.scss(45 lines)Files Modified:
src/components/Page/Page.jsximport CopyCodeButton from '../CopyCodeButton/CopyCodeButton'import ReactDOM from 'react-dom'useEffecthook to enhance code blocks when content loads/conceptsroutesHow It Works:
/concepts/*loads, thePagecomponent detects content has loaded<pre>elements in the pageCopyCodeButtoncomponent into the wrappertop: 8px, right: 8pxwithz-index: 10dataset.copyButtonAddedflag to avoid duplicatesBrowser Compatibility:
navigator.clipboard.writeText)Performance:
/conceptsroutesquerySelectorAllcall)Visual Representation
Light Mode:
Dark Mode:
After Click:
Testing Evidence
Code Quality:
/conceptspagesManual Testing:
/concepts/*routesDeployment Notes
No special deployment steps required:
PR Checklist
--
Test Cases Passing
--
Copy Button (Before Click)
--
Copy Button (After Click – "Copied!" State)