@molecule/app-copy-link-field-angular

Feature · copy-link-field · App (browser) · v1.2.2 · Apache-2.0

Read-only input + copy-to-clipboard button for share links, API keys, webhook URLs

npm install @molecule/app-copy-link-field-angular

npm · Source on GitHub

How it works

@molecule/app-copy-link-field-angular is a ready-made copy-link-field feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

import { Component } from '@angular/core'
import { CopyLinkField } from '@molecule/app-copy-link-field-angular'

@Component({
  selector: 'invite-card',
  standalone: true,
  imports: [CopyLinkField],
  template: `
    <div
      mol-copy-link-field
      [value]="'https://example.com/invite/abc123'"
      [label]="'Share link'"
      [feedbackMs]="2000"
      (copy)="onCopied()"
    ></div>
  `,
})
export class InviteCard {
  onCopied(): void {
    console.log('copied')
  }
}

Works with: @molecule/app-angular, @molecule/app-clipboard, @molecule/app-ui, @molecule/app-ui-angular

Reference

Angular copy-link field.

Exports <CopyLinkField> — read-only input + copy-to-clipboard button with "Copied!" feedback. Angular counterpart of @molecule/app-copy-link-field-react: the same props (React names), the same DOM for the same input, the same ClassMap tokens and i18n keys.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

import { Component } from '@angular/core'
import { CopyLinkField } from '@molecule/app-copy-link-field-angular'

@Component({
  selector: 'invite-card',
  standalone: true,
  imports: [CopyLinkField],
  template: `
    <div
      mol-copy-link-field
      [value]="'https://example.com/invite/abc123'"
      [label]="'Share link'"
      [feedbackMs]="2000"
      (copy)="onCopied()"
    ></div>
  `,
})
export class InviteCard {
  onCopied(): void {
    console.log('copied')
  }
}

Type

feature

Installation

npm install @molecule/app-copy-link-field-angular @angular/common @angular/core @angular/platform-browser @molecule/app-angular @molecule/app-clipboard @molecule/app-ui @molecule/app-ui-angular

API

Interfaces

CopyLinkFieldProps

Props for CopyLinkField — the same props @molecule/app-copy-link-field-react accepts. onCopy is an Angular output (copy) in this counterpart: bind it in the host template as (copy)="onCopy()".

interface CopyLinkFieldProps {
  /** Value to display (and copy on click). */
  value: string
  /** Optional label above the field. */
  label?: string
  /** Called after a successful copy (the `copy` output). */
  onCopy?: () => void
  /** How long the "Copied!" state lingers (ms). Defaults to 1500. */
  feedbackMs?: number
  /** Size of the copy button. Defaults to `'sm'`. */
  size?: 'sm' | 'md'
  /** Extra classes. */
  className?: string
}

Classes

CopyLinkField

CopyLinkField component.

Read-only input + copy-to-clipboard button. Common in share-link panels, API-key cards, webhook URLs. The host IS the outer <div> (div[mol-copy-link-field]), matching React's root exactly. Styling routes through ClassMap tokens (cm.stack(1), cm.flex, cm.input(), cm.flex1).

The copy button copies through @molecule/app-clipboard (copyTextWithFallback — the bonded clipboard provider first, then a legacy copy fallback — which resolves false instead of throwing), so it works on insecure (non-HTTPS) contexts too; the copied feedback shows and the copy output fires only when the copy succeeded. The input is read-only and selects its content on focus, so manual Ctrl/Cmd+C still works as the fallback. Labels use copyLink.* i18n keys, read through the binding's reactive useTranslation(), so a locale switch refreshes the text (React's components re-render through useTranslation()).

@Component({
  selector: 'div[mol-copy-link-field]',
  standalone: true,
  imports: [Button],
  inputs: ['value', 'label', 'feedbackMs', 'size', 'className'],
  host: {
    '[class]': 'classes()',
  },
  template: `@if (label) {
      <span [class]="labelClasses()">{{ label }}</span>
    }
    <div [class]="rowClasses()">
      <input
        readonly
        type="text"
        [attr.value]="valueText()"
        (focus)="onFieldFocus($event)"
        [class]="inputClasses()"
        [attr.aria-label]="ariaLabel()"
      />
      <button mol-button [variant]="'solid'" [size]="size ?? 'sm'" (click)="copyValue()">
        {{ buttonLabel() }}
      </button>
    </div>`,
})
export class CopyLinkField implements DoCheck, OnDestroy {
  /** Value to display (and copy on click). */
  value?: string
  /** Optional label above the field. */
  label?: string
  /** How long the "Copied!" state lingers (ms; defaults to 1500 at read time). */
  feedbackMs?: number
  /** Size of the copy button (defaults to `'sm'` at read time). */
  size?: 'sm' | 'md'
  /** Extra classes. */
  className?: string

  /** Fires after a successful copy (React's `onCopy`). */
  @Output() readonly copy = new EventEmitter<void>()

  /** The element ref of the host (the outer div). */
  private readonly el = inject(ElementRef)

  /**
   * Reactive translation, read in the injection context. Its `t` reads the
   * locale, so the view re-renders the text on a locale switch (the static `t`
   * does not).
   */
  private readonly i18n = useTranslation()

  /** Whether the last copy succeeded ("Copied!" is showing). */
  private readonly copied = signal(false)

  /** The pending feedback reset timer, cleared when the component dies. */
  private timer?: ReturnType<typeof setTimeout>

  /**
   * The current class map.
   * @returns The UIClassMap.
   */
  cm(): UIClassMap {
    return getClassMap()
  }

  /**
   * The wrapper class list (React's `cm.cn(cm.stack(1), className)`).
   * @returns The class string.
   */
  classes(): string {
    return this.cm().cn(this.cm().stack(1), this.className)
  }

  /**
   * The label span's class list (React's
   * `cm.cn(cm.textSize('xs'), cm.fontWeight('semibold'))`).
   * @returns The class string.
   */
  labelClasses(): string {
    return this.cm().cn(this.cm().textSize('xs'), this.cm().fontWeight('semibold'))
  }

  /**
   * The input row's class (React's `cm.flex({ align: 'center', gap: 'sm' })`).
   * @returns The class string.
   */
  rowClasses(): string {
    return this.cm().flex({ align: 'center', gap: 'sm' })
  }

  /**
   * The input's class list (React's `cm.cn(cm.input(), cm.flex1)`).
   * @returns The class string.
   */
  inputClasses(): string {
    return this.cm().cn(this.cm().input(), this.cm().flex1)
  }

  /**
   * The displayed value (React's required `value` prop, `''` until bound).
   * @returns The value text.
   */
  valueText(): string {
    return this.value ?? ''
  }

  /**
   * The input's accessible label (React's `label ?? t('copyLink.field', …)`).
   * @returns The label text.
   */
  ariaLabel(): string {
    return this.label ?? this.i18n.t('copyLink.field', undefined, { defaultValue: 'Link' })
  }

  /**
   * The copy button's text (React's `t('copyLink.copied'|'copyLink.copy', …)`),
   * following the copied state and the locale.
   * @returns The button text.
   */
  buttonLabel(): string {
    return this.copied()
      ? this.i18n.t('copyLink.copied', undefined, { defaultValue: 'Copied!' })
      : this.i18n.t('copyLink.copy', undefined, { defaultValue: 'Copy' })
  }

  /**
   * Selects the input's whole content on focus (React's
   * `onFocus={(e) => e.target.select()}`), so manual Ctrl/Cmd+C works.
   *
   * @param event - The native focus event.
   */
  onFieldFocus(event: FocusEvent): void {
    ;(event.target as HTMLInputElement).select()
  }

  /**
   * Copies the value, then shows the "Copied!" feedback for `feedbackMs` and
   * fires the `copy` output — React's `copy` callback. Nothing happens when
   * the copy reports failure.
   */
  copyValue(): void {
    void copyTextWithFallback(this.valueText()).then((copiedOk) => {
      if (!copiedOk) return
      this.copied.set(true)
      this.copy.emit()
      this.timer = setTimeout(() => this.copied.set(false), this.feedbackMs ?? 1500)
    })
  }

  /**
   * React's controlled input: the live value always follows the `value` prop
   * (the `[attr.value]` binding writes the attribute; the property is synced
   * here so both reads of the field agree after a re-render).
   */
  ngDoCheck(): void {
    const input = (this.el.nativeElement as HTMLElement).querySelector(
      'input',
    ) as HTMLInputElement | null
    const text = this.valueText()
    if (input && input.value !== text) input.value = text
  }

  /** Detaches the feedback timer with the component. */
  ngOnDestroy(): void {
    clearTimeout(this.timer)
  }
}

Injection Notes

Requirements

Peer dependencies:

  • @angular/common ^22.0.0
  • @angular/core ^22.0.0
  • @angular/platform-browser ^22.0.0
  • @molecule/app-angular ^3.6.0 || ^4.0.0
  • @molecule/app-clipboard ^1.0.1
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-angular ^1.1.0

Runtime Dependencies

  • @angular/common
  • @angular/core
  • @angular/platform-browser
  • @molecule/app-angular
  • @molecule/app-clipboard
  • @molecule/app-ui
  • @molecule/app-ui-angular

The copy button copies through @molecule/app-clipboard (copyTextWithFallback — the bonded clipboard provider first, then a legacy copy fallback — which resolves false instead of throwing), so it works on insecure (non-HTTPS) contexts too; the copied feedback shows and the copy output fires only when the copy succeeded. The input is read-only and selects its content on focus, so manual Ctrl/Cmd+C still works as the fallback. Labels use copyLink.* i18n keys (companion bond: @molecule/app-locales-copy-link-field), read through the binding's reactive useTranslation(), so a locale switch refreshes the text. Styling routes through ClassMap tokens (cm.stack(1), cm.flex, cm.input(), cm.flex1) and requires a bonded ClassMap (e.g. @molecule/app-ui-tailwind).

Translations

Translation strings are provided by @molecule/app-locales-copy-link-field.