Skip to main content

Menu

Struct Menu 

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

Source

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.

Source

pub fn trigger(self, variant: ButtonVariant, size: ButtonSize) -> El<S>

The button that opens and closes the menu.

Source

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

The menu, holding items in order.

Trait Implementations§

Source§

impl<S> Clone for Menu<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 Menu<S>

Source§

impl<S> Debug for Menu<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 Menu<S>

§

impl<S> RefUnwindSafe for Menu<S>

§

impl<S> Send for Menu<S>

§

impl<S> Sync for Menu<S>

§

impl<S> Unpin for Menu<S>

§

impl<S> UnsafeUnpin for Menu<S>

§

impl<S> UnwindSafe for Menu<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.
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.