Avatar
- Usage
- Styling
Avatar is a graphical representation of an object or entity, for example, a person or an organization.
new tab
Avatar avatarBasic = new Avatar();
Avatar avatarName = new Avatar(name);
Avatar avatarImage = new Avatar(name);
avatarImage.setImage(pictureUrl);
Note
|
Real-time collaboration
This component is optimized for use with Collaboration Kit — a simple way to build real-time collaboration into your app — but can also be used standalone as a regular component.
|
Content
Avatar has three properties: name, abbreviation, and image.
Name and Abbreviation
The name is shown on hover in a tooltip. When a name is set, Avatar auto-generates and display an abbreviation of the specified name. For example, “Allison Torres” becomes “AT”, “John Smith” becomes “JS”, and so on.
The abbreviation can also be set manually. Abbreviations should be kept to a maximum of 2–3 characters.
new tab
Avatar avatarName = new Avatar("Augusta Ada King");
Avatar avatarAbbr = new Avatar("Augusta Ada King");
avatarAbbr.setAbbreviation("AK");
Image
Avatar can be used to display images, such as user profile pictures or company logos. Abbreviations aren’t shown when images are used.
new tab
Avatar user = new Avatar(name);
user.setImage(pictureUrl);
Avatar company = new Avatar("Company Inc.");
StreamResource imageResource = new StreamResource("company-logo.png",
() -> getClass()
.getResourceAsStream("/images/company-logo.png"));
company.setImageResource(imageResource);
Avatar Group
Avatar Group is used to group multiple Avatars together. It can be used, for example, to show that there are multiple users viewing the same page or for listing members of a project.
new tab
AvatarGroup avatarGroup = new AvatarGroup();
for (Person person : people) {
String name = person.getFirstName() + " " + person.getLastName();
AvatarGroupItem avatar = new AvatarGroupItem(name);
avatarGroup.add(avatar);
}
Maximum Number of Items
You can specify the maximum number of items an Avatar Group should display. Items that overflow are grouped into a single Avatar that displays the overflow count. The name of each hidden item is shown on hover in a tooltip. Clicking the overflow item displays the overflowing avatars and names in a list.
new tab
AvatarGroup avatarGroup = new AvatarGroup();
avatarGroup.setMaxItemsVisible(3);
for (Person person : people) {
String name = person.getFirstName() + " " + person.getLastName();
AvatarGroup.AvatarGroupItem avatar = new AvatarGroup.AvatarGroupItem(
name);
avatarGroup.add(avatar);
}
Background Color
By default, there are 7 different background colors you can use for Avatar. The background color is set using a color index.
new tab
AvatarGroup avatarGroup = new AvatarGroup();
for (Person person : people) {
String name = person.getFirstName() + " " + person.getLastName();
AvatarGroupItem avatar = new AvatarGroupItem(name);
avatar.setColorIndex(colorIndex++);
avatarGroup.add(avatar);
}
Using different background colors can be useful when you need to be able to distinguish between users, for example during collaborative work.
Internationalisation (i18n)
All texts in Avatar and Avatar Group are configurable:
Avatar Texts
Property | Text | Description |
---|---|---|
| "Anonymous" | Avatar’s default name. Shown on hover in a tooltip and announced by screen readers when the Avatar is focused. |
Avatar Group Texts
Property | Text | Description |
---|---|---|
| "Anonymous" | Default name for all Avatars in the Avatar Group. |
| "Currently one active user" | Announced by screen readers when there is exactly one Avatar in an Avatar Group and the Avatar is focused. The name of the Avatar is read aloud first. |
| "Currently {count} active users" | Announced by screen readers when there are multiple Avatars in an Avatar Group and an Avatar is focused. The name of the focused Avatar is read aloud first. *{count} is the Avatar Group item count. |
| "{user} joined" | Announced by screen readers when an Avatar is added to the group. *{user} is the Avatar’s name. |
| "{user} left" | Announced by screen readers when an Avatar is removed from the group. *{user} is the Avatar’s name. |
new tab
AvatarGroupI18n i18n = new AvatarGroupI18n();
i18n.setAnonymous("Anonyymi");
i18n.setManyActiveUsers("Yksi käyttäjä aktiivisena");
i18n.setOneActiveUser("{count} käyttäjää aktiivisena");
AvatarGroup avatarGroup = new AvatarGroup();
avatarGroup.setI18n(i18n);
// Add anonymous user
avatarGroup.add(new AvatarGroupItem());
for (Person person : people) {
String name = person.getFirstName() + " " + person.getLastName();
AvatarGroupItem avatar = new AvatarGroupItem(name);
avatarGroup.add(avatar);
}
Size Variants
Avatar has four available size variants:
new tab
Avatar xl = new Avatar(name);
xl.addThemeVariants(AvatarVariant.LUMO_XLARGE);
Avatar l = new Avatar(name);
l.addThemeVariants(AvatarVariant.LUMO_LARGE);
Avatar s = new Avatar(name);
s.addThemeVariants(AvatarVariant.LUMO_SMALL);
Avatar xs = new Avatar(name);
xs.addThemeVariants(AvatarVariant.LUMO_XSMALL);
Variant | Theme name |
---|---|
Extra large |
|
Large |
|
Small |
|
Extra small |
|
Size variants should be used only in special cases. See Size and Space for details on how to change the default Avatar size.
Use Cases
Avatar can be paired with Menu Bar to create a user account menu.
new tab
Avatar avatar = new Avatar(name);
avatar.setImage(pictureUrl);
MenuBar menuBar = new MenuBar();
menuBar.addThemeVariants(MenuBarVariant.LUMO_TERTIARY_INLINE);
MenuItem menuItem = menuBar.addItem(avatar);
SubMenu subMenu = menuItem.getSubMenu();
subMenu.addItem("Profile");
subMenu.addItem("Settings");
subMenu.addItem("Help");
subMenu.addItem("Sign out");
57551FAE-0B53-46FB-B384-71C3EB9D2CE1