Documentation
¶
Overview ¶
Package bubbledropdown provides Dropdown: a trigger element that opens a scrollable selection panel as a bubble-overlay modal. Embed it in your BubbleTea model, call SetBounds and forward messages, then call ViewWithOverlay to composite the open panel over your main view.
Usage overview:
d := bubbledropdown.New(
bubbledropdown.WithOptions([]string{"Apple", "Banana", "Cherry"}),
bubbledropdown.WithPlaceholder("Pick a fruit"),
)
d.SetZoneManager(zm) // optional; enables bubblezone hit-testing
In your View:
tw, th := d.TriggerSize()
d.SetBounds(row, col, tw, th)
mainView := zm.Mark("my-dropdown", d.TriggerView())
return d.ViewWithOverlay(mainView, width, height)
In your Update, forward all messages:
d, cmd = d.Update(msg)
On ItemChosenMsg, call d.SetSelectedIndex(msg.Index).
Index ¶
- type Config
- type Dropdown
- func (d *Dropdown) AccentColor() string
- func (d *Dropdown) Focused() bool
- func (d *Dropdown) Open() bool
- func (d *Dropdown) Selected() string
- func (d *Dropdown) SelectedIndex() int
- func (d *Dropdown) SetAccentColor(color string)
- func (d *Dropdown) SetBounds(row, col, w, h int)
- func (d *Dropdown) SetFocused(f bool)
- func (d *Dropdown) SetSelectedIndex(i int)
- func (d *Dropdown) SetZoneManager(zm *zone.Manager)
- func (d *Dropdown) TriggerSize() (width, height int)
- func (d *Dropdown) TriggerView() string
- func (d *Dropdown) Update(msg tea.Msg) (*Dropdown, tea.Cmd)
- func (d *Dropdown) ViewWithOverlay(mainView string, viewWidth, viewHeight int) string
- type ItemCanceledMsg
- type ItemChosenMsg
- type Option
- func WithAccentColor(color string) Option
- func WithCursorStyle(s lipgloss.Style) Option
- func WithInitialIndex(i int) Option
- func WithItemStyle(s lipgloss.Style) Option
- func WithListStyle(s lipgloss.Style) Option
- func WithMaxVisible(n int) Option
- func WithOptions(opts []string) Option
- func WithPlaceholder(p string) Option
- func WithTriggerStyle(s lipgloss.Style) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Options []string
InitialIndex int
Placeholder string
MaxVisible int
// Styling. Each style has a corresponding Custom* flag so New can tell an
// intentionally-empty style apart from "not set".
TriggerStyle lipgloss.Style
ListStyle lipgloss.Style
ItemStyle lipgloss.Style
CursorStyle lipgloss.Style
AccentColor string
CustomTriggerStyle bool
CustomListStyle bool
CustomItemStyle bool
CustomCursorStyle bool
}
Config holds static options for a Dropdown built with New.
type Dropdown ¶
type Dropdown struct {
// contains filtered or unexported fields
}
Dropdown is a trigger + panel component. Use New to create one; the zero value is not valid. All methods are safe to call on a nil pointer (they are no-ops / return zero values) so partial initialisation is safe.
func (*Dropdown) AccentColor ¶ added in v0.0.3
AccentColor returns the current accent color used for the panel border, the highlighted item, and the focused trigger arrow. An empty string means the dropdown is neutral (uncolored). Lets consumers detect changes without shadow-tracking the value themselves.
func (*Dropdown) Focused ¶
Focused returns true when the trigger is marked as focused (highlighted arrow).
func (*Dropdown) Selected ¶
Selected returns the currently selected option string, or the placeholder if nothing is selected.
func (*Dropdown) SelectedIndex ¶
SelectedIndex returns the zero-based index of the selected option.
func (*Dropdown) SetAccentColor ¶ added in v0.0.3
SetAccentColor changes the accent color at runtime. Pass any lipgloss color string (e.g. "62" or "#7D56F4"); an empty string clears the accent and returns the dropdown to its neutral, uncolored appearance. This is the live-theming counterpart to WithAccentColor: consumers with a user-editable theme can recolor the dropdown in place instead of rebuilding it.
The focused trigger arrow re-reads the accent on each render, so it updates automatically. If the panel is currently open it is recolored immediately so the change is visible without waiting for the next open. Custom styles set via WithListStyle / WithItemStyle / WithCursorStyle still take precedence.
func (*Dropdown) SetBounds ¶
SetBounds records where the trigger is drawn (0-based row, col) and its display size (w cells wide, h rows tall). Call this every frame before ViewWithOverlay so the panel is positioned correctly.
If w or h is zero, TriggerSize() is used for the missing dimension.
func (*Dropdown) SetFocused ¶
SetFocused marks the trigger as focused or unfocused. When focused, the ▼ arrow is emphasized (bold, plus the accent color when one is set) so keyboard-only users have a visible indicator.
func (*Dropdown) SetSelectedIndex ¶
SetSelectedIndex updates which item appears selected. Out-of-range values are clamped. This is typically called after receiving ItemChosenMsg.
func (*Dropdown) SetZoneManager ¶
SetZoneManager sets the bubblezone manager. When set, the host marks the trigger with zm.Mark and the dropdown skips its own coordinate hit-test (trusting the zone was already checked). The host must call zm.Scan on the final view string.
func (*Dropdown) TriggerSize ¶
TriggerSize returns the stable display-cell width and height (always 1) of the trigger. Width is computed from the longest option so the trigger does not resize as the selection changes.
Use this when building your layout and when calling SetBounds.
func (*Dropdown) TriggerView ¶
TriggerView renders the closed-state trigger: "[ Label ▼ ]".
Focus is indicated on the ▼ arrow. For the default (unstyled) trigger the arrow is drawn in the accent color so keyboard-only users get a visible affordance. When a custom trigger style is set, the arrow instead keeps the trigger's own color (emphasized with bold) so the glyph always matches the caller's styling rather than clashing with the accent.
func (*Dropdown) Update ¶
Update handles all BubbleTea messages. Forward every tea.Msg here.
- When the dropdown is open it routes keys and mouse events to the list panel.
- On ItemChosenMsg / ItemCanceledMsg it closes the dropdown; the host should also handle ItemChosenMsg to call SetSelectedIndex(msg.Index).
- When closed, a mouse press on the trigger (or zone hit) opens the panel.
func (*Dropdown) ViewWithOverlay ¶
ViewWithOverlay returns the view to display. When the dropdown is open it composites the panel over mainView using bubble-overlay. Call this at the end of your View method; it also caches overlay geometry for Update.
mainView := buildMain() mainView = d.ViewWithOverlay(mainView, width, height) return zm.Scan(mainView)
type ItemCanceledMsg ¶
type ItemCanceledMsg struct{}
ItemCanceledMsg is sent when the user dismisses the dropdown without selecting an item (Esc key or click outside the panel).
type ItemChosenMsg ¶
type ItemChosenMsg struct {
Index int // zero-based index into the options slice
Value string // the option string at that index
}
ItemChosenMsg is sent when the user selects an item from the dropdown (Enter key or mouse click on an option). Handle it in your root model; call SetSelectedIndex(msg.Index) on the dropdown to update displayed value.
type Option ¶
type Option func(*Config)
Option configures a Dropdown via New(opts...).
func WithAccentColor ¶ added in v0.0.2
WithAccentColor sets the accent color (any lipgloss color string, e.g. "62" or "#7D56F4") used for the panel border, the highlighted item background, and the focused trigger arrow. It is opt-in: without it the dropdown renders neutrally. Styles set via WithListStyle / WithCursorStyle take precedence over the accent color where they overlap.
func WithCursorStyle ¶ added in v0.0.2
WithCursorStyle overrides the lipgloss style applied to the highlighted (hovered / keyboard-cursor) item row. The default is reverse video, or an inverted accent color when an accent is set via WithAccentColor.
Note: the style should keep symmetric horizontal padding (e.g. Padding(0, 1)) so item rows align with the panel width computation.
func WithInitialIndex ¶
WithInitialIndex sets which item is shown as selected when the dropdown is first rendered. Out-of-range values are clamped to [0, len(options)-1].
func WithItemStyle ¶ added in v0.0.2
WithItemStyle overrides the lipgloss style applied to non-highlighted item rows in the open panel. The default adds one cell of horizontal padding.
Note: the style should keep symmetric horizontal padding (e.g. Padding(0, 1)) so item rows align with the panel width computation.
func WithListStyle ¶
WithListStyle overrides the lipgloss style applied to the open dropdown panel border. The default is a neutral rounded border (the accent color is applied only when set via WithAccentColor).
func WithMaxVisible ¶
WithMaxVisible sets the maximum number of items shown in the open dropdown panel before scrolling kicks in. Defaults to 8.
func WithOptions ¶
WithOptions sets the list of selectable items. An empty slice produces a disabled dropdown that shows only the placeholder.
func WithPlaceholder ¶
WithPlaceholder sets the text shown when no item is selected or the options slice is empty. Defaults to "Select…".
func WithTriggerStyle ¶
WithTriggerStyle overrides the lipgloss style applied to the trigger row (the closed-state "[ Label ▼ ]" element).
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
Basic example: a small form with three dropdowns — fruit, color, and size.
|
Basic example: a small form with three dropdowns — fruit, color, and size. |
|
styled
command
Styled example: two dropdowns with fully customized appearance.
|
Styled example: two dropdowns with fully customized appearance. |
|
v2basic
command
Basic Bubble Tea v2 example: a small form with three dropdowns.
|
Basic Bubble Tea v2 example: a small form with three dropdowns. |
|
Package dropdownv2 provides Dropdown for Bubble Tea v2 (charm.land/bubbletea/v2).
|
Package dropdownv2 provides Dropdown for Bubble Tea v2 (charm.land/bubbletea/v2). |

