ClipboardButton 是一个已弃用的 WordPress 组件,用于通过点击复制文本或其他元素。开发者应改用 @wordpress/compose 包中的 useCopyToClipboard Hook。
import { useState } from 'react';
import { ClipboardButton } from '@wordpress/components';
const MyClipboardButton = () => {
const [ hasCopied, setHasCopied ] = useState( false );
return (
<ClipboardButton
variant="primary"
text="Text to be copied."
onCopy={ () => setHasCopied( true ) }
onFinishCopy={ () => setHasCopied( false ) }
>
{ hasCopied ? 'Copied!' : 'Copy Text' }
</ClipboardButton>
);
};With a clipboard button, users copy text (or other elements) with a single click or tap.

import { useState } from 'react';
import { ClipboardButton } from '@wordpress/components';
const MyClipboardButton = () => {
const [ hasCopied, setHasCopied ] = useState( false );
return (
<ClipboardButton
variant="primary"
text="Text to be copied."
onCopy={ () => setHasCopied( true ) }
onFinishCopy={ () => setHasCopied( false ) }
>
{ hasCopied ? 'Copied!' : 'Copy Text' }
</ClipboardButton>
);
};
The component accepts the following props:
The class that will be added to the classes of the underlying <Button> component.
stringThe text that will be copied to the clipboard.
stringThe function that will be called when the text is copied.
— Type: () => void
— Required: yes
The function that will be called when the text is copied and the copy animation is finished.
— Type: () => void
— Required: no
Any additional props will be passed the underlying <Button/> component. See the Button component for more details on the available props.