Skip to content

Commit 337bd20

Browse files
authored
feat(daffio): add @debounce decorator to DaffioDocsScrollToTopComponent (#3837)
1 parent c823e15 commit 337bd20

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

apps/daffio/src/app/docs/components/scroll-to-top/scroll-to-top.component.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { DOCUMENT } from '@angular/common';
22
import {
33
ChangeDetectionStrategy,
44
Component,
5-
HostListener,
65
Inject,
6+
ChangeDetectorRef,
77
} from '@angular/core';
88
import { FaIconComponent } from '@fortawesome/angular-fontawesome';
99
import { faArrowUp } from '@fortawesome/free-solid-svg-icons';
1010

11+
import { debounce } from '@daffodil/core';
12+
1113
@Component({
1214
selector: 'daffio-docs-scroll-to-top',
1315
templateUrl: './scroll-to-top.component.html',
@@ -16,15 +18,21 @@ import { faArrowUp } from '@fortawesome/free-solid-svg-icons';
1618
imports: [
1719
FaIconComponent,
1820
],
21+
host: {
22+
'(document:scroll)': 'onScroll()',
23+
},
1924
})
2025

2126
export class DaffioDocsScrollToTopComponent {
2227
showButton = false;
2328
faArrowUp = faArrowUp;
2429

25-
constructor(@Inject(DOCUMENT) private document: Document) {}
30+
constructor(
31+
@Inject(DOCUMENT) private document: Document,
32+
private cdr: ChangeDetectorRef,
33+
) {}
2634

27-
@HostListener('document:scroll')
35+
@debounce(100)
2836
onScroll() {
2937
const currentScrollPosition = this.document.documentElement.scrollTop || this.document.body.scrollTop;
3038

@@ -33,10 +41,14 @@ export class DaffioDocsScrollToTopComponent {
3341
} else {
3442
this.showButton = false;
3543
}
44+
45+
this.cdr.markForCheck();
3646
}
3747

3848
scrollToTop() {
3949
this.document.documentElement.scrollTop = 0;
4050
this.document.body.scrollTop = 0;
51+
52+
this.showButton = false;
4153
}
4254
}

0 commit comments

Comments
 (0)