Feature: Allow passing all Button Props through ClipboardButton component#399
Feature: Allow passing all Button Props through ClipboardButton component#399fabiankaegy wants to merge 1 commit intodevelopfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the ClipboardButton component by making it more flexible and allowing consumers to pass additional button props. The component now extends ButtonProps, properly handles external refs, and makes several props optional with sensible defaults.
Key Changes:
- Extended
ClipboardButtonPropsto accept all standard button props exceptchildren - Made
disabled,labels, andonSuccessprops optional - Implemented proper ref merging to handle both internal and external refs
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| const ref = useCopyToClipboard(text, successfullyCopied); | ||
| const mergedRefs = useMergeRefs([ref, (rest.ref as any) || null]); |
There was a problem hiding this comment.
The ref property should be extracted from rest before spreading to avoid passing it twice to the Button component. Destructure ref from rest at the component parameter level alongside other props.
| } | ||
|
|
||
| const ref = useCopyToClipboard(text, successfullyCopied); | ||
| const mergedRefs = useMergeRefs([ref, (rest.ref as any) || null]); |
There was a problem hiding this comment.
Avoid using as any type assertion. Instead, properly type the ref or use type guards to ensure type safety.
| const mergedRefs = useMergeRefs([ref, (rest.ref as any) || null]); | |
| const validRef = | |
| typeof rest.ref === 'function' || | |
| (rest.ref && typeof rest.ref === 'object' && 'current' in rest.ref) | |
| ? rest.ref | |
| : null; | |
| const mergedRefs = useMergeRefs([ref, validRef]); |
|
|
||
| return ( | ||
| <Button disabled={disabled} ref={ref}> | ||
| <Button {...(rest as any)} ref={mergedRefs}> |
There was a problem hiding this comment.
The as any type assertion bypasses type checking. Since rest should already be typed as Partial<Omit<ButtonProps, 'children'>>, the assertion is unnecessary and should be removed.
| <Button {...(rest as any)} ref={mergedRefs}> | |
| <Button {...rest} ref={mergedRefs}> |
| * Function to run when the text is successfully copied. | ||
| */ | ||
| onSuccess: Function; | ||
| onSuccess?: Function; |
There was a problem hiding this comment.
Use a specific function signature instead of the generic Function type. Consider defining it as onSuccess?: () => void for better type safety.
| onSuccess?: Function; | |
| onSuccess?: () => void; |
10up Block Components
|
||||||||||||||||||||||||||||
| Project |
10up Block Components
|
| Branch Review |
feature/clipboard-button-rest-props
|
| Run status |
|
| Run duration | 04m 55s |
| Commit |
|
| Committer | Fabian Kägy |
| View all properties for this run ↗︎ | |
| Test results | |
|---|---|
|
|
0
|
|
|
0
|
|
|
0
|
|
|
0
|
|
|
35
|
| View all changes introduced in this branch ↗︎ | |
|
The changed functionality as described sounds like it would solve my use case, but I have not had a chance to compile these changes and test them locally. |
Closes #398
This pull request refactors the
ClipboardButtoncomponent to improve its flexibility and integration with other components. The main changes focus on making the button props more extensible, handling refs correctly, and making some props optional.Component API improvements
ClipboardButtonPropsinterface now extends the baseButtonprops (usingPartial<Omit<ButtonProps, 'children'>>), allowing the component to accept any valid button prop except forchildren. This makes the component more flexible and easier to use in different contexts.disabled,labels, andonSuccessprops are now optional, providing sensible defaults and reducing the need for boilerplate when using the component. [1] [2]Ref handling improvements
useMergeRefs, ensuring that both internal and external refs are handled correctly when the button is used in more complex scenarios.Usage and rendering changes
rest) onto the underlyingButtoncomponent, further improving extensibility and allowing for easier customization.