Components › Components

On this page

Avatar

A round picture of someone, or their initials.

clarusjs AL

Usage #

tscl
@import uiAvatar from './avatar.tscl'

<div class="demo">
  <uiAvatar src="/logo.svg" alt="clarusjs" />
  <uiAvatar alt="Ada Lovelace" fallback="AL" />
</div>

<style scoped>
  :scope {
    display: flex;
    gap: 12px;
    padding: 24px;
    border: 1px solid var(--ui-border);
    border-radius: var(--ui-radius);
    background: var(--ui-bg);
    color: var(--ui-fg);
  }
</style>

Source #

Copy it into your components folder (next to the others) as avatar.tscl.

tscl
{{-- Avatar: a round picture of someone, or their initials when there's no picture. alt names them either way. --}}
@input parts: { image?: Record<string, string | boolean>; fallback?: Record<string, string | boolean> } = {}
@input src: string = ''
@input alt: string
@input fallback: string = ''

<span data-part="avatar">
  @if (src) {
    <img [...]="parts.image ?? {}" [src]="src" [alt]="alt" data-part="avatar-image" />
  } @else {
    <span [...]="parts.fallback ?? {}" role="img" [aria-label]="alt" data-part="avatar-fallback">{{ fallback }}</span>
  }
</span>

<style scoped>
  @layer theme, base, components, utilities;
  @layer components {
    :where(:scope) {
      display: inline-flex;
      width: 40px;
      height: 40px;
      overflow: hidden;
      border-radius: 50%;
      background: var(--ui-accent-soft);
      color: var(--ui-fg);
      font: 600 var(--ui-font-size) var(--ui-font);
    }
    :where([data-part='avatar-image']) {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    :where([data-part='avatar-fallback']) {
      display: grid;
      place-items: center;
      width: 100%;
    }
  }
</style>

Styling #

Needs #