react-copy-to-clipboard icon indicating copy to clipboard operation
react-copy-to-clipboard copied to clipboard

Copied text has yellow background when pasted into gmail or google docs.

Open rlpacht opened this issue 9 years ago • 22 comments

Do you know what might cause this and if there is a way to make that not happen?

rlpacht avatar Aug 12 '16 20:08 rlpacht

wow

looks like text is copied as rich text. I suppose the place you copy it from has yellow background, right?

nkbt avatar Aug 12 '16 23:08 nkbt

It doesn't. I'm copying the text from a standard input.

rlpacht avatar Aug 12 '16 23:08 rlpacht

Any example how to reproduce this?

nkbt avatar Aug 12 '16 23:08 nkbt

oh actually it works like that from codepen example!

nkbt avatar Aug 12 '16 23:08 nkbt

Opened issue in upstream https://github.com/sudodoki/copy-to-clipboard/issues/43

nkbt avatar Aug 12 '16 23:08 nkbt

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

nkbt avatar Aug 16 '16 00:08 nkbt

Published [email protected] with updated libs

nkbt avatar Aug 16 '16 00:08 nkbt

Thank you for your help!

rlpacht avatar Aug 16 '16 18:08 rlpacht

No worries 👌

nkbt avatar Aug 17 '16 06:08 nkbt

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.

screen shot 2016-09-06 at 9 49 36 am screen shot 2016-09-06 at 9 50 23 am

davidkaminsky avatar Sep 06 '16 16:09 davidkaminsky

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.

dsacramone avatar Feb 10 '17 23:02 dsacramone

Browsers keep doing some awkward stuff...

nkbt avatar Feb 11 '17 01:02 nkbt

Can't reproduce anymore. Works well for me in several browsers.

nkbt avatar Apr 20 '17 04:04 nkbt

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();
  }
});

webuniverseio avatar Jul 12 '17 15:07 webuniverseio

@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.

webuniverseio avatar Jul 12 '17 15:07 webuniverseio

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.

webuniverseio avatar Jul 12 '17 15:07 webuniverseio

Thanks @szarouski for all the examples!

nkbt avatar Jul 12 '17 22:07 nkbt

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

nkbt avatar Sep 30 '17 13:09 nkbt

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.

wdlb avatar Jul 03 '18 07:07 wdlb

A cleaner workaround:

onCopy(copiedText) {
    navigator.clipboard.writeText(copiedText)
}

render() {
    return (
        <CopyToClipboard onCopy={this.onCopy} key="copy" text={`some\ntext\nhere`}>
            Copy text
        </CopyToClipboard>
    )
}

alexisig avatar Nov 07 '18 16:11 alexisig

any help here?

mrjosshi avatar Jul 06 '19 11:07 mrjosshi

@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.

  1. 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.

  2. 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 :)

nikkirostollan avatar Jul 18 '19 19:07 nikkirostollan