pub struct Tooltip<S> { /* private fields */ }Expand description
A tooltip on a page whose signals are S, describing the button it is
attached to.
Declare one as a const and use the same value for the button and the
tooltip, so they agree on the tooltip’s id. 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. It opens while the pointer is on the button or
the tooltip, or the button has focus, and closes when both are left or a
key is pressed on the button.
The tooltip is a popover="hint", which the browser closes on Escape
without closing an open menu. browser-compat-data 8.1.6 lists hint
popovers for Chrome 151 and Firefox 153, an older partial version for
Chrome 133 to 150 and Firefox 149 to 152, and Safari only in its
preview. A browser without them treats the value as manual, and there
Escape closes the tooltip only while focus is on its button.
The button carries the attributes the tooltip needs, so build it here
rather than adding a tooltip to a button made elsewhere. The tooltip
carries a node ref with its name, so two tooltips with one name on a page
make Page::render panic.
use hypersig::Signals;
use hypersig_ui::{ButtonVariant, Tooltip};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Editor {}
const BOLD: Tooltip<Editor> = Tooltip::new("bold-tip");
let button = BOLD.icon_trigger(ButtonVariant::Outline, "Bold").text("B");
let tip = BOLD.tooltip("Ctrl+B");
assert!(button.render().contains(r#"aria-describedby="bold-tip""#));
assert!(tip.render().contains(r#"role="tooltip""#));Implementations§
Source§impl<S> Tooltip<S>
impl<S> Tooltip<S>
Sourcepub const fn new(name: &'static str) -> Self
pub const fn new(name: &'static str) -> Self
A tooltip whose id is name.
§Panics
Panics if name is empty or holds a character other than an ASCII
letter, digit, -, or _. In a const, that is a compile error.
Sourcepub fn trigger(self, variant: ButtonVariant, size: ButtonSize) -> El<S>
pub fn trigger(self, variant: ButtonVariant, size: ButtonSize) -> El<S>
A button described by the tooltip.
Sourcepub fn icon_trigger(
self,
variant: ButtonVariant,
label: impl Into<String>,
) -> El<S>
pub fn icon_trigger( self, variant: ButtonVariant, label: impl Into<String>, ) -> El<S>
An icon button named label and described by the tooltip.