-
Notifications
You must be signed in to change notification settings - Fork 51
Feature: Allow passing all Button Props through ClipboardButton component #399
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,28 +1,25 @@ | ||||||||||||||||
| import { useCopyToClipboard } from '@wordpress/compose'; | ||||||||||||||||
| import { useCopyToClipboard, useMergeRefs } from '@wordpress/compose'; | ||||||||||||||||
| import { useState, useEffect } from '@wordpress/element'; | ||||||||||||||||
| import { Button } from '@wordpress/components'; | ||||||||||||||||
| import { __ } from '@wordpress/i18n'; | ||||||||||||||||
|
|
||||||||||||||||
| interface ClipboardButtonProps { | ||||||||||||||||
| type ButtonProps = React.ComponentProps<typeof Button>; | ||||||||||||||||
|
|
||||||||||||||||
| interface ClipboardButtonProps extends Partial<Omit<ButtonProps, 'children'>> { | ||||||||||||||||
| /** | ||||||||||||||||
| * The text to copy to the clipboard. | ||||||||||||||||
| */ | ||||||||||||||||
| text: string; | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Boolean to disable the button. | ||||||||||||||||
| */ | ||||||||||||||||
| disabled: boolean; | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Function to run when the text is successfully copied. | ||||||||||||||||
| */ | ||||||||||||||||
| onSuccess: Function; | ||||||||||||||||
| onSuccess?: Function; | ||||||||||||||||
|
|
||||||||||||||||
| /** | ||||||||||||||||
| * Labels for the button. | ||||||||||||||||
| */ | ||||||||||||||||
| labels: ButtonLabels; | ||||||||||||||||
| labels?: ButtonLabels; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| interface ButtonLabels { | ||||||||||||||||
|
|
@@ -39,9 +36,9 @@ interface ButtonLabels { | |||||||||||||||
|
|
||||||||||||||||
| export const ClipboardButton: React.FC<ClipboardButtonProps> = ({ | ||||||||||||||||
| text = '', | ||||||||||||||||
| disabled = false, | ||||||||||||||||
| onSuccess = () => {}, | ||||||||||||||||
| labels = {}, | ||||||||||||||||
| ...rest | ||||||||||||||||
| }) => { | ||||||||||||||||
| const [hasCopied, setHasCopied] = useState(false); | ||||||||||||||||
| const copy = labels.copy ? labels.copy : __('Copy'); | ||||||||||||||||
|
|
@@ -77,9 +74,10 @@ export const ClipboardButton: React.FC<ClipboardButtonProps> = ({ | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const ref = useCopyToClipboard(text, successfullyCopied); | ||||||||||||||||
| const mergedRefs = useMergeRefs([ref, (rest.ref as any) || null]); | ||||||||||||||||
|
||||||||||||||||
| 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]); |
Copilot
AI
Oct 27, 2025
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 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}> |
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.
Use a specific function signature instead of the generic
Functiontype. Consider defining it asonSuccess?: () => voidfor better type safety.