Primitives
Display an image that represents a user with a text fallback.
import { Component } from "@angular/core";
import {
NgpAvatar,
NgpAvatarFallback,
NgpAvatarImage,
} from "ng-primitives/avatar";
@Component({
selector: "app-avatar",
imports: [NgpAvatar, NgpAvatarImage, NgpAvatarFallback],
styles: `
[ngpAvatar] {
position: relative;
display: inline-flex;
width: 3rem;
height: 3rem;
align-items: center;
justify-content: center;
border-radius: 9999px;
border-width: 2px;
border-color: var(--ngp-avatar-border);
background-color: var(--ngp-avatar-background);
vertical-align: middle;
}
[ngpAvatar]:before {
content: "";
position: absolute;
inset: 0;
border-radius: 9999px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
}
[ngpAvatarImage] {
width: 100%;
height: 100%;
}
[ngpAvatarFallback] {
text-align: center;
font-weight: 500;
color: var(--ngp-text-emphasis);
}
`,
template: `
<span ngpAvatar>
<img
ngpAvatarImage
src="https://angularprimitives.com/assets/avatar.png"
alt="Profile Image"
/>
<span ngpAvatarFallback>NG</span>
</span>
`,
})
export default class AvatarExample {}
Import the Avatar primitives from ng-primitives/avatar
.
import { NgpAvatar, NgpAvatarImage, NgpAvatarFallback } from 'ng-primitives/avatar';
Assemble the avatar directives in your template.
<span ngpAvatar>
<img ngpAvatarImage src="..." alt="..." />
<span ngpAvatarFallback>NG</span>
</span>
Create a reusable component that uses the NgpAvatar
directive.
import { Component } from '@angular/core';
import { Avatar } from './avatar';
@Component({
selector: 'app-root',
imports: [Avatar],
template: `
<app-avatar image="https://mighty.tools/mockmind-api/content/human/104.jpg" fallback="AH" />
`,
})
export default class App {}
Generate a reusable avatar component using the Angular CLI.
ng g ng-primitives:primitive avatar
path
: The path at which to create the component file.prefix
: The prefix to apply to the generated component selector.componentSuffix
: The suffix to apply to the generated component class name.fileSuffix
: The suffix to apply to the generated component file name. Defaults to component
.exampleStyles
: Whether to include example styles in the generated component file. Defaults to true
.The following directives are available to import from the ng-primitives/avatar
package:
Apply the `ngpAvatar` directive to an element that represents the avatar. This directive is a container for the image and/or fallback.[ngpAvatar]
ngpAvatar
Attribute | Description | Value |
---|---|---|
data-status |
The loading status of the avatar image. | idle | loading | loaded | error |
data-hover |
The added to the avatar when hovered. | - |
data-focus-visible |
The added to the avatar when focus visible. | - |
data-press |
The added to the avatar when pressed. | - |
Apply the `ngpAvatarImage` directive to an element that represents the avatar image. This would typically be an `img` element or a `div` with a background image.img[ngpAvatarImage]
ngpAvatarImage
Apply the `ngpAvatarFallback` directive to an element that represents the user in the absence of an image. This is typically the user's initials. Define a delay before the fallback is shown. This is useful to only show the fallback for those with slower connections.
[ngpAvatarFallback]
ngpAvatarFallback
You can configure the default options for all avatars in your application by using the provideAvatarConfig
function in a providers array.
import { provideAvatarConfig } from 'ng-primitives/avatar';
bootstrapApplication(AppComponent, {
providers: [provideAvatarConfig({ delay: 1000 })],
});
Define a delay before the fallback is shown. This is useful to only show the fallback for those with slower connections.
Copyright © 2025 Angular Primitives