Avatar and avatar group
A round photo that falls back to the person's initials, in three sizes and with a status dot, and a stacked group that ends with a count of who is left over. Shown in a small list of people.
People with access
MO
JW
PN
TH
+3
JW
PN
TH
+3
-
MO
Maya Okafor[email protected]
Owner -
JW
Jonas Weber[email protected]
Admin -
PN
Priya Nair[email protected]
Member
Options
Code
import { useState } from "react";
import "./avatar.css";
const initials = (name) =>
name
.split(" ")
.map((word) => word[0])
.slice(0, 2)
.join("")
.toUpperCase();
/* <Avatar name="Maya Okafor" src="/maya.jpg" size="lg" status="online" />
The initials show when there is no photo or it fails to load.
size is "sm" or "lg"; status is "online" or "away". */
export function Avatar({ name, src, size, status }) {
const [failed, setFailed] = useState(false);
return (
<span className="dh-avatar" data-size={size} data-status={status} role="img" aria-label={status ? name + ", " + status : name}>
{initials(name)}
{src && !failed && <img src={src} alt="" onError={() => setFailed(true)} />}
</span>
);
}
/* Shows the first few people, then how many more there are. */
export function AvatarGroup({ people, max = 4, size }) {
const more = people.length - max;
return (
<div className="dh-avatar-group" role="group" aria-label={people.length + " people"}>
{people.slice(0, max).map((person) => (
<Avatar key={person.name} name={person.name} src={person.src} size={size} />
))}
{more > 0 && (
<span className="dh-avatar dh-avatar-more" data-size={size} role="img" aria-label={more + " more"}>
+{more}
</span>
)}
</div>
);
}
const team = [
{ name: "Maya Okafor", src: "/blocks/dash-face-1.jpg", email: "[email protected]", role: "Owner", status: "online" },
{ name: "Jonas Weber", email: "[email protected]", role: "Admin" },
{ name: "Priya Nair", src: "/blocks/dash-face-3.jpg", email: "[email protected]", role: "Member", status: "away" },
{ name: "Tom Hale", src: "/blocks/dash-face-4.jpg" },
{ name: "Ana Ruiz" },
{ name: "Lee Park" },
{ name: "Sam Cole" },
];
export function Example() {
return (
<div className="dh-avatar-card">
<header>
<h3>People with access</h3>
<AvatarGroup people={team} size="sm" />
</header>
<ul>
{team.slice(0, 3).map((person) => (
<li key={person.name}>
<Avatar name={person.name} src={person.src} status={person.status} />
<p>
<b>{person.name}</b>
<span>{person.email}</span>
</p>
<em>{person.role}</em>
</li>
))}
</ul>
</div>
);
}/* An avatar: a round photo that falls back to the person's initials, in three
sizes, with an optional status dot. A group stacks avatars and ends with a
count of who is left over. Shown here in a small list of people. */
.dh-avatar {
position: relative;
display: inline-grid;
flex: none;
place-items: center;
width: 2.75em;
height: 2.75em;
border-radius: 50%;
background: color-mix(in srgb, var(--dh-accent, #2563ff) 12%, var(--dh-well, #ededeb));
box-shadow: 0 0 0 1px color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
color: color-mix(in srgb, var(--dh-accent, #2563ff) 60%, var(--dh-fg, #171717));
font-size: 0.78em;
font-style: normal;
font-weight: 600;
line-height: 1;
letter-spacing: 0.02em;
user-select: none;
}
/* An avatar is always 2.75 times its own text, so a size is just a text size. */
.dh-avatar[data-size="sm"] {
font-size: 0.62em;
}
.dh-avatar[data-size="lg"] {
font-size: 1.13em;
}
.dh-avatar img {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
border-radius: inherit;
object-fit: cover;
}
/* A status dot, cut out of the avatar by a ring in the surface colour. */
.dh-avatar[data-status]::after {
content: "";
position: absolute;
right: -0.1em;
bottom: -0.1em;
width: 0.7em;
height: 0.7em;
border-radius: 50%;
background: #1fa750;
box-shadow: 0 0 0 0.2em var(--dh-raised, #fff);
}
.dh-avatar[data-status="away"]::after {
background: #e39b0b;
}
.dh-avatar-group {
display: inline-flex;
padding-left: 0.35em;
}
.dh-avatar-group .dh-avatar {
margin-left: -0.65em;
box-shadow: 0 0 0 0.24em var(--dh-raised, #fff);
}
.dh-avatar-more {
background: var(--dh-well, #ededeb);
color: var(--dh-muted, #5c5c5c);
font-variant-numeric: tabular-nums;
letter-spacing: 0;
}
.dh-avatar-card {
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
width: 23.5em;
max-width: 100%;
overflow: hidden;
border-radius: 1em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent),
0 1em 2em -1em color-mix(in srgb, var(--dh-fg, #171717) 28%, transparent);
color: var(--dh-fg, #171717);
font-size: 0.8125em;
line-height: 1.54;
text-align: left;
}
.dh-avatar-card header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.85em 1.1em 0.85em 1.25em;
border-bottom: 1px solid var(--line);
}
.dh-avatar-card h3 {
margin: 0;
font-size: 1.08em;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-avatar-card ul {
margin: 0;
padding: 0.45em;
list-style: none;
}
.dh-avatar-card li {
display: flex;
align-items: center;
gap: 0.75em;
padding: 0.45em 0.8em;
border-radius: 0.7em;
animation: dh-avatar-in 280ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-avatar-card li:nth-child(2) {
animation-delay: 40ms;
}
.dh-avatar-card li:nth-child(3) {
animation-delay: 80ms;
}
.dh-avatar-card p {
display: grid;
flex: 1;
min-width: 0;
margin: 0;
}
.dh-avatar-card b {
font-weight: 500;
line-height: 1.4;
letter-spacing: -0.01em;
}
.dh-avatar-card p span {
color: var(--dh-muted, #5c5c5c);
font-size: 0.92em;
line-height: 1.35;
}
.dh-avatar-card em {
color: var(--dh-faint, #8a8a8a);
font-size: 0.92em;
font-style: normal;
}
@keyframes dh-avatar-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-avatar-card li {
animation: none;
}
}<div class="dh-avatar-card">
<header>
<h3>People with access</h3>
<div class="dh-avatar-group" role="group" aria-label="7 people">
<span class="dh-avatar" data-size="sm" role="img" aria-label="Maya Okafor">MO<img src="/blocks/dash-face-1.jpg" alt="" /></span>
<span class="dh-avatar" data-size="sm" role="img" aria-label="Jonas Weber">JW</span>
<span class="dh-avatar" data-size="sm" role="img" aria-label="Priya Nair">PN<img src="/blocks/dash-face-3.jpg" alt="" /></span>
<span class="dh-avatar" data-size="sm" role="img" aria-label="Tom Hale">TH<img src="/blocks/dash-face-4.jpg" alt="" /></span>
<span class="dh-avatar dh-avatar-more" data-size="sm" role="img" aria-label="3 more">+3</span>
</div>
</header>
<ul>
<li>
<span class="dh-avatar" data-status="online" role="img" aria-label="Maya Okafor, online">MO<img src="/blocks/dash-face-1.jpg" alt="" /></span>
<p><b>Maya Okafor</b><span>[email protected]</span></p>
<em>Owner</em>
</li>
<li>
<span class="dh-avatar" role="img" aria-label="Jonas Weber">JW</span>
<p><b>Jonas Weber</b><span>[email protected]</span></p>
<em>Admin</em>
</li>
<li>
<span class="dh-avatar" data-status="away" role="img" aria-label="Priya Nair, away">PN<img src="/blocks/dash-face-3.jpg" alt="" /></span>
<p><b>Priya Nair</b><span>[email protected]</span></p>
<em>Member</em>
</li>
</ul>
</div>/* An avatar: a round photo that falls back to the person's initials, in three
sizes, with an optional status dot. A group stacks avatars and ends with a
count of who is left over. Shown here in a small list of people. */
.dh-avatar {
position: relative;
display: inline-grid;
flex: none;
place-items: center;
width: 2.75em;
height: 2.75em;
border-radius: 50%;
background: color-mix(in srgb, var(--dh-accent, #2563ff) 12%, var(--dh-well, #ededeb));
box-shadow: 0 0 0 1px color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
color: color-mix(in srgb, var(--dh-accent, #2563ff) 60%, var(--dh-fg, #171717));
font-size: 0.78em;
font-style: normal;
font-weight: 600;
line-height: 1;
letter-spacing: 0.02em;
user-select: none;
}
/* An avatar is always 2.75 times its own text, so a size is just a text size. */
.dh-avatar[data-size="sm"] {
font-size: 0.62em;
}
.dh-avatar[data-size="lg"] {
font-size: 1.13em;
}
.dh-avatar img {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
border-radius: inherit;
object-fit: cover;
}
/* A status dot, cut out of the avatar by a ring in the surface colour. */
.dh-avatar[data-status]::after {
content: "";
position: absolute;
right: -0.1em;
bottom: -0.1em;
width: 0.7em;
height: 0.7em;
border-radius: 50%;
background: #1fa750;
box-shadow: 0 0 0 0.2em var(--dh-raised, #fff);
}
.dh-avatar[data-status="away"]::after {
background: #e39b0b;
}
.dh-avatar-group {
display: inline-flex;
padding-left: 0.35em;
}
.dh-avatar-group .dh-avatar {
margin-left: -0.65em;
box-shadow: 0 0 0 0.24em var(--dh-raised, #fff);
}
.dh-avatar-more {
background: var(--dh-well, #ededeb);
color: var(--dh-muted, #5c5c5c);
font-variant-numeric: tabular-nums;
letter-spacing: 0;
}
.dh-avatar-card {
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
width: 23.5em;
max-width: 100%;
overflow: hidden;
border-radius: 1em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent),
0 1em 2em -1em color-mix(in srgb, var(--dh-fg, #171717) 28%, transparent);
color: var(--dh-fg, #171717);
font-size: 0.8125em;
line-height: 1.54;
text-align: left;
}
.dh-avatar-card header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.85em 1.1em 0.85em 1.25em;
border-bottom: 1px solid var(--line);
}
.dh-avatar-card h3 {
margin: 0;
font-size: 1.08em;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-avatar-card ul {
margin: 0;
padding: 0.45em;
list-style: none;
}
.dh-avatar-card li {
display: flex;
align-items: center;
gap: 0.75em;
padding: 0.45em 0.8em;
border-radius: 0.7em;
animation: dh-avatar-in 280ms cubic-bezier(0.23, 1, 0.32, 1) both;
}
.dh-avatar-card li:nth-child(2) {
animation-delay: 40ms;
}
.dh-avatar-card li:nth-child(3) {
animation-delay: 80ms;
}
.dh-avatar-card p {
display: grid;
flex: 1;
min-width: 0;
margin: 0;
}
.dh-avatar-card b {
font-weight: 500;
line-height: 1.4;
letter-spacing: -0.01em;
}
.dh-avatar-card p span {
color: var(--dh-muted, #5c5c5c);
font-size: 0.92em;
line-height: 1.35;
}
.dh-avatar-card em {
color: var(--dh-faint, #8a8a8a);
font-size: 0.92em;
font-style: normal;
}
@keyframes dh-avatar-in {
from {
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-avatar-card li {
animation: none;
}
}/* A photo that fails to load is taken away, so the initials under it show. */
document.querySelectorAll(".dh-avatar img").forEach((photo) => {
const drop = () => photo.remove();
if (photo.complete && photo.naturalWidth === 0) drop();
else photo.addEventListener("error", drop);
});Avatar and avatar group is not in the Figma file yet.
Use the React or HTML code for now.