TermUI.Widgets.ContextMenu.Inline (TermUI v1.0.0)

View Source

Inline context menu variant for keyboard-only environments.

Unlike the standard ContextMenu which appears at a mouse position, the Inline variant renders in place with numbered items for direct selection. This makes it ideal for TTY mode where mouse positioning may not be available.

Usage

ContextMenu.Inline.new(
  items: [
    ContextMenu.action(:copy, "Copy"),
    ContextMenu.action(:paste, "Paste"),
    ContextMenu.action(:delete, "Delete")
  ],
  on_select: fn id -> handle_action(id) end,
  on_close: fn -> handle_close() end
)

Rendering

Items are rendered with numbered prefixes:

[1] Copy  [2] Paste  [3] Delete

In vertical orientation:

[1] Copy
[2] Paste
[3] Delete

Keyboard Controls

  • Number keys (1-9): Directly select the numbered item
  • Up/Down (vertical) or Left/Right (horizontal): Navigate between items
  • Enter/Space: Select the currently focused item
  • Escape: Close the menu without selecting

Notes

  • Separators and disabled items are not numbered
  • Maximum of 9 items can be numbered (items 10+ require arrow navigation)
  • Only selectable items (non-disabled actions) get numbers

Callback Error Handling

Callbacks (on_select, on_close) are executed synchronously. Callback exceptions are rescued and logged by the shared behaviour.

See TermUI.Widgets.ContextMenu moduledoc for callback best practices.

Summary

Functions

Gets the currently focused item ID.

Hides the menu.

Creates new ContextMenu.Inline widget props.

Shows the menu.

Gets whether the menu is visible.

Types

orientation()

@type orientation() :: :horizontal | :vertical

Functions

get_cursor(state)

@spec get_cursor(map()) :: term()

Gets the currently focused item ID.

hide(state)

@spec hide(map()) :: map()

Hides the menu.

new(opts)

@spec new(keyword()) :: map()

Creates new ContextMenu.Inline widget props.

Options

  • :items - List of menu items (required). Use ContextMenu.action/3 and ContextMenu.separator/0 to create items.
  • :on_select - Callback when item is selected: fn id -> ... end Executed synchronously. Should not raise exceptions.
  • :on_close - Callback when menu is closed without selection: fn -> ... end Executed synchronously. Should not raise exceptions.
  • :orientation - :horizontal (side by side) or :vertical (stacked). Default: :horizontal
  • :item_style - Style for normal items
  • :selected_style - Style for focused item
  • :disabled_style - Style for disabled items
  • :number_style - Style for the [n] prefix

show(state)

@spec show(map()) :: map()

Shows the menu.

visible?(state)

@spec visible?(map()) :: boolean()

Gets whether the menu is visible.