Clipboard
A component to copy text to the clipboard
Anatomy
<Clipboard.Root>
<Clipboard.Label />
<Clipboard.Control>
<Clipboard.Input />
<Clipboard.Trigger>
<Clipboard.Indicator />
</Clipboard.Trigger>
</Clipboard.Control>
</Clipboard.Root>
Examples
Controlled
Use value and onValueChange to control the clipboard value.
Root Provider
Use RootProvider with the useClipboard hook to access the state and methods from outside the component.
Context
Use Clipboard.Context or the useClipboardContext hook to read copied, value and setValue.
Copy Status
onStatusChange fires on each copy. Use its copied property to show a success message.
Timeout
Set timeout to change how long the copied status lasts. The default is 3000ms.
Value Text
Use Clipboard.ValueText to display the current clipboard value.
API Reference
Props
Root
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. 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 | ElementIdsThe ids of the elements in the clipboard. Useful for composition. | |
onStatusChange | (details: CopyStatusDetails) => voidThe function to be called when the copied status changes, or copying fails | |
onValueChange | (details: ValueChangeDetails) => voidThe function to be called when the value changes | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
timeout | 3000 | numberThe timeout for the copy operation |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
value | stringThe controlled value of the clipboard |
| Attribute | Description |
|---|---|
[data-clipboard-root] | <uid> |
[data-copied] | Present when copied state is true |
Control
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-clipboard-control] | <uid> |
[data-copied] | Present when copied state is true |
Indicator
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
copied | string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...> | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<ClipboardIndicatorState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Input
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-clipboard-input] | <uid> |
[data-copied] | Present when copied state is true |
[data-readonly] | Present when read-only |
Label
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-clipboard-label] | <uid> |
[data-copied] | Present when copied state is true |
RootProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseClipboardReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Trigger
Defines the <button> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-clipboard-trigger] | <uid> |
[data-copied] | Present when copied state is true |
ValueText
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Context
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 |
getRootState | () => RootStateReturns the state of the root |
getIndicatorState | (props: IndicatorProps) => IndicatorStateReturns the state of the indicator |