Avatar
A small picture of a person, with their initials or an icon when there is no image.
Usage
import { Avatar, AvatarGroup } from '@leverui/components';
<AvatarGroup>
<Avatar.Root>
<Avatar.Fallback />
<Avatar.Image />
</Avatar.Root>
</AvatarGroup>
For one component with props, see the snippet below.
Examples
Sizes
Use the size prop to change the size of the avatar
Variants
Use the variant prop to change the variant of the avatar
Shape
Use the shape prop to change the shape of the avatar, from rounded to square
Colors
Use the colorPalette prop to change the color of the avatar
gray
red
green
blue
teal
pink
purple
cyan
orange
yellow
Fallback
Render Avatar.Icon as the fallback when the name is not provided or when the image fails to load.
Random Color
Combine the colorPalette prop with some custom logic to dynamically change the color of the avatar
Ring
Use outline styles in authored CSS to add a ring around the avatar
Group
Use the AvatarGroup component to group multiple avatars together
Stacking
When using the AvatarGroup component, you can use the stacking prop to change the stacking order of the avatars
Persona
Here's an example of how to use the Avatar component to display a user persona.
Oskar Brandt
oskar.brandt@example.com
Mara Ellison
mara.ellison@example.com
Badge
Show a badge on the right corner of the avatar by composing the Float and Circle components
Overflow
Here's an example of how to handle an overflow of avatars by composing the Menu and Avatar components.
Store
An alternative way to access the avatar state and methods is to use the RootProvider component and the useAvatar store hook.
not loadedClosed Component
import { omit } from 'solid-js';
import type { JSX } from '@solidjs/web';
import { Avatar as AvatarParts, type AvatarRootProps } from '@leverui/components';
export type ClosedAvatarProps = AvatarRootProps & {
name?: string | undefined;
src?: string | undefined;
srcSet?: string | undefined;
loading?: 'eager' | 'lazy' | undefined;
icon?: JSX.Element;
fallback?: JSX.Element;
};
export function ClosedAvatar(props: ClosedAvatarProps) {
const root = omit(props, 'name', 'src', 'srcSet', 'loading', 'icon', 'fallback', 'children');
return (
<AvatarParts.Root {...root}>
<AvatarParts.Fallback name={props.name}>{props.fallback ?? props.icon}</AvatarParts.Fallback>
<AvatarParts.Image src={props.src} srcset={props.srcSet} loading={props.loading} />
{props.children}
</AvatarParts.Root>
);
}
Group them with AvatarGroup from @leverui/components.
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
colorPalette | gray | string | Color palette, built-in or your own. |
size | md | 'full' | '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | Avatar size. Responsive. |
variant | subtle | 'solid' | 'subtle' | 'outline' | Avatar style. Responsive. |
shape | full | 'square' | 'rounded' | 'full' | Corner shape. Responsive. |
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with this part's props. |
unstyled | false | boolean | Turn off the Lever UI styles. |
ids | — | Element ID map | Your own IDs for the root, image, and fallback. |
onStatusChange | — | (details) => void | Called when the image loading status changes. |
borderless | — | boolean | Remove the border the avatar gets in a group. |
Fallback
| Prop | Default | Type | Description |
|---|---|---|---|
name | — | string | Name the initials come from. Without it, shows a generic icon. |
as | div | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with this part's props. |
Image
| Prop | Default | Type | Description |
|---|---|---|---|
as | img | Native HTML tag | HTML tag to render. |
asChild | — | Solid render function | Render your own element with this part's props. |
Make the image alt and the fallback name match the person or thing shown.
Leave alt empty only when text next to the avatar already names it.
AvatarGroup overlaps its avatars. Set --leverui-avatar-group-overlap on
the group to change the overlap.
Customize with CSS
Give the root or a part a class. Load your CSS after the Lever UI stylesheet.
Classes: .leverui-avatar__root, .leverui-avatar__image,
.leverui-avatar__fallback, .leverui-avatar__icon, .leverui-avatar-group.
.profile-avatar {
--leverui-colors-color-palette-muted: #dbeafe;
}
.profile-avatar .leverui-avatar__fallback {
letter-spacing: 0.04em;
}
Explorer
Click a part name to highlight it in the preview.
Component Anatomy
Hover to highlight, click to select parts