Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Copy Button for Astro

Copy text with an Astro button or bind clipboard behavior to your own button.

bun run build

Installation

bunx --bun shadcn@latest add @gnostic/copy-button

Usage

---
import { CopyButton } from "@/components/ui/copy-button"
---

<CopyButton value="bun run build" />

The button uses the shared Button styles. Its defaults are variant="ghost" and size="icon". Position it with a caller class or a parent layout. The copied state starts after a successful clipboard write and lasts 2000 ms. Set timeout={0} to keep that state until the controller is reset or destroyed. Empty values do nothing. Whitespace and final newlines are copied exactly.

Examples

Custom content

Use the default slot for other icons or text. The button’s data-copied attribute controls the state. The group/copy marker lets child elements respond to it. Native button attributes, including disabled, pass through.

Optional tooltip

CopyButton has no tooltip by default. Add Tooltip when you want a visible hint on hover or focus. Set data-slot="tooltip-trigger" on CopyButton to use its button as the trigger.

---
import { CopyButton } from "@/components/ui/copy-button"
import { Tooltip, TooltipContent } from "@/components/ui/tooltip"
---

<Tooltip>
  <CopyButton
    value="bun run build"
    aria-label="Copy command"
    data-slot="tooltip-trigger"
  />
  <TooltipContent>Copy command</TooltipContent>
</Tooltip>

Change the value

The button reads its current data-value when clicked. Set it in Astro with value or data-value. In a browser script, assign text to button.dataset.value.

Copy text from a URL

This example fetches the Markdown guide, sets button.dataset.value to the response text, and enables the button. Click the copy button to copy the guide.

Bind a native button

The Astro component handles setup and cleanup. To use the clipboard behavior on your own button, pass the button to createCopyToClipboard.

import { createCopyToClipboard } from "@/components/ui/copy-button/copy-to-clipboard"

const button = document.querySelector<HTMLButtonElement>("#copy-command")!
button.dataset.value = "bun run build"
const clipboard = createCopyToClipboard(button)

// Before removing the button:
clipboard.destroy()

A second call for the same button returns its controller. Use copy(value?) from a user action to copy text directly. It returns a promise with true after a successful write. Use isCopied to read the state and reset() to clear it. The options include value, timeout, onCopy, onError, and onStateChange.

To bind a group of native buttons, mark each with data-copy-control and call create(container). It returns the new controllers. Call destroy() on each before removing those buttons.

Accessibility and browser support

The component includes an accessible button label and a polite status message. Icon buttons use aria-label for their accessible name. A tooltip is optional and does not replace that label. Failed writes show failure feedback and do not show the check icon. Clipboard access requires HTTPS or localhost and browser permission. The browser module reports an unavailable API through onError.

Design source

Adapted from the Coss CopyButton and useCopyToClipboard source. The Astro port uses native events, the shared Button component, and data-copied for icon changes. It does not require a React hook.

API Reference

CopyButtonProps

PropType
value?string

Initial text for the button's data-value attribute.

Typestring
timeout?number

Copied-state duration in milliseconds. Zero keeps it until reset.

Typenumber
Default2000

Read the installed source for the complete prop types.

Theme and layout

Copy Button uses the shared Button styles. Use its variant and size props for the control, and place it with a parent layout. Keep the success and error feedback visible when you supply custom content. For a full code sample with copy controls, use Code Block.