Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fallback avatar to initials on image load error #326

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions thaw/src/avatar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn Avatar(
let name = StoredValue::new(name);
let src = StoredValue::new(src);
let initials = StoredValue::new(initials);
let image_failed = RwSignal::new(false);
let is_show_default_icon = Memo::new(move |_| {
if name.with_value(|n| n.with(|n| n.is_some())) {
false
Expand Down Expand Up @@ -85,10 +86,10 @@ pub fn Avatar(
}
}}
{move || {
let src = src.with_value(|s| s.get());
let src = src.with_value(|s| if image_failed.get() { None } else { s.get() });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the first image loading fails, modify the src, and the image will never be displayed.

tips: MaybeProp has implemented Copy, and the src prop can remove the StoredValue wrapper.

view! {
<OptionComp value=src let:src>
<img src=src class="thaw-avatar__image" />
<img src=src class="thaw-avatar__image" on:error=move |_| image_failed.set(true) />
</OptionComp>
}
}}
Expand Down
Loading