Import
import { CopyButton } from '@contentful/f36-components';// orimport { CopyButton } from '@contentful/f36-copybutton';
Examples
Basic
function CopyButtonExample() { return ( <CopyButton value="A value that will be put into clipboard when you click this button" /> ); }
Changing the tooltip
You can modify tooltip text and its placement by using the following properties: tooltipCopiedText, tooltipText and tooltipProps.
To read more about all possible values of tooltipProps object, refer to Tooltip documentation.
function CopyButtonModifiedTooltipsExample() { return ( <CopyButton value="Copy value" tooltipCopiedText="The text has been copied" tooltipText="Copy this text to clipboard" tooltipProps={{ placement: 'bottom', usePortal: true }} /> ); }
Using with TextInput
function CopyButtonWithInputExample() { const value = 'myContentTypeId'; return ( <Flex flexDirection="column"> <Paragraph> Use this ID to retrieve everything related to this content type via the API. </Paragraph> <TextInput.Group> <TextInput isDisabled isReadOnly value={value} /> <CopyButton value={value} tooltipProps={{ placement: 'right', usePortal: true }} /> </TextInput.Group> </Flex> ); }
Using with promises
Sometimes you want to await a Promise before know the value to copy to the clipboard. You can do this in the code where the copy button is implemented.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck -- disable checking since we cant cast the Promise return type in examples function CopyButtonWithPromiseExample() { const [value, setValue] = useState(); const [isLoading, setIsLoading] = useState(false); // In your application this could be any async function returning a string to // copy to the clipboard const asyncValue = () => new Promise((resolve) => setTimeout(resolve, 5000, 'A value that you will have to wait for'), ); useEffect(() => { async function loadValue() { setIsLoading(true); const resolvedValue = await asyncValue(); setValue(resolvedValue); setIsLoading(false); } loadValue(); }, []); const hasValue = typeof value !== 'undefined'; return ( <Flex flexDirection="column" gap="spacingM"> <Flex alignItems="center" flexDirection="column"> <Paragraph> This CopyButton can copy the value returned from the asynchronous call after it’s done loading </Paragraph> <CopyButton isDisabled={!hasValue} isLoading={isLoading} value={hasValue ? value : ''} /> </Flex> </Flex> ); }
Accessibility
You can pass a custom aria-label through the label prop.
Props (API reference)
Open in StorybookName | Type | Default |
|---|---|---|
| value required | string Value that will be copied to clipboard when the button is clicked | |
| as | "button" The element used for the root node. | button |
| children | ReactNode | |
| className | string CSS class to be appended to the root element | |
| isActive | false true Applies active styles | false |
| isDisabled | false true Allows to disable the copy button, when true the tooltip would not be shown | false |
| isFullWidth | false true Forces button to take 100% of the container | |
| isLoading | false true Adds loading indicator icon and disables interactions | |
| label | string Label to be used on aria-label for the button | Copy to clipboard |
| onCopy | (string: any) => void Function that gets called when the button is clicked | |
| size | "small" "medium" Allows setting size of the copy button to small | medium |
| startIcon | ReactElement<IconProps<"svg">, string | JSXElementConstructor<any>> Expects any of the icon components. Renders the icon aligned to the start | |
| testId | string A [data-test-id] attribute used for testing purposes | |
| tooltipCopiedText | string Text to be shown when the button is clicked | Copied! |
| tooltipProps | Omit<TooltipProps, "children" | "content" | "withTriggerWrapper"> Props that are passed to the tooltip component | |
| tooltipText | string Text to be shown when button is hovered or focused | Copy to clipboard |
| variant | "negative" "positive" "primary" "secondary" "transparent" Determines style variation of Button component | secondary |