Skip to content

HyCopyButton

Usage examples on this page are written for Lit / plain HTML (<hy-copy-button>). The same component ships as HyCopyButton in @whitespaceux/harmony-react (native React 19), with typed wrappers for Angular, Solid, Svelte, and Vue. The API reference below applies to all frameworks.

A button that copies a string to the clipboard with built-in feedback.

Copies either a static value or — via from — the resolved value of another element by id (from="tok", from="tok.value", from="link[href]"), runs a rest → success → error feedback machine that auto-reverts after feedback-duration, swaps the icon per state, and surfaces the state label through a built-in hy-tooltip (never title=).

Key Features:

  • value (static) or from (resolve another element by id / property / attribute)
  • rest → success → error machine with auto-revert
  • Per-state icon names + a11y/tooltip labels
  • Optional transform to wrap/trim before copy (e.g. v => \var(${v})``)
  • size / emphasis forwarded to the inner hy-icon-button

Examples

Copy a static value

html
<hy-copy-button value="--hy-background-brand-rest"></hy-copy-button>

Copy the resolved value of another element

html
<code id="tok">#4f46e5</code> <hy-copy-button from="tok"></hy-copy-button>

API

Properties

PropertyAttributeTypeDefaultDescription
valuevaluestring | undefinedStatic string to copy. Ignored when from resolves a value.
fromfromstring | undefinedCopy from another element by id. Forms: id, id.prop, id[attr]. Resolved within the copy button's root (document or shadow root).
feedbackDurationfeedback-durationnumber1000Auto-revert delay (ms) for the success/error state.
copyIconcopy-iconstring'copy'Rest-state icon name.
successIconsuccess-iconstring'check'Success-state icon name.
errorIconerror-iconstring'close'Error-state icon name.
copyLabelcopy-labelstring'Copy'Rest-state a11y + tooltip label.
successLabelsuccess-labelstring'Copied'Success-state a11y + tooltip label.
errorLabelerror-labelstring'Could not copy'Error-state a11y + tooltip label.
disableddisabledbooleanfalseWhether the button is disabled.
sizesize'small' | 'medium' | 'large''medium'Size forwarded to the inner icon-button.
emphasisemphasis'solid' | 'soft' | 'outlined' | 'tinted' | 'plain''plain'Emphasis forwarded to the inner icon-button.
tooltipPlacementtooltip-placement'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'right''top'Tooltip placement.
transform(raw: string) => string | undefinedTransform the resolved value before copying. Property-only (not an attribute).

Events

EventDetailDescription
copyFired after a successful copy. Detail: { value: string }
errorFired when the clipboard write fails (non-bubbling; listen on the element). Detail: {}

CSS Parts

PartDescription
baseThe host wrapper.

CSS Custom Properties

PropertyDescription
--hy-copy-button-success-colorIcon color in the success state.
--hy-copy-button-error-colorIcon color in the error state.

Built with Lit. Documented with VitePress.