Skip to content

Commit

Permalink
feat: display empty div for not visible avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Sep 15, 2023
1 parent 5fb9916 commit d653fd4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@
[initialsType]="initialsType"
></stream-avatar>
</ng-template>
<ng-container
*ngTemplateOutlet="
(customTemplatesService.avatarTemplate$ | async) || defaultAvatar;
context: context
"
></ng-container>
<ng-container *ngIf="isVisible; else emptyPlaceholder">
<ng-container
*ngTemplateOutlet="
(customTemplatesService.avatarTemplate$ | async) || defaultAvatar;
context: context
"
></ng-container>
</ng-container>
<ng-template #emptyPlaceholder>
<div
class="str-chat__avatar"
[ngStyle]="{
width: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')',
height: 'calc(var(--str-chat__spacing-px, 1px) * ' + size + ')'
}"
></div>
</ng-template>
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Component, Input, OnChanges } from '@angular/core';
import {
AfterViewChecked,

Check failure on line 2 in projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.ts

View workflow job for this annotation

GitHub Actions / workflow (16.x)

'AfterViewChecked' is defined but never used

Check failure on line 2 in projects/stream-chat-angular/src/lib/avatar-placeholder/avatar-placeholder.component.ts

View workflow job for this annotation

GitHub Actions / workflow (16.x)

'AfterViewChecked' is defined but never used
AfterViewInit,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnChanges,
OnDestroy,
} from '@angular/core';
import { Channel, User } from 'stream-chat';
import { CustomTemplatesService } from '../custom-templates.service';
import {
Expand All @@ -16,7 +25,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)
*/
Expand Down Expand Up @@ -61,7 +72,32 @@ 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<HTMLElement>,
private cdRef: ChangeDetectorRef
) {}

ngAfterViewInit(): void {
this.checkIfVisible();
if (this.location !== 'message-sender') {
return;
}
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 = {
Expand All @@ -75,4 +111,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();
}
}
}

0 comments on commit d653fd4

Please sign in to comment.