Skip to content

fix(site): update useClipboard to work better with effect logic#20183

Merged
buenos-nachos merged 7 commits intomainfrom
mes/clipboard-update
Oct 6, 2025
Merged

fix(site): update useClipboard to work better with effect logic#20183
buenos-nachos merged 7 commits intomainfrom
mes/clipboard-update

Conversation

@buenos-nachos
Copy link
Contributor

@buenos-nachos buenos-nachos commented Oct 6, 2025

No issue to track – spurred by #20129

Basically, the useClipboard hook worked just fine up until now because we could always derive the clipboard text from within a render. There was never a case before where we would need to derive the text from outside React, and then sync it with both React and the browser. This PR updates the API to make that use case much more ergonomic and avoid risks of infinite renders

Changes made

  • Updated useClipboard API to require passing the text in via the copyToClipboard function, rather than requiring that the text gets specified in render logic
  • Ensured that the copyToClipboard function always stays stable across all React lifecycles
  • Updated all existing uses to use the new function signatures
  • Updated all tests and added new cases

@buenos-nachos buenos-nachos self-assigned this Oct 6, 2025
@buenos-nachos buenos-nachos requested a review from aslilac as a code owner October 6, 2025 16:18
};
}

function renderUseClipboard<TInput extends UseClipboardInput>(inputs: TInput) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why I made this generic originally

Copy link
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing thank you! I tried it out on my PR as well and it works wonderfully.

* will dispatch an error message to the GlobalSnackbar
*/
onError?: (errorMessage: string) => void;
clearErrorOnSuccess?: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to say, as a gut reaction, I feel like I would expect this to clear the error on success by default, but actually it looks like we never use error anywhere? Maybe we could even drop storing and exposing the error for now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can see that. I feel like we'll eventually need it, and the code is already set up to account for it in the tests, so I think merging it in now has the least friction. But I'll look into removing the error behavior in a bit

Copy link
Contributor Author

@buenos-nachos buenos-nachos Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, the clearErrorOnSuccess prop isn't "new", because it was already in the Registry codebase for a few months after I copied this file over there

@buenos-nachos buenos-nachos merged commit 156f985 into main Oct 6, 2025
26 checks passed
@buenos-nachos buenos-nachos deleted the mes/clipboard-update branch October 6, 2025 19:41
@github-actions github-actions bot locked and limited conversation to collaborators Oct 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants