Skip to content

Instantly share code, notes, and snippets.

@NetanelBasal
Last active August 9, 2022 16:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NetanelBasal/84cfdb8b9d94426ddb1ba6483b9c03fc to your computer and use it in GitHub Desktop.
Save NetanelBasal/84cfdb8b9d94426ddb1ba6483b9c03fc to your computer and use it in GitHub Desktop.
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