menu

package
v0.46.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 17 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package menu is a grouped, filterable list of things to open: the sections of an app, the screens a home page leads to. Each item is a label, a dimmed description beside it, and an optional group it is filed under; choosing one emits a ChosenMsg and the screen decides what opening it means — usually screen.Push.

It is a pane component like pkg/list, and sits beside other components the same way: it takes focus, filters with /, and answers the mouse from its own rect. What it adds over a list is the groups, drawn as headers the cursor never lands on, by the same rules pkg/action's menu draws them:

  • headers only once the items name two or more groups;
  • groups in the order first seen, header text as written;
  • items without a group last, under "other";
  • tab / shift+tab and ] / [ jump to the next and previous group.

In a focus.Group, tab and shift+tab move focus between components instead, and the menu jumps on [ and ] alone (see JoinGroup).

A menu is not pkg/action's Menu. That one is a modal over a screen's verbs; this one is a screen's content.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChosenMsg

type ChosenMsg struct {
	Item  Item
	Token focus.Token
}

ChosenMsg is emitted when the user opens an item — enter, or a double click. Token says which menu sent it; Chosen matches both for you.

type Item

type Item struct {
	// ID identifies the item to the caller. Optional; when set, it is also
	// what keeps the cursor on the same item across SetItems.
	ID string
	// Label is the text the user picks by. Required.
	Label string
	// Group files the item under a header. See the package doc for the rules.
	Group string
	// Description is drawn dimmed beside the label and cut off when the pane
	// runs out of room. It is not searched by the filter.
	Description string
	// Data is the caller's payload, handed back in ChosenMsg. The menu never
	// reads it.
	Data any
}

Item is one entry in the menu.

type Keys

type Keys struct {
	Up, Down         key.Binding
	Top, Bottom      key.Binding
	HalfUp, HalfDown key.Binding
	// PrevGroup and NextGroup jump to the first item of the previous or next
	// group. They do nothing when the menu has no headers. In a focus.Group
	// the menu leaves the Group's cycling keys (tab, shift+tab) to it and
	// jumps on the rest.
	PrevGroup, NextGroup key.Binding
	Filter               key.Binding
	Choose               key.Binding
}

Keys is the menu's keymap. Vertical movement follows rule 25.

func DefaultKeys

func DefaultKeys() Keys

DefaultKeys returns the menu's stock keymap.

type Model

type Model struct {
	// contains filtered or unexported fields
}

Model is the menu. Embed as a value; mutate via the setters.

func New

func New(opts Options) Model

New constructs a menu.

func (*Model) Blur

func (m *Model) Blur()

Blur releases the keyboard from both the body and the filter.

func (*Model) BlurFilter

func (m *Model) BlurFilter()

BlurFilter returns input from the filter to the body.

func (Model) Chosen

func (m Model) Chosen(msg tea.Msg) (Item, bool)

Chosen reports whether msg is this menu's ChosenMsg, and the item in it.

if it, ok := s.menu.Chosen(msg); ok {
    return s, screen.Push(it.Data.(func(theme.Theme) screen.Screen)(s.t))
}

func (Model) Filtering

func (m Model) Filtering() bool

Filtering reports whether the filter has input.

func (*Model) Focus

func (m *Model) Focus() tea.Cmd

Focus gives the menu the keyboard. It never takes input from the filter: a click on the filter also asks the group for focus, and that grant lands after the click (rule 27).

func (*Model) FocusFilter

func (m *Model) FocusFilter() tea.Cmd

FocusFilter moves input to the filter. The pane stays lit: the menu has focus, and the filter row shows where the keys go.

func (Model) FocusToken

func (m Model) FocusToken() focus.Token

FocusToken returns the menu's stable focus identity. See focus.Identified.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the body or the filter has input.

func (Model) Help

func (m Model) Help() []key.Binding

Help returns the bindings the menu currently responds to.

func (Model) HelpSections

func (m Model) HelpSections() []help.Section

HelpSections groups the bindings by what they do. The group keys are listed only when there are headers to jump between.

func (Model) Init

func (m Model) Init() tea.Cmd

Init satisfies tea.Model — nothing to kick off.

func (Model) IsCapturingKeys

func (m Model) IsCapturingKeys() bool

IsCapturingKeys satisfies focus.Capturer: true while the filter has input.

func (Model) Items

func (m Model) Items() []Item

Items returns the full, unfiltered item set.

func (*Model) JoinGroup added in v0.43.0

func (m *Model) JoinGroup(cycle focus.Keys)

JoinGroup satisfies focus.Member: inside a focus.Group, the Group's cycling keys move focus, so the group jumps give them up.

func (Model) Rect

func (m Model) Rect() geom.Rect

Rect is the area the menu occupies.

func (*Model) Restore

func (m *Model) Restore(s State)

Restore puts s back: items, then the filter over them, then the cursor. A State from a menu never built restores nothing.

func (*Model) Select

func (m *Model) Select(id string) bool

Select moves the cursor to the item with this ID, reporting whether one is showing.

func (Model) Selected

func (m Model) Selected() (Item, bool)

Selected returns the item under the cursor. ok is false when the filter keeps nothing.

func (*Model) SetItems

func (m *Model) SetItems(items []Item)

SetItems replaces the items and re-applies the filter. The cursor stays on the item with the same ID when there is one, and otherwise on the same position.

func (*Model) SetRect

func (m *Model) SetRect(r geom.Rect)

SetRect places the menu. The filter, when there is one, is a row inside the pane, placed once the pane has measured its own edge.

func (*Model) SetTitle

func (m *Model) SetTitle(s string)

SetTitle updates the label on the pane's top border.

func (*Model) SetValue

func (m *Model) SetValue(s string)

SetValue sets the filter text without it reading as a user action.

func (Model) State

func (m Model) State() State

State captures the menu's state.

func (Model) Title

func (m Model) Title() string

Title returns the label on the pane's border.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update handles keys and mouse. While the filter has input every key goes to it; otherwise the menu's own keys move the cursor and enter chooses.

func (Model) Value

func (m Model) Value() string

Value is the filter text ("" when not filterable).

func (Model) View

func (m Model) View() string

View renders the pane.

type Options

type Options struct {
	// Title sits on the pane's top border. Defaults to "Menu".
	Title string
	// Items is the menu's content, copied.
	Items []Item
	// Filterable embeds a filter as a header row inside the pane. / focuses
	// it; it matches the label and the group, case-insensitively.
	Filterable bool

	LabelStyle lipgloss.Style
	// DescStyle styles the description; theme.Menu() dims it.
	DescStyle lipgloss.Style
	// A group header is a thin divider with the group's name set into it.
	// HeaderStyle styles the name; GroupStyle, when set, styles it by its
	// group instead. RuleStyle colours the divider, drawn in the surface's
	// divider glyph.
	HeaderStyle lipgloss.Style
	GroupStyle  func(group string) lipgloss.Style
	RuleStyle   lipgloss.Style
	// SelectedStyle is applied to the whole cursor row.
	SelectedStyle lipgloss.Style
	// SelectedBlurredStyle replaces SelectedStyle while the menu doesn't
	// have focus. Used only when it sets a background.
	SelectedBlurredStyle lipgloss.Style

	// Pane pass-throughs. See pkg/pane.Options.
	ActiveColor    lipgloss.TerminalColor
	InactiveColor  lipgloss.TerminalColor
	ActiveBorder   lipgloss.Border
	InactiveBorder lipgloss.Border
	// Glyphs are the marks this component draws, plus the scrollbar thumb
	// and track it hands to its pane. Empty fields fall back to
	// glyph.Default.
	Glyphs       glyph.Set
	SlotBrackets pane.SlotBracketStyle
	// Surface is how the component's edge and body are drawn. A theme sets
	// it from the app's chosen surface. See docs/surfaces.md.
	Surface surface.Resolved

	// Filter configures the embedded filter. Ignored when Filterable=false.
	Filter filter.Options

	Keys Keys
}

Options configures a menu. Build it from theme.Menu() and set Title, Items and Filterable before passing to New.

type State

type State struct {
	// contains filtered or unexported fields
}

State is what the user and the data did to a menu — its items, filter, cursor and scroll — held across a rebuild (rule 4):

st := s.menu.State()
s.menu = menu.New(opts)
s.menu.Restore(st)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL