# Menu

Crate: `libero`
Import: `use libero::components::{Menu, MenuEntry, MenuItem, use_menu};`
Feature: `menu` (off by default, turns on `anchor`, `kbd`, `divider`, `paper`, `use-popover`, `use-swipe`, `use-timers`, `use-media-query`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/overlay/menu>
Index: [index.md](index.md) lists every other page
Description: A list of commands that drops from a trigger, with groups, separators and submenus.

A list of commands that drops from a trigger. The items are data, not
children. `use_menu()` keeps the open state in your scope, and the trigger is
your own `Button`, wired by `menu.a11y_attributes()`. The menu is portaled, so
no `overflow: hidden` ancestor clips it.

## Usage

```rust
use dioxus::prelude::*;
use libero::{
    components::{Button, Flex, Menu, MenuEntry, MenuItem, Text, use_menu},
    sx::sx,
    theme::Size,
};

#[component]
fn Demo() -> Element {
    let menu = use_menu();
    let mut last = use_signal(|| String::from("nothing yet"));
    let pick = move |name: &'static str| move |_| last.set(name.to_string());
    let mut sort = use_signal(|| "Name");
    let mut hidden = use_signal(|| false);
    let sort_by = move |name: &'static str| -> MenuEntry {
        MenuItem::new(name)
            .radio(sort() == name)
            .onselect(move |_| sort.set(name))
            .into()
    };

    let items = vec![
        MenuEntry::Group {
            label: "Edit".into(),
            items: vec![
                MenuItem::new("Cut")
                    .shortcut("Control+X")
                    .onselect(pick("Cut"))
                    .into(),
                MenuItem::new("Copy")
                    .shortcut("Control+C")
                    .onselect(pick("Copy"))
                    .into(),
                MenuItem::new("Paste")
                    .shortcut("Control+V")
                    .disabled(true)
                    .description("The clipboard is empty")
                    .onselect(pick("Paste"))
                    .into(),
            ],
        },
        MenuEntry::Separator,
        MenuItem::new("Save").onselect(pick("Save")).into(),
        MenuItem::new("Save as").onselect(pick("Save as")).into(),
        MenuItem::new("Zoom in")
            .keep_open()
            .onselect(pick("Zoom in"))
            .into(),
        MenuItem::new("Share")
            .submenu(vec![
                MenuItem::new("Copy link")
                    .onselect(pick("Copy link"))
                    .into(),
                MenuItem::new("Email").onselect(pick("Email")).into(),
                MenuItem::new("Messages").onselect(pick("Messages")).into(),
            ])
            .into(),
        MenuItem::new("Documentation")
            .href("https://libero-ui.dev")
            .new_tab_icon(true)
            .into(),
        MenuEntry::Separator,
        MenuEntry::Group {
            label: "Sort by".into(),
            items: vec![sort_by("Name"), sort_by("Date"), sort_by("Size")],
        },
        MenuItem::new("Show hidden")
            .checkbox(hidden())
            .onselect(move |_| hidden.toggle())
            .into(),
        MenuEntry::Separator,
        MenuItem::new("Delete")
            .color("error")
            .onselect(pick("Delete"))
            .into(),
    ];

    rsx! {
        Flex {
            direction: "row",
            align: "center",
            gap: "md",
            Menu {
                state: menu,
                items,
                sx: sx().breakpoint(
                    Size::Sm,
                    sx().width("var(--lsx-popover-anchor-width)")
                        .max_width("none")
                        .margin_top("var(--lsx-popover-gap)")
                        .box_shadow("none"),
                ),
                Button {
                    variant: "light",
                    radius: "0",
                    attributes: menu.a11y_attributes(),
                    sx: sx().width("100%").height("52px"),
                    "Actions"
                }
            }
            Text { size: "sm", role: "status", "Last chosen: {last()}" }
        }
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `state` | `MenuState` | required | From `use_menu()`. Holds the open state and wires the trigger to the menu. |
| `items` | `Vec<MenuEntry>` | required | The menu, in order. `MenuEntry::Item`, `MenuEntry::Group { label, items }` for a named section, and `MenuEntry::Separator`. |
| `children` | `Element` | required | The trigger, carrying `menu.a11y_attributes()`. It needs no click or key handler of its own. |
| `side` | `Responsive<Side>` | `Bottom` | Which side of the trigger the menu opens on. It flips when that side has no room. `Start`/`End` are logical: `Start` is the left under `dir="ltr"`, the right under `rtl`. Or one per breakpoint, `side: responsive(Side::Bottom).md(Side::End)`, read when the menu opens and on a resize. Breakpoints follow the window, not the parent. |
| `align` | `Responsive<Align>` | `Start` | Where the menu lines up along that side. Or one per breakpoint, `align: responsive(Align::Start).md(Align::End)`. |
| `close_on_select` | `bool` | `true` | Whether choosing an item closes the menu. |
| `loop_focus` | `bool` | `true` | Whether the arrow keys wrap from the last item to the first. |
| `size` | `Responsive<Size>` | `md` | Item height and font size. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. Breakpoints follow the window, not the parent. |
| `sheet_size` | `Size` | `size` | Item height and font size on the phone sheet, below the `sm` breakpoint, e.g. `sheet_size: "lg"` for larger rows on a phone and compact ones on a desktop. Submenus follow it. |
| `radius` | `Responsive<ThemeAwareValue>` | `sm` | The menu's corner radius, or any CSS, e.g. `radius: "0"`. The items' corners follow it. Or one per breakpoint, `radius: responsive(Size::Xs).md(Size::Lg)`. Breakpoints follow the window, not the parent. |
| `disabled` | `bool` | `false` | The trigger opens nothing, and an open menu closes. Disable the trigger too, so it looks disabled. Set it while the trigger is `loading` too: a press on a busy button opens nothing, but the arrow keys still reach the menu. |
| `onedge` | `Callback<MenuEdge>` | - | Called with ArrowLeft on the top level, or ArrowRight on an item without a submenu. `Menubar` uses it to move to the next menu. |
| `parts` | `Parts<MenuPart>` | - | Styles for the inner parts in the Style API tab, on every menu level, submenus too, as `sx` does: `Parts::new().part(MenuPart::Label, sx().font_weight("500"))`. |

Like every component, `Menu` also takes the shared props `sx`, `class`,
`style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes. They land on the menu; `sx`
also on every submenu.

### MenuItem

| Method | Type | Default | Description |
|---|---|---|---|
| `new(label)` | `String` | required | The visible text, the accessible name, and what typeahead matches. |
| `onselect` | `FnMut(())` | - | Runs when the item is chosen by a click, Enter or Space. |
| `submenu` | `Vec<MenuEntry>` | - | Opens a second menu beside the item instead, on Right, a click, or when the pointer rests on the item for 150ms. Moving to a sibling waits as long, so the pointer can cross one on its way in. An item runs a command or opens a submenu, and the later call wins. |
| `href` | `String` | - | Makes the item a link: an `<a>` that opens the URL in a new tab, so middle-click and the context menu work. Space activates it like Enter. Replaces `onselect` and `submenu`. A disabled link has no `href`. The label ends in a hidden "(opens in a new tab)", as on `Anchor`. |
| `new_tab_hint` | `bool` | `true` | With `href`: `false` drops the hidden hint and the new-tab icon. |
| `new_tab_icon` | `bool` | `false` | With `href`: draws the new-tab icon after the label. Unset, the theme's `anchor.new_tab_icon`, which is off. |
| `keep_open` | `()` | - | Choosing the item leaves the menu open, and focus stays on it, whatever the menu's `close_on_select` says. `close_on_select(bool)` overrides it either way. |
| `leading` | `Element` | - | Before the label, such as an icon. Nothing interactive, since it sits inside the item's button. |
| `trailing` | `Element` | - | At the far end, such as a badge. It joins the accessible name. Nothing interactive. |
| `shortcut` | `&str` | - | The key that runs the item outside the menu, in `aria-keyshortcuts` syntax (`"Control+X"`). Drawn as a hint ("Ctrl+X") and kept out of the name. `Control`, `Shift`, `Alt` and `Meta` are drawn in the localization's `menu` words ("Strg+Umschalt+S" in German). You bind the key yourself. |
| `radio` | `bool` | - | Makes the item one choice of several, with a check while `true`. Put the choices in one `Group` and keep one checked. A menu opens on its checked item. |
| `mark` | `MenuRadioMark` | `Check` | How a `radio` item shows that it is the current choice. `Check` draws a check in the leading column. `Background` tints the row and marks its start edge, with no check column unless another item needs one. It stays `aria-checked`, and a checkbox keeps its check. |
| `checkbox` | `bool` | - | Makes the item an on/off setting, with a check while `true`. Flip it in `onselect`. An item is `radio` or `checkbox`, and the later call wins. |
| `disabled` | `bool` | `false` | Stays in the arrow-key order but cannot be chosen, and typeahead skips it. |
| `description` | `String` | - | Read after the label through `aria-describedby`, never shown. Say why a `disabled` item cannot be chosen. |
| `color` | `ThemeAwareValue` | - | Colours the label and `leading`, such as `"error"` for a destructive command. A palette colour takes its readable text shade. The trailing content, shortcut and check keep the menu's colour. |

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Style API in Styling](styling.md#style-api)
explains how parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `MenuPart::Item` | `item` | A row: `menuitem`, `menuitemradio` or `menuitemcheckbox`. |
| `MenuPart::GroupLabel` | `group-label` | A group's visible name. |
| `MenuPart::Check` | `check` | The check column, on every row of a level with a checkable item. |
| `MenuPart::Leading` | `leading` | The item's `leading` content. |
| `MenuPart::Label` | `label` | The item's text. |
| `MenuPart::Trailing` | `trailing` | The item's `trailing` content. |
| `MenuPart::Shortcut` | `shortcut` | The key hint, hidden from screen readers. |
| `MenuPart::Chevron` | `chevron` | A submenu item's arrow. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Enter`, `Space` or `Down` | On the trigger: opens the menu on the checked radio item, else its first item. |
| `Up` | On the trigger: opens the menu on its last item. |
| `Down` or `Up` | In the menu: moves an item, wrapping unless `loop_focus` is off. |
| `Home` or `End` | In the menu: goes to the first or last item. |
| `Enter` or `Space` | In the menu: chooses the item. |
| `Right` | Opens a submenu. |
| `Left` | Closes a submenu again. |
| `Escape` | Closes only the menu it is pressed in and returns focus to what opened it. |
| `Tab` | Closes every level and moves on from the trigger. |

### Libero handles

- Typing jumps to an item, "s" to the next one starting with S and "sav" to
  Save. A pause of half a second starts over.
- `menu.a11y_attributes()` wires your trigger. `menu.a11y_attributes_on(attributes)` wires a trigger that already has an `id`, such as a `FileField` trigger's: the menu keeps that id and takes the trigger's name.
- Below the `sm` breakpoint the menu opens as a sheet at the foot of the screen, with the same items. A swipe down on it, a press outside, Escape and Back close it. A submenu opens as a second sheet over it and closes alone.
- Android's Back button closes the menu as Escape does, rather than the app.
- A tap or a pen opens the menu on the menu itself, with no item highlighted. Down or Up then moves to the first or last item.

### You must

- Put a shortcut hint in `shortcut`, not `trailing`. A screen reader then
  hears it as `aria-keyshortcuts`, not as part of the item's name.

### Example

An "Actions" menu with a Save item, `shortcut: "Ctrl+S"`: Enter on the trigger
opens the menu on its first item, a screen reader reads "Save" and the
shortcut apart, and Escape returns focus to the trigger.

### Limits

- On Android, a menu opened without a tap (on mount or from a timer) may let
  Back close the app.

## Theme defaults

`MenuDefaults` on the theme holds `size`, `radius`, `sheet_size` (`None`: the
phone sheet keeps `size`), `max_height` (`"340px"`),
the per-size `sizes`, `submenu_delay` (150ms), and the defaults of
`close_on_select` and `loop_focus`. The surface is the theme's `paper`.

## CSS variables

Per size: `--lsx-menu-font-size-{size}`, `--lsx-menu-item-height-{size}`,
`--lsx-menu-padding-x-{size}` and `--lsx-menu-label-font-size-{size}`. Also
`--lsx-menu-max-height`. On the open menu, for its `sx`:
`--lsx-popover-anchor-width`, the trigger's width. Set a docked width from the
`sm` breakpoint up with `sx().breakpoint(Size::Sm, ..)`: below it the menu is a
full-width sheet.

## Data attributes

On the menu: `size-{size}`, `radius-{size}` and `bordered`.
