Skip to main content
cngx-src documentation

CngxCopyText

DirectivePrimaryv0.1.0WCAG AA

projects/common/interactive/copy/copy-text.directive.ts

Import#

import { CngxCopyText } from '@cngx/common/interactive'

Description#

Clipboard copy behavior - forms-free version for @cngx/common.

Place on a button or any clickable element. Copies the provided text to the clipboard. Shows a copied signal for visual feedback (auto-resets after resetDelay). Falls back to execCommand('copy') when Clipboard API is unavailable.

Unlike CngxCopyValue from @cngx/forms/input, this has no forms dependency and lives at Level 2 (@cngx/common), making it available to any consumer.

Copy a token

<code>{{ apiKey() }}</code>
<button [cngxCopyText]="apiKey()" #cp="cngxCopyText">
  {{ cp.copied() ? 'Copied!' : 'Copy' }}
</button>

Copy with SR announcement

<button [cngxCopyText]="shareUrl()">Copy Link</button>
<span aria-live="polite" class="sr-only">
  {{ cp.copied() ? 'Link copied to clipboard' : '' }}
</span>

https://cngxjs.github.io/cngx/examples/#/common/interactive/copy/text/copy-token https://cngxjs.github.io/cngx/examples/#/common/interactive/copy/text/copy-url-with-sr-announcement

Metadata#

Host#

Relationships

Index#

Inputs#

resetDelay#number
input()

Duration in ms to keep copied true after a successful copy.

default 2000
input()Required

The text to copy to the clipboard.

default { alias: 'cngxCopyText' }

Outputs#

copyFailed#unknown
output()

Emitted when a copy attempt fails.

didCopy#string
output()

Emitted after a successful copy with the copied text.

Instance Properties#

copied#Signal
Readonly

true for resetDelay ms after a successful copy.

this.copiedState.asReadonly()
failed#Signal
Readonly

true when the last copy attempt failed (permission denied, etc.). Resets on next attempt.

this.failedState.asReadonly()
supported#unknown
Readonly

Whether the Clipboard API is available in this environment.

typeof navigator !== 'undefined' && !!navigator.clipboard

Methods#

copy#Promise
Async

Copies the text to the clipboard.

HostListeners#

EventHandler
(click)click()