Skip to content

Commit 73cfdce

Browse files
authored
Merge pull request #11 from thisissoon/feature/fix-universal
fix(inViewport): Fix app breaking when rendering on universal
2 parents db44a09 + 5b5dd4f commit 73cfdce

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

src/lib/src/in-viewport/in-viewport.directive.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,11 @@ describe('InViewportDirective', () => {
183183
expect(directive.isInViewport).toBeFalsy();
184184
});
185185
});
186+
187+
describe('universal render', () => {
188+
it('should emit event when `inViewport` value changes', () => {
189+
const result = directive.isInElementViewport({ left: 0, right: 1366, top: 0, bottom: 500 }, { getBoundingClientRect: null });
190+
expect(result).toBeFalsy();
191+
});
192+
});
186193
});

src/lib/src/in-viewport/in-viewport.directive.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
7979
/**
8080
* A parent element to listen to scroll events from
8181
*
82-
* @type {HTMLElement}
82+
* @type {*}
8383
* @memberof InViewportDirective
8484
*/
8585
@Input('snInViewportParent')
86-
public parentEl: HTMLElement;
86+
public parentEl: any;
8787
/**
8888
* Returns true if element is in viewport
8989
*
@@ -178,7 +178,7 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
178178
* @memberof InViewportDirective
179179
*/
180180
public calculateInViewportStatus(): void {
181-
const el: HTMLElement = this.el.nativeElement;
181+
const el = this.el.nativeElement;
182182
let inParentViewport = false;
183183
let inWindowViewport = false;
184184

@@ -201,24 +201,30 @@ export class InViewportDirective implements AfterViewInit, OnDestroy {
201201
* Returns true if an element is currently within the `viewport`
202202
*
203203
* @param {Viewport} viewport
204-
* @param {HTMLElement} el
204+
* @param {*} el
205205
* @returns {boolean}
206206
* @memberof InViewportDirective
207207
*/
208-
public isInElementViewport(viewport: Viewport, el: HTMLElement): boolean {
209-
const elBounds = el.getBoundingClientRect();
210-
return (
211-
(
212-
(elBounds.top >= viewport.top) && (elBounds.top <= viewport.bottom) ||
213-
(elBounds.bottom >= viewport.top) && (elBounds.bottom <= viewport.bottom) ||
214-
(elBounds.top <= viewport.top) && (elBounds.bottom >= viewport.bottom)
215-
) &&
216-
(
217-
(elBounds.left >= viewport.left) && (elBounds.left <= viewport.right) ||
218-
(elBounds.right >= viewport.left) && (elBounds.right <= viewport.right) ||
219-
(elBounds.left <= viewport.left && elBounds.right >= viewport.right)
220-
)
221-
);
208+
public isInElementViewport(viewport: Viewport, el: any): boolean {
209+
// Check if `getBoundingClientRect` is a function in case running this code
210+
// in an evironment without the DOM
211+
if (typeof el.getBoundingClientRect === 'function') {
212+
const elBounds = el.getBoundingClientRect();
213+
return (
214+
(
215+
(elBounds.top >= viewport.top) && (elBounds.top <= viewport.bottom) ||
216+
(elBounds.bottom >= viewport.top) && (elBounds.bottom <= viewport.bottom) ||
217+
(elBounds.top <= viewport.top) && (elBounds.bottom >= viewport.bottom)
218+
) &&
219+
(
220+
(elBounds.left >= viewport.left) && (elBounds.left <= viewport.right) ||
221+
(elBounds.right >= viewport.left) && (elBounds.right <= viewport.right) ||
222+
(elBounds.left <= viewport.left && elBounds.right >= viewport.right)
223+
)
224+
);
225+
} else {
226+
return false;
227+
}
222228
}
223229
/**
224230
* trigger `ngUnsubscribe` complete on

0 commit comments

Comments
 (0)