Menu

Renders a menu that offers a list of actions or functions.

Read more Read less

This component is meant for organizing actions within an application, rather than for navigating between different pages or sections of a website.

See also menu_bar/1, menu_group/1, menu_button/1, menu_item/1, and menu_item_checkbox/1.

Maturity: Developing

Usage

If the menu is always visible or can only be toggled by a keyboard shortcut, set the label attribute.

<.menu id="actions-menu" label="Actions">
  <:item>
    <.menu_item on_click={JS.push("copy")}>Copy</.menu_item>
  </:item>
  <:item>
    <.menu_item on_click={JS.push("paste")}>Paste</.menu_item>
  </:item>
  <:item role="separator"></:item>
  <:item>
    <.menu_item on_click={JS.push("sort")}>Sort lines</.menu_item>
  </:item>
</.menu>

If the menu is toggled by a menu_button/1, ensure that the controls attribute of the button matches the DOM ID of the menu and that the labelledby attribute of the menu matches the DOM ID of the button.

<.menu_button controls="actions-menu" id="actions-button"> Actions </.menu_button> <.menu id="actions-menu" labelledby="actions-button" hidden></.menu>

When an item is chosen, the menu closes and the focus returns to the button. Checkbox and radio items and items that open a submenu leave the menu open. If the item moves the focus, for example to a dialog, the focus stays there.

This component needs the Doggo.Menu JavaScript hook. See Phoenix LiveView Hooks for registering it.

Keyboard

  • Down and Up - move between the items, wrapping at the ends.
  • Home and End - first and last item.
  • Printable characters - move to the next item whose label starts with what you type. Typing the same character again walks through the items that start with it.
  • Esc - close the menu and return the focus to the button that opened it, if a button controls this menu.
  • Enter or Space - activate the focused item.

The menu is a single tab stop. Items inside a menu_group or a menu_item_radio_group are part of it; a nested menu has its own.

<.menu id="menu-single-default" label="Actions">
  <:item>
    <.menu_item on_click={JS.push("view-dog-profiles")}>
      View Dog Profiles
    </.menu_item>
  </:item>
  <:item>
    <.menu_item on_click={JS.push("add-dog-profile")}>
      Add Dog Profile
    </.menu_item>
  </:item>
  <:item role="separator" />
  <:item>
    <.menu_item on_click={JS.push("dog-care-tips")}>
      Dog Care Tips
    </.menu_item>
  </:item>
</.menu>
<div id="menu-all-items">
  <.menu id="menu-single-all-item-types" label="View">
    <:item>
      <.menu_group label="Profiles">
        <:item>
          <.menu_item on_click={JS.push("view-dog-profiles")}>
            View Dog Profiles
          </.menu_item>
        </:item>
        <:item>
          <.menu_item on_click={JS.push("add-dog-profile")}>
            Add Dog Profile
          </.menu_item>
        </:item>
      </.menu_group>
    </:item>
    <:item role="separator" />
    <:item>
      <.menu_item_checkbox on_click={JS.toggle_attribute({"aria-checked", "true", "false"})}>
        Word wrap
      </.menu_item_checkbox>
    </:item>
    <:item role="separator" />
    <:item>
      <.menu_item_radio_group label="Theme">
        <:item checked on_click={JS.set_attribute({"aria-checked", "false"}, to: "#menu-all-items [role='menuitemradio']") |> JS.set_attribute({"aria-checked", "true"}) |> JS.dispatch("switch-theme-light")}>Light</:item>
        <:item on_click={JS.set_attribute({"aria-checked", "false"}, to: "#menu-all-items [role='menuitemradio']") |> JS.set_attribute({"aria-checked", "true"}) |> JS.dispatch("switch-theme-dark")}>Dark</:item>
      </.menu_item_radio_group>
    </:item>
  </.menu>
</div>