Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Clipboard for Solid

A copy to clipboard control for Solid and Ark UI apps: a read-only field or plain text, a copy button, and a check that shows for three seconds.

On this page9 sections
UI library: Ark UI

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. Every version of this kit builds it on Ark UI's clipboard.

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.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Clipboard anatomy: root, label, input, trigger, indicator

Usage

import {
  Clipboard, ClipboardIndicator, ClipboardInput, ClipboardTrigger,
} from "@/components/ui/clipboard";
import {
  InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput,
} from "@/components/ui/input-group";
<div class="w-full max-w-sm">
  <Clipboard value="https://example.com">
    <InputGroup>
      <ClipboardInput aria-label="Link"
        asChild={(props) => (
          <InputGroupInput {...props()} />
        )}
      />
      <InputGroupAddon align="inline-end">
        <ClipboardTrigger
          asChild={(props) => (
            <InputGroupButton {...props()} size="icon-xs">
              <ClipboardIndicator />
            </InputGroupButton>
          )}
        />
      </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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

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.

Preview width

FAQ

How do I add a copy to clipboard button in Solid?

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 asChild, 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. The hook returns an accessor, so call it to reach copy and copied, as the Root provider demo does.

How do I change the Clipboard copy icon?

Put the resting icon inside ClipboardIndicator as its children and pass the copied icon as its copied prop, 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

<div> data-slot="clipboard"

PropType
value?string

The controlled value of the clipboard.

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.

onValueChange?(details: ValueChangeDetails) → void

The function to be called when the value changes.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: string }

Returns
void
id?string

The unique identifier of the machine.

ids?object

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

Type
{ root?: string; input?: string; label?: string }
onStatusChange?(details: CopyStatusDetails) → void

The function to be called when the value is copied to the clipboard.

Type
(details: CopyStatusDetails) → void
Parameters

details – { copied: boolean }

Returns
void
rootClassName?string

Class names on the root. class is applied to the baked control.

timeout?number

The timeout for the copy operation.

Default
3000
translations?object

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

Type
{ triggerLabel?: (copied: boolean) → string }
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

AttributeType
data-copied?string

Present when copied state is true.

ClipboardControl

<div> data-slot="clipboard-control"

PropType
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
AttributeType
data-copied?string

Present when copied state is true.

ClipboardTrigger

<button> data-slot="clipboard-trigger"

PropType
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
AttributeType
data-copied?string

Present when copied state is true.

ClipboardIndicator

<div> data-slot="clipboard-indicator"

PropType
copied?JSX.Element

What it shows once the value is copied; its children show before.

Default
<CheckIcon aria-hidden="true" />
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

ClipboardInput

<input>

PropType
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

AttributeType
data-copied?string

Present when copied state is true.

data-readonly"true"

Present when read-only.

Default
"true"

ClipboardLabel

<label> data-slot="clipboard-label"

PropType
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

AttributeType
data-copied?string

Present when copied state is true.

ClipboardValue

<span> data-slot="clipboard-value"

PropType
size?"xs" | "sm" | "md" | "lg" | "xl"

The size.

Type
"xs" | "sm" | "md" | "lg" | "xl"
Default
"md"
asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

ClipboardRootProvider

<div> data-slot="clipboard"

PropType
valueUseClipboardReturn

What useClipboard() returns: the state and API, made outside the component to control it.

rootClassName?string

Class names on the root. class is applied to the baked control.

asChild?(props: ParentProps) => JSX.Element

Render your own element: a function that gets the part's props to spread on it.

Type
(props: ParentProps) => JSX.Element
class?string

Merged with the part's classes.

AttributeType
data-copied?string

Present when copied is true.

useClipboard

PropType
value?string

The controlled value of the clipboard.

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.

onValueChange?(details: ValueChangeDetails) → void

The function to be called when the value changes.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: string }

Returns
void
id?string

The unique identifier of the machine.

ids?object

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

Type
{ root?: string; input?: string; label?: string }
onStatusChange?(details: CopyStatusDetails) → void

The function to be called when the value is copied to the clipboard.

Type
(details: CopyStatusDetails) → void
Parameters

details – { copied: boolean }

Returns
void
timeout?number

The timeout for the copy operation.

Default
3000
translations?object

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

Type
{ triggerLabel?: (copied: boolean) → string }

ClipboardContext

PropType
copiedboolean

Whether the value has been copied to the clipboard.

copyVoidFunction

Copy the value to the clipboard.

setValue(value: string) → void

Set the value to be copied to the clipboard.

Parameters

value – string

Returns
void
valuestring

The value to be copied to the clipboard.