File

src/app/shared/components/copy-token/copy-token.component.ts

Metadata

Index

Properties
Methods
Inputs

Constructor

constructor()

Inputs

value
Required :  true

Methods

Async copyToClipboard
copyToClipboard($event: any, string: string)

Copy token to system clipboard.

Parameters :
Name Type Optional
$event any No
string string No
Returns : any

Properties

clicked
Default value : signal(false)
value
import { Component, input, signal } from '@angular/core';
import { NgIf } from '@angular/common';
import { Clipboard } from '@capacitor/clipboard';
import { timer } from 'rxjs';

import { IonButton, IonIcon } from '@ionic/angular/standalone';

import { addIcons } from 'ionicons';
import { copyOutline, clipboardOutline } from 'ionicons/icons';

import { TokenPipe } from '../../pipes/token.pipe';

@Component({
  selector: 'app-copy-token',
  templateUrl: './copy-token.component.html',
  styleUrls: ['./copy-token.component.scss'],
  standalone: true,
  imports: [IonButton, IonIcon, NgIf, TokenPipe],
})
export class CopyTokenComponent {
  clicked = signal(false);
  value = input.required<string>();

  constructor() {
    addIcons({ copyOutline, clipboardOutline });
  }

  /**
   * Copy token to system clipboard.
   *
   * @param $event
   * @param string
   */
  async copyToClipboard($event: any, string: string) {
    try {
      this.clicked.set(true);
      timer(1000).subscribe(() => this.clicked.set(false));
      await Clipboard.write({ string });
    } catch (error) {
      throw Error(`${error}`);
    }
  }
}
<ion-button
  shape="round"
  color="secondary"
  (click)="copyToClipboard($event, value())"
  *ngIf="!clicked()"
>
  <ion-icon name="copy-outline" slot="start"></ion-icon>
  {{ value() | token }}
</ion-button>

<ion-button
  shape="round"
  fill="outline"
  color="secondary"
  [disabled]="true"
  *ngIf="clicked()"
>
  <ion-icon name="clipboard-outline" slot="start"></ion-icon>
  Copied!
</ion-button>

./copy-token.component.scss

ion-button {
    width: 140px;
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""