Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ssr): resize observe and animation player errors 17.2.x #15098

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions projects/igniteui-angular/src/lib/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CurrencyPipe, formatDate as _formatDate, isPlatformBrowser } from '@angular/common';
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { mergeWith } from 'lodash-es';
import { Observable } from 'rxjs';
import { NEVER, Observable } from 'rxjs';
import { setImmediate } from './setImmediate';
import { isDevMode } from '@angular/core';

Expand Down Expand Up @@ -238,9 +238,9 @@
export class PlatformUtil {
public isBrowser: boolean = isPlatformBrowser(this.platformId);
public isIOS = this.isBrowser && /iPad|iPhone|iPod/.test(navigator.userAgent) && !('MSStream' in window);
public isSafari = this.isBrowser && /Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent);

Check warning on line 241 in projects/igniteui-angular/src/lib/core/utils.ts

View workflow job for this annotation

GitHub Actions / run-tests (18.x)

Unnecessary escape character: \/

Check warning on line 241 in projects/igniteui-angular/src/lib/core/utils.ts

View workflow job for this annotation

GitHub Actions / run-tests (20.x)

Unnecessary escape character: \/
public isFirefox = this.isBrowser && /Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent);

Check warning on line 242 in projects/igniteui-angular/src/lib/core/utils.ts

View workflow job for this annotation

GitHub Actions / run-tests (18.x)

Unnecessary escape character: \/

Check warning on line 242 in projects/igniteui-angular/src/lib/core/utils.ts

View workflow job for this annotation

GitHub Actions / run-tests (20.x)

Unnecessary escape character: \/
public isEdge = this.isBrowser && /Edge[\/\s](\d+\.\d+)/.test(navigator.userAgent);

Check warning on line 243 in projects/igniteui-angular/src/lib/core/utils.ts

View workflow job for this annotation

GitHub Actions / run-tests (18.x)

Unnecessary escape character: \/

Check warning on line 243 in projects/igniteui-angular/src/lib/core/utils.ts

View workflow job for this annotation

GitHub Actions / run-tests (20.x)

Unnecessary escape character: \/
public isChromium = this.isBrowser && (/Chrom|e?ium/g.test(navigator.userAgent) ||
/Google Inc/g.test(navigator.vendor)) && !/Edge/g.test(navigator.userAgent);
public browserVersion = this.isBrowser ? parseFloat(navigator.userAgent.match(/Version\/([\d.]+)/)?.at(1)) : 0;
Expand Down Expand Up @@ -439,14 +439,23 @@
* Run the resizeObservable outside angular zone, because it patches the MutationObserver which causes an infinite loop.
* Related issue: https://github.com/angular/angular/issues/31712
*/
export const resizeObservable = (target: HTMLElement): Observable<ResizeObserverEntry[]> => new Observable((observer) => {
const instance = new (getResizeObserver())((entries: ResizeObserverEntry[]) => {
observer.next(entries);
});
instance.observe(target);
const unsubscribe = () => instance.disconnect();
return unsubscribe;
});
export const resizeObservable = (target: HTMLElement): Observable<ResizeObserverEntry[]> => {
const resizeObserver = getResizeObserver();
// check whether we are on server env or client env
if (resizeObserver) {
return new Observable((observer) => {
const instance = new resizeObserver((entries: ResizeObserverEntry[]) => {
observer.next(entries);
});
instance.observe(target);
const unsubscribe = () => instance.disconnect();
return unsubscribe;
});
} else {
// if on a server env return a empty observable that does not complete immediately
return NEVER;
}
}

/**
* @hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,10 @@ export class IgxForOfDirective<T, U extends T[] = T[]> extends IgxForOfToken<T,U
public ngAfterViewInit(): void {
if (this.igxForScrollOrientation === 'vertical') {
this._zone.runOutsideAngular(() => {
this.contentObserver = new (getResizeObserver())(() => this.contentResizeNotify.next());
this.contentObserver.observe(this.dc.instance._viewContainer.element.nativeElement);
if (this.platformUtil.isBrowser) {
this.contentObserver = new (getResizeObserver())(() => this.contentResizeNotify.next());
this.contentObserver.observe(this.dc.instance._viewContainer.element.nativeElement);
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class IgxAngularAnimationPlayer implements AnimationPlayer {
// To workaround this we are getting the positions from the inner player.
// This is logged in Angular here - https://github.com/angular/angular/issues/18891
// As soon as this is resolved we can remove this hack
this._innerPlayer = innerRenderer.engine.players[innerRenderer.engine.players.length - 1];
const rendererEngine = innerRenderer.engine || innerRenderer.delegate.engine;
// A workaround because of Angular SSR is using some delegation.
this._innerPlayer = rendererEngine.players[rendererEngine.players.length - 1];
}

public init(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ export class IgxTabHeaderComponent extends IgxTabHeaderDirective implements Afte
/** @hidden @internal */
public ngAfterViewInit(): void {
this.ngZone.runOutsideAngular(() => {
this._resizeObserver = new (getResizeObserver())(() => {
this.tabs.realignSelectedIndicator();
});
this._resizeObserver.observe(this.nativeElement);
if (this.platform.isBrowser) {
this._resizeObserver = new (getResizeObserver())(() => {
this.tabs.realignSelectedIndicator();
});
this._resizeObserver.observe(this.nativeElement);
}
});
}

Expand Down
17 changes: 10 additions & 7 deletions projects/igniteui-angular/src/lib/tabs/tabs/tabs.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AfterViewInit, ChangeDetectorRef, Component, ElementRef, HostBinding, Inject, Input, NgZone, OnDestroy, ViewChild } from '@angular/core';
import { getResizeObserver, mkenum } from '../../core/utils';
import { getResizeObserver, mkenum, PlatformUtil } from '../../core/utils';
import { IgxAngularAnimationService } from '../../services/animation/angular-animation-service';
import { AnimationService } from '../../services/animation/animation';
import { IgxDirectionality } from '../../services/direction/directionality';
Expand Down Expand Up @@ -129,6 +129,7 @@ export class IgxTabsComponent extends IgxTabsDirective implements AfterViewInit,
@Inject(IgxAngularAnimationService) animationService: AnimationService,
cdr: ChangeDetectorRef,
private ngZone: NgZone,
private platform: PlatformUtil,
dir: IgxDirectionality) {
super(animationService, cdr, dir);
}
Expand All @@ -139,12 +140,14 @@ export class IgxTabsComponent extends IgxTabsDirective implements AfterViewInit,
super.ngAfterViewInit();

this.ngZone.runOutsideAngular(() => {
this._resizeObserver = new (getResizeObserver())(() => {
this.updateScrollButtons();
this.realignSelectedIndicator();
});
this._resizeObserver.observe(this.headerContainer.nativeElement);
this._resizeObserver.observe(this.viewPort.nativeElement);
if (this.platform.isBrowser) {
this._resizeObserver = new (getResizeObserver())(() => {
this.updateScrollButtons();
this.realignSelectedIndicator();
});
this._resizeObserver.observe(this.headerContainer.nativeElement);
this._resizeObserver.observe(this.viewPort.nativeElement);
}
});
}

Expand Down
Loading