styles.go

 1package textinput
 2
 3import (
 4	"image/color"
 5	"time"
 6
 7	tea "github.com/charmbracelet/bubbletea/v2"
 8	"github.com/charmbracelet/lipgloss/v2"
 9)
10
11// DefaultStyles returns the default styles for focused and blurred states for
12// the textarea.
13func DefaultStyles(isDark bool) Styles {
14	lightDark := lipgloss.LightDark(isDark)
15
16	var s Styles
17	s.Focused = StyleState{
18		Placeholder: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
19		Suggestion:  lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
20		Prompt:      lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
21		Text:        lipgloss.NewStyle(),
22	}
23	s.Blurred = StyleState{
24		Placeholder: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
25		Suggestion:  lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
26		Prompt:      lipgloss.NewStyle().Foreground(lipgloss.Color("7")),
27		Text:        lipgloss.NewStyle().Foreground(lightDark(lipgloss.Color("245"), lipgloss.Color("7"))),
28	}
29	s.Cursor = CursorStyle{
30		Color: lipgloss.Color("7"),
31		Shape: tea.CursorBlock,
32		Blink: true,
33	}
34	return s
35}
36
37// DefaultLightStyles returns the default styles for a light background.
38func DefaultLightStyles() Styles {
39	return DefaultStyles(false)
40}
41
42// DefaultDarkStyles returns the default styles for a dark background.
43func DefaultDarkStyles() Styles {
44	return DefaultStyles(true)
45}
46
47// Styles are the styles for the textarea, separated into focused and blurred
48// states. The appropriate styles will be chosen based on the focus state of
49// the textarea.
50type Styles struct {
51	Focused StyleState
52	Blurred StyleState
53	Cursor  CursorStyle
54}
55
56// StyleState that will be applied to the text area.
57//
58// StyleState can be applied to focused and unfocused states to change the styles
59// depending on the focus state.
60//
61// For an introduction to styling with Lip Gloss see:
62// https://github.com/charmbracelet/lipgloss
63type StyleState struct {
64	Text        lipgloss.Style
65	Placeholder lipgloss.Style
66	Suggestion  lipgloss.Style
67	Prompt      lipgloss.Style
68}
69
70// CursorStyle is the style for real and virtual cursors.
71type CursorStyle struct {
72	// Style styles the cursor block.
73	//
74	// For real cursors, the foreground color set here will be used as the
75	// cursor color.
76	Color color.Color
77
78	// Shape is the cursor shape. The following shapes are available:
79	//
80	// - tea.CursorBlock
81	// - tea.CursorUnderline
82	// - tea.CursorBar
83	//
84	// This is only used for real cursors.
85	Shape tea.CursorShape
86
87	// CursorBlink determines whether or not the cursor should blink.
88	Blink bool
89
90	// BlinkSpeed is the speed at which the virtual cursor blinks. This has no
91	// effect on real cursors as well as no effect if the cursor is set not to
92	// [CursorBlink].
93	//
94	// By default, the blink speed is set to about 500ms.
95	BlinkSpeed time.Duration
96}