ComponentAdded in v16

Copy Value

A value with a copy button beside it.

Installation

Import the class, and @use the entry point in your global styles. Nothing warns you when the @use is missing.

import { Component } from '@angular/core';
import { WrCopyValue } from 'ngwr/copy-value';

@Component({ imports: [WrCopyValue] })
export class MyComponent {}

Basic

Takes a string or a number. After a copy the button shows a tick and “Copied” is announced to screen readers.

<wr-copy-value [value]="order.id" />
<wr-copy-value [value]="device.serial" (copied)="onCopied($event)" />
ORD-10424815162342

Empty

null, undefined and '' show emptyLabel and no button. 0 is a value.

<wr-copy-value [value]="null" />
<wr-copy-value [value]="''" emptyLabel="not set" />
-not set0

Long values

A value that does not fit is cut with an ellipsis and keeps the full text in its title. The copy always takes the whole value.

<div style="width: 16rem">
  <wr-copy-value [value]="token" />
</div>
sk_test_9f8b2c41d7e04a6fb3c5a1e2d4f6a8b0c2e4f6a8b0c2e4f6

Wrap

wrap lets the value run over several lines instead.

<div style="width: 16rem">
  <wr-copy-value [value]="address" wrap />
</div>
Building 12, Riverside Business Park, 48 Harbour Road, North Quay

Labels

copyLabel, copiedLabel and emptyLabel override one instance. The copyValue.copy, copyValue.copied and copyValue.empty catalog keys change them all.

<wr-copy-value [value]="order.id" copyLabel="Copy order id" copiedLabel="Order id copied" />
ORD-1042
provideWrI18nStaticLoader({
  en: { copyValue: { copy: 'Copy', copied: 'Copied', empty: '-' } },
});

API

NameDescriptionTypeDefault
valueThe value shown and copied. null, undefined and '' are empty.string | number | null | undefinednull
wrapLet a long value wrap over several lines instead of truncating it.booleanfalse
copyLabelAccessible name and tooltip of the button. Falls back to copyValue.copy, then 'Copy'.string | nullnull
copiedLabelTooltip and announcement after a copy. Falls back to copyValue.copied, then 'Copied'.string | nullnull
emptyLabelWhat stands in for an empty value. Falls back to copyValue.empty, then '-'.string | nullnull
(copied)Emitted with the text that reached the clipboard.string—
(copyFailed)Emitted when the browser refused the write.void—

CSS variables

Set these on the element itself, with a rule more specific than its Declared on selector. In a global stylesheet, put a class of yours on an ancestor it really has: <body> for an overlay panel, which renders outside your markup. In a component stylesheet the selector alone works, but only for an element in that component's own template. A value on a wrapper or :root is shadowed, because ngwr/copy-value declares each default on its own element.

VariableDefaultDeclared on
--wr-copy-value-button-colorvar(--wr-color-on-surface-muted).wr-copy-value
--wr-copy-value-button-copied-colorvar(--wr-color-success).wr-copy-value
--wr-copy-value-button-hover-bgvar(--wr-color-hover).wr-copy-value
--wr-copy-value-button-hover-colorvar(--wr-color-on-surface).wr-copy-value
--wr-copy-value-button-paddingvar(--wr-space-xs).wr-copy-value
--wr-copy-value-button-radiusvar(--wr-border-radius-sm).wr-copy-value
--wr-copy-value-empty-colorvar(--wr-color-on-surface-muted).wr-copy-value
--wr-copy-value-gapvar(--wr-space-xs).wr-copy-value

Dependencies

Built onWrClipboard

See also