pub struct ContextMenu<S> { /* private fields */ }Expand description
A menu of actions that opens where the person asks for a context menu.
It sits on a page whose signals are S.
Declare one as a const from a NodeRef that
node_ref! declares, place its
menu once on the page, and open it from
El::on_context_menu with open_at. An element that
opens it, such as a row of a list, sets the signals that tell the actions
which row they act on in the same statement. The menu has role="menu"
and its actions role="menuitem", and it is a popover, as Menu’s
is, so it takes the same keys: focus moves to the first action that is
not disabled, the arrow keys, Home, and End move between actions, and
Escape, Tab, or a click outside closes it.
Safari and the web view of macOS send no context menu event from a key,
so on those a person with no pointer cannot open it. Give its actions
another way in as well, such as a Menu button on the row.
use hypersig::html::{each, li, ul};
use hypersig::{NodeRef, Signals};
use hypersig_ui::{ContextMenu, MenuItem};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize, Clone, PartialEq)]
struct Message {
id: i32,
text: String,
}
#[derive(Signals, Serialize, Deserialize)]
struct Chat {
messages: Vec<Message>,
picked: i32,
}
hypersig::node_ref!(const MESSAGE_MENU: NodeRef<Chat>);
const MESSAGE_ACTIONS: ContextMenu<Chat> = ContextMenu::new(MESSAGE_MENU);
let list = ul::<Chat>().child(each(Chat::messages(), |message| {
li().bind_text(message.at(Message::text())).on_context_menu(move |at| {
Chat::picked().set(message.at(Message::id())).then(MESSAGE_ACTIONS.open_at(at))
})
}));
let menu = MESSAGE_ACTIONS.menu("Message", [MenuItem::new("Delete", Chat::picked().set(0))]);
assert!(list.render().contains("data-hs-on-contextmenu"));
assert!(menu.render().contains(r#"aria-label="Message""#));Implementations§
Source§impl<S> ContextMenu<S>
impl<S> ContextMenu<S>
Sourcepub const fn new(menu: NodeRef<S>) -> Self
pub const fn new(menu: NodeRef<S>) -> Self
A context menu that menu names, whose id is the name of menu.
Sourcepub fn open_at(self, at: impl IntoExpr<S, Point>) -> Stmt<S>
pub fn open_at(self, at: impl IntoExpr<S, Point>) -> Stmt<S>
A statement that opens the menu with its top left corner at at.
Where the menu would cross the viewport’s right or bottom edge, it
opens left of at or above it. Opening it while it is open moves it.
The menu, named label for a screen reader, holding items in order.