button

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 7 Imported by: ? 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 button is a pressable button: a label that takes focus and reports a press. Enter or Space presses it when focused, and so does a left click that goes down and comes up inside it.

A button draws on one row. Its states are told apart without colour: square brackets mark it as a button, angle brackets mark focus ("< Save >"), reverse video marks a press, and a disabled or loading button says so in its label. Angle brackets are how every control of this family shows focus: buttongroup, radiogroup, checkbox, toggle, slider and rating too.

With Toggle set it is a toggle button: each press turns it on or off, and a dot before the label shows that it is on.

Stability: experimental. Its API may change in any minor release.

Example

A focused button reports a press on Enter.

package main

import (
	"fmt"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/button"
)

func main() {
	save := button.New("Save")
	save.ID = "save"
	save.Focus()
	fmt.Println(ansi.StripANSI(save.View()))

	_, cmd := save.Update(tui.Key{Type: tui.KeyEnter})
	fmt.Println(cmd().(button.PressedMsg).ID)
}
Output:
< Save >
save

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyMap

type KeyMap struct {
	Press keymap.Binding // press the button
}

KeyMap names the keys of a Model's one action.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns Enter and Space.

type Model

type Model struct {
	// ID is carried by PressedMsg, to tell one button's press from another's.
	ID string
	// Label is the button's text.
	Label   string
	Variant Variant
	Size    Size
	// Disabled stops the button being pressed; it is drawn dimmed.
	Disabled bool
	// Loading stops the button being pressed while the action it started is
	// still running, and adds an ellipsis to the label.
	Loading bool
	// Toggle makes this a toggle button: a press turns it on or off (see On)
	// as well as delivering PressedMsg. One cell before the label is kept
	// for the dot that marks it on, so turning it on does not change its
	// width.
	Toggle bool
	Theme  theme.Theme

	// KeyMap holds the keys that press the button. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: a left button
	// that goes down and comes up inside Bounds presses the button, and
	// pointer motion over it sets Hovered. Off (the default) ignores the
	// mouse.
	Mouse bool
	// Bounds is the screen rectangle where the app draws the button.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is one button. The zero value is not ready to use; build one with New.

func New

func New(label string) Model

New returns a default-variant, default-size button labelled label.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the action the button honours, for help text. A button that cannot be pressed has none.

func (*Model) Blur

func (m *Model) Blur()

Blur takes keyboard focus away, and lets go of a pointer press in progress.

func (Model) Down

func (m Model) Down() bool

Down reports whether a pointer press on the button is in progress: the left button went down inside it and has not come up yet.

func (*Model) Focus

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

Focus gives the button keyboard focus. It returns no Cmd; the result is there so a button can stand where any focusable widget does.

func (Model) Focused

func (m Model) Focused() bool

Focused reports whether the button has keyboard focus.

func (Model) Hovered

func (m Model) Hovered() bool

Hovered reports whether the pointer is over the button. It is only ever true with Mouse on and the Program reporting all pointer motion.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode adapts the button to a layout.Node of its own width on one row. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the button as plain text for accessible output (see tui.Linearizer): the label, the word "button", and its state, e.g. "Save, button, focused" or "Delete, button, unavailable". A toggle button reads "Bold, toggle button, on".

func (Model) On

func (m Model) On() bool

On reports whether a toggle button is on. It is always false for a button that is not a toggle.

func (*Model) SetOn

func (m *Model) SetOn(on bool)

SetOn turns a toggle button on or off without a press: no PressedMsg is delivered. It does nothing to a button that is not a toggle.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentButton, ...) and then by WithTokens.

func (Model) Update

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

Update presses the button on Enter or Space when it is focused, and, with Mouse on, on a left click inside Bounds. A click is a press and a release that both land inside the button: a pointer dragged off it before the release presses nothing. A disabled or loading button ignores both.

func (Model) View

func (m Model) View() string

View renders the button on one row. Its width is the label's width plus two brackets and the padding of its Size, in every state, so a button does not move its neighbours when it is focused, pressed or toggled; only Loading, which adds an ellipsis, makes it one cell wider.

func (Model) Width

func (m Model) Width() int

Width is the number of cells View takes.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

type PressedMsg

type PressedMsg struct {
	// ID is the pressed button's ID.
	ID string
	// On is a toggle button's state after the press; false for a button
	// that is not a toggle.
	On bool
}

PressedMsg is delivered (via the Cmd Update returns) when the button is pressed, by key or by pointer.

type Size

type Size int

Size is how much room a button takes around its label.

const (
	// SizeDefault pads the label with one space each side: "[ Save ]".
	SizeDefault Size = iota
	// SizeSmall has no padding: "[Save]".
	SizeSmall
	// SizeLarge pads the label with three spaces each side.
	SizeLarge
)

type Variant

type Variant int

Variant is how much weight a button carries on the screen.

const (
	// VariantDefault is the main action: "[ Save ]" in the primary colour.
	VariantDefault Variant = iota
	// VariantSecondary is an action of less weight, in the text colour.
	VariantSecondary
	// VariantDestructive is an action that removes or cannot be undone, in
	// the error colour.
	VariantDestructive
	// VariantGhost has no brackets until it is focused or pressed.
	VariantGhost
	// VariantLink is drawn as underlined text with no brackets.
	VariantLink
)

Jump to

Keyboard shortcuts

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