charmbracelet / charmbracelet/bubbles
textinput: placeholder changes component width
- Dominant language
- Go
- Stars
- 8.9k
- Forks
- 457
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 5
Description
I'm seeing strange behavior using a Placeholder with the textinput bubble. Here is a minimum reproducible example:
```
package main
import (
"os"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type model struct {
input textinput.Model
}
func main() {
m := newModel()
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
os.Exit(1)
}
}
func newModel() model {
input := textinput.New()
input.Prompt = " "
input.Width = 5
input.Placeholder = "123"
input.Focus()
return model{input: input}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
}
}
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
}
func (m model) View() string {
return lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Render(m.input.View())
}
```
Changing the length of the Placeholder string changes the way that the textinput is rendered before it receives the first character, triggering a resize after the first character is received. See examples with various Placeholders.
Placeholder = "123"

Placeholder = "12345"

Placeholder = "1234567890"

Placeholder = "12345 " (with trailing space)

no Placeholder

So to avoid a resize it seems there needs to either be no Placeholder at all, or the length of the Placeholder must be equal to Width + 1.
Contributor guide
Assessment
This issue has not been assessed yet.