Avatar

A round user image, or a placeholder with an icon or initials when there is none.

Installation

bun add @harmo-ui/css @harmo-ui/react

Usage

import type { CSSProperties } from "react";
import { Avatar } from "@harmo-ui/react";

<Avatar src="images/imran.webp" alt="Imran Hajiyev" />
<Avatar alt="Imran Hajiyev">IH</Avatar>
<Avatar alt="Imran Hajiyev" style={{ "--harmo-avatar-size": "4rem" } as CSSProperties}>
  IH
</Avatar>

Examples

This site is using your theme

Image, icon, or initials

An image covers the circle. Without one, a placeholder shows an icon or initials on a soft gray. Name an image with alt, and a placeholder with role="img" and aria-label.

Imran HajiyevIH
<>
  <Avatar src="images/imran.webp" alt="Imran Hajiyev" />
  <Avatar alt="Imran Hajiyev">
    <UserIcon />
  </Avatar>
  <Avatar alt="Imran Hajiyev">IH</Avatar>
</>

Sizes

40px by default. Override --harmo-avatar-size on the element and its content scales with it: initials are 40% of the size, and an icon 62.5% of the space inside the border.

IHIHIH
<>
  <Avatar alt="Imran Hajiyev" style={{ "--harmo-avatar-size": "4rem" } as CSSProperties}>
    IH
  </Avatar>
  <Avatar alt="Imran Hajiyev" style={{ "--harmo-avatar-size": "4rem" } as CSSProperties}>
    <UserIcon />
  </Avatar>
  <Avatar alt="Imran Hajiyev">IH</Avatar>
  <Avatar alt="Imran Hajiyev" style={{ "--harmo-avatar-size": "1.5rem" } as CSSProperties}>
    IH
  </Avatar>
</>

Next to a name

When the name is shown beside it, hide the avatar from screen readers so the name isn't read twice: alt="" on an image, or aria-hidden="true" on a placeholder.

Imran Hajiyev
<>
  <Avatar src="images/imran.webp" alt="" />
  <span>Imran Hajiyev</span>
</>

API reference

React

<Avatar> renders the image with src, otherwise the placeholder, holding its children. Other props go to the image or the placeholder.

PropEffect
srcRenders the image, through HarmoProvider's imageComponent (an <img> by default)
altNames it: the image's alt, or the placeholder's role="img" and aria-label. alt="" hides either from screen readers, for an avatar next to its name
childrenThe placeholder's initials or icon

HTML

An <img class="havatar"> with alt, or a <span class="havatar" role="img"> with aria-label for a placeholder, holding a direct <svg> or initials. Load avatar.css. It needs no script.

HookEffect
--harmo-avatar-size on the elementSize (default 2.5rem); text and icon scale with it

See Avatar's full contract.

Accessibility

  • An image needs alt with the person's name; a placeholder takes role="img" and aria-label.
  • When the name already appears next to the avatar, use alt="" (or aria-hidden="true" on a placeholder) so it isn't read twice.