import { Clipboard } from '@cerberus/react'The Clipboard component provides an accessible way to copy text to the user's clipboard with built-in visual feedback.
The Clipboard component is unstyled by default, allowing for full customization.
Use the asChild prop on the Clipboard.Trigger to create a copy button.
Use the asChild or style props prop on the Clipboard.Input to create a copy input.
Use the useClipboard hook to create a fully custom solution.
You can utilize the primitive components or the css prop to customize the clipboard component.
| Component | Description |
|---|---|
| ClipboardRoot | The root context provider for the clipboard family. |
| ClipboardLabel | A label element for accessibility. |
| ClipboardControl | The wrapper for input and trigger elements. |
| ClipboardInput | The input element containing the text to copy. |
| ClipboardTrigger | The button that triggers the copy action. |
| ClipboardIndicator | Shows the current state (copy/copied) with icons. |
| CopyText | Shows the current state (copy/copied) with text. |
The Clipboard component exposes several data attributes for better accessibility and customization.
| Name | Value | Description |
|---|---|---|
data-scope | clipboard | The scope of the components. |
data-part | root | The root container part. |
data-part | label | The label part. |
data-part | control | The control part. |
data-part | input | The input part. |
data-part | trigger | The trigger part. |
data-part | indicator | The indicator part. |
data-copied | Present when copied state is true |
| Prop | Type | Required | Description |
|---|---|---|---|
asChild | boolean | No | Use the provided child element as the default rendered element, combining their props and behavior. |
defaultValue | string | No | 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 | No | The unique identifier of the machine. |
ids | Partial<{ root: string; input: string; label: string }> | No | The ids of the elements in the clipboard. Useful for composition. |
onStatusChange | (details: CopyStatusDetails) => void | No | The function to be called when the value is copied to the clipboard |
onValueChange | (details: ValueChangeDetails) => void | No | The function to be called when the value changes |
timeout | number | No | The timeout for the copy operation |
value | string | No | The controlled value of the clipboard |
The Clipboard.Root is an abstraction of the Root component.
The indicator accepts the following props:
| Name | Default | Description |
|---|---|---|
| size | 16 | The size of the indicator (in pixels). Can be 16, 20, 24, or 32. |
The Clipboard API is an Object containing the full family of components.
| Name | Description |
|---|---|
| Root | The ClipboardRoot component which is the Provider for the family. |
| Label | The ClipboardLabel component for accessibility. |
| Control | The ClipboardControl component wrapper. |
| Input | The ClipboardInput component displaying the text. |
| Trigger | The ClipboardTrigger component button. |
| Indicator | The CopyIndicator component with default copy/checkmark icons. |
| CopyText | The CopyText component with default "Copy"/"Copied" text indicators. |
| Context | The raw context for accessing clipboard state within the component hierarchy. |
On this page
'use client'
import { HStack } from '@/styled-system/jsx'
import { Clipboard, IconButton, Text } from '@cerberus/react'
export function BasicDemo() {
return (
<Clipboard.Root value="Hello, World!">
<Clipboard.Control asChild>
<HStack gap="md" w="full">
<Clipboard.Label asChild>
<Text as="small" textStyle="label-sm">
Hello, World!
</Text>
</Clipboard.Label>
<Clipboard.Trigger asChild>
<IconButton clipboard shape="square" size="sm" rounded="xs">
<Clipboard.Indicator />
</IconButton>
</Clipboard.Trigger>
</HStack>
</Clipboard.Control>
</Clipboard.Root>
)
}
'use client'
import { Button, Show, useClipboard } from '@cerberus/react'
export function StoreDemo() {
const clipboard = useClipboard({ value: 'https://chakra-ui.com' })
return (
<Button size="sm" onClick={clipboard.copy}>
<Show when={clipboard.copied} fallback="Copy">
Copied
</Show>
</Button>
)
}
'use client'
import { Button, Clipboard, Group } from '@cerberus/react'
export function InputDemo() {
return (
<Clipboard.Root maxW="300px" value="https://cerberus.digitalu.design/">
<Clipboard.Control asChild>
<Group layout="attached">
<Clipboard.Input
border="1px solid"
borderColor="page.border.initial"
px="md"
rounded="sm"
textStyle="label-sm"
w="15rem"
/>
<Clipboard.Trigger color="action.text.initial" asChild>
<Button color="inherit" shape="default" size="md">
<Clipboard.Indicator />
</Button>
</Clipboard.Trigger>
</Group>
</Clipboard.Control>
</Clipboard.Root>
)
}
'use client'
import { Button, Clipboard } from '@cerberus/react'
export function ButtonDemo() {
return (
<Clipboard.Root value="Hello, World!">
<Clipboard.Control>
<Clipboard.Trigger asChild>
<Button color="action.text.initial">
<Clipboard.CopyText color="currentColor" />
<Clipboard.Indicator />
</Button>
</Clipboard.Trigger>
</Clipboard.Control>
</Clipboard.Root>
)
}