shadcn.io is an independent project built on shadcn/ui
On this page9 sections
Clipboard copies a fixed string, such as a share link, an API key or an install command, and swaps its icon for a check for three seconds afterward. It pairs the copy button with a read-only field or a line of text, so people see what they are about to copy. UI SDK has no clipboard, so it's built on UI SDK's core.
When people should edit the text before copying it, pair an Input with a Button whose click handler writes the field's current value to the clipboard.
Installation
Anatomy

Usage
<script lang="ts">
const SITE_URL = "https://www.shadcn.io"
import {
Clipboard, ClipboardIndicator, ClipboardInput, ClipboardTrigger,
} from "@/components/ui/clipboard"
import {
InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput,
} from "@/components/ui/input-group"
</script>
<div class="w-full max-w-sm"><Clipboard value={SITE_URL}><InputGroup><ClipboardInput aria-label="Link">{#snippet render({ props })}<InputGroupInput {...props} />{/snippet}</ClipboardInput><InputGroupAddon align="inline-end"><ClipboardTrigger>{#snippet render({ props })}<InputGroupButton {...props} size="icon-xs"><ClipboardIndicator /></InputGroupButton>{/snippet}</ClipboardTrigger></InputGroupAddon></InputGroup></Clipboard></div>Accessibility
ClipboardTrigger is a button with an aria-label that reads Copy to clipboard, and Copied to clipboard while the check shows, so an icon-only trigger has a name without any work from you. The icons in ClipboardIndicator are hidden from screen readers. ClipboardInput is read-only and selects its whole value when it gets focus. It has no name of its own, so add a ClipboardLabel, which is tied to it, or give it an aria-label, as the Usage snippet does with Link.
- Enter
- When focus is on the trigger, copies the value.
- Space
- When focus is on the trigger, copies the value.
- Ctrl+C
- When focus is on the field, copies the selected text, which is the whole value unless the selection changed, and starts the copied state. Command + C does the same on a Mac.
Demos
Context
A single outline button that reads Copy and turns into Copied, built from ClipboardContext instead of ClipboardTrigger. The context hands you copied and copy, so the words carry the feedback when an icon swap would be too quiet. The button's own text is its name here, since only ClipboardTrigger gets the automatic label.
Controlled
Clipboard takes its value from state, and a Change URL button below it switches the value to the docs path, which the field and the next copy both follow. Hold the value yourself like this when something outside the clipboard decides what gets copied, such as a selected environment or a freshly generated token. A string that never changes needs only value.
Copy status
A copy button with the URL beside it in ClipboardValue, and a counter underneath that goes up on every copy through onStatusChange. The callback is where a toast, an analytics event or a usage count belongs. It fires the moment a copy is requested, with copied set to true, and not again when the check goes away.
Custom timeout
The same field and button with timeout set to 5000, so the check stays for five seconds instead of the default three. Lengthen it when the check is a small icon at the end of a long field that people glance away from. Shorten it when people copy several values in a row, so a leftover check doesn't point at the wrong one.
Different icon
A filled icon button whose indicator shows one sparkle at rest and a cluster of sparkles after the copy. The resting icon is the indicator's content and the copied one goes in copied. Swap both when a clipboard and a check clash with the icon set in the rest of your product.
Icon only
A filled icon button that copies the URL, with no field and no visible text. The trigger still has a name, Copy to clipboard, which changes to Copied to clipboard while the check shows. Use it where the value is already on screen, in a table cell or beside a code sample, and the row has no room for a word.
Root provider
useClipboard creates the state, and ClipboardRootProvider hands it to the field and its button. A Copy from outside button above calls copy on that same state, and a line above it prints whether copied is true. Reach for this when the control that copies lives elsewhere, such as a page header far from the field.
Value text
The URL as text in ClipboardValue, a span styled with an input's border and height, followed by an icon button. Only the button takes focus, so this suits a token or an ID repeated down a list, where a read-only field per row would add a tab stop each. Its size follows the input scale, from xs to xl.
With label
ClipboardLabel puts Copy this link above the field and is tied to the read-only input, so the label names the field, and clicking it focuses the input and selects the URL. The copy button keeps its own automatic name. Add a label whenever the value alone doesn't explain what it is.
FAQ
How do I add a copy to clipboard button in Svelte?
Put a ClipboardTrigger inside Clipboard and give the root the text as value. Pressing the trigger writes that text to the clipboard, and a ClipboardIndicator inside it shows a check for three seconds, as in the Icon only demo. The trigger comes with an accessible name, so an icon alone is enough.
value can come from state when the text changes, as in the Controlled demo, and the next press copies whatever it holds at that moment.
How do I put the copy button inside the input?
Compose it with an Input Group. ClipboardInput renders onto InputGroupInput through render, and ClipboardTrigger renders onto an InputGroupButton inside an InputGroupAddon aligned to the end. The first preview on this page and the Usage snippet are that exact layout.
The field it makes is read-only and selects its whole value on focus, so people can check what they are about to copy but can't change it. For an editable field, use a plain Input and copy its value yourself.
How long does the Clipboard stay in the copied state?
Three seconds. Set timeout on Clipboard, in milliseconds, to change it, as the Custom timeout demo does with 5000.
A copy from the trigger, from a call to copy or from someone copying out of the field starts it, and another copy before it ends doesn't restart the count. While it lasts the root carries data-copied and the indicator shows its copied icon, and when it ends both go back to rest.
How do I know when the Clipboard copied the text?
Listen to the status callback on Clipboard, the onStatusChange prop. It receives { copied: true } each time a copy starts, from the trigger, from copy or from someone copying out of the field, which makes it the place for a toast, an analytics event or a counter like the one in the Copy status demo.
It fires when the copy is requested, without waiting for the browser to confirm the write, and it doesn't fire again when the copied state ends.
How do I copy from a button outside the Clipboard?
Create the state with useClipboard, passing it the props Clipboard would take, such as value, and render ClipboardRootProvider with that state where Clipboard would go. Pass an id as well, as the Root provider demo does with $props.id(), so the server and the browser give the field the same one. The hook returns an accessor, so call it to reach copy and copied.
How do I change the Clipboard copy icon?
Put the resting icon inside ClipboardIndicator and give it a copied snippet holding the icon to show after the copy, as the Different icon demo does with a sparkle and a cluster of sparkles. Left empty, the indicator shows a clipboard and then a check.
Mark both icons aria-hidden, because the trigger's label already says what happened.
How do I change the label a screen reader hears on the Clipboard button?
The trigger's aria-label reads Copy to clipboard, then Copied to clipboard while the check shows. To reword or translate both, pass translations to Clipboard with a triggerLabel function, which receives whether the value was just copied and returns the label.
An aria-label set on ClipboardTrigger replaces the automatic one outright, so it no longer changes after the copy.
Does the Clipboard work without HTTPS?
Yes. The copy goes through the browser's Clipboard API, navigator.clipboard.writeText, when the page has it, which means HTTPS or localhost. On a plain HTTP page that API is missing, so it falls back to selecting the text in a temporary element and running the older execCommand copy.
Either way the check appears as soon as the copy is requested. Nothing waits for the browser's answer, so a write it rejects still shows the check.
API Reference
Clipboard
value?string
The value. Pair with onValueChange to control it. Two-way with bind:value.
defaultValue?string
The value it starts with, when you don't control value.
onValueChange?(details: { value: string }) → void
Called with the new value when setValue changes it.
- Type
(details: { value: string }) → void- Parameters
details– { value: string }- Returns
void
ids?{ root?: string; input?: string; label?: string }
Element ids. The input's id names it for the label.
- Type
{ root?: string; input?: string; label?: string }
onStatusChange?(details: { copied: boolean }) → void
Called with { copied: true } each time the value is copied.
- Type
(details: { copied: boolean }) → void- Parameters
details– { copied: boolean }- Returns
void
rootClassName?string
Class names on the root. class is applied to the baked control.
timeout?number
How long it shows as copied, in milliseconds.
- Default
3000
translations?{ triggerLabel?: (copied: boolean) → string }
The trigger's label, copied or not, also read out once it copies.
- Type
{ triggerLabel?: (copied: boolean) → string }
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Forwarded to the part.
ClipboardRootProvider
valueobject
What useClipboard returned.
- Type
{ copied: boolean; copy: () → void; setValue: (value: string) → void; value: string }
rootClassName?string
Class names on the root. class is applied to the baked control.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Forwarded to the part.
ClipboardLabel
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ClipboardTrigger
ClipboardInput
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ClipboardValue
size?"xs" | "sm" | "md" | "lg" | "xl"
The size.
- Type
"xs" | "sm" | "md" | "lg" | "xl"- Default
"md"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ClipboardIndicator
copied?Snippet
What shows once the value is copied.
- Default
<CheckIcon aria-hidden="true" />
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
useClipboard
value?string
The ard's value. Two-way with bind:value.
defaultValue?string
The value it starts with, when you don't control value.
onValueChange?(details: { value: string }) → void
Called with the new value when setValue changes it.
- Type
(details: { value: string }) → void- Parameters
details– { value: string }- Returns
void
ids?{ root?: string; input?: string; label?: string }
Element ids. The input's id names it for the label.
- Type
{ root?: string; input?: string; label?: string }
onStatusChange?(details: { copied: boolean }) → void
Called with { copied: true } each time the value is copied.
- Type
(details: { copied: boolean }) → void- Parameters
details– { copied: boolean }- Returns
void
timeout?number
How long it shows as copied, in milliseconds.
- Default
3000
translations?{ triggerLabel?: (copied: boolean) → string }
The trigger's label, copied or not, also read out once it copies.
- Type
{ triggerLabel?: (copied: boolean) → string }











