-
-
Save AntonGorelov/cde9cb3d7910bc64bc0192eaa572258d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Directive, ElementRef, Input, NgZone } from '@angular/core'; | |
import { HotToastService } from '@ngneat/hot-toast'; | |
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; | |
import { fromEvent, switchMap } from 'rxjs'; | |
@UntilDestroy() | |
@Directive({ | |
selector: '[copy]' | |
}) | |
export class CopyDirective { | |
@Input() copy: string; | |
constructor( | |
private host: ElementRef<HTMLElement>, | |
private zone: NgZone, | |
private toast: HotToastService | |
) { | |
} | |
ngOnInit() { | |
this.zone.runOutsideAngular(() => { | |
fromEvent(this.host.nativeElement, 'click').pipe( | |
switchMap(() => navigator.clipboard.writeText(this.copy)), | |
untilDestroyed(this) | |
).subscribe(() => this.toast.success('Copied!')) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment