File tree Expand file tree Collapse file tree 1 file changed +23
-2
lines changed
src/app/shared/components/copy-button Expand file tree Collapse file tree 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' ;
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}
You can’t perform that action at this time.
0 commit comments