Skip to content

Commit 9d8c53e

Browse files
Merge pull request #57 from DongreJaipal/my-fix-branch
feat(sitewide): add scroll to top button to conditionally appear in longer pages
2 parents e572d7c + 6c2f3d5 commit 9d8c53e

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

angular-primeng-app/src/app/app.component.html

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
<router-outlet />
44
</div>
55
<app-footer />
6+
@if (showScrollButton) {
7+
<p-button class="pageUpButton" (click)="scrollToTop()" icon="pi pi-angle-up" [rounded]="true" size="small"></p-button>
8+
}

angular-primeng-app/src/app/app.component.scss

+7
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
min-height: 74vh;
66
width: 100%;
77
}
8+
9+
.pageUpButton {
10+
position: fixed;
11+
bottom: 10px;
12+
right: 14px;
13+
}
14+

angular-primeng-app/src/app/app.component.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { Component, OnInit, OnDestroy, inject, Inject } from "@angular/core";
1+
import { Component, OnInit, OnDestroy, inject, Inject, HostListener } from "@angular/core";
22
import { RouterOutlet } from "@angular/router";
33
import { Subscription } from "rxjs";
44
import { BlogInfo } from "./models/blog-info";
55
import { BlogService } from "./services/blog.service";
66
import { ThemeService } from "./services/theme.service";
7-
import { DOCUMENT } from "@angular/common";
7+
import { DOCUMENT, ViewportScroller } from "@angular/common";
88
import { HeaderComponent } from "./components/header/header.component";
99
import { FooterComponent } from "./components/footer/footer.component";
10+
import { ButtonModule } from "primeng/button";
1011

1112
@Component({
1213
selector: "app-root",
1314
standalone: true,
14-
imports: [RouterOutlet, HeaderComponent, FooterComponent],
15+
imports: [RouterOutlet, HeaderComponent, FooterComponent, ButtonModule],
1516
templateUrl: "./app.component.html",
1617
styleUrl: "./app.component.scss",
1718
})
@@ -23,8 +24,10 @@ export class AppComponent implements OnInit, OnDestroy {
2324
themeService: ThemeService = inject(ThemeService);
2425
blogService: BlogService = inject(BlogService);
2526
private querySubscription?: Subscription;
27+
showScrollButton: boolean = false;
28+
private readonly scroller = inject(ViewportScroller);
2629

27-
constructor(@Inject(DOCUMENT) private document: Document) {}
30+
constructor(@Inject(DOCUMENT) private document: Document) { }
2831

2932
ngOnInit(): void {
3033
this.blogURL = this.blogService.getBlogURL();
@@ -52,6 +55,20 @@ export class AppComponent implements OnInit, OnDestroy {
5255
});
5356
}
5457

58+
@HostListener('window:scroll', [])
59+
onWindowScroll() {
60+
const yOffset = window.pageYOffset || document.documentElement.scrollTop;
61+
if (yOffset > 300) {
62+
this.showScrollButton = true;
63+
} else {
64+
this.showScrollButton = false;
65+
}
66+
}
67+
68+
scrollToTop() {
69+
this.scroller.scrollToPosition([0, 0]);
70+
}
71+
5572
ngOnDestroy(): void {
5673
this.querySubscription?.unsubscribe();
5774
}

0 commit comments

Comments
 (0)