Tooltip
A short description shown while a button is hovered or focused.
Result
tooltip.rs
use hypersig::html::div;
use hypersig::style::pattern::hstack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Tooltip};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub struct Editor {
bold: bool,
}
const BOLD: Tooltip<Editor> = Tooltip::new("bold-tip");
const SHARE: Tooltip<Editor> = Tooltip::new("share-tip");
pub fn page() -> Page<Editor> {
Page::new(
"Editor",
Editor { bold: false },
)
.child(
div()
.css(hstack(rem(0.75)))
.child(
BOLD.icon_trigger(
ButtonVariant::Outline,
"Bold",
)
.on_click(Editor::bold().toggle())
.text("B"),
)
.child(BOLD.tooltip("Ctrl+B"))
.child(
SHARE
.trigger(ButtonVariant::Outline, ButtonSize::Default)
.text("Share"),
)
.child(SHARE.tooltip("Copies a link to this document")),
)
}Accessibility
It follows the WAI-ARIA tooltip pattern. The tooltip has role="tooltip" and the button aria-describedby pointing at it, so a screen reader reads the text after the button's name. Tooltip builds the button and puts on it the attributes the tooltip needs.
The tooltip is a popover="hint", which browser-compat-data 8.1.6 lists for Chrome 151 and Firefox 153, with partial older implementations in earlier versions, and for Safari only in its preview. A browser without it treats the value as manual, and there Escape closes the tooltip only while focus is on its button.
| Key | What it does |
|---|---|
| Tab | Focusing the button opens the tooltip, and leaving it closes the tooltip. |
| Any key | With focus on the button, closes the tooltip. Escape also reaches a dialog around the tooltip. |
API
| Item | Summary |
|---|---|
Tooltip::new | A tooltip whose id is name. |
trigger | A button described by the tooltip. |
icon_trigger | An icon button named label and described by the tooltip. |
tooltip | The tooltip, holding text. |