Clipboard

A component to copy text to the clipboard

Loading...

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.

AttributeDefaultType
asChild
boolean

Use 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
string

The initial value to be copied to the clipboard when rendered. Use when you don't need to control the value of the clipboard.

id
string

The unique identifier of the machine.

ids
ElementIds

The ids of the elements in the clipboard. Useful for composition.

onStatusChange
(details: CopyStatusDetails) => void

The function to be called when the copied status changes, or copying fails

onValueChange
(details: ValueChangeDetails) => void

The 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.

timeout3000
number

The timeout for the copy operation

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

value
string

The controlled value of the clipboard

AttributeDescription
[data-clipboard-root]<uid>
[data-copied]Present when copied state is true

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-clipboard-control]<uid>
[data-copied]Present when copied state is true

Indicator

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-clipboard-input]<uid>
[data-copied]Present when copied state is true
[data-readonly]Present when read-only

Label

Defines the <label> element.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-clipboard-label]<uid>
[data-copied]Present when copied state is true

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseClipboardReturn

asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[data-clipboard-trigger]<uid>
[data-copied]Present when copied state is true

ValueText

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use 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

PropertyType
copied
boolean

Whether the value has been copied to the clipboard

value
string

The value to be copied to the clipboard

setValue
(value: string) => void

Set the value to be copied to the clipboard

copy
VoidFunction

Copy the value to the clipboard

getRootState
() => RootState

Returns the state of the root

getIndicatorState
(props: IndicatorProps) => IndicatorState

Returns the state of the indicator