-
Notifications
You must be signed in to change notification settings - Fork 340
Update the copy-to-clipboard functionality in our code blocks #1825
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
base: dev
Are you sure you want to change the base?
Conversation
realityking
left a comment
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.
IMHO a good change
Worth noting that this will break the copy functionality when used outside a secure context (without HTTPS).
| navigator.clipboard.writeText(text) | ||
| .then(() => { | ||
| // Add success feedback | ||
| const button = container.parentNode.querySelector('.copy-clipboard'); |
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.
You should be able to pass the button into copyToClipboard() from the event handler avoiding an extra lookup.
| .then(() => { | ||
| // Add success feedback | ||
| const button = container.parentNode.querySelector('.copy-clipboard'); | ||
| const originalHTML = button.innerHTML; |
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.
Instead of reading from the DOM this could be a variable also used in setupCodeCopy().
| }); | ||
| } | ||
|
|
||
| function setupCodeCopy() { |
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.
I'd suggest adding some feature detection code and checking for a secure context. If one of those conditions is not met, we shouldn't show the (then non-functional) copy button.
Dustinturner44
left a comment
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.
This PR updates the copy-to-clipboard functionality in our code blocks. The previous implementation used the now‑deprecated
document.execCommand('copy')per MDN guidance. To modernize our approach and ensure future compatibility, we've replaced the deprecated method with the new asynchronous Clipboard API. In addition, we've added immediate visual feedback to inform users when text has been successfully copied.Key Changes
The code now leverages
navigator.clipboard.writeText()to handle the copy operation, ensuring a more robust and future‑proof solution.On successful copying, the copy button temporarily changes its icon to indicate success (displaying a checkmark icon), then reverts back after 1 second. This provides clear, actionable feedback to users.
Code Changes
Before:
After:
Preview

This update not only aligns our implementation with modern web standards but also enhances the overall user experience with clear visual feedback. Let me know if there are any questions or further improvements you'd like to discuss!