Avatar
A user's picture with a fallback, such as initials or an icon, while it loads or if it fails.
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarFallback, AvatarImage, AvatarRoot } from "#ui/avatar"
export const AvatarBasic = Html.component({
name: "AvatarBasic",
model: Schema.Struct({}),
message: Schema.Never
}).make({
init: () => ({}),
update: ($) => $.model,
view: (h) =>
h.div.class("flex items-center gap-3").children(
Avatar(h, {
key: "loaded",
props: { src: "/avatars/ada.svg" },
view: (avatar) =>
AvatarRoot(h, avatar).children(
AvatarImage(h, avatar, { alt: "Ada Moreau" }),
AvatarFallback(h, avatar)?.children("AM")
)
}),
// The picture can't load, so the fallback stays.
Avatar(h, {
key: "broken",
props: { src: "/avatars/missing.svg" },
view: (avatar) =>
AvatarRoot(h, avatar).children(
AvatarImage(h, avatar, { alt: "Mira Santos" }),
AvatarFallback(h, avatar)?.children("MS")
)
}),
Avatar(h, {
key: "none",
view: (avatar) => AvatarRoot(h, avatar).children(AvatarFallback(h, avatar)?.children("NH"))
})
)
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.Copy the component into your project
The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.
src/ui/avatar.ts import type { Component } from "@effect/ui" import type { Html } from "@effect/ui/html" import type { Avatar } from "@effect/ui-kit" import { cn } from "#lib/cn" type AvatarParts = Component.Headless.Parts<typeof Avatar> const sizes = { xs: "size-6 text-[10px]", sm: "size-8 text-xs", md: "size-10 text-sm", lg: "size-12 text-base", xl: "size-16 text-xl" } as const const shapes = { circle: "rounded-full", rounded: "rounded-md", square: "rounded-none" } as const const variants = { default: "bg-muted text-muted-foreground", brand: "bg-brand-soft text-brand-soft-foreground [&>[data-slot=fallback]]:border-transparent" } as const const statuses = { online: "bg-success", away: "bg-warning", offline: "bg-muted-foreground" } as const export type AvatarSize = keyof typeof sizes export type AvatarShape = keyof typeof shapes export type AvatarVariant = keyof typeof variants export type AvatarStatusValue = keyof typeof statuses interface ClassOption { readonly class?: string | undefined } /** The classes of the avatar's root, for an element that should look like one, such as a "+3" counter. */ export const avatarVariants = ( options: ClassOption & { readonly size?: AvatarSize | undefined readonly shape?: AvatarShape | undefined readonly variant?: AvatarVariant | undefined } = {} ): string => cn( "relative inline-flex shrink-0 align-middle leading-none font-medium select-none", sizes[options.size ?? "md"], shapes[options.shape ?? "circle"], variants[options.variant ?? "default"], options.class ) const fallbackClass = "flex size-full items-center justify-center rounded-[inherit] border [&_svg]:size-[55%]" /** The `<span>` that holds the image and the fallback. It doesn't clip, so a status dot can overhang it. */ export const AvatarRoot = <Msg>( h: Html.H<Msg>, avatar: AvatarParts, options: Parameters<typeof avatarVariants>[0] = {} ) => avatar.root(h.span.class(avatarVariants(options))) /** The picture. `undefined` until it has loaded. */ export const AvatarImage = <Msg>( h: Html.H<Msg>, avatar: AvatarParts, options: ClassOption & { readonly alt: string } ) => avatar.image( h.img.class(cn("block size-full rounded-[inherit] object-cover", options.class)).attr("alt", options.alt) ) /** The initials or icon shown while the picture isn't loaded. `undefined` otherwise, so chain with `?.children(...)`. */ export const AvatarFallback = <Msg>(h: Html.H<Msg>, avatar: AvatarParts, options: ClassOption = {}) => avatar.fallback(h.span.class(cn(fallbackClass, options.class)).attr("data-slot", "fallback")) /** A counter that sits in a group as one more avatar: `AvatarCount(h, { count: 3 })` draws "+3". */ export const AvatarCount = <Msg>( h: Html.H<Msg>, options: Parameters<typeof avatarVariants>[0] & { readonly count: number } ) => h.span.class(avatarVariants(options)).children( h.span.class(fallbackClass).attrs({ "data-slot": "fallback", "aria-label": `${options.count} more` }) .children(`+${options.count}`) ) /** A dot on the avatar's corner. Place it inside the root, after the image and the fallback. */ export const AvatarStatus = <Msg>( h: Html.H<Msg>, options: ClassOption & { readonly status: AvatarStatusValue } ) => h.span .class(cn( "absolute right-0 bottom-0 size-[28%] min-h-2 min-w-2 rounded-full ring-2 ring-background", statuses[options.status], options.class )) .attrs({ role: "img", "aria-label": options.status }) /** A stack in which each avatar overlaps the one before it. */ export const AvatarGroup = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) => h.div.class(cn("flex items-center *:ring-2 *:ring-background [&>*+*]:-ml-2.5", options.class))Import it
import { avatarVariants, AvatarRoot, AvatarImage, AvatarFallback, AvatarCount, AvatarStatus, AvatarGroup } from "#ui/avatar"
Usage
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarFallback, AvatarImage, AvatarRoot } from "#ui/avatar"
export const AvatarBasic = Html.component({
name: "AvatarBasic",
model: Schema.Struct({}),
message: Schema.Never
}).make({
init: () => ({}),
update: ($) => $.model,
view: (h) =>
h.div.class("flex items-center gap-3").children(
Avatar(h, {
key: "loaded",
props: { src: "/avatars/ada.svg" },
view: (avatar) =>
AvatarRoot(h, avatar).children(
AvatarImage(h, avatar, { alt: "Ada Moreau" }),
AvatarFallback(h, avatar)?.children("AM")
)
}),
// The picture can't load, so the fallback stays.
Avatar(h, {
key: "broken",
props: { src: "/avatars/missing.svg" },
view: (avatar) =>
AvatarRoot(h, avatar).children(
AvatarImage(h, avatar, { alt: "Mira Santos" }),
AvatarFallback(h, avatar)?.children("MS")
)
}),
Avatar(h, {
key: "none",
view: (avatar) => AvatarRoot(h, avatar).children(AvatarFallback(h, avatar)?.children("NH"))
})
)
})
Examples
Fallback
Initials, a brand fill, an icon, and a fallback that waits out fallbackDelay.
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarFallback, AvatarImage, AvatarRoot } from "#ui/avatar"
export const AvatarFallbacks = Html.component({
name: "AvatarFallbacks",
model: Schema.Struct({}),
message: Schema.Never
}).make({
init: () => ({}),
update: ($) => $.model,
view: (h) =>
h.div.class("flex items-center gap-3").children(
Avatar(h, {
key: "initials",
view: (avatar) => AvatarRoot(h, avatar).children(AvatarFallback(h, avatar)?.children("MN"))
}),
Avatar(h, {
key: "brand",
view: (avatar) =>
AvatarRoot(h, avatar, { variant: "brand" }).children(AvatarFallback(h, avatar)?.children("KT"))
}),
Avatar(h, {
key: "icon",
view: (avatar) =>
AvatarRoot(h, avatar).children(
AvatarFallback(h, avatar)?.attrs({ role: "img", "aria-label": "Unknown user" }).children(
h.el("svg").attrs({
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
"stroke-width": "2",
"stroke-linecap": "round",
"aria-hidden": "true"
}).children(
h.el("circle").attrs({ cx: "12", cy: "8", r: "4" }),
h.el("path").attr("d", "M4 21c0-4 3.6-7 8-7s8 3 8 7")
)
)
)
}),
// The fallback waits 600ms, so a fast picture doesn't flash the initials.
Avatar(h, {
key: "delayed",
props: { src: "/avatars/grace.svg", fallbackDelay: 600 },
view: (avatar) =>
AvatarRoot(h, avatar).children(
AvatarImage(h, avatar, { alt: "Grace Okafor" }),
AvatarFallback(h, avatar)?.children("GO")
)
})
)
})
Sizes
Five sizes from the root's size option.
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarFallback, AvatarImage, AvatarRoot } from "#ui/avatar"
const sizes = ["xs", "sm", "md", "lg", "xl"] as const
export const AvatarSizes = Html.component({
name: "AvatarSizes",
model: Schema.Struct({}),
message: Schema.Never
}).make({
init: () => ({}),
update: ($) => $.model,
view: (h) =>
h.div.class("flex flex-col items-center gap-4").children(
h.div.class("flex items-center gap-3").children(
sizes.map((size) =>
Avatar(h, {
key: `image-${size}`,
props: { src: "/avatars/ada.svg" },
view: (avatar) =>
AvatarRoot(h, avatar, { size }).children(
AvatarImage(h, avatar, { alt: "Ada Moreau" }),
AvatarFallback(h, avatar)?.children("AM")
)
})
)
),
h.div.class("flex items-center gap-3").children(
sizes.map((size) =>
Avatar(h, {
key: `initials-${size}`,
view: (avatar) => AvatarRoot(h, avatar, { size }).children(AvatarFallback(h, avatar)?.children("AM"))
})
)
)
)
})
Shapes
The image and the fallback inherit the root's radius, set by the shape option.
circle
rounded
square
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarFallback, AvatarImage, AvatarRoot } from "#ui/avatar"
const shapes = ["circle", "rounded", "square"] as const
export const AvatarShapes = Html.component({
name: "AvatarShapes",
model: Schema.Struct({}),
message: Schema.Never
}).make({
init: () => ({}),
update: ($) => $.model,
view: (h) =>
h.div.class("flex flex-wrap items-start justify-center gap-8").children(
shapes.map((shape) =>
h.div.class("flex flex-col items-center gap-3").children(
h.div.class("flex items-center gap-2.5").children(
Avatar(h, {
key: `image-${shape}`,
props: { src: "/avatars/linus.svg" },
view: (avatar) =>
AvatarRoot(h, avatar, { shape, size: "lg" }).children(
AvatarImage(h, avatar, { alt: "Linus Vik" }),
AvatarFallback(h, avatar)?.children("LV")
)
}),
Avatar(h, {
key: `initials-${shape}`,
view: (avatar) =>
AvatarRoot(h, avatar, { shape, size: "lg" }).children(AvatarFallback(h, avatar)?.children("LV"))
})
),
h.p.class("m-0 text-xs text-muted-foreground").children(shape)
)
)
)
})
Group
A stack that shows four members and counts the rest.
6 of 7 members
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarCount, AvatarFallback, AvatarGroup, AvatarImage, AvatarRoot } from "#ui/avatar"
import { Button } from "#ui/button"
const AvatarStackMessage = UiSchema.TaggedEnum({ PressedInvite: {}, PressedReset: {} })
const members = [
{ name: "Ada Moreau", initials: "AM", src: "/avatars/ada.svg" },
{ name: "Linus Vik", initials: "LV", src: "/avatars/linus.svg" },
{ name: "Grace Okafor", initials: "GO", src: "/avatars/grace.svg" },
{ name: "Mira Santos", initials: "MS", src: "/avatars/mira.svg" },
{ name: "Noor Haddad", initials: "NH", src: "/avatars/noor.svg" },
{ name: "Theo Lang", initials: "TL", src: "/avatars/theo.svg" },
{ name: "Ines Dahl", initials: "ID", src: undefined }
]
const shown = 4
export const AvatarStack = Html.component({
name: "AvatarStack",
model: Schema.Struct({ count: Schema.Number }),
message: AvatarStackMessage
}).make({
init: () => ({ count: 6 }),
update: {
PressedInvite: ($) => $.evolve({ count: (count) => Math.min(members.length, count + 1) }),
PressedReset: ($) => $.evolve({ count: 3 })
},
view: (h, model) => {
const team = members.slice(0, model.count)
const overflow = team.length - shown
return h.div.class("flex flex-col items-center gap-4").children(
AvatarGroup(h).attrs({ role: "group", "aria-label": `${team.length} members` }).children(
team.slice(0, shown).map((member) =>
Avatar(h, {
key: member.name,
props: { src: member.src },
view: (avatar) =>
AvatarRoot(h, avatar).children(
AvatarImage(h, avatar, { alt: member.name }),
AvatarFallback(h, avatar)?.attr("title", member.name).children(member.initials)
)
})
),
overflow > 0 ? AvatarCount(h, { count: overflow }) : undefined
),
h.div.class("flex items-center gap-2").children(
Button(h, { variant: "outline", size: "sm", onPress: AvatarStackMessage.PressedInvite() })
.children("Invite a member"),
Button(h, { variant: "ghost", size: "sm", onPress: AvatarStackMessage.PressedReset() }).children("Reset")
),
h.p.class("m-0 text-xs text-muted-foreground tabular-nums").children(
`${team.length} of ${members.length} members`
)
)
}
})
With a status dot
AvatarStatus is an extra element in the root, coloured by its status.
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarFallback, AvatarImage, AvatarRoot, AvatarStatus } from "#ui/avatar"
const people = [
{ name: "Ada Moreau", initials: "AM", src: "/avatars/ada.svg", status: "online" },
{ name: "Mira Santos", initials: "MS", src: "/avatars/mira.svg", status: "away" },
{ name: "Noor Haddad", initials: "NH", src: undefined, status: "offline" }
] as const
export const AvatarPresence = Html.component({
name: "AvatarPresence",
model: Schema.Struct({}),
message: Schema.Never
}).make({
init: () => ({}),
update: ($) => $.model,
view: (h) =>
h.div.class("flex items-center gap-6").children(
people.map((person) =>
Avatar(h, {
key: person.name,
props: { src: person.src },
view: (avatar) =>
AvatarRoot(h, avatar, { size: "lg" }).children(
AvatarImage(h, avatar, { alt: person.name }),
AvatarFallback(h, avatar)?.children(person.initials),
AvatarStatus(h, { status: person.status })
)
})
)
)
})
In a list
An avatar beside a name and an email, as in a list of members.
Ada Moreau
ada@example.com
Mira Santos
mira@example.com
Theo Lang
theo@example.com
import { Html } from "@effect/ui/html"
import { Avatar } from "@effect/ui-kit"
import { Schema } from "effect"
import { AvatarFallback, AvatarImage, AvatarRoot } from "#ui/avatar"
const people = [
{ name: "Ada Moreau", email: "ada@example.com", initials: "AM", src: "/avatars/ada.svg" },
{ name: "Mira Santos", email: "mira@example.com", initials: "MS", src: "/avatars/mira.svg" },
{ name: "Theo Lang", email: "theo@example.com", initials: "TL", src: undefined }
]
export const AvatarList = Html.component({
name: "AvatarList",
model: Schema.Struct({}),
message: Schema.Never
}).make({
init: () => ({}),
update: ($) => $.model,
view: (h) =>
h.ul.class("m-0 flex w-full max-w-xs list-none flex-col gap-4 p-0").children(
people.map((person) =>
h.li.class("flex items-center gap-3").children(
Avatar(h, {
key: person.email,
props: { src: person.src },
view: (avatar) =>
// The name is beside the picture, so the picture itself is decorative.
AvatarRoot(h, avatar).attr("aria-hidden", "true").children(
AvatarImage(h, avatar, { alt: "" }),
AvatarFallback(h, avatar)?.children(person.initials)
)
}),
h.div.class("min-w-0").children(
h.p.class("m-0 truncate text-sm font-medium").children(person.name),
h.p.class("m-0 truncate text-[13px] text-muted-foreground").children(person.email)
)
)
)
)
})
Anatomy
The parts the view closure receives. Apply each one to an element of your own.
| Part | Description |
|---|---|
root | The <span> that holds the image and the fallback. |
image | The <img>, returned once the current source has loaded and undefined before. |
fallback | The <span> returned after the delay while the image isn't loaded, undefined otherwise. |
imageLoadingStatus | "loading", "loaded" or "error", for markup that depends on it. |
API reference
Props
| Prop | Type | Description |
|---|---|---|
src | string | The picture. Without a src or a srcSet the status is "error" at once. |
srcSet, sizes | string | As the <img> attributes of the same names. |
crossOrigin | "anonymous" | "use-credentials" | The request's CORS mode. |
referrerPolicy | string | The request's referrer policy. |
fallbackDelay | number | Milliseconds the fallback waits before it shows, so a fast image doesn't flash it. 0 by default. |
Data attributes
| Attribute | Description |
|---|---|
(none) | The avatar sets no data attributes: a part is either in the page or not. |
Related components
The components this page's file and examples import.
- Button: A native button that can stay focusable while disabled.