-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Implement useCopyToClipboard hook #883
Conversation
3d99b72
to
4713313
Compare
4713313
to
97250e6
Compare
Codecov Report
@@ Coverage Diff @@
## master #883 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 58 59 +1
Lines 1003 1029 +26
Branches 181 185 +4
=========================================
+ Hits 1003 1029 +26
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
New hook inspired by react-use's `useCopyToClipboard`. Countrary to react-use's implementation, this hook does not rely on `copy-to-clipboard` external dependency. Instead it simply uses navigator.clipboard API which seems to have good support on major browser vendors see https://caniuse.com/?search=navigator.clipboard.
97250e6
to
7661884
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
due to component lack of clipboard read - i really dont seee the need in state at all.
IMO there is only need in returned callback that returns copied text or throw an error in case of any. Current Implementation will lead to unnecessary re-renders, as of my experience - i never dont care what been copied - most of the time it is simple success or error message depending on results.
So i'd suggest simplifying the hook, but threfore component would be redundant, as it han be replaced with single function.
Instead of that i can suggest reimplementing this hook as more complex hook
useClipboard
that will give access to clipboard reads and reaction to clipboard events occured on docment, events list can be configurable for hook to provide maximal granularity of configuration. -
clipboard reset and clipboard set can be merged into single function - do reset when
undefined
received. -
copy function can receive any value, as there are loads of stringable objects that are not strings, just perform
String(val)
before passing to clipboard. -
you did not provide any jsdoc for hook, as it is required by contribution guide.
Is this new hook, |
@ArttuOll I'm not working on it so feel free to do so. My goal was to port |
@ArttuOll you can take and fix my remarks |
@ArttuOll next pick this one when you'll have free time🍻 |
@xobotyi I'm currently sketching the API for Return
|
im not sure about returned event handlers - i think usage of ClipboardAPI would be better approach |
You mean something like exposing |
and ways to assign clipboard events handlers, yes. |
How about this? Return
I'm confused about this. Does eventHandlers not already cover all the possible clipboard events? Also, should we go with |
Rethinking this now, I think I'll start implementing something like this. We can change it when I submit a PR, if we need to. Return
|
Yes =) This is exactly what i've been concerned of - you previous approach offered callbacks that should be passed to desired element which is not seems to be necessary. The only concern now - it seems no possbility to somehow validate\filter the target when we're dealing with clipboard, at least im not able to see it in offered signature. Proposal: [
readText: ()=> Promise<string>,
writeText: (text: string)=> Promise<void>,
] |
No problem, we're not in a hurry here. If the hook would have that return type, what is the benefit of having a hook at all instead of just using those methods from |
Given this more thinking lately. Previously such hook been needed due to poor spreading of clipboad api and usage of third-party libs, which is not the case nowadays. @JoeDuncko @ArttuOll |
This is pretty much what I thought when I started implementing this. The hook would do pretty much nothing of value, so the hook is unnecessary in my opinion also. |
Sorry for the delay. I agree. |
What new hook does?
New hook inspired by react-use's
useCopyToClipboard
, copies some text to clipboard. It returns an array composed as follow:copyState
that reflects copy process (if successful, erroneous and what data has been copied),copyToClipboard
, main callback to copy a string into clipboardresetCopyState
, new callback not present in react-use's implementation to easily resetcopyState
to initial value (all fields undefined)Countrary to react-use's implementation, this hook does not rely on
copy-to-clipboard
external dependency. Instead it simply uses navigator.clipboard API which seems to have good support on major browser vendors (see caniuse compatibly table). ThenoUserInteraction: boolean
has been removed fromcopyState
because it seemed specific tocopy-to-clipboard
usage.Also added a
success: boolean
field incopyState
to ease usage but this is up to debate since we could do as react-use's implementation and only rely onvalue
not being undefined.Checklist
react-use
#33 (useCopyToClipboard)