Avatar
A round picture of a person, with their initials where there is no picture or it fails to load.
Result
use hypersig::html::div;
use hypersig::style::pattern::hstack;
use hypersig::style::value::rem;
use hypersig::{Page, SafeUrl, Signals};
use hypersig_ui::{Avatar, ButtonSize, ButtonVariant, button};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Team {
picture: SafeUrl,
}
const LOGO: SafeUrl = SafeUrl::new("https://imaimai17468.github.io/hypersig/icon.svg");
const MISSING: SafeUrl = SafeUrl::new("https://imaimai17468.github.io/hypersig/missing.png");
pub(super) fn page() -> Page<Team> {
let picture = Team::picture();
Page::new(
"Team",
Team { picture: LOGO },
)
.child(
div()
.css(hstack(rem(0.75)))
.child(Avatar::new("hypersig", "HS").picture(picture))
.child(Avatar::new("Grace Hopper", "GH"))
.child(
button(ButtonVariant::Outline, ButtonSize::Sm)
.on_click(picture.set(MISSING))
.text("Point at a missing picture"),
)
.child(
button(ButtonVariant::Outline, ButtonSize::Sm)
.on_click(picture.set(LOGO))
.text("Restore"),
),
)
}Accessibility
The avatar is a role="img" named after the person through aria-label, so a screen reader reads the name once, as an image, and nothing inside it: neither the initials nor the picture's alt. The avatar takes no keys.
Without a picture it shows the initials. With one, it shows the picture, and where the picture fails to load, the broken <img> draws the initials over itself through its ::after. Chrome draws that ::after. A browser that draws no ::after on a broken image shows its own marker for one instead.
Building an avatar
Avatar::new takes the person's name, which a screen reader reads, and the initials shown in place of a picture. picture adds a picture from a checked URL:
impl<S> Avatar<S> {
pub fn new(name: impl Into<String>, initials: impl Into<String>) -> Self;
pub fn picture(self, url: impl IntoHref<S>) -> Self;
pub fn render(self) -> El<S>;
}
impl<S> From<Avatar<S>> for El<S>;
impl<S> From<Avatar<S>> for Node<S>;url | Where the picture comes from |
|---|---|
A SafeUrl constant, SafeUrl::new("https://…") | A URL checked when the program is compiled |
A Sig<S, SafeUrl> | A signal, so the browser swaps the picture when a statement or a patch changes it |
| A route's link | A page or a file the server serves |
El::child takes the Avatar itself, and render returns the <span> as an El<S> for a method of El. Values the types do not check covers SafeUrl and the URLs it checks.
The HTML
The example at the end of this page renders these two avatars, with the classes left out. The initials of the first are the alt of its picture:
<span role="img" aria-label="Ada Lovelace">
<img src="https://example.com/ada.png" alt="AL">
</span>
<span role="img" aria-label="Grace Hopper">GH</span>What fails to compile
picture takes impl IntoHref<S>, which a &str or a String does not implement, so a URL from a visitor's input cannot reach src unchecked. It is E0277, with a message that says the string is not a URL:
use hypersig::Signals;
use hypersig_ui::Avatar;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Team {}
let user_input = String::from("javascript:alert(1)");
// A string is not a checked URL.
let ada = Avatar::<Team>::new("Ada Lovelace", "AL").picture(user_input.as_str());The name and the initials are both required, so an avatar with no name for a screen reader is E0061.
What is not checked
Nothing checks that the initials match the name, or that they fit: the avatar is 2rem across, and more than two or three letters overflow it and are cut off. Nothing checks that the picture exists either. A missing one shows the initials in Chrome and the browser's own marker for a broken image elsewhere, as the demo's first button shows.
Example: a team
The first avatar's picture comes from a signal, and the second shows initials alone:
use hypersig::html::div;
use hypersig::{Page, SafeUrl, Signals};
use hypersig_ui::Avatar;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Team {
picture: SafeUrl,
}
const ADA: SafeUrl = SafeUrl::new("https://example.com/ada.png");
let html = Page::new("Team", Team { picture: ADA })
.child(
div()
// The picture comes from a signal, so the server can change it.
.child(Avatar::new("Ada Lovelace", "AL").picture(Team::picture()))
.child(Avatar::new("Grace Hopper", "GH")),
)
.render();Badge puts a short label, such as a role, next to the avatar, and Card holds a person's details around it.