Skip to content

Commit c710159

Browse files
committed
feature: allow assing ll button props to the ClipboardButton
1 parent 766fc8d commit c710159

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

components/clipboard-button/index.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
import { useCopyToClipboard } from '@wordpress/compose';
1+
import { useCopyToClipboard, useMergeRefs } from '@wordpress/compose';
22
import { useState, useEffect } from '@wordpress/element';
33
import { Button } from '@wordpress/components';
44
import { __ } from '@wordpress/i18n';
55

6-
interface ClipboardButtonProps {
6+
type ButtonProps = React.ComponentProps<typeof Button>;
7+
8+
interface ClipboardButtonProps extends Partial<Omit<ButtonProps, 'children'>> {
79
/**
810
* The text to copy to the clipboard.
911
*/
1012
text: string;
1113

12-
/**
13-
* Boolean to disable the button.
14-
*/
15-
disabled: boolean;
16-
1714
/**
1815
* Function to run when the text is successfully copied.
1916
*/
20-
onSuccess: Function;
17+
onSuccess?: Function;
2118

2219
/**
2320
* Labels for the button.
2421
*/
25-
labels: ButtonLabels;
22+
labels?: ButtonLabels;
2623
}
2724

2825
interface ButtonLabels {
@@ -39,9 +36,9 @@ interface ButtonLabels {
3936

4037
export const ClipboardButton: React.FC<ClipboardButtonProps> = ({
4138
text = '',
42-
disabled = false,
4339
onSuccess = () => {},
4440
labels = {},
41+
...rest
4542
}) => {
4643
const [hasCopied, setHasCopied] = useState(false);
4744
const copy = labels.copy ? labels.copy : __('Copy');
@@ -77,9 +74,10 @@ export const ClipboardButton: React.FC<ClipboardButtonProps> = ({
7774
}
7875

7976
const ref = useCopyToClipboard(text, successfullyCopied);
77+
const mergedRefs = useMergeRefs([ref, (rest.ref as any) || null]);
8078

8179
return (
82-
<Button disabled={disabled} ref={ref}>
80+
<Button {...(rest as any)} ref={mergedRefs}>
8381
{hasCopied ? copied : copy}
8482
</Button>
8583
);

0 commit comments

Comments
 (0)