From 614e2c4a718e968b508b11011583533082c34564 Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Fri, 15 Sep 2023 14:05:39 +0200 Subject: [PATCH] feat: display empty div for not visible avatars --- .../avatar-placeholder.component.html | 23 ++++++-- .../avatar-placeholder.component.ts | 58 ++++++++++++++++++- 2 files changed, 72 insertions(+), 9 deletions(-) diff --git a/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.html b/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.html index 13f1fd10..1f6edde5 100644 --- a/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.html +++ b/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.html @@ -20,9 +20,20 @@ [initialsType]="initialsType" > - + + + + +
+
diff --git a/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.ts b/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.ts index 9310b124..ff344275 100644 --- a/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.ts +++ b/projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.ts @@ -1,4 +1,12 @@ -import { Component, Input, OnChanges } from '@angular/core'; +import { + AfterViewInit, + ChangeDetectorRef, + Component, + ElementRef, + Input, + OnChanges, + OnDestroy, +} from '@angular/core'; import { Channel, User } from 'stream-chat'; import { CustomTemplatesService } from '../custom-templates.service'; import { @@ -16,7 +24,9 @@ import { templateUrl: './avatar-placeholder.component.html', styles: [], }) -export class AvatarPlaceholderComponent implements OnChanges { +export class AvatarPlaceholderComponent + implements OnChanges, AfterViewInit, OnDestroy +{ /** * An optional name of the image, used for fallback image or image title (if `imageUrl` is provided) */ @@ -61,7 +71,34 @@ export class AvatarPlaceholderComponent implements OnChanges { type: undefined, initialsType: undefined, }; - constructor(public customTemplatesService: CustomTemplatesService) {} + isVisible = true; + private mutationObserver?: MutationObserver; + constructor( + public customTemplatesService: CustomTemplatesService, + private hostElement: ElementRef, + private cdRef: ChangeDetectorRef + ) {} + + ngAfterViewInit(): void { + if (this.location !== 'message-sender') { + this.isVisible = true; + this.cdRef.detectChanges(); + return; + } + this.checkIfVisible(); + const elementToObserve = + this.hostElement.nativeElement.parentElement?.parentElement + ?.parentElement; + if (!elementToObserve) { + return; + } + this.mutationObserver = new MutationObserver(() => { + this.checkIfVisible(); + }); + this.mutationObserver.observe(elementToObserve, { + attributeFilter: ['class'], + }); + } ngOnChanges(): void { this.context = { @@ -75,4 +112,19 @@ export class AvatarPlaceholderComponent implements OnChanges { initialsType: this.initialsType, }; } + + ngOnDestroy(): void { + this.mutationObserver?.disconnect(); + } + + private checkIfVisible() { + const isVisible = + getComputedStyle(this.hostElement.nativeElement).getPropertyValue( + 'visibility' + ) === 'visible'; + if (isVisible !== this.isVisible) { + this.isVisible = isVisible; + this.cdRef.detectChanges(); + } + } }