mirror of
https://github.com/Maks1mS/bubbles.git
synced 2024-12-23 14:22:58 +03:00
Add Button component
This commit is contained in:
parent
9b47f26bdd
commit
34cd93d6b5
@ -36,6 +36,11 @@ the common, and many customization options.
|
||||
* [Example code, many fields](https://github.com/charmbracelet/tea/tree/master/examples/textinputs/main.go)
|
||||
|
||||
|
||||
## Button
|
||||
|
||||
A button component.
|
||||
|
||||
|
||||
## Paginator
|
||||
|
||||
<img src="https://stuff.charm.sh/bubbles-examples/pagination.gif" width="200" alt="Paginator Example">
|
||||
|
90
button/button.go
Normal file
90
button/button.go
Normal file
@ -0,0 +1,90 @@
|
||||
package button
|
||||
|
||||
import (
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
|
||||
var (
|
||||
// color is a helper for returning colors.
|
||||
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
||||
)
|
||||
|
||||
// Model is the Bubble Tea model for a button element.
|
||||
type Model struct {
|
||||
Err error
|
||||
|
||||
Label string
|
||||
Default bool
|
||||
|
||||
TextColor string
|
||||
BackgroundColor string
|
||||
FocusedTextColor string
|
||||
FocusedBackgroundColor string
|
||||
|
||||
// Focus indicates whether user focus should be on this button component
|
||||
focus bool
|
||||
}
|
||||
|
||||
// NewModel creates a new model with default settings.
|
||||
func NewModel() Model {
|
||||
return Model{
|
||||
Label: "Button",
|
||||
}
|
||||
}
|
||||
|
||||
// Update is the Tea update loop.
|
||||
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
||||
if !m.focus {
|
||||
return m, nil
|
||||
}
|
||||
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch msg.String() {
|
||||
case "enter":
|
||||
// TODO: implement
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// View renders the button in its current state.
|
||||
func (m Model) View() string {
|
||||
margin := m.styled(" ").String()
|
||||
label := m.styled(m.Label)
|
||||
if m.Default {
|
||||
label = label.Underline()
|
||||
}
|
||||
|
||||
return margin + label.String() + margin
|
||||
}
|
||||
|
||||
// Focused returns the focus state on the model.
|
||||
func (m Model) Focused() bool {
|
||||
return m.focus
|
||||
}
|
||||
|
||||
// Focus sets the focus state on the model.
|
||||
func (m *Model) Focus() {
|
||||
m.focus = true
|
||||
}
|
||||
|
||||
// Blur removes the focus state on the model.
|
||||
func (m *Model) Blur() {
|
||||
m.focus = false
|
||||
}
|
||||
|
||||
func (m Model) styled(s string) termenv.Style {
|
||||
view := termenv.String(s)
|
||||
if m.focus {
|
||||
view = view.Foreground(color(m.FocusedTextColor)).
|
||||
Background(color(m.FocusedBackgroundColor))
|
||||
} else {
|
||||
view = view.Foreground(color(m.TextColor)).
|
||||
Background(color(m.BackgroundColor))
|
||||
}
|
||||
|
||||
return view
|
||||
}
|
Loading…
Reference in New Issue
Block a user