Change smooth-scroll to check last component instead of Route. Page content sticky base on new uikit version

This commit is contained in:
Konstantinos Triantafyllou 2022-03-14 18:35:00 +02:00
parent c49a79c798
commit 4ccabced6d
3 changed files with 163 additions and 160 deletions

View File

@ -6,26 +6,27 @@ declare var UIkit;
@Component({
selector: '[page-content]',
template: `
<div id="page_content" [class.sticky]="sticky">
<div #header id="page_content_header">
<div id="page_content">
<div id="header">
<div #header id="page_content_header" uk-sticky="top: #header; media: @m" [attr.offset]="offset">
<div class="uk-container uk-container-large uk-padding-remove-vertical">
<ng-content select="[header]"></ng-content>
</div>
</div>
<div id="page_content_inner" [class.sticky]="sticky" class="uk-section uk-container uk-container-large">
</div>
<div id="page_content_inner" class="uk-section uk-container uk-container-large">
<ng-content select="[inner]"></ng-content>
</div>
</div>
`
`,
})
export class PageContentComponent implements OnInit, OnDestroy {
private current;
private shouldSticky: boolean = false;
public offset: number;
public sticky: boolean = false;
@Output()
public stickyEmitter: EventEmitter<boolean> = new EventEmitter<boolean>();
@ViewChild("header") public header: ElementRef;
private current;
private subscriptions: any[] = [];
constructor() {
@ -38,35 +39,33 @@ export class PageContentComponent implements OnInit, OnDestroy {
if (scroll > this.current) {
this.offset = 0;
} else {
this.offset = Number.parseInt(getComputedStyle(document.documentElement).getPropertyValue('--structure-header-height') + 1);
this.offset = Number.parseInt(getComputedStyle(document.documentElement).getPropertyValue('--structure-header-height'));
}
if (this.offset !== previous) {
UIkit.sticky(this.header.nativeElement, {
offset: this.offset
});
this.initSticky();
}
this.current = scroll;
if(this.current > this.header.nativeElement.offsetTop + this.header.nativeElement.offsetHeight && this.shouldSticky) {
this.sticky = true;
this.stickyEmitter.emit(this.sticky);
}
}
ngOnInit() {
if (typeof window !== "undefined") {
this.current = window.pageYOffset;
}
}
initSticky() {
this.clear();
this.subscriptions.push(UIkit.util.on(document, 'active', '#page_content_header', (): void => {
this.shouldSticky = true;
this.sticky = true;
this.stickyEmitter.emit(this.sticky);
}));
this.subscriptions.push(UIkit.util.on(document, 'inactive', '#page_content_header', (): void => {
this.sticky = false;
this.shouldSticky = false;
this.stickyEmitter.emit(this.sticky);
}));
}
ngOnDestroy() {
clear() {
this.subscriptions.forEach(subscription => {
if (subscription instanceof Subscription) {
subscription.unsubscribe();
@ -75,4 +74,8 @@ export class PageContentComponent implements OnInit, OnDestroy {
}
});
}
ngOnDestroy() {
this.clear();
}
}

View File

@ -92,18 +92,18 @@
</div>
</div>
</div>
<div class="uk-visible@m">
<div class="uk-navbar-transparent" [ngClass]="portal + '-menu'"
uk-sticky="show-on-up: true" media="@m" cls-active="uk-active uk-navbar-sticky"
cls-inactive="uk-navbar-transparent"
<div id="main-menu" class="uk-visible@m">
<div class="uk-navbar-container" [ngClass]="portal + '-menu'"
uk-sticky="show-on-up: true; top: #main-menu" media="@m" cls-active="uk-active uk-navbar-sticky"
[attr.animation]="(header.stickyAnimation?'uk-animation-slide-top':null)">
<div
*ngIf="(properties.environment =='beta' || properties.environment =='development') && showLogo && header.badge">
<img class="uk-position-top-left"
[src]="'assets/common-assets/'+(properties.environment =='beta'?'beta_flag.svg':'prototype_flag.svg')"
alt="BETA" style="height: 100px; width: 100px; z-index: 1000">
alt="BETA" style="height: 65px; width: 65px; z-index: 1000">
</div>
<nav class="uk-navbar uk-flex uk-navbar-container uk-padding-large uk-padding-remove-vertical" uk-navbar>
<div class="uk-container uk-container-expand">
<nav class="uk-navbar" uk-navbar>
<ng-container *ngIf="!onlyTop">
<div class="uk-navbar-left">
<ng-container *ngIf="showLogo && isHeaderLeft">
@ -235,6 +235,7 @@
</ng-container>
</nav>
</div>
</div>
<!-- New navbar for featured menu items - test only -->
<ng-container *ngIf="featuredMenuItems?.length > 0 && properties.environment == 'development'">
<div class="featuredNavBar">

View File

@ -1,4 +1,4 @@
import {NavigationEnd, Router} from '@angular/router';
import {ActivationStart, NavigationEnd, Router} from '@angular/router';
import {Injectable} from '@angular/core';
import {Subscription} from 'rxjs';
@ -8,17 +8,20 @@ import {Subscription} from 'rxjs';
export class SmoothScroll {
private interval;
private readonly sub;
private lastRoute;
private lastComponent;
private currentComponent: any;
constructor(private router: Router) {
if(typeof window !== "undefined") {
if (typeof window !== "undefined") {
this.sub = router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
if (event instanceof ActivationStart) {
this.currentComponent = event.snapshot.component;
} else if (event instanceof NavigationEnd) {
if (this.interval) {
clearInterval(this.interval);
}
const fragment = router.parseUrl(router.url).fragment;
if (this.lastRoute !== this.getUrl(event.url)) {
if (this.lastComponent !== this.currentComponent) {
window.scrollTo({top: 0});
}
if (fragment) {
@ -49,16 +52,12 @@ export class SmoothScroll {
} else {
window.scrollTo({top: 0, behavior: 'smooth'});
}
this.lastRoute = this.getUrl(event.url);
this.lastComponent = this.currentComponent;
}
});
}
}
private getUrl(url: string) {
return url.split('?')[0].split('#')[0];
}
public clearSubscriptions() {
if (this.sub && this.sub instanceof Subscription) {
this.sub.unsubscribe();