ContextMenu
Key bindings
Section titled “Key bindings”| Key | Action |
|---|---|
↑ / k | Move selection up |
↓ / j | Move selection down |
→ / l | Open submenu |
← / h | Close submenu (return to parent) |
Home | Jump to first item |
End | Jump to last item |
Enter | Activate selected item |
Esc | Close menu |
Showing the menu
Section titled “Showing the menu”menu := components.NewContextMenu()menu.SetItems([]components.MenuItem{ {Label: "Edit", Action: func() { ... }}, {Label: "Delete", Action: func() { ... }}, {Label: "More", Submenu: []components.MenuItem{ {Label: "Export CSV", Action: func() { ... }}, }},})
// At a specific terminal coordinatemenu.ShowAt(x, y)
// Centered within the screenmenu.ShowCentered(screenW, screenH)Separator items
Section titled “Separator items”Add MenuItem{Separator: true} to insert a visual divider line. Separators are skipped by keyboard navigation.
Usage
menu := components.NewContextMenu(). SetItems([]components.MenuItem{ {ID: "edit", Label: "Edit", Shortcut: "e", Handler: func() {}}, {ID: "dup", Label: "Duplicate", Handler: func() {}}, {ID: "del", Label: "Delete", Danger: true, Handler: func() {}}, })menu.AddDivider()menu.AddSubmenu("export", "Export", []components.MenuItem{ {ID: "csv", Label: "CSV", Handler: func() {}}, {ID: "json", Label: "JSON", Handler: func() {}},})
fmt.Println(menu != nil)Methods
| Method | Signature | Description |
|---|---|---|
AddDivider | func (m *ContextMenu) AddDivider() *ContextMenu | AddDivider adds a horizontal line separator |
AddItem | func (m *ContextMenu) AddItem(id, label string, handler func()) *ContextMenu | AddItem adds a simple menu item |
AddItemWithIcon | func (m *ContextMenu) AddItemWithIcon(id, label, icon string, handler func()) *ContextMenu | AddItemWithIcon adds item with icon |
AddItemWithShortcut | func (m *ContextMenu) AddItemWithShortcut(id, label, shortcut string, handler func()) *ContextMenu | AddItemWithShortcut adds item with displayed shortcut |
AddSection | func (m *ContextMenu) AddSection(header string, items []MenuItem) *ContextMenu | AddSection adds a group of items with optional header |
AddSubmenu | func (m *ContextMenu) AddSubmenu(id, label string, items []MenuItem) *ContextMenu | AddSubmenu adds item that opens a submenu |
Close | func (m *ContextMenu) Close() | Close closes the menu and any open submenus |
IsOpen | func (m *ContextMenu) IsOpen() bool | IsOpen returns true if menu is visible |
SetChecked | func (m *ContextMenu) SetChecked(id string, checked bool) *ContextMenu | SetChecked sets checked state for toggle items |
SetDanger | func (m *ContextMenu) SetDanger(id string, danger bool) *ContextMenu | SetDanger marks item as destructive (red color) |
SetDisabled | func (m *ContextMenu) SetDisabled(id string, disabled bool) *ContextMenu | SetDisabled enables/disables an item by ID |
SetItems | func (m *ContextMenu) SetItems(items []MenuItem) *ContextMenu | SetItems sets all menu items at once |
SetOnClose | func (m *ContextMenu) SetOnClose(fn func()) *ContextMenu | SetOnClose is called when menu is closed |
SetOnSelect | func (m *ContextMenu) SetOnSelect(fn func(item MenuItem)) *ContextMenu | SetOnSelect is called when any item is selected |
SetSections | func (m *ContextMenu) SetSections(sections []MenuSection) *ContextMenu | SetSections sets sectioned items |
ShowAt | func (m *ContextMenu) ShowAt(x, y int) | ShowAt displays menu at specific coordinates |
ShowCentered | func (m *ContextMenu) ShowCentered(screenW, screenH int) | ShowCentered displays the menu centered within a screen of the given dimensions. |
Types
type MenuItem struct MenuItem represents a single context menu item. Set Submenu to add a nested
menu; set Handler to nil and isDivider (via AddDivider) for a separator line.
Danger colors the item red; Checked renders a checkmark icon. | Field | Type | Description |
|---|---|---|
ID | string | |
Label | string | |
Icon | string | Optional icon |
Shortcut | string | Displayed shortcut (e.g., "Ctrl+C") |
Disabled | bool | Grayed out if true |
Danger | bool | Red color for destructive actions |
Checked | bool | For toggle items (shows checkmark) |
Submenu | []MenuItem | Nested menu items |
Handler | func() | Action when selected |
type MenuSection struct MenuSection groups items under an optional header label for visual separation. | Field | Type | Description |
|---|---|---|
Header | string | Optional section header |
Items | []MenuItem |