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

Add `useClipboard` hook

Open kripod opened this issue 5 years ago • 14 comments

Motivation

When it comes to interacting with the clipboard, multiple options are available. The Clipboard API is not yet widely supported by browsers.

A compatible solution should be available out of the box:

Basic example

function Example() {
  const { copy, cut, paste } = useClipboard();
  const [value, setValue] = useState('foo');

  return (
    <>
      <input value={value} onChange={(e) => setValue(e.target.value)} />
      <button type="button" onClick={() => copy(value)}>Copy</button>
    </>
  );
}

Details

Lack of permissions (e.g. for pasting) should be handled gracefully with opt-in support for error callbacks.

kripod avatar Sep 22 '19 12:09 kripod

Hey @kripod do you need someone to build that? I could give it a go :)

maciekgrzybek avatar Sep 23 '19 13:09 maciekgrzybek

@maciekgrzybek I would appreciate if you could take the challenge of implementation!

kripod avatar Sep 23 '19 13:09 kripod

@kripod I'll crack on then :)

maciekgrzybek avatar Sep 29 '19 09:09 maciekgrzybek

@kripod could please explain a little bit more about the 'Details' part? 'Lack of permissions (e.g. for pasting) should be handled gracefully with opt-in support for error callbacks.' Not sure if I understand it. Thanks :)

maciekgrzybek avatar Sep 29 '19 09:09 maciekgrzybek

When using the Clipboard API, permissions should be requested from the user at first. If the user refuses to give them, call an optionally supplied errorCallback method with the reason as a parameter (e.g. Error object or explanation).

kripod avatar Sep 29 '19 10:09 kripod

Ok, that makes sense, so I'm guessing I should use this API right? https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/permissions

maciekgrzybek avatar Sep 29 '19 10:09 maciekgrzybek

Or is it native browser behaviour to ask the user for the permission?

maciekgrzybek avatar Sep 29 '19 10:09 maciekgrzybek

I'm not sure about it, as it isn't documented in depth.

kripod avatar Sep 29 '19 11:09 kripod

Ok thanks, @kripod, I'll investigate it.

maciekgrzybek avatar Sep 29 '19 18:09 maciekgrzybek

@kripod one more thing, what's the approach for developing hooks in this project? is there something like an example page to test it? or do I need to create something external?

maciekgrzybek avatar Oct 07 '19 20:10 maciekgrzybek

@maciekgrzybek Unfortunately, there is no example page to test hooks with, but Jest is being used for writing tests. I am considering migration to a monorepo to resolve #77 and prepare structural requirements for #26.

kripod avatar Oct 09 '19 23:10 kripod

Hey, I've added PR for this - https://github.com/kripod/react-hooks/pull/85

maciekgrzybek avatar Oct 12 '19 15:10 maciekgrzybek

@maciekgrzybek Thank you for your valuable contribution! In the meantime, the project has been migrated to a monorepo with a package called web-api-hooks. I would be grateful if you could update your pull request, moving useClipboard under the aforementioned package.

Unfortunately, I will be busy in the next days due to tasks at university, so I may not be able to review your code immediately. I appreciate your efforts, keep up the nice work!

kripod avatar Oct 13 '19 22:10 kripod

Hey @kripod I've added it to a new repo :)

maciekgrzybek avatar Oct 14 '19 09:10 maciekgrzybek