Avatar
A person's picture, with initials when there is none — alone, in a group, with a status badge. In the look of the app's style.
Usage
import { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarImage,} from '@semantic-components/ui';
@Component({ selector: 'app-avatar-usage-demo', imports: [ScAvatar, ScAvatarFallback, ScAvatarImage], template: ` <span scAvatar> <img scAvatarImage src="https://github.com/shadcn.png" alt="@shadcn" /> <span scAvatarFallback>CN</span> </span> `, host: { class: 'flex w-full justify-center' }, encapsulation: ViewEncapsulation.None,})export class AvatarUsageDemo {}Anatomy
Every part is a directive on your element. The image and the fallback fill the avatar, the image on top.
import { ScAvatar, ScAvatarImage, ScAvatarFallback, ScAvatarBadge, ScAvatarGroup, ScAvatarGroupCount,} from '@semantic-components/ui';<div scAvatarGroup> <span scAvatar size="lg"> <img scAvatarImage src="…" alt="…" /> <span scAvatarFallback>…</span> <span scAvatarBadge></span> </span> <div scAvatarGroupCount>+3</div></div>Loading
- The image is rendered at once and loads in place, so
loading="lazy"andNgOptimizedImagework. Until it has loaded, and if it fails, it is invisible (data-loading,data-error) and the fallback shows through. - Hidden with
visibility, notdisplay: none: an element with no box never enters the viewport, and a lazy image would never load. - Rendered on the server, an image that loads before the app wakes up is seen as loaded — its load event has gone by then.
- Loading, the image is not read: the fallback names the avatar. Give the image an
altfor when it shows.
Examples
Basic
import { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarImage,} from '@semantic-components/ui';
/** An image over its fallback; loaded lazily, it still shows (it is hidden by visibility, not display, while it loads). */@Component({ selector: 'app-basic-avatar-demo',Fallback
import { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarImage,} from '@semantic-components/ui';
/** The fallback while the image loads, when it fails, or with no image. */@Component({ selector: 'app-fallback-avatar-demo',Badge
CNimport { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarBadge, ScAvatarFallback, ScAvatarImage,} from '@semantic-components/ui';
/** A badge in the corner: a status, in your color. */@Component({Badge with Icon
PPimport { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarBadge, ScAvatarFallback, ScAvatarImage,} from '@semantic-components/ui';import { SiPlusIcon } from '@semantic-icons/lucide-icons';
@Component({Avatar Group
CN
LR
ERimport { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarGroup, ScAvatarImage,} from '@semantic-components/ui';
/** Overlapping, each ringed by the background. */@Component({Avatar Group Count
CN
LR
ER+3
import { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarGroup, ScAvatarGroupCount, ScAvatarImage,} from '@semantic-components/ui';
/** How many more, after the avatars shown. */Avatar Group with Icon
CN
LR
ERimport { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarGroup, ScAvatarGroupCount, ScAvatarImage,} from '@semantic-components/ui';import { SiPlusIcon } from '@semantic-icons/lucide-icons';Sizes
CN
CN
CNimport { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarImage,} from '@semantic-components/ui';
/** size: sm, default, lg. */@Component({ selector: 'app-sizes-avatar-demo',Dropdown
import { Component, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarFallback, ScAvatarImage, ScButton, ScMenu, ScMenuContent, ScMenuItem, ScMenuPortal,RTL
ش
ش
م+٣
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import { ScAvatar, ScAvatarBadge, ScAvatarFallback, ScAvatarGroup, ScAvatarGroupCount, ScAvatarImage,} from '@semantic-components/ui';API reference
Each part takes class, merged over the style (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScAvatar | span[scAvatar] | The avatar: round, with a thin outline. Input size: 'sm', 'default' or 'lg'. data-image: the image's state. |
ScAvatarImage | img[scAvatarImage] | The picture, over the fallback; invisible while it loads and if it fails (data-loading, data-error). |
ScAvatarFallback | span[scAvatarFallback] | Initials or an icon: shown until the image has loaded, or with none. |
ScAvatarBadge | span[scAvatarBadge] | A dot in the corner (a status, your color), or a small icon; sized by the avatar. |
ScAvatarGroup | div[scAvatarGroup] | Avatars overlapping, each ringed by the background; mirrored right to left. |
ScAvatarGroupCount | div[scAvatarGroupCount] | How many more, after the group's avatars: text or an icon, sized by them. |