Clipboard
A component to copy text to the clipboard
Anatomy
To set up the Clipboard correctly, you'll need to understand its anatomy and how we name its parts.
Each part includes a
data-partattribute to help identify them in the DOM.
Examples
Learn how to use the Clipboard component in your project. Let's take a look at the most basic example:
Example not foundExample not foundExample not foundExample not foundUsing Context
Access the clipboard state and methods using Clipboard.Context. This provides access to properties like copied,
value, and setValue
Alternatively, you can use the
useClipboardContexthook to access the clipboard context.
Example not foundExample not foundExample not foundExample not foundCopy Status
Use the onStatusChange prop to listen for copy operations. It exposes a copied property that you can use to display
a success message.
Example not foundExample not foundExample not foundExample not foundControlled
Control the clipboard value externally by managing the state yourself and using onValueChange to handle updates.
Example not foundExample not foundExample not foundExample not foundRoot Provider
Use the useClipboard hook to create the clipboard store and pass it to the Clipboard.RootProvider component. This
allows you to have maximum control over the clipboard programmatically.
If you're using the
Clipboard.RootProvidercomponent, you don't need to use theClipboard.Rootcomponent.
Example not foundExample not foundExample not foundExample not foundCustom Timeout
Configure the copy status timeout duration using the timeout prop. Default is 3000ms (3 seconds).
Example not foundExample not foundExample not foundExample not foundProgrammatic Copy
Trigger copy operations programmatically using the context's copy() method.
Example not foundExample not foundExample not foundExample not foundValue Text
Use Clipboard.ValueText to display the current clipboard value.
Example not foundExample not foundExample not foundExample not foundAPI Reference
Props
Root
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
defaultValue | stringThe initial value to be copied to the clipboard when rendered. Use when you don't need to control the value of the clipboard. | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{ root: string; input: string; label: string }>The ids of the elements in the clipboard. Useful for composition. | |
onStatusChange | (details: CopyStatusDetails) => voidThe function to be called when the value is copied to the clipboard | |
onValueChange | (details: ValueChangeDetails) => voidThe function to be called when the value changes | |
timeout | 3000 | numberThe timeout for the copy operation |
value | stringThe controlled value of the clipboard |
| Data Attribute | Value |
|---|---|
[data-scope] | clipboard |
[data-part] | root |
[data-copied] | Present when copied state is true |
Control
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | clipboard |
[data-part] | control |
[data-copied] | Present when copied state is true |
Indicator
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
copied | string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...> |
Input
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | clipboard |
[data-part] | input |
[data-copied] | Present when copied state is true |
[data-readonly] | Present when read-only |
Label
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | clipboard |
[data-part] | label |
[data-copied] | Present when copied state is true |
RootProvider
| Prop | Default | Type |
|---|---|---|
value | UseClipboardReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Trigger
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | clipboard |
[data-part] | trigger |
[data-copied] | Present when copied state is true |
ValueText
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Context
These are the properties available when using Clipboard.Context, useClipboardContext hook or useClipboard hook.
API
| Property | Type |
|---|---|
copied | booleanWhether the value has been copied to the clipboard |
value | stringThe value to be copied to the clipboard |
setValue | (value: string) => voidSet the value to be copied to the clipboard |
copy | VoidFunctionCopy the value to the clipboard |