toggle

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package toggle provides a yes/no (boolean) selector wrapped in a pane — two labelled buttons rendered side-by-side with the active side highlighted, inside a bordered titled box.

Like every other tuilib component (input, filter, list, …), toggle owns its own pane: View() returns the bordered render. Don't wrap a toggle in another pane — set Options.Title to put the label on the border instead.

While focused, left/right/h/l/space toggle, and y/n set explicitly:

tg := toggle.New(theme.Dark().Toggle())
tg.SetTitle("Send notifications?")
// in your screen's Update: tg, _ = tg.Update(msg)
// later: send := tg.Value()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Model

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

Model is the toggle's exported state. Focus state lives on both the internal flag and the pane (so the border color reflects focus) — toggle them together via Focus/Blur.

func New

func New(opts Options) Model

New constructs a toggle.

func (*Model) Blur

func (m *Model) Blur()

Blur releases focus.

func (*Model) Focus

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

Focus grabs focus. Returns nil — there's no cursor to blink.

func (Model) FocusToken added in v0.18.1

func (m Model) FocusToken() focus.Token

FocusToken returns the toggle's stable focus identity.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the toggle is accepting keys.

func (Model) Help

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

Help returns the keys this toggle responds to. Compose into a screen's Help() to drive a per-component help line.

func (Model) HelpSections added in v0.25.0

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

HelpSections reports the toggle's bindings under Edit — flipping a toggle is editing its value, and grouping it with the fields around it is how the overlay reads on a form-shaped screen.

func (Model) Init

func (m Model) Init() tea.Cmd

Init is a no-op.

func (Model) Rect added in v0.18.0

func (m Model) Rect() geom.Rect

Rect returns the rect the toggle was last placed at.

func (*Model) SetActiveBorder added in v0.24.0

func (m *Model) SetActiveBorder(b lipgloss.Border)

SetActiveBorder updates the border shape drawn while focused.

func (*Model) SetActiveColor

func (m *Model) SetActiveColor(c lipgloss.TerminalColor)

SetActiveColor updates the border color used when focused.

func (*Model) SetInactiveBorder added in v0.24.0

func (m *Model) SetInactiveBorder(b lipgloss.Border)

SetInactiveBorder updates the border shape drawn while unfocused.

func (*Model) SetInactiveColor

func (m *Model) SetInactiveColor(c lipgloss.TerminalColor)

SetInactiveColor updates the border color used when unfocused.

func (*Model) SetRect added in v0.18.0

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

SetRect places the toggle at r. Height is fixed at 3 regardless of what the rect offers, since the pane is border + one content row + border.

func (*Model) SetSelectedStyle

func (m *Model) SetSelectedStyle(s lipgloss.Style)

SetSelectedStyle updates the rendered style of the active side. Useful when reacting to a theme swap without rebuilding the model.

func (*Model) SetTitle

func (m *Model) SetTitle(s string)

func (*Model) SetTopRight added in v0.18.1

func (m *Model) SetTopRight(s string)

SetTitle sets the title shown on the pane's top border. SetTopRight writes into the pane's top-right border slot. Hosts use it for a short annotation that belongs to the component as a whole — pkg/form paints validation errors there. Pass "" to clear.

func (*Model) SetUnselectedStyle

func (m *Model) SetUnselectedStyle(s lipgloss.Style)

SetUnselectedStyle updates the rendered style of the inactive side.

func (*Model) SetValue

func (m *Model) SetValue(v bool)

SetValue overwrites the value.

func (Model) Title added in v0.25.0

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 toggle keys when focused; no-op when blurred. The caller should still forward every message — the toggle decides whether to act.

func (Model) Value

func (m Model) Value() bool

Value returns the current bool.

func (Model) View

func (m Model) View() string

View renders the toggle as a bordered three-line pane.

type Options

type Options struct {
	// Width sets the pane's outer width. Height is fixed at 3.
	Width int

	// Title sits on the top border of the pane. Defaults to "toggle" — set
	// it to the field's question (e.g. "Send notifications?").
	Title string

	// Initial is the starting value. Defaults to false.
	Initial bool
	// YesLabel and NoLabel are the rendered button labels. Default to "yes"
	// and "no" — wrap your own text (e.g. "on"/"off", "save"/"discard").
	YesLabel string
	NoLabel  string

	// SelectedStyle styles the active side, brackets included.
	SelectedStyle lipgloss.Style
	// UnselectedStyle styles the inactive side.
	UnselectedStyle lipgloss.Style

	// Pane pass-throughs. Unset fields fall back to NormalBorder both states
	// and SlotBracketsNone.
	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
}

Options configures a new toggle. Zero-value fields fall back to defaults.

Jump to

Keyboard shortcuts

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