Blocks

Avatar

A circular image with an optional text fallback, composed from three sub-helpers.

Installation

bin/rails g shadcnrb:component avatar

Usage

Compose sui.avatar do |a| with a.image and/or a.fallback inside a block.

JD
<%= sui.avatar do |a| %>
  <%= a.fallback "JD" %>
<% end %>
User U
<%= sui.avatar do |a| %>
  <%= a.image src: "https://i.pravatar.cc/100?img=3", alt: "User" %>
  <%= a.fallback "U" %>
<% end %>

Fallback on broken image

Pass a URL that fails to load (404, network error) and the fallback shows instead. The onerror handler removes the broken <img> so the sibling fallback takes over.

Missing SC
<%= sui.avatar do |a| %>
  <%= a.image src: "/does-not-exist.png", alt: "Missing" %>
  <%= a.fallback "SC" %>
<% end %>

Sizes

Pass size: to sui.avatar to adjust the circle diameter.

User U User U User U
<!-- size: :sm -->
<%= sui.avatar size: :sm do |a| %>
  <%= a.image src: "https://i.pravatar.cc/100?img=3", alt: "User" %>
  <%= a.fallback "U" %>
<% end %>

<!-- size: :default -->
<%= sui.avatar size: :default do |a| %>
  <%= a.image src: "https://i.pravatar.cc/100?img=3", alt: "User" %>
  <%= a.fallback "U" %>
<% end %>

<!-- size: :lg -->
<%= sui.avatar size: :lg do |a| %>
  <%= a.image src: "https://i.pravatar.cc/100?img=3", alt: "User" %>
  <%= a.fallback "U" %>
<% end %>

API

HelperPropTypeDefaultDescription
sui.avatarsizeSymbol:defaultCircle size — :sm (24 px), :default (32 px), :lg (40 px)
a.imagesrcStringImage URL (required)
a.imagealtString""Alt text for the image
a.fallbacknameStringnilInitials or text shown when the image is absent or fails to load
all helpers**optsHashAny extra HTML attributes are forwarded to the tag

Next: Button