import { Component } from '@angular/core';
import { WrAvatar } from 'ngwr/avatar';
@Component({ imports: [WrAvatar] })
export class MyComponent {}Avatar
A user's picture, with initials as the fallback.
Installation
Import the class, and @use the entry point in your global styles: without it the component renders unstyled.
Basic usage
<wr-avatar url="/me.png" alt="Roman" />Sizes
size takes pixels (48, '48px') or rem, not a percentage.
<wr-avatar url="/me.png" alt="Roman" [size]="32" />
<wr-avatar url="/me.png" alt="Roman" size="4rem" />
<wr-avatar url="/me.png" alt="Roman" size="6rem" />Shape
shape is rounded (default), square, circle or squircle.
<wr-avatar url="/me.png" alt="Roman" shape="square" />
<wr-avatar url="/me.png" alt="Roman" shape="rounded" />
<wr-avatar url="/me.png" alt="Roman" shape="circle" />
<wr-avatar url="/me.png" alt="Roman" shape="squircle" />Initials fallback
With no url, the projected content fills the avatar.
<wr-avatar [size]="48" shape="circle">RK</wr-avatar>Initials at every size
Initials shrink to fit the box, up to --wr-avatar-initials-scale of its size (0.375 by default).
<wr-avatar shape="circle" [size]="16">ХР</wr-avatar>
<wr-avatar shape="circle" [size]="24">ХР</wr-avatar>
<wr-avatar shape="circle" [size]="40">ХР</wr-avatar>
<wr-avatar shape="circle" size="6rem">ХР</wr-avatar>.team .wr-avatar {
--wr-avatar-initials-scale: 0.3;
}Inside a chip and a tag
In a select, size the avatar by placement: 16px in a chip, 24px in an option row.
<wr-select
mode="multi"
searchable
virtualScroll
placeholder="Nobody"
ariaLabel="Assignees"
[options]="peopleIds"
[displayWith]="personName"
[(value)]="assignees"
>
<ng-template wrOptionLeading let-personId let-placement="placement">
<wr-avatar
shape="circle"
[size]="placement === 'chip' ? 16 : 24"
[url]="personPhoto(personId)"
[alt]="personName(personId)"
>{{ personInitials(personId) }}</wr-avatar
>
</ng-template>
</wr-select>
@for (id of assignees(); track id) {
<wr-tag>
<span class="assignee">
<wr-avatar shape="circle" [size]="16" [url]="personPhoto(id)" [alt]="personName(id)">{{
personInitials(id)
}}</wr-avatar>
<a routerLink="/people">{{ personName(id) }}</a>
</span>
</wr-tag>
}Initials behind an image
Give both a url and initials: the initials show until the image loads.
<wr-avatar
[url]="user.photo"
alt="Roman Khegay"
[size]="64"
shape="circle"
style="background: var(--wr-color-primary-soft); color: var(--wr-color-primary-ink); font-weight: 600"
>
RK
</wr-avatar>Broken image
If url fails to load the initials stay, and a new url tries again.
<!-- The request fails, so the initials stay. -->
<wr-avatar
url="/missing.png"
alt="Roman Khegay"
[size]="64"
shape="circle"
style="background: var(--wr-color-primary-soft); color: var(--wr-color-primary-ink); font-weight: 600"
>
RK
</wr-avatar>Gotchas
- Projected content sized in
em, a<wr-icon>too, shrinks with the initials. Give it apxorremsize. - Without
alt, the image is announced as "Avatar". Setaltto the person's name.
API
| Name | Description | Type | Default |
|---|---|---|---|
url | Image URL. Projected content (e.g. initials) shows while it is unset, loading or broken, and gives way once the image has loaded. | string | null | null |
alt | Alt text for the image. Falls back to avatar.alt, then 'Avatar' — pass the person's name where you have it, since that is what a reader wants. With an alt, projected initials are hidden from assistive technology while an image is in play, so the name is announced once. Without one, the image carries only the generic name, so the initials stay announced beside it — visually hidden once it has loaded, never dropped. alt="" marks the avatar decorative, as it does on an <img>: the image is silent and so are the initials while an image is in play — for an avatar beside a name that is already printed. After a failed load with nothing projected, the alt is still announced, so a broken image never leaves the avatar nameless. | string | null | null |
shape | Corner treatment. rounded (default) is a soft rounded square, circle is the classic profile avatar, squircle is the iOS look. | WrAvatarShape | 'rounded' |
size | Box size. See {@link WrAvatarSize} for accepted values. The resolved value is published on the host as --wr-avatar-size. | WrAvatarSize | '6rem' |
CSS variables
Set these on the component's selector or a wrapper, not on :root: the defaults are declared on ngwr/avatar's own element.
| Variable | Default | Declared on |
|---|---|---|
--wr-avatar-img-opacity | 0 | .wr-avatar +1 variant override |
--wr-avatar-initials-scale | 0.375 | .wr-avatar |
--wr-avatar-radius | 10% | .wr-avatar +2 variant overrides |
--wr-avatar-ring | var(--wr-border-strong) | .wr-avatar +1 variant override |
Dependencies
| Built on | Spinner |
|---|
See also
Component ListA list of rows with leading, body and trailing slots.Component Image CropperCrop an image and get the result as a Blob.Component File UploadPick files by dropping them on a zone or browsing.Component SpinnerAn inline loading indicator.Component BadgeA small label for a count or a status.Component SelectPick one or several values from a list, with search and free-text tags.
