react-copy-to-clipboard
react-copy-to-clipboard copied to clipboard
Copied text has yellow background when pasted into gmail or google docs.
Do you know what might cause this and if there is a way to make that not happen?
wow
looks like text is copied as rich text. I suppose the place you copy it from has yellow background, right?
It doesn't. I'm copying the text from a standard input.
Any example how to reproduce this?
oh actually it works like that from codepen example!
Opened issue in upstream https://github.com/sudodoki/copy-to-clipboard/issues/43
https://github.com/sudodoki/copy-to-clipboard/issues/43#issuecomment-239844822
[email protected] is published with the fix.
Please, update your dependencies and problem should go away
Published [email protected] with updated libs
Thank you for your help!
No worries 👌
I'm using version 4.2.3 and I'm still seeing this it's just that the background is light grey now. Is this expected? There is no grey background for the text. Attached are some screenshots.

Same (with the light gray). Is there a fix for this - I can't use it, as it does this. Please let me know. Thanks a bunch.
Browsers keep doing some awkward stuff...
Can't reproduce anymore. Works well for me in several browsers.
So this is still an issue in copy-to-clipboard dependency (still happens to me in Chrome): https://github.com/sudodoki/copy-to-clipboard/issues/46, but there is a workaround.
My implementation of the workaround:
//your-component.js
<CopyToClipboard key="copy" text={`CopyToClipboard\n${textYouWantToCopy}`}>
Copy text
</CopyToClipboard>
//main.js
document.addEventListener('copy', e => {
const textContent = e.target.textContent;
const filterText = 'CopyToClipboard\n';
if (textContent.startsWith(filterText)) {
e.clipboardData.setData('text/plain', textContent.replace(filterText, ''));
e.preventDefault();
}
});
@nkbt I'm thinking that this might be added inside react-copy-to-clipboard, either behind the boolean property or even as a default behavior.
Also not sure if that matters but for me issue is happening when text attribute has multi line string for example:
<CopyToClipboard key="copy" text={`blah\nblah\nblah`}>
Copy text
</CopyToClipboard>
Example above would have a background (inherits color from body & background from html elements).
However when it is not a multi-line string, no problem.
Thanks @szarouski for all the examples!
I've updated http://nkbt.github.io/react-copy-to-clipboard/ to have multiline textarea and tried to paste to google docs - no issues. Do I need to do something else to reproduce it?
I also updated https://codepen.io/nkbt/pen/eNPoQv?editors=0010, so if you can clone it and make "buggy" - that would be great
I managed to reproduce the issue with this pen: https://codepen.io/anon/pen/aKMRxO. The buggy behaviour happens when copying from Chrome to Google Docs or Microsoft Word.
A cleaner workaround:
onCopy(copiedText) {
navigator.clipboard.writeText(copiedText)
}
render() {
return (
<CopyToClipboard onCopy={this.onCopy} key="copy" text={`some\ntext\nhere`}>
Copy text
</CopyToClipboard>
)
}
any help here?
@anilljoshi The underlying package (copy-to-clipboard) has been updated, and you can now pass along a format in the options prop to avoid capturing some of the underlying styles.
-
Check you're yarn-lock or package-lock to see if your version for copy-to-clipboard is 3.2.0 or higher, then upgrade as necessary.
-
Pass the options prop an object containing:
{format: 'text/plain'}
<CopyToClipboard onCopy={this.onCopy} text={text} options={{format: 'text/plain'}}>
{...children}
</CopyToClipbaord>
Works like a charm :)