How BubbleZone works: Mark, Scan, and Check
masterBubbleZone solves the problem of determining which component was clicked in complex, nested BubbleTea/Lipgloss interfaces. The workflow consists of three steps:
- Mark: In your child components'
View()methods, wrap the desired area withzone.Mark(id, content). This assigns a unique identifier to that area. - Scan: In your root model's
View()method, wrap the entire output inzone.Scan(). This registers all zones and strips the invisible markers from the final output so they don't affect layout. - Check: In your
Update()method, usezone.Get(id).InBounds(msg)to check if a mouse event (liketea.MouseReleaseMsg) occurred within the bounds of a specific zone.
Requirements:
- You must enable
AltScreenin your BubbleTea view. - You must set
MouseModetotea.MouseModeCellMotionto enable mouse motion tracking.
// 1. Mark in child View
func (m model) View() string {
return zone.Mark("confirm", okButton)
}
// 2. Scan in root View
func (r app) View() tea.View {
view := tea.NewView()
view.AltScreen = true
view.MouseMode = tea.MouseModeCellMotion
view.SetContent(zone.Scan(r.someStyle.Render(content)))
return view
}
// 3. Check in Update
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if msg, ok := msg.(tea.MouseReleaseMsg); ok {
if zone.Get("confirm").InBounds(msg) {
// Handle click
}
}
return m, nil
}