-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref: copytoclipboard with less magic #102190
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
Conversation
❌ 1 Tests Failed:
View the full list of 1 ❄️ flaky test(s)
To view more test analytics, go to the Test Analytics Dashboard |
| children?: never; | ||
| onCopy?: undefined | ((copiedText: string) => void); | ||
| onError?: undefined | ((error: Error) => void); | ||
| } & Overwrite< |
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.
The type signature here was dropping the aria-label requirement from types.
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.
could be a mutation 🤷 either way does look better
Yeah, we can just drop the hook and expose the copyToClipboard directly :D not like there's anything react related in there now anyways... |
| * @default {successMessage: t('Copied to clipboard'), errorMessage: t('Error copying to clipboard')} | ||
| * Pass `null` to disable any toast messages. | ||
| */ | ||
| options?: {errorMessage?: React.ReactNode; successMessage?: React.ReactNode} | null |
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.
really small thing but why would we need options: null if it’s already optional?
| options?: {errorMessage?: React.ReactNode; successMessage?: React.ReactNode} | null | |
| options?: {errorMessage?: React.ReactNode; successMessage?: React.ReactNode} |
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.
@TkDodo there used to be a hook prop called hideMessages which is now expressed as passing the null value. I find null to be more explicit in this case as we initialize default success and error messages. I'd be open to better alternatives though, I'm not a big fan of it either
Issues attributed to commits in this pull requestThis pull request was merged and Sentry observed the following issues: |
Refactor and reduce the abstraction of useCopyToClipboard to mostly manage navigator.clipboard calls. The improvements decouple the hook call functionality by deferring more responsibility to the caller (inversion of control). **Changes:** - Remove 1:1 coupling between Copy and react hook calls. (extra flexibility) Copy callback now receives the text arguments directly, meaning one instance of the copy callback can be called with multiple different values without doing `useCopyToClipboard` many times. - Explicit control flow (easier call tracing) The indirection of passing props to a hook that creates a closure and calls the callback creates an indirection where `copy()` requires you to see what arguments went into the call by investigating props. By changing this to `copy(...arguments)`, we make the calls explicit - Remove onCopy and onError in favor of returning the promise from the copy callback This is now explicit, which improves readability and understanding of when these callbacks are executed. - Remove label handling While this may have been helpful, it has also been largely unused, and in many cases resulted in our tooltips changing text to "Copied!", as well as the toast messages being dispatched. Having multiple indicators for a single action is not a good user experience. If this functionality is required, it can be implemented by hooking into the returned promise value. Given the simplification, I think it's actually sensible to argue that this probably does not need to be a react hook at all, and could simply just be function call that wraps around the navigator.clipboard API, but I'll leave that up to discussion and a future change
Refactor and reduce the abstraction of useCopyToClipboard to mostly manage navigator.clipboard calls. The improvements decouple the hook call functionality by deferring more responsibility to the caller (inversion of control).
Changes:
Copy callback now receives the text arguments directly, meaning one instance of the copy callback can be called with multiple different values without doing
useCopyToClipboardmany times.The indirection of passing props to a hook that creates a closure and calls the callback creates an indirection where
copy()requires you to see what arguments went into the call by investigating props. By changing this tocopy(...arguments), we make the calls explicitThis is now explicit, which improves readability and understanding of when these callbacks are executed.
While this may have been helpful, it has also been largely unused, and in many cases resulted in our tooltips changing text to "Copied!", as well as the toast messages being dispatched. Having multiple indicators for a single action is not a good user experience. If this functionality is required, it can be implemented by hooking into the returned promise value.
Given the simplification, I think it's actually sensible to argue that this probably does not need to be a react hook at all, and could simply just be function call that wraps around the navigator.clipboard API, but I'll leave that up to discussion and a future change