suiss/ui · Component
Avatar
A circular image that represents a person, with initials or an icon when no image is available.
Avatar is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Avatar wraps the avatar primitive of the chosen base. An Avatar contains an AvatarImage and an AvatarFallback; the fallback shows while the image is loading or if it fails to load. Add an AvatarBadge to show a status dot or icon in the bottom-end corner.
To show several people together, place avatars in an AvatarGroup, which overlaps them and rings each one with the background color. End the group with an AvatarGroupCount to indicate how many more people there are.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a Avatar:
Examples
Fallback
Put the person's initials in AvatarFallback. They show when no image is provided.
Fallback sizes
Initials fallbacks at every size. The fallback text scales with the avatar.
Placeholder
Put a person icon in AvatarFallback. The icon scales to 60% of the avatar.
Sizes
Set size on each Avatar to sm, default, lg or xl.
With badge
Add an AvatarBadge with a check-mark icon to each size. At the sm size, icons inside the badge are hidden.
Group
Place avatars in an AvatarGroup and end it with an AvatarGroupCount showing +4. The count matches the size of the avatars in the group.
Group with icon count
Put a plus icon in AvatarGroupCount instead of a number.
Accessibility
- Give
AvatarImageanaltthat names the person. AvatarBadgeandAvatarGroupCountare plain elements; if they carry meaning, label them or describe the status in nearby text.
API Reference
Avatar
The circular root that sets the size.
| Prop | Type | Default |
|---|---|---|
size | "default" | "sm" | "lg" | "xl" | "default" |
Also accepts the props of the primitive part it wraps.
size sets the diameter and also scales the fallback text and badge. It is exposed as data-size.
AvatarImage
The person's image, cropped to a circle.
Also accepts the props of the primitive part it wraps.
AvatarFallback
Content shown when the image is missing or still loading.
| Prop | Type | Default |
|---|---|---|
delay | number | - |
delay is the number of milliseconds to wait before showing the fallback.
Also accepts the props of the primitive part it wraps.
Always provide an AvatarFallback alongside AvatarImage, usually the person's initials. The fallback can also hold an icon, which scales to 60% of the avatar. It uses a vertical gradient of muted-foreground with text-primary-foreground.
AvatarBadge
A small status indicator pinned to the bottom-end corner.
Also accepts the props of React.ComponentProps<"span">.
The badge uses bg-primary by default; override it, for example with bg-green-500. At the sm size, icons inside the badge are hidden.
AvatarGroup
A row that overlaps avatars with a ring between them.
Also accepts the props of React.ComponentProps<"div">.
AvatarGroupCount
A trailing circle that shows how many avatars are not displayed.
Also accepts the props of React.ComponentProps<"div">.
Inside an AvatarGroup, AvatarGroupCount matches the size of the avatars in the group automatically.
Each part sets a data-slot, such as avatar, avatar-fallback or avatar-group-count, for styling.
Web preview
Avatar is a round View that holds an AvatarImage and an AvatarFallback. The image stays hidden until it loads, and the fallback is shown while it loads or if it fails. Four sizes are available: sm, default, lg and xl; the fallback text, icons and badge scale with the size.
AvatarBadge adds a small status dot in the bottom-end corner, and AvatarGroup overlaps several avatars with an outline between them, with AvatarGroupCount for the remaining number.
Installation
- Install the following dependencies:
- Copy and paste the following code into
components/ui/avatar.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
- Copy and paste the following code into
components/ui/avatar.tsx:
- Update the import paths to match your project setup.
- Copy and paste the following code into
components/ui/avatar.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Fallback
Put initials in AvatarFallback. They show when there is no image.
Web preview
Sizes
Set size on each Avatar to sm, default, lg or xl.
Web preview
Group
Overlapping avatars in an AvatarGroup with a count. Pass the same size to AvatarGroupCount as to the avatars.
Web preview
With badge
An AvatarBadge with a check icon at each size. On the sm size, the badge hides its children and shows a plain dot.
Web preview
Accessibility
AvatarImageusesaltas itsaccessibilityLabel.- The image is hidden from screen readers until it has loaded.
- The badge has no label; describe the status in nearby text if it matters.
API Reference
Avatar
Round container that sets the size.
| Prop | Type | Default |
|---|---|---|
size | AvatarSize | "default" |
Also accepts the props of React.ComponentProps<typeof AvatarPrimitive.Root>.
AvatarImage
Image that appears once it has loaded.
Also accepts the props of React.ComponentProps<typeof AvatarPrimitive.Image>.
Give AvatarImage a source and an alt. Use onLoadingStatusChange to react to loading.
AvatarFallback
Initials or icon shown while the image is missing.
Also accepts the props of React.ComponentProps<typeof AvatarPrimitive.Fallback>.
Put initials or an Icon in the fallback; plain strings are wrapped in Text. Use delay to avoid a flash on fast connections. The fallback is softer in dark mode.
AvatarBadge
Status dot in the bottom-end corner.
Also accepts the props of React.ComponentProps<typeof View>, React.RefAttributes<View>.
AvatarGroup
Row that overlaps its avatars.
Also accepts the props of React.ComponentProps<typeof View>, React.RefAttributes<View>.
AvatarGroupCount
Round counter for the remaining items in a group.
| Prop | Type | Default |
|---|---|---|
size | AvatarSize | "default" |
Also accepts the props of React.ComponentProps<typeof View>, React.RefAttributes<View>.
Set size to match the avatars in the group.
AvatarSize
Values: .sm, .default, .lg, .xl.
Avatar
Round container that sets the size.
| Parameter | Type | Default |
|---|---|---|
size | Size: .sm | .default | .lg | .xl | .default |
content | () -> Content | - |
url | URL? | - |
fallback | () -> Fallback | - |
initials | String | - |
Overloads: init(size:content:), init(url:size:fallback:), init(url:initials:size:).
AvatarImage
Image that appears once it has loaded.
| Parameter | Type | Default |
|---|---|---|
url | URL? | - |
_ image | Image | - |
Overloads: init(url:), init(_:).
AvatarFallback
Initials or icon shown while the image is missing.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
_ initials | String | - |
icon | Image | - |
Overloads: init(content:), init(_:), init(icon:).
AvatarIcon
| Parameter | Type | Default |
|---|---|---|
_ image | Image | - |
AvatarBadge
Status dot in the bottom-end corner.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
icon | Image | - |
Overloads: init(content:), init(), init(icon:).
AvatarGroup
Row that overlaps its avatars.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
AvatarGroupCount
Round counter for the remaining items in a group.
| Parameter | Type | Default |
|---|---|---|
size | AvatarSize: .sm | .default | .lg | .xl | .default |
content | () -> Content | - |
_ count | String | - |
Overloads: init(size:content:), init(_:size:).
AvatarSize
Values: Sm, Default, Lg, Xl.
Avatar
Round container that sets the size.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
size | AvatarSize: Sm | Default | Lg | Xl | AvatarSize.Default |
image | Painter? | null |
contentDescription | String? | null |
badge | (@Composable () -> Unit)? | null |
fallback | (@Composable () -> Unit)? | null |
AvatarFallback
Initials or icon shown while the image is missing.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable () -> Unit | - |
text | String | - |
Overloads: AvatarFallback(content), AvatarFallback(text).
AvatarBadge
Status dot in the bottom-end corner.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable () -> Unit | {} |
AvatarGroup
Row that overlaps its avatars.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable () -> Unit | - |
AvatarGroupCount
Round counter for the remaining items in a group.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
size | AvatarSize: Sm | Default | Lg | Xl | AvatarSize.Default |
content | @Composable () -> Unit | - |
text | String | - |
Overloads: AvatarGroupCount(content), AvatarGroupCount(text).