Skip to content

Add copy-to-clipboard button for code snippets#814

Merged
MakisH merged 3 commits intoprecice:masterfrom
SnehaChaursia:feat/add-copy-button
Mar 14, 2026
Merged

Add copy-to-clipboard button for code snippets#814
MakisH merged 3 commits intoprecice:masterfrom
SnehaChaursia:feat/add-copy-button

Conversation

@SnehaChaursia
Copy link
Copy Markdown
Contributor

@SnehaChaursia SnehaChaursia commented Feb 26, 2026

Description of Changes

This PR introduces a highly requested quality-of-life feature for the documentation website: a "Copy to Clipboard" button for all code blocks.
Changes made:

  • Code Injection (js/copy-code.js): Added a vanilla JavaScript file that dynamically injects a <button> into all .highlighter-rouge elements.
  • Clipboard API: Utilizes the modern navigator.clipboard.writeText API to securely copy code text, with a robust fallback for older browsers or non-secure contexts.
  • Styling (css/customstyles-precice.css): Styled the button to sit unobtrusively in the top right corner of code snippets. The button remains transparent and elegantly fades in on hover.
  • Global Integration (_includes/footer.html): Included the new JavaScript file in the global footer so it functions seamlessly across all documentation pages and tutorials.
  • Visual Feedback: The button temporarily changes to read "Copied!" with a checkmark upon successful click.

Verification

  • Tested locally using bundle exec jekyll serve.
  • Verified the button correctly targets all code blocks without capturing unintended HTML structure.
  • Verified visual feedback timeout works as expected.

ScreenShot

image

@MakisH MakisH added technical Technical issues on the website GSoC Contributed in the context of the Google Summer of Code labels Feb 26, 2026
@SnehaChaursia
Copy link
Copy Markdown
Contributor Author

@MakisH Please take a look on this feature.

@MakisH
Copy link
Copy Markdown
Member

MakisH commented Mar 1, 2026

I am aware of this PR (and many other new PRs), I will take a look later in a few days. Busy week coming up.

@hanzalahwaheed
Copy link
Copy Markdown
Contributor

hanzalahwaheed commented Mar 3, 2026

Tested this locally. Works superb!

Minor suggestion: I did notice at some places the text gets hidden due to the button, so maybe we get rid of the button texts here, i.e. no "Copy"/"Copied" texts, just the Copy Icon and the Check Icon would make the button smaller. Many websites do this.

Thanks!

@precice-bot
Copy link
Copy Markdown
Contributor

This pull request has been mentioned on preCICE Forum on Discourse. There might be relevant details there:

https://precice.discourse.group/t/gsoc-2026-introduction-hanzalah-waheed/2745/5

Copy link
Copy Markdown
Member

@MakisH MakisH left a comment

Choose a reason for hiding this comment

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

Looks indeed very nice, thank you!

I guess the implementation could be clearer at parts, but we will probably not need it later. Still, in the meantime, this seems to be simple enough.

Comment thread js/copy-code.js
Comment on lines +26 to +27
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this needed?

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.

This is a well-known pattern for cross-browser clipboard copying the textarea must be in the DOM to be selectable, but positioned way off-screen so users never see it. @MakisH

@MakisH MakisH merged commit 979edc3 into precice:master Mar 14, 2026
2 checks passed
@MakisH MakisH mentioned this pull request Mar 14, 2026
@SnehaChaursia
Copy link
Copy Markdown
Contributor Author

Tested this locally. Works superb!

Minor suggestion: I did notice at some places the text gets hidden due to the button, so maybe we get rid of the button texts here, i.e. no "Copy"/"Copied" texts, just the Copy Icon and the Check Icon would make the button smaller. Many websites do this.

Thanks!

Hi, sorry for the late response—I missed this earlier due to some other commitments.

Thanks a lot for the suggestion, I really like the idea of using just icons to make the button cleaner as per modern technique. Since the PR is already merged, I won’t be able to update it now, but I’ve noted this for future contributions and even made the change already in local . I’ll definitely incorporate this improvement in upcoming PRs.

Thank you for testing it and sharing feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GSoC Contributed in the context of the Google Summer of Code technical Technical issues on the website

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants