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

useCopyToClipboard: add mimeType param, default it to text/plain

Open sudhanshug16 opened this issue 2 years ago • 2 comments

Description

When you use the hook to copy a text, it copies as text/html (since it is default for copy-to-clipboard package). This results in weird rendering in rich text editors, example:

image

The value supplied to the method was of type string.

Since almost everyone will be copying plain text using this hook, it makes sense to use text/plain as the default value for mimeType. We can go the other way as well, and set the default to text/html but most people will have to supply text/plain as a param. I am actually surprised no one noticed/asked for this before. I assume most people will be using it to copy things like links, emails or plain text. 🤔

Feel free to correct me if I am wrong with the above assumption. Looking forward to a review. Thanks

Type of change

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • [ ] Read the Contributing Guide
  • [x] Perform a code self-review
  • [ ] Comment the code, particularly in hard-to-understand areas
  • [x] Add documentation
  • [ ] Add hook's story at Storybook
  • [ ] Cover changes with tests
  • [ ] Ensure the test suite passes (yarn test)
  • [ ] Provide 100% tests coverage
  • [ ] Make sure code lints (yarn lint). Fix it with yarn lint:fix in case of failure.
  • [ ] Make sure types are fine (yarn lint:types).

sudhanshug16 avatar Sep 24 '21 19:09 sudhanshug16

Hi @streamich, can we please add this mimeType option in useCopyToClipboard hook, we can keep text/html as default format for copy so there won't be any backward compatibly issue. but without text/plain format option, it is creating issue in rich text editor. Without this option currently, I have to use copy-to-clipboard library for only this use case.

KushalShah2619 avatar Apr 13 '22 09:04 KushalShah2619

@streamich Having a configurable mimetype will help. Facing the same rich-editor issue as the user above. Unless you think it will break backwards compatibility, I think text/plain should be safe as default. The navigator also defaults to that afaik.

lakbychance avatar Mar 07 '23 14:03 lakbychance