From 90c3747edfa66fe81f93a949056b570f95c83d25 Mon Sep 17 00:00:00 2001 From: Markus Block Date: Wed, 25 Sep 2024 10:28:52 +0200 Subject: [PATCH] Added PlatformBrowser check for IntersectionObserver to support SSR --- package.json | 2 +- src/lib/ngx-globe.component.ts | 48 +++++++++++++++++----------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index b4dffda..29cfb11 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@omnedia/ngx-globe", "description": "A simple component library to create a container with an animated and interactive globe.", - "version": "1.0.2", + "version": "1.0.3", "peerDependencies": { "@angular/common": "^18.2.0", "@angular/core": "^18.2.0", diff --git a/src/lib/ngx-globe.component.ts b/src/lib/ngx-globe.component.ts index 3f5432f..7c35d3e 100644 --- a/src/lib/ngx-globe.component.ts +++ b/src/lib/ngx-globe.component.ts @@ -1,15 +1,8 @@ -import { CommonModule } from "@angular/common"; -import { - AfterViewInit, - Component, - ElementRef, - Input, - OnDestroy, - ViewChild, -} from "@angular/core"; +import {CommonModule, isPlatformBrowser} from "@angular/common"; +import {AfterViewInit, Component, ElementRef, Inject, Input, OnDestroy, PLATFORM_ID, ViewChild,} from "@angular/core"; import createGlobe from "cobe"; import Phenomenon from "phenomenon"; -import { COBEOptionsPart, GlobeOptions } from "./ngx-globe.types"; +import {COBEOptionsPart, GlobeOptions} from "./ngx-globe.types"; @Component({ selector: "om-globe", @@ -74,16 +67,16 @@ export class NgxGlobeComponent implements AfterViewInit, OnDestroy { glowColor: [1, 1, 1], offset: [0, 0], markers: [ - { location: [14.5995, 120.9842], size: 0.03 }, - { location: [19.076, 72.8777], size: 0.1 }, - { location: [23.8103, 90.4125], size: 0.05 }, - { location: [30.0444, 31.2357], size: 0.07 }, - { location: [39.9042, 116.4074], size: 0.08 }, - { location: [-23.5505, -46.6333], size: 0.1 }, - { location: [19.4326, -99.1332], size: 0.1 }, - { location: [40.7128, -74.006], size: 0.1 }, - { location: [34.6937, 135.5022], size: 0.05 }, - { location: [41.0082, 28.9784], size: 0.06 }, + {location: [14.5995, 120.9842], size: 0.03}, + {location: [19.076, 72.8777], size: 0.1}, + {location: [23.8103, 90.4125], size: 0.05}, + {location: [30.0444, 31.2357], size: 0.07}, + {location: [39.9042, 116.4074], size: 0.08}, + {location: [-23.5505, -46.6333], size: 0.1}, + {location: [19.4326, -99.1332], size: 0.1}, + {location: [40.7128, -74.006], size: 0.1}, + {location: [34.6937, 135.5022], size: 0.05}, + {location: [41.0082, 28.9784], size: 0.06}, ], }; @@ -94,13 +87,20 @@ export class NgxGlobeComponent implements AfterViewInit, OnDestroy { private intersectionObserver?: IntersectionObserver; private isAnimating = false; + constructor( + @Inject(PLATFORM_ID) private platformId: object + ) { + } + ngAfterViewInit(): void { this.initGlobe(); - this.intersectionObserver = new IntersectionObserver(([entry]) => { - this.renderContents(entry.isIntersecting); - }); - this.intersectionObserver.observe(this.globeCanvas.nativeElement); + if (isPlatformBrowser(this.platformId)) { + this.intersectionObserver = new IntersectionObserver(([entry]) => { + this.renderContents(entry.isIntersecting); + }); + this.intersectionObserver.observe(this.globeCanvas.nativeElement); + } } ngOnDestroy(): void {