Skip to content

Commit

Permalink
feat(experimental): add padding when use img inside tui-avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
mdlufy committed May 23, 2024
1 parent 0646624 commit 9397bdc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
31 changes: 29 additions & 2 deletions projects/experimental/components/avatar/avatar.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import {ChangeDetectionStrategy, Component, Inject, Input} from '@angular/core';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
Inject,
Input,
Renderer2,
} from '@angular/core';
import {SafeResourceUrl} from '@angular/platform-browser';
import {tuiIsString, TuiStringHandler} from '@taiga-ui/cdk';
import {TUI_ICON_RESOLVER} from '@taiga-ui/experimental/tokens';
Expand All @@ -19,7 +27,7 @@ import {TUI_AVATAR_OPTIONS, TuiAvatarOptions} from './avatar.options';
'[class._round]': 'round',
},
})
export class TuiAvatarComponent {
export class TuiAvatarComponent implements AfterViewInit {
@Input()
size = this.options.size;

Expand All @@ -35,8 +43,23 @@ export class TuiAvatarComponent {
constructor(
@Inject(TUI_AVATAR_OPTIONS) private readonly options: TuiAvatarOptions,
@Inject(TUI_ICON_RESOLVER) readonly resolver: TuiStringHandler<string>,
@Inject(ElementRef)
private readonly elementRef: ElementRef,
@Inject(Renderer2) private readonly renderer: Renderer2,
) {}

ngAfterViewInit(): void {
const firstChild = (this.elementRef.nativeElement as HTMLElement)
.firstElementChild;

if (
firstChild?.tagName === 'IMG' &&
this.tuiIconPipeUsed((firstChild as HTMLImageElement).src)
) {
this.renderer.setStyle(firstChild, 'padding', '20%');
}
}

get safeSrc(): string {
return this.src?.toString() ?? '';
}
Expand All @@ -60,4 +83,8 @@ export class TuiAvatarComponent {

return this.value.length ? 'img' : 'content';
}

tuiIconPipeUsed(src: string): boolean {
return src.includes('/assets/taiga-ui/icons');
}
}
1 change: 1 addition & 0 deletions projects/experimental/components/avatar/avatar.style.less
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,6 @@
::ng-deep video {
.fullsize();
object-fit: cover;
box-sizing: border-box;
}
}

0 comments on commit 9397bdc

Please sign in to comment.