<cs-copy-button>
StableActionsSince 0.1

Copy buttons copy text to the clipboard when the user activates them. They provide built-in success and error feedback so users know the copy worked.

Copying requires a secure context.
Copy buttons use the browser’s clipboard.writeText() method, which requires a secure context (HTTPS) in most browsers.

Examples

Copying from Other Elements

Set the value attribute to copy a literal string, or point the from attribute at another element’s id to copy live content. When both are present, from wins.

By default from copies the target’s textContent. Add a modifier to copy an attribute or property instead:

SyntaxCopiesExample
from="id"The element’s textContentfrom="my-phone"
from="id[attr]"The named attributefrom="my-link[href]"
from="id.prop"The named propertyfrom="my-input.value"
+1 (234) 456-7890

Custom Labels

The copy button shows a tooltip on hover and focus, then briefly swaps it to confirm a copy. Set the copy-label, success-label, and error-label attributes to customize the text for each state. copy-label also serves as the button’s accessible name.

Custom Icons

Use the copy-icon, success-icon, and error-icon slots to replace the icon shown in each state. <cs-icon> works best, but any image will do.

Custom Trigger

By default the copy button renders an icon-only button. Slot in any clickable element to use as the trigger instead — a Cornerstone button, a native button, or anything else.

Copy to Clipboard

Custom triggers get the same feedback with no extra wiring.
They receive the same tooltip and copy feedback as the default trigger; the icon swap is the one piece unique to it. Set tooltip="none" to opt out of the tooltip, and listen for the cs-copy and cs-error events or style the :state(success) and :state(error) custom states for your own feedback.

Disabled

Add the disabled attribute to turn off the copy button.

Handling Errors

A copy fails when value is empty, when from points to an id that doesn’t exist, or when the browser rejects the operation. Either way, the button shows its error state and emits the cs-error event. Customize the message with error-label and the icon with the error-icon slot.

Feedback Duration

After a copy, the tooltip briefly shows the success or error label. Set the feedback-duration attribute (in milliseconds) to control how long it stays visible.

Tooltip Mode

The tooltip attribute controls when the built-in tooltip appears, on both the default and custom triggers.

ValueBehavior
full defaultShows on hover and focus, and reused for copy feedback
copyStays silent on hover and focus; appears only to confirm a copy
noneNever shown

Tooltip Placement

The tooltip sits above the trigger by default. Set the tooltip-placement attribute to top, right, bottom, or left to move it.

Customizing

Style the button through its CSS parts — button, copy-icon, success-icon, and error-icon — to match your design.

API

Importing

If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick this component, use one of the following snippets.

npm Self-Hosted React
import '@cruglobal/cornerstone-components/components/copy-button/copy-button.js';
import './cornerstone/components/copy-button/copy-button.js';
import CsCopyButton from '@cruglobal/cornerstone-components/react/copy-button/index.js';

Slots

Learn more about using slots.

NameDescription
(default)The trigger element. By default, a copy icon button is rendered so this is optional. If desired, you can slot in a custom element such as <cs-button> or <button>.
copy-iconThe icon to show in the default copy state. Works best with <cs-icon>.
error-iconThe icon to show when a copy error occurs. Works best with <cs-icon>.
success-iconThe icon to show when the content is copied. Works best with <cs-icon>.

Attributes & Properties

Learn more about attributes and properties.

PropertyAttributeDescriptionTypeDefaultReflects
copyLabelcopy-labelA custom label to use as the accessible name and tooltip text in the default copy state.string''
disableddisabledDisables the copy button.booleanfalseYes
errorLabelerror-labelA custom label to show in the tooltip when a copy error occurs.string''
feedbackDurationfeedback-durationThe length of time to show feedback before restoring the default trigger.number1000
fromfromAn id that references an element in the same document from which data will be copied. If both this and value are present, this value will take precedence. By default, the target element's textContent will be copied. To copy an attribute, append the attribute name wrapped in square brackets, e.g. from="el[value]". To copy a property, append a dot and the property name, e.g. from="el.value".string''
successLabelsuccess-labelA custom label to show in the tooltip after copying.string''
tooltiptooltipControls the built-in tooltip. full (default) shows the tooltip on hover and focus and during copy feedback. copy keeps the tooltip silent on hover/focus and only shows it briefly to confirm a successful or failed copy. none disables the tooltip entirely. Applies to both the default and custom triggers.'full' | 'copy' | 'none''full'Yes
tooltipPlacementtooltip-placementThe preferred placement of the tooltip.'top' | 'right' | 'bottom' | 'left''top'Yes
valuevalueThe text value to copy.string''

Events

Learn more about events.

NameDescription
cs-copyEmitted when the data has been copied.
cs-errorEmitted when the data could not be copied.

Custom States

Learn more about custom states.

NameDescriptionCSS selector
errorApplied when the copy operation fails.:state(error)
successApplied when the copy operation succeeds.:state(success)

CSS Parts

Learn more about CSS parts.

NameDescriptionCSS selector
buttonThe internal <button> element.::part(button)
copy-iconThe container that holds the copy icon.::part(copy-icon)
error-iconThe container that holds the error icon.::part(error-icon)
feedbackThe internal <cs-tooltip> element.::part(feedback)
success-iconThe container that holds the success icon.::part(success-icon)

Dependencies

This component automatically imports the following elements. Sub-dependencies, if any exist, are included in this list.