Last active
August 9, 2022 16:40
-
-
Save NetanelBasal/84cfdb8b9d94426ddb1ba6483b9c03fc to your computer and use it in GitHub Desktop.
This file contains hidden or 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