Copy Button
A button that copies a string to the clipboard and shows "Copied!" when the copy works.
<arc-copy-button>Overview
CopyButton writes its value to the clipboard when clicked. It shows a clipboard icon and the word "Copy". After a successful copy it turns success-colored with a check mark and "Copied!" for 2 seconds, then goes back, and fires arc-copy with the copied value.
It uses navigator.clipboard.writeText, which needs a secure context (HTTPS or localhost). If the write fails, as it can in a non-secure iframe, the error is swallowed and the button doesn't change, so nothing tells the user it failed.
icon-only drops the word and draws a round, borderless 30px button for toolbars, whose check shows for 1.4 seconds. Its accessible name is "Copy to clipboard", or your label (such as "Copy code"), and "Copied" after a copy.
Guidelines
Do
- Put it next to the text it copies, such as a code snippet, URL or API key
- Set `value` to the exact string people expect to paste, not a formatted or truncated version
- Set `label` when several copy buttons share a page, so each name says what it copies
- Serve the page over HTTPS so the Clipboard API is available
- Set `disabled` until the value exists, for example while it loads
Don't
- Don't put it inside another button or interactive element. HTML doesn't allow nested interactive content
- Don't rely on the "Copied!" state alone. Pair it with a toast from `arc-copy` for people who look away
Features
- Clicks are ignored while the "Copied!" state is showing
- `disabled` lowers the opacity and blocks pointer events
Preview
npm install @arclux/arc-ui
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-copy-button value="npm install @arclux/arc-ui"></arc-copy-button>import { CopyButton } from '@arclux/arc-ui-react';
export default function Example() {
return (
<CopyButton value="npm install @arclux/arc-ui" />
);
}<script setup>
import { CopyButton } from '@arclux/arc-ui-vue';
</script>
<template>
<CopyButton value="npm install @arclux/arc-ui" />
</template><script>
import { CopyButton } from '@arclux/arc-ui-svelte';
</script>
<CopyButton value="npm install @arclux/arc-ui" />import { Component } from '@angular/core';
import { CopyButton } from '@arclux/arc-ui-angular';
@Component({
imports: [CopyButton],
template: `
<arc-copy-button value="npm install @arclux/arc-ui"></arc-copy-button>
`,
})
export class MyComponent {}import { CopyButton } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<CopyButton value="npm install @arclux/arc-ui" />
);
}import { CopyButton } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<CopyButton value="npm install @arclux/arc-ui" />
);
}API
valuestring''- The text string to copy to the clipboard when the button is clicked.
labelstring''- Accessible name before copying, such as "Copy code". Defaults to "Copy to clipboard". After copying the name is "Copied".
disabledbooleanfalse- Disables the button, preventing clicks and reducing visual opacity.
iconOnlybooleanfalse- Shows the icon alone in a round, borderless 30px button, for a toolbar or a header bar. The icon turns into a check for 1.4 seconds after copying.
Events
arc-copydetail:{ value: string }- Fired when text is successfully copied to the clipboard.
event.detail.valuecontains the copied string.