toggle

package
v0.0.0-...-ef14c93 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 4 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) 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) Init

func (m Model) Init() tea.Cmd

Init is a no-op.

func (*Model) SetActiveColor

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

SetActiveColor updates the border color used when focused.

func (*Model) SetInactiveColor

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

SetInactiveColor updates the border color used when unfocused.

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)

SetTitle sets the title shown on the pane's top border.

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) SetWidth

func (m *Model) SetWidth(w int)

SetWidth resizes the surrounding pane. Height is fixed at 3.

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
	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