1 import { Component, HostListener, Input } from '@angular/core';
3 import { detect } from 'detect-browser';
4 import { ToastrService } from 'ngx-toastr';
6 import { Icons } from '~/app/shared/enum/icons.enum';
9 selector: 'cd-copy-2-clipboard-button',
10 templateUrl: './copy2clipboard-button.component.html',
11 styleUrls: ['./copy2clipboard-button.component.scss']
13 export class Copy2ClipboardButtonComponent {
15 private source: string;
22 constructor(private toastr: ToastrService) {}
24 private getText(): string {
25 const element = document.getElementById(this.source) as HTMLInputElement;
29 @HostListener('click')
32 const browser = detect();
33 const text = this.byId ? this.getText() : this.source;
34 const toastrFn = () => {
35 this.toastr.success('Copied text to the clipboard successfully.');
37 if (['firefox', 'ie', 'ios', 'safari'].includes(browser.name)) {
38 // Various browsers do not support the `Permissions API`.
39 // https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API#Browser_compatibility
40 navigator.clipboard.writeText(text).then(() => toastrFn());
42 // Checking if we have the clipboard-write permission
44 .query({ name: 'clipboard-write' as PermissionName })
45 .then((result: any) => {
46 if (result.state === 'granted' || result.state === 'prompt') {
47 navigator.clipboard.writeText(text).then(() => toastrFn());
52 this.toastr.error('Failed to copy text to the clipboard.');