Avatar
@codeui/styles v0.3.0
A person in plain HTML: their picture, or their initials on a colour from their id.
Load @codeui/styles/avatar.css, or all.css. The Solid component is documented in Avatar.
Demo
<div class="cui-stack" data-direction="row" data-gap="2" data-align="center"> <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"> <span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span> </span> <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Alan Turing" style="--cui-avatar-hue: 142"> <span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">AT</span> </span> <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Guest" style="--cui-avatar-chroma: 0"> <span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">G</span> </span> <span class="cui-avatar" data-cui="avatar" data-size="sm" data-unassigned role="img" aria-label="Unassigned"></span></div>Markup
| Part | Element | Class | data-cui |
|---|---|---|---|
| Root | span with role="img" and the name as aria-label | cui-avatar | avatar |
| Initials | span with aria-hidden="true" | cui-avatar-fallback | avatar-fallback |
| Picture | img with alt="", after the initials | cui-avatar-image | avatar-image |
| Attribute | Values | Default |
|---|---|---|
data-size | xs, sm, md, lg | xs |
data-unassigned | Presence | Off |
--cui-avatar-hue (inline style) | A hue in degrees | 0 |
--cui-avatar-chroma (inline style) | 0 for a neutral fill | Set per theme |
Variants
Size
<div class="cui-stack" data-direction="row" data-gap="3" data-align="center"> <span class="cui-avatar" data-cui="avatar" data-size="xs" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span> <span class="cui-avatar" data-cui="avatar" data-size="sm" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span> <span class="cui-avatar" data-cui="avatar" data-size="md" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span> <span class="cui-avatar" data-cui="avatar" data-size="lg" role="img" aria-label="Grace Hopper" style="--cui-avatar-hue: 25"><span class="cui-avatar-fallback" data-cui="avatar-fallback" aria-hidden="true">GH</span></span></div>The sizes are the shared control heights, --cui-control-height-xs to --cui-control-height-lg, so they follow data-cui-density. Comfortable density gives 28, 36, 44 and 52px.
Hue
Set --cui-avatar-hue from a stable id, never from the name. avatarHue(id) from @codeui/styles/avatar returns the hue the Solid component uses. Without an id, set --cui-avatar-chroma: 0 for a neutral fill.
States
Picture
Put the img after the initials. It covers them while it loads. When it fails, remove it and the initials show.
Unassigned
Set data-unassigned and leave the root empty. It draws a dashed circle with no fill. Name it "Unassigned".
What you supply
- The hue from a stable id, and the initials from the name.
avatarHueandavatarInitialsfrom@codeui/styles/avatardo both. - Removing the picture when it fails to load.
- A tooltip with the name, such as the Tooltip markup.
Helper
import "@codeui/styles/avatar.css";
import { applyStyleProps } from "@codeui/styles";
import { avatarHue, avatarInitials, avatarStyleProps } from "@codeui/styles/avatar";
const avatar = document.createElement("span");
applyStyleProps(avatar, avatarStyleProps({ size: "sm" }));
avatar.role = "img";
avatar.ariaLabel = "Grace Hopper";
avatar.style.setProperty("--cui-avatar-hue", String(avatarHue("user_grace")));
avatar.innerHTML = `<span class="cui-avatar-fallback" aria-hidden="true">${avatarInitials("Grace Hopper")}</span>`;
// class="cui-avatar" data-cui="avatar" data-size="sm"