pub struct Menu<S> { /* private fields */ }Expand description
A menu of actions on a page whose signals are S, opened by a button.
Declare one as a const and use the same value for the button and the
menu, so they agree on the menu’s id. It follows the WAI-ARIA menu button
pattern: the button has aria-haspopup="menu", the menu role="menu" and
each action role="menuitem". The menu is a popover, so the browser
puts it above the page, closes it on Escape or on a click outside, and
returns focus to the button. When it opens, focus moves to the first
action. The arrow keys, Home, and End move between actions, an action
runs its statement and closes the menu, and Tab closes it.
The menu is placed under the button with CSS anchor positioning and flips
above it where there is no room below. The menu carries a node ref with
its name, so two menus with one name on a page make
Page::render panic.
use hypersig::Signals;
use hypersig_ui::{ButtonSize, ButtonVariant, Menu, MenuItem};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Doc {
archived: bool,
}
const ACTIONS: Menu<Doc> = Menu::new("doc-actions");
let open = ACTIONS.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Actions");
let menu = ACTIONS.menu([
MenuItem::new("Archive", Doc::archived().set(true)),
MenuItem::new("Delete", Doc::archived().set(false)).destructive(),
]);
assert!(open.render().contains(r#"aria-haspopup="menu""#));
assert!(menu.render().contains(r#"role="menuitem""#));Implementations§
Source§impl<S> Menu<S>
impl<S> Menu<S>
Sourcepub const fn new(name: &'static str) -> Self
pub const fn new(name: &'static str) -> Self
A menu 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>
The button that opens and closes the menu.
The menu, holding items in order.