Skip to main content

ContextMenu

Struct ContextMenu 

Source
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>

Source

pub const fn new(menu: NodeRef<S>) -> Self

A context menu that menu names, whose id is the name of menu.

Source

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.

Source

pub fn menu( self, label: impl Into<String>, items: impl IntoIterator<Item = MenuItem<S>>, ) -> El<S>

The menu, named label for a screen reader, holding items in order.

Trait Implementations§

Source§

impl<S> Clone for ContextMenu<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S> Copy for ContextMenu<S>

Source§

impl<S> Debug for ContextMenu<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for ContextMenu<S>

§

impl<S> RefUnwindSafe for ContextMenu<S>

§

impl<S> Send for ContextMenu<S>

§

impl<S> Sync for ContextMenu<S>

§

impl<S> Unpin for ContextMenu<S>

§

impl<S> UnsafeUnpin for ContextMenu<S>

§

impl<S> UnwindSafe for ContextMenu<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more