Skip to content

Commit 500a7c6

Browse files
committed
fix: Copy Icon is not restoring back from right tick ,when Copied
1 parent 32203b0 commit 500a7c6

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { Component, ElementRef, inject } from '@angular/core';
1+
import {
2+
Component,
3+
ElementRef,
4+
inject,
5+
ChangeDetectorRef,
6+
OnDestroy,
7+
} from '@angular/core';
28

39
@Component({
410
selector: 'app-copy-button',
511
templateUrl: './copy-button.component.html',
612
styleUrls: ['./copy-button.component.scss'],
713
})
8-
export class CopyButtonComponent {
14+
export class CopyButtonComponent implements OnDestroy {
915
public elRef = inject<ElementRef<HTMLElement>>(ElementRef<HTMLElement>);
1016
public copied = false;
17+
public cdr = inject<ChangeDetectorRef>(ChangeDetectorRef);
18+
private revertIconTimeout: ReturnType<typeof setTimeout | null> = null;
1119

1220
onCopy() {
1321
const preRef = this.elRef.nativeElement.querySelector('pre:not(.hide)');
@@ -16,5 +24,16 @@ export class CopyButtonComponent {
1624
}
1725
navigator.clipboard.writeText(preRef.firstChild.textContent);
1826
this.copied = true;
27+
this.cdr.detectChanges();
28+
29+
this.revertIconTimeout = setTimeout(() => {
30+
this.copied = false;
31+
this.cdr.detectChanges();
32+
}, 2000);
33+
}
34+
ngOnDestroy(): void {
35+
if (this.revertIconTimeout) {
36+
clearTimeout(this.revertIconTimeout);
37+
}
1938
}
2039
}

0 commit comments

Comments
 (0)