Skip to content

Commit 039162a

Browse files
authoredJan 15, 2025
Merge pull request #3178 from aliahmadCode/copy_button_toggle
fix: Copy Icon is not restoring back from right tick ,when Copied
2 parents 32203b0 + cd8f6ab commit 039162a

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-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,18 @@ 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+
this.revertIconTimeout = null;
33+
}, 2000);
34+
}
35+
36+
ngOnDestroy(): void {
37+
if (this.revertIconTimeout) {
38+
clearTimeout(this.revertIconTimeout);
39+
}
1940
}
2041
}

0 commit comments

Comments
 (0)