Introducing a new way to review your PRs.

Read the announcement

petalframework / petal_components

Phoenix + Live View HEEX Components

View on GitHub

24 documents · Updated Sep 2, 2026

docs / menus-and-command-navigation

Menus and command navigation give users fast ways to reach actions and application destinations. The components keep links and buttons as the real targets while adding nested navigation, flyout panels, account menus, search, and keyboard control around them. This page is a child of overlays-and-menus; see transient-overlays for shared overlay dismissal and placement behavior.

The library separates navigation by the user's goal. A vertical menu organizes application destinations. A navigation menu exposes site-level destinations in horizontal flyouts. A dropdown presents a short set of actions, while a command palette helps users find actions or destinations by typing.

SurfacePrimary goalMain contentKeyboard model
Vertical menuMove through application areasLinks, groups, and nested linksNative links and buttons; submenu opening moves focus into the submenu
Navigation menuBrowse site or product areasPlain links inside optional flyout panelsFocus opens the relevant flyout; Escape closes it
Dropdown menuRun a short action or routeMenu items, labels, separators, or a control rowNative trigger button; Escape and click-away close the panel
Command paletteFind an action or destination quicklyFiltered links and buttonsSearch input keeps focus; arrows move a virtual selection and Enter activates it
User dropdown menuReach account actionsUser identity plus account links or custom account contentUses the dropdown trigger and dismissal behavior

References: lib/…/menu.ex• L128-188 lib/…/navigation_menu.ex• L1-19 lib/…/dropdown.ex• L1-3 lib/…/command.ex• L1-25 lib/…/user_dropdown_menu.ex• L1-6

Vertical menus

The vertical menu is an application navigation tree. It accepts menu item data with a name, label, destination, and optional icon. Items can be grouped for a sidebar and can contain nested items that expand below a parent item.

The current page controls exact active styling through item names. A parent branch opens when one of its descendants is current, so the open branch and the user's location stay aligned. The parent is not treated as the exact active leaf. Related items can share a patch group when navigation should update the current LiveView without a full redirect.

When a parent has children, its button toggles the submenu and moves focus to its first item. A submenu opens by default when it contains the current page. Leaf items remain normal navigation links, which preserves the usual browser and LiveView navigation behavior.

References: lib/…/menu.ex• L9-49 lib/…/menu.ex• L76-125 lib/…/menu.ex• L141-262 lib/…/menu.ex• L292-382

Flyout navigation menus

The navigation menu is a horizontal site-navigation pattern for destinations that need more context than a single link. An item can be a plain link or a disclosure trigger with a panel. Panel content stays flexible and can contain linked rows with titles, descriptions, icons, grids, and footer calls to action.

The default hover mode suits pointer-based top navigation. It also opens on keyboard focus and keeps the panel open while the pointer crosses the gap between the trigger and the panel. Click mode gives touch-first layouts an explicit toggle path with click-away and Escape dismissal. Both modes keep one panel open at a time.

Panels can align to the start or end of their trigger. They can also span the nearest positioned ancestor as a full-width mega menu. In hover mode, the browser hook keeps a regular panel inside the horizontal viewport when its trigger is near an edge.

The following flow shows how a navigation item becomes either a direct destination or a disclosed set of destinations.

flowchart LR
    A[Navigation item] --> B{Has direct destination?}
    B -->|Yes| C[Plain link]
    B -->|No| D[Disclosure trigger]
    D --> E[Flyout panel]
    E --> F[Navigation links]
    E --> G[Optional footer actions]

References: lib/…/navigation_menu.ex• L47-75 lib/…/navigation_menu.ex• L112-165 lib/…/navigation_menu.ex• L168-241 assets/…/petal_components.js• L2771-2886

The dropdown is the shared menu surface for short action lists and account choices. Its built-in trigger is a real button. Its panel contains menu items that can be buttons, plain links, LiveView patches, or LiveView redirects, so an action keeps the same behavior whether it appears in a dropdown or elsewhere.

The panel supports labels and separators for groups. A menu row can hold a native control that is not itself a menu item, such as a preference switch. Use that row only when the control has another clear name and place in the surrounding interface because menu-mode assistive technology may skip it.

Dropdowns close on Escape and click-away. The panel can open below, above, or beside its trigger. side selects the main direction and align selects the edge on the other axis. When no side is fixed, the panel measures the visible viewport and flips upward when a lower trigger has insufficient room below. Read transient-overlays for the broader anchored-surface rules.

References: lib/…/dropdown.ex• L47-167 lib/…/dropdown.ex• L170-265 lib/…/dropdown.ex• L277-391 assets/…/petal_components.js• L2888-2991

User dropdown menus

The user dropdown is an account-focused wrapper around the dropdown surface. It combines an avatar trigger with generated account links, while allowing a caller to replace the generated list with a custom panel for content such as an organization switcher or a preference group.

The compact icon variant fits a top bar. The sidebar variant shows the user's name and optional email in a full-width row, which suits the bottom of a sidebar. The wrapper passes placement choices to the shared dropdown, so a sidebar menu can open above the row or beside the sidebar instead of leaving the visible screen.

An empty user menu renders no dropdown. When custom panel content is supplied, it replaces the generated item list rather than adding a second list below it. The trigger remains the component-owned button in both cases, so trigger content must not add another button or link.

References: lib/…/user_dropdown_menu.ex• L7-62 lib/…/user_dropdown_menu.ex• L64-145 lib/…/user_dropdown_menu.ex• L146-153 test/…/user_dropdown_menu_test.exs• L232-413

Command palettes

The command palette is a search surface for actions and destinations. An inline palette can sit in a page or sidebar. A command dialog places the same palette in a native dialog and can open from a platform shortcut such as Cmd/Ctrl+K, a visible trigger, or an explicit open request.

Command items remain links when they have a navigation target and buttons when they run an event. They can expose a separate search value and keyword aliases. Filtering happens in the browser as the user types, and unmatched items are hidden without changing their order. This lets LiveView continue to own the rendered item structure.

The command input uses the combobox pattern. Keyboard focus stays in the input while the active item is represented by aria-activedescendant. Arrow keys move through visible enabled items. Home and End select the first or last item. Enter activates the highlighted item. A loop option controls whether movement wraps at either end.

The command dialog adds top-layer dialog behavior, focus on open, Escape and backdrop close, background scroll lock, and optional query reset on close. A command item closes the dialog after it runs unless it opts to keep the palette open. The dialog also restores its open state and focus when a LiveView patch changes client-owned dialog state.

This flow shows the separation between server-owned command content and browser-owned search state.

flowchart LR
    A[Server-rendered links and buttons] --> B[Command palette]
    B --> C[User types a query]
    C --> D[Client filters visible items]
    D --> E[Virtual highlight]
    E --> F[Enter activates the real item]
    F --> G[Navigation or LiveView action]

References: lib/…/command.ex• L32-143 lib/…/command.ex• L146-207 lib/…/command.ex• L210-373 assets/…/petal_components.js• L2190-2486 assets/…/petal_components.js• L2738-2754 test/…/command_dialog.test.js• L68-287

Keyboard interaction

Keyboard support follows the type of surface instead of forcing every menu into one widget model. Direct links and buttons use native activation. Disclosure triggers expose their expanded state and controls relationship. Command palettes use a combobox and virtual selection because moving focus into each filtered item would interrupt search.

Use the interaction paths below when choosing a surface:

  • Use a vertical menu for a stable application tree with nested branches.
  • Use a navigation menu when a top-level destination needs a flyout of related links.
  • Use a dropdown for a short action list where the trigger owns the panel.
  • Use a command palette when users need search, aliases, arrow-key selection, or a global shortcut.
  • Use a user dropdown when the trigger represents the signed-in user's account.

Navigation menus open on focus in hover mode and close when focus leaves the item. Escape closes an open flyout. Dropdown triggers are keyboard-accessible buttons, but the dropdown does not add arrow-key movement between menu items. Use command navigation when that movement is part of the task.

References: lib/…/navigation_menu.ex• L7-19 lib/…/navigation_menu.ex• L138-147 lib/…/dropdown.ex• L123-165 lib/…/command.ex• L219-239 assets/…/petal_components.js• L2194-2357 assets/…/petal_components.js• L2771-2825

Change checklist

When these files or sections are changed, remember to consider these:

  • When a menu or panel ID changes, update its trigger relationship, expanded state, and every browser selector or dispatched command that targets it.
  • When command item markup changes, preserve the search value and keyword data with the item role, disabled state, selected state, and real link or button target.
  • When command filtering changes, keep server-owned item order stable so LiveView patches do not apply client state to the wrong item.
  • When navigation-menu trigger mode changes, keep hover, focus, touch, click-away, and Escape behavior consistent with the selected mode.
  • When a user menu trigger changes, keep the avatar or identity content inside the component-owned button and preserve the placement passed to the shared dropdown.
  • When a submenu or flyout gains a new destination type, confirm that its link behavior matches the route update expected by the owning LiveView.

Important failure modes

TriggerConsequenceGuard
A command item is reordered during client filteringThe active descendant and a later LiveView patch can refer to different actionsHide unmatched items and keep the server-rendered order unchanged
A command item has no stable value or keyword data after a markup changeUsers cannot find the action by its intended search termsPreserve the visible-text fallback or provide the item's search value and aliases
A navigation trigger loses its panel ID or expanded-state relationshipKeyboard and assistive technology users cannot identify the open flyoutKeep the trigger's aria-controls, panel ID, and aria-expanded updates aligned
Interactive content is nested inside a dropdown or user-menu triggerThe browser gets nested controls and the toggle can failUse the component's trigger content slot without adding another button or link
A sidebar user menu keeps automatic placement without the bundled dropdown hookThe panel can open outside the visible viewportRegister the browser hooks or set an explicit side when the layout knows the correct direction
A dropdown row contains a control that is treated as a menu itemMenu semantics can conflict with the control's native roleUse the row's non-menu role and keep the control's own semantics intact

Key files

FileWhy to read it
lib/petal_components/menu.exDefines vertical application navigation, groups, nested submenus, active branches, and patch-aware links
lib/petal_components/navigation_menu.exDefines direct navigation links, disclosure flyouts, hover or click modes, and panel composition
lib/petal_components/dropdown.exDefines the shared action-menu surface, menu item variants, dismissal, and placement choices
lib/petal_components/user_dropdown_menu.exDefines compact and sidebar account triggers plus generated or custom account panels
lib/petal_components/command.exDefines inline palettes, command dialogs, triggers, search groups, items, and keyboard-facing semantics
assets/…/petal_components.jsCoordinates command filtering and selection, navigation-menu focus and pointer state, and dropdown viewport placement
test/petal/navigation_menu_test.exsShows the navigation menu contract for flyouts, direct links, panel variants, and active destinations
test/petal/command_test.exsShows the command palette contract for roles, item targets, disabled state, dialog wiring, and triggers
test/petal/user_dropdown_menu_test.exsShows account trigger variants, custom panel replacement, accessible identity content, and placement propagation