Basic usage
Copyable copies valueToCopy to the clipboard when it is activated. It does not show a toast by
itself, so pass a callback and show the feedback in your own code. The callback receives
{isSuccess, message}.
import {Copyable, useToaster} from '@pleo-io/telescope'
const ApiKeyCopy = ({apiKey}: {apiKey: string}) => {
const {showToast} = useToaster()
return (
<Copyable
text="Copy API key"
valueToCopy={apiKey}
callback={({isSuccess, message}) =>
showToast(message, {variant: isSuccess ? 'positive' : 'negative'})
}
/>
)
}Variants
By default Copyable renders a button with a copy icon. Use text to show a label, or children
to render your own content, for example a Tooltip.
<Copyable text="Copy email" valueToCopy="john.doe@pleo.io" callback={handleCallback} />
<Copyable text="Copy email" valueToCopy="john.doe@pleo.io" callback={handleCallback} isWithoutIcon />
<Copyable text="Copy email" valueToCopy="john.doe@pleo.io" callback={handleCallback} iconPosition="right" />Set isInline to render the component as an inline element instead of a button. Inline copies are
focusable and can be activated with Enter or Space.
<Copyable
text="john.doe@pleo.io"
valueToCopy="john.doe@pleo.io"
callback={handleCallback}
isInline
/>Set asIconButton to render an icon-only IconButton. Its variant, size and tooltipProps are
passed through to the IconButton.
<Copyable
valueToCopy="john.doe@pleo.io"
callback={handleCallback}
asIconButton={{variant: 'quiet', size: 'small', tooltipProps: {content: 'Copy'}}}
/>Use CustomIcon to replace the copy icon with another Telescope icon. It also works with
asIconButton.
import {Email} from '@pleo-io/telescope-icons'
;<Copyable
text="Copy email"
valueToCopy="john.doe@pleo.io"
callback={handleCallback}
CustomIcon={Email}
/>Set disabled to block the copy action and apply the disabled style.
<Copyable text="Copy email" valueToCopy="john.doe@pleo.io" callback={handleCallback} disabled />Callback
callback is called after every copy attempt with:
isSuccess:truewhen the value was copied,falseotherwisemessage: the localised success or error message, unless overridden
Use successMessage and errorMessage to override the message passed to the callback.
<Copyable
text="Copy reference"
valueToCopy={reference}
successMessage="Reference copied"
errorMessage="Could not copy the reference"
callback={handleCallback}
/>Localisation
The labels come from the TelescopeProvider translations under Copyable:
Copyable.CopyButton: the accessible name of the component, for example "Copy to clipboard"Copyable.CopySuccess: the default success message passed to the callbackCopyable.CopyError: the default error message passed to the callback
Accessibility
- The accessible name is always
Copyable.CopyButton, even whentextorchildrenare shown. - Inline copyables have
role="button", are focusable and can be activated with Enter or Space. - When
disabled, clicks and keyboard activation do not copy. - Feedback is delivered through the
callback, so show it with a toast or another live region.
API reference
| Prop | Type | Default |
|---|---|---|
asIconButtonRenders an icon-only IconButton instead of a button or inline element | enum{ variant: "primary" | "secondary" | "quiet"; size: "small" | "medium" | "large" | "extraSmall" | undefined; tooltipProps: IconButtonTooltipProps; } | |
callback (required)Called after the copy attempt with whether it succeeded and a message | function(result: CopyResult) => void | |
childrenContent rendered when | enumnull, string, number, bigint, false, true, ReactElement<unknown, string | JSXElementConstructor<any>>, Iterable<ReactNode>, ReactPortal, Promise<AwaitedReactNode> | |
CustomIconTelescope icon rendered instead of the copy icon | enumIconComponent | undefined | |
dataTestIdOverrides the default data-testid | enumstring | undefined | |
disabledDisables the copy action and applies the disabled style | boolean | |
errorMessageOverrides the message passed to | enumstring | undefined | |
iconPositionPosition of the copy icon, defaults to left | enum"left" | "right" | undefined | left |
isInlineRenders the component as an inline element instead of a button | boolean | |
isWithoutIconRemoves the copy icon | boolean | |
successMessageOverrides the message passed to | enumstring | undefined | |
textText to display inside a span instead of children | enumstring | undefined | |
valueToCopy (required)Value copied to the clipboard when activated | string |