Components
Avatar
Who or what something belongs to, in a circle: a picture cropped round, or the name's initials in White DM Sans Medium on #262626. Three sizes.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add avatar<script type="module" src="/spyglass-ds/elements/avatar.js"></script>Usage
A board's members, the account in a header, who saved a creative.
Never
- A real person's photo or name in examples: use Acme
- A spectrum color behind the initials
- The logomark as an avatar for anything but the brand itself
Props
The web component takes the same as attributes or properties (<spyglass-avatar>); title is heading there.
<Avatar>
| Prop | Type | Default |
|---|---|---|
name | string Whose it is: its initials, and the name for assistive tech. | |
src | string A picture, cropped round. | |
size | AvatarSize sm (24px), md (32px) or lg (40px). | 'md' |
Examples
The same example in React and as web components; both render identically.
import { Avatar } from '@/react/components/ui/Avatar'
// Initials in three sizes, and a board's members stacked with a Black ring between them.
const members = ['Acme Studio', 'Acme Growth', 'Acme Brand', 'Acme Social']
export default function Sizes() {
return (
<div className="flex flex-wrap items-center gap-8">
<div className="flex items-center gap-3">
<Avatar name="Acme Studio" size="sm" />
<Avatar name="Acme Studio" size="md" />
<Avatar name="Acme Studio" size="lg" />
</div>
<div className="flex items-center">
{members.map((m, i) => <span key={m} className={i ? '-ml-2 inline-flex rounded-full border-2 border-surface' : 'inline-flex rounded-full border-2 border-surface'}><Avatar name={m} /></span>)}
<span className="ml-3 type-caption text-fg-muted">4 members</span>
</div>
</div>
)
}<!-- Initials in three sizes, and a board's members stacked with a Black ring between them. -->
<div class="flex flex-wrap items-center gap-8">
<div class="flex items-center gap-3">
<spyglass-avatar name="Acme Studio" size="sm"></spyglass-avatar>
<spyglass-avatar name="Acme Studio" size="md"></spyglass-avatar>
<spyglass-avatar name="Acme Studio" size="lg"></spyglass-avatar>
</div>
<div class="flex items-center">
<span class="inline-flex rounded-full border-2 border-surface"><spyglass-avatar name="Acme Studio"></spyglass-avatar></span>
<span class="-ml-2 inline-flex rounded-full border-2 border-surface"><spyglass-avatar name="Acme Growth"></spyglass-avatar></span>
<span class="-ml-2 inline-flex rounded-full border-2 border-surface"><spyglass-avatar name="Acme Brand"></spyglass-avatar></span>
<span class="-ml-2 inline-flex rounded-full border-2 border-surface"><spyglass-avatar name="Acme Social"></spyglass-avatar></span>
<span class="ml-3 type-caption text-fg-muted">4 members</span>
</div>
</div>Source
'use client'
import { useState, type HTMLAttributes } from 'react'
import { cn } from '@/react/lib/cn'
// shadcn's avatar in the brand: who or what something belongs to, in a circle. A `src` shows a picture cropped round;
// without one, or if it fails to load, the name's initials (two at most) in White DM Sans Medium on #262626. Sizes sm
// (24px, in a row), md (32px) and lg (40px). The name is read out; the picture is decoration.
export type AvatarSize = 'sm' | 'md' | 'lg'
const SIZES: Record<AvatarSize, string> = { sm: 'size-6 text-label', md: 'size-8 text-caption', lg: 'size-10 text-ui' }
export interface AvatarProps extends Omit<HTMLAttributes<HTMLSpanElement>, 'children'> {
/** Whose it is: its initials, and the name for assistive tech. */
name?: string
/** A picture, cropped round. */
src?: string
/** sm (24px), md (32px) or lg (40px). */
size?: AvatarSize
}
/** The first letters of the first two words. */
export function initials(name = '') {
return name.trim().split(/\s+/).slice(0, 2).map((w) => w[0] ?? '').join('').toUpperCase()
}
export function Avatar({ name, src, size = 'md', className, ...rest }: AvatarProps) {
const [failed, setFailed] = useState(false)
return (
<span
data-ds="avatar"
className={cn('inline-flex shrink-0 items-center justify-center overflow-hidden rounded-full bg-action-secondary leading-none font-medium text-fg select-none', SIZES[size] ?? SIZES.md, className)}
role={name ? 'img' : undefined}
aria-label={name}
aria-hidden={name ? undefined : 'true'}
{...rest}
>
{src && !failed ? <img src={src} alt="" className="size-full object-cover" onError={() => setFailed(true)} /> : initials(name)}
</span>
)
}
export default Avatar// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]
export function cn(...values: ClassValue[]): string {
const out: string[] = []
for (const v of values) {
if (!v) continue
if (Array.isArray(v)) {
const s = cn(...v)
if (s) out.push(s)
}
else out.push(v)
}
return out.join(' ')
}import { html } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import styles from 'virtual:tw'
// Avatar.tsx as <spyglass-avatar>: who or what something belongs to, in a circle. A `src` shows a picture cropped round;
// without one, or if it fails to load, the name's initials (two at most) in White DM Sans Medium on #262626. Sizes sm
// (24px), md (32px) and lg (40px). The name is read out; the picture is decoration.
export type AvatarSize = 'sm' | 'md' | 'lg'
const SIZES: Record<AvatarSize, string> = { sm: 'size-6 text-label', md: 'size-8 text-caption', lg: 'size-10 text-ui' }
const initials = (name = '') => name.trim().split(/\s+/).slice(0, 2).map((w) => w[0] ?? '').join('').toUpperCase()
/**
* @summary Who or what something belongs to: a picture or initials in a circle.
*/
export class Avatar extends DsElement {
static styles = [DsElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="avatar"]'))
static properties = { name: {}, src: {}, size: { reflect: true }, failed: { state: true } }
/** Whose it is: its initials, and the name for assistive tech. */
name?: string
/** A picture, cropped round. */
src?: string
/** sm (24px), md (32px) or lg (40px). */
size: AvatarSize = 'md'
private failed = false
render() {
const classes = ['inline-flex shrink-0 items-center justify-center overflow-hidden rounded-full bg-action-secondary leading-none font-medium text-fg select-none', SIZES[this.size] ?? SIZES.md].join(' ')
return html`<span data-ds="avatar" class=${classes} role=${ifDefined(this.name ? 'img' : undefined)} aria-label=${ifDefined(this.name)} aria-hidden=${ifDefined(this.name ? undefined : 'true')}>${this.src && !this.failed ? html`<img src=${this.src} alt="" class="size-full object-cover" @error=${() => { this.failed = true }}>` : initials(this.name)}</span>`
}
}
define('avatar', Avatar)Reference: Derived from the guidelines: the neutral steps (#262626 fill), White DM Sans Medium labels. shadcn's avatar for anatomy and the image fallback..