Icon button
A square button that shows only an icon. Its name for assistive technology is a required argument.
Result
use hypersig::html::{div, p, span, attr};
use hypersig::style::pattern::{stack, wrap};
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, button, icon_button};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Buttons {
clicks: i32,
}
pub(super) fn page() -> Page<Buttons> {
let clicks = Buttons::clicks();
let count = clicks.set(clicks + 1);
let variants = [
(ButtonVariant::Default, "Default"),
(ButtonVariant::Secondary, "Secondary"),
(ButtonVariant::Outline, "Outline"),
(ButtonVariant::Ghost, "Ghost"),
(ButtonVariant::Destructive, "Destructive"),
(ButtonVariant::Link, "Link"),
];
Page::new(
"Buttons",
Buttons { clicks: 0 },
)
.child(
div()
.css(stack(rem(1)))
.child(div().css(wrap(rem(0.5))).children(variants.into_iter().map(
|(variant, text)| {
button(variant, ButtonSize::Default)
.on_click(count.clone())
.text(text)
},
)))
.child(
div()
.css(wrap(rem(0.5)))
.child(
button(ButtonVariant::Outline, ButtonSize::Sm).text("Small"),
)
.child(
button(ButtonVariant::Outline, ButtonSize::Lg).text("Large"),
)
.child(
button(ButtonVariant::Outline, ButtonSize::Default)
.attr(attr::DISABLED, "")
.text("Disabled"),
)
.child(
icon_button(
ButtonVariant::Outline,
"Add",
)
.text("+"),
),
)
.child(
p().text("Clicked ")
.child(span().bind_text(clicks.to_text()))
.text(" times"),
),
)
}Accessibility
It is a native <button type="button">, so it takes focus and Enter and Space press it, as Button does. An icon is not text a screen reader can read, so the button's name comes from aria-label, which the label argument fills: the screen reader announces "Louder, button" for the button in the example below. The + button in the demo above is one.
| Key | What it does |
|---|---|
| Enter, Space | Presses the button. |
`icon_button`
icon_button takes a variant and the name, and returns a square button 2.25rem on each side with no padding. It has no size argument:
pub fn icon_button<S>(variant: ButtonVariant, label: impl Into<String>) -> El<S>icon_button(ButtonVariant::Ghost, "More actions").text("…") renders as follows, without the class that carries the style. Add an <svg> with El::child in place of the text for a drawn icon:
<button type="button" aria-label="More actions">…</button>The name is a parameter, so an icon button without one fails to compile with E0061, this function takes 2 arguments but 1 argument was supplied:
use hypersig::Signals;
use hypersig_ui::{ButtonVariant, icon_button};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct App {}
// An icon button with no name for a screen reader to announce.
let more = icon_button::<App>(ButtonVariant::Ghost).text("…");What the types leave unchecked
- An empty label compiles and leaves the button without a name. Name the action, such as "Delete", rather than the icon, such as "Trash can".
aria-labelreplaces the button's text for assistive technology, so any text inside the button, such as…, is not read. Speech control software matches what the visitor says against the label.- An
<svg>you add stays visible to assistive technology unless it hasaria-hidden="true". Add that attribute, so the icon is not announced as an image after the name.
Example: a volume control
use hypersig::html::{attr, div, el, span};
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonVariant, icon_button};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Player {
volume: i32,
}
// A Lucide icon, drawn in the button's text color and hidden from
// assistive technology, which reads the button's label instead.
let plus = el(hypersig::tag!("svg"))
.attr(attr::ARIA_HIDDEN, "true")
.attr(attr::VIEW_BOX, "0 0 24 24")
.attr(attr::FILL, "none")
.attr(attr::STROKE, "currentColor")
.attr(attr::STROKE_WIDTH, "2")
.child(el(hypersig::tag!("path")).attr(attr::D, "M5 12h14"))
.child(el(hypersig::tag!("path")).attr(attr::D, "M12 5v14"));
let volume = Player::volume();
let page = Page::new("Player", Player { volume: 5 }).child(
div()
.child(
icon_button(ButtonVariant::Outline, "Quieter")
.on_click(volume.set(volume - 1))
.disabled(volume.le(0))
.text("−"),
)
.child(span().bind_text(volume.to_text()))
.child(
icon_button(ButtonVariant::Outline, "Louder")
.on_click(volume.set(volume + 1))
.disabled(volume.ge(10))
.child(plus),
),
);