Skip to content

Instantly share code, notes, and snippets.

@AntonGorelov
Forked from NetanelBasal/copy-44.directive.ts
Created August 9, 2022 16:40
Show Gist options
  • Save AntonGorelov/cde9cb3d7910bc64bc0192eaa572258d to your computer and use it in GitHub Desktop.
Save AntonGorelov/cde9cb3d7910bc64bc0192eaa572258d 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