Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Avatar RS

Edit Component

Y Avatar Yew Usage

Adding Avatar to your project is simple:

  1. Make sure your project is set up with Yew. Follow their Getting Started Guide for setup instructions.

  2. Add the Avatar component to your dependencies by including it in your Cargo.toml file:

    cargo add avatar --features=yew
    
  3. Import the Avatar, Image, and Fallback components into your Yew component and start using them in your app.

🛠️ Usage

Basic Avatar with Image

use avatar::yew::{Avatar, Image, Fallback};
use yew::prelude::*;

#[function_component(MyAvatar)]
pub fn my_avatar() -> Html {
    html! {
        <Avatar aria_label="Ferris Prophet">
            <Image src="https://i.pravatar.cc/300" alt="Ferris Prophet" />
            <Fallback>{"FP"}</Fallback>
        </Avatar>
    }
}

Initials Only (No Image)

use avatar::yew::{Avatar, Fallback};
use avatar::Color;
use yew::prelude::*;

#[function_component(InitialsAvatar)]
pub fn initials_avatar() -> Html {
    html! {
        <Avatar aria_label="Ferris Prophet">
            <Fallback color={Some(Color::Accent)}>{"FP"}</Fallback>
        </Avatar>
    }
}

Different Sizes

use avatar::yew::{Avatar, Fallback};
use avatar::{Color, Size};
use yew::prelude::*;

#[function_component(SizedAvatars)]
pub fn sized_avatars() -> Html {
    html! {
        <>
            <Avatar size={Size::Sm}><Fallback color={Some(Color::Success)}>{"SM"}</Fallback></Avatar>
            <Avatar size={Size::Md}><Fallback color={Some(Color::Warning)}>{"MD"}</Fallback></Avatar>
            <Avatar size={Size::Lg}><Fallback color={Some(Color::Danger)}>{"LG"}</Fallback></Avatar>
        </>
    }
}

Fallback Delay

Prevents a flash-of-fallback on fast image loads.

use avatar::yew::{Avatar, Image, Fallback};
use yew::prelude::*;

#[function_component(DelayedFallback)]
pub fn delayed_fallback() -> Html {
    html! {
        <Avatar aria_label="User">
            <Image src="https://i.pravatar.cc/300" alt="User" />
            <Fallback delay_ms={600}>{"US"}</Fallback>
        </Avatar>
    }
}

🔧 Props

Avatar

PropertyTypeDescriptionDefault
sizeSizeDimensions of the avatar.Size::Md
colorColorColor theme for the fallback.Color::Default
variantVariantDefault (solid) or Soft (tinted).Variant::Default
class&'static strExtra CSS classes on the root element.""
style&'static strExtra inline CSS on the root element.""
id&'static strid attribute on the root element.""
aria_label&'static strAccessible name for screen readers."Avatar"
tabindex&'static strTab focus order."-1"
container_class&'static strCSS class on the root <span>."avatar"
container_style&'static strExtra inline CSS on the root <span>.""

Image

PropertyTypeDescriptionDefault
src&'static strImage source URL.""
srcset&'static strResponsive srcset attribute.""
sizes&'static strResponsive sizes attribute.""
alt&'static strAlternative text (required for a11y).""
class&'static strCSS class on the <img>."avatar__image"
style&'static strExtra inline CSS on the <img>.""
loading&'static str"lazy" or "eager"."eager"
crossorigin&'static strCORS setting.""
decoding&'static str"auto", "async", or "sync"."auto"
referrerpolicy&'static strReferrer policy.""
id&'static strid on the <img>.""
widthOption<u32>Pixel width.None
heightOption<u32>Pixel height.None
on_loadCallback<Event>Called when image loads.no-op
on_errorCallback<Event>Called when image fails.no-op

Fallback

PropertyTypeDescriptionDefault
delay_msu32Ms before fallback appears.0
colorOption<Color>Color override.None (inherits from Avatar)
variantOption<Variant>Variant override.None (inherits from Avatar)
class&'static strCSS class on the fallback <span>."avatar__fallback"
style&'static strExtra inline CSS on the fallback.""
id&'static strid on the fallback <span>.""

💡 Notes

  • Image and Fallback must be used inside an Avatar component, they rely on its context.
  • The fallback is shown until the image loads. Set delay_ms to prevent a flash on fast connections.
  • Use Color and Variant props to match your design system.
  • Looking for layered or clipped groups? Check out the Group API!

Usage

CLI Usage

You can import Avatar RS directly into your project to customize and modify it using the opensass cli:

os add avatar yew