-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Back To Top button (#1180)
* changed button styling to an icon button * fix e2e test concerning scroll behavior smooth Co-authored-by: Stefan Hauke <s.hauke@intershop.de> Co-authored-by: Silke <s.grueber@intershop.de>
- Loading branch information
1 parent
c7d8d00
commit 27f1f27
Showing
13 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<button | ||
type="button" | ||
class="btn btn-primary shadow back-to-top-btn" | ||
[style.display]="isVisible ? 'block' : 'none'" | ||
[title]="'back_to_top.title' | translate" | ||
(click)="jump()" | ||
> | ||
<fa-icon [icon]="['fas', 'angle-up']"></fa-icon> | ||
</button> |
31 changes: 31 additions & 0 deletions
31
src/app/shell/header/back-to-top/back-to-top.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { FaIconComponent } from '@fortawesome/angular-fontawesome'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { MockComponent } from 'ng-mocks'; | ||
|
||
import { BackToTopComponent } from './back-to-top.component'; | ||
|
||
describe('Back To Top Component', () => { | ||
let component: BackToTopComponent; | ||
let fixture: ComponentFixture<BackToTopComponent>; | ||
let element: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [TranslateModule.forRoot()], | ||
declarations: [BackToTopComponent, MockComponent(FaIconComponent)], | ||
}).compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(BackToTopComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(element).toBeTruthy(); | ||
expect(() => fixture.detectChanges()).not.toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { ChangeDetectionStrategy, Component, HostListener } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'ish-back-to-top', | ||
templateUrl: './back-to-top.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class BackToTopComponent { | ||
/** | ||
* @description | ||
* Button will not show if window.scrollY is less than MARGIN_TOP. | ||
* Should be any desired value > 0 | ||
*/ | ||
private readonly MARGIN_TOP = 50; | ||
|
||
/** | ||
* @description | ||
* Button will show if user has scrolled at least SCROLL_MIN upwards. | ||
* If set to 0 the button will show as soon as user scrolls upwards | ||
*/ | ||
private readonly SCROLL_MIN = 50; | ||
|
||
isVisible = false; | ||
private previousOffset = 0; | ||
|
||
jump() { | ||
window.scrollTo(0, 0); | ||
} | ||
|
||
private hide() { | ||
this.isVisible = false; | ||
} | ||
|
||
private show() { | ||
this.isVisible = true; | ||
} | ||
|
||
private updateOffset() { | ||
this.previousOffset = window.scrollY; | ||
} | ||
|
||
@HostListener('window:scroll') onWindowScroll() { | ||
const diff = this.previousOffset - window.scrollY; | ||
|
||
const scrollsDown = diff < 0; | ||
const isAtTop = window.scrollY < this.MARGIN_TOP; | ||
const hasEnoughIntention = diff > this.SCROLL_MIN; | ||
|
||
if (scrollsDown || isAtTop) { | ||
this.updateOffset(); | ||
this.hide(); | ||
} else if (!this.isVisible && !hasEnoughIntention) { | ||
this.hide(); | ||
} else { | ||
this.updateOffset(); | ||
this.show(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ | |
></ish-header-default> | ||
</ng-container> | ||
</ng-container> | ||
|
||
<ish-back-to-top></ish-back-to-top> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// apply scroll-behavior except when running in Cypress context (it breaks a lot of tests) | ||
html:not(.cypress-tests) { | ||
scroll-behavior: smooth; | ||
} | ||
|
||
@keyframes back-to-top-btn-animation { | ||
from { | ||
opacity: 0; | ||
} | ||
|
||
to { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
.back-to-top-btn { | ||
position: fixed; | ||
right: $space-default; | ||
bottom: $space-default; | ||
z-index: $zindex-fixed; | ||
animation: back-to-top-btn-animation 0.15s ease-in-out; | ||
|
||
@media (max-width: $screen-xs-max) { | ||
width: auto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters