File tree 1 file changed +23
-2
lines changed
src/app/shared/components/copy-button
1 file changed +23
-2
lines changed Original file line number Diff line number Diff line change 1
- import { Component , ElementRef , inject } from '@angular/core' ;
1
+ import {
2
+ Component ,
3
+ ElementRef ,
4
+ inject ,
5
+ ChangeDetectorRef ,
6
+ OnDestroy ,
7
+ } from '@angular/core' ;
2
8
3
9
@Component ( {
4
10
selector : 'app-copy-button' ,
5
11
templateUrl : './copy-button.component.html' ,
6
12
styleUrls : [ './copy-button.component.scss' ] ,
7
13
} )
8
- export class CopyButtonComponent {
14
+ export class CopyButtonComponent implements OnDestroy {
9
15
public elRef = inject < ElementRef < HTMLElement > > ( ElementRef < HTMLElement > ) ;
10
16
public copied = false ;
17
+ public cdr = inject < ChangeDetectorRef > ( ChangeDetectorRef ) ;
18
+ private revertIconTimeout : ReturnType < typeof setTimeout | null > = null ;
11
19
12
20
onCopy ( ) {
13
21
const preRef = this . elRef . nativeElement . querySelector ( 'pre:not(.hide)' ) ;
@@ -16,5 +24,18 @@ export class CopyButtonComponent {
16
24
}
17
25
navigator . clipboard . writeText ( preRef . firstChild . textContent ) ;
18
26
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
+ }
19
40
}
20
41
}
You can’t perform that action at this time.
0 commit comments