Documentation
¶
Overview ¶
Package dropdown provides a dropdown/select widget that displays a list of items in a floating menu when activated.
The user selects an item from the list using mouse clicks or keyboard navigation (Up/Down arrows to highlight, Enter to confirm, Escape to close). The dropdown consists of two parts: a trigger element that shows the current selection, and a menu overlay that appears below the trigger when activated.
Construction ¶
Construction uses functional options for immutable configuration:
dd := dropdown.New(
dropdown.Items("Red", "Green", "Blue"),
dropdown.Selected(0),
dropdown.Placeholder("Choose a color..."),
dropdown.OnChange(func(index int, value string) {
fmt.Println("Selected:", value)
}),
)
For items with separate labels and values, use ItemDefs:
dd := dropdown.New(
dropdown.ItemDefs([]dropdown.ItemDef{
{Value: "sm", Label: "Small"},
{Value: "md", Label: "Medium"},
{Value: "lg", Label: "Large"},
}),
)
Visual Style ¶
The visual rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) supplies its own painter to render dropdowns in the appropriate visual style.
If no painter is set, DefaultPainter is used, which draws a minimal dropdown suitable for testing and prototyping.
Overlay Menu ¶
The menu is displayed as an overlay.Overlay obtained from widget.Context. This keeps the menu above other widgets and allows it to extend beyond the dropdown's own bounds. The menu supports:
- Scrolling when items exceed MaxVisibleItems
- Keyboard navigation within the menu
- Click-outside dismissal
Signal Binding ¶
The selected index can be bound to a reactive signal for two-way synchronization:
idx := state.NewSignal(0)
dd := dropdown.New(
dropdown.Items("A", "B", "C"),
dropdown.SelectedSignal(idx),
)
Focus ¶
Dropdowns implement widget.Focusable and participate in tab navigation. When focused, pressing Space or Enter opens the menu.
Package dropdown provides a dropdown/select widget that displays a list of items in a floating menu when activated. The user can select an item from the list using mouse clicks or keyboard navigation (Up/Down/Enter/Escape).
The dropdown consists of two parts:
- A trigger element (button-like) that shows the current selection
- A menu overlay that appears below the trigger when activated
Dropdown follows the functional options pattern for construction:
dd := dropdown.New(
dropdown.Items("Red", "Green", "Blue"),
dropdown.Selected(0),
dropdown.OnChange(func(index int, value string) {
fmt.Println("Selected:", value)
}),
)
The visual appearance is controlled by a pluggable Painter interface. The default painter provides a minimal fallback; use a design system painter (e.g., material3.DropdownPainter) for production styling.
Index ¶
- type DefaultPainter
- type DropdownColorScheme
- type ItemDef
- type MenuPaintState
- type Option
- func A11yHint(hint string) Option
- func Disabled(d bool) Option
- func DisabledFn(fn func() bool) Option
- func ItemDefs(items []ItemDef) Option
- func Items(items ...string) Option
- func MaxVisibleItems(n int) Option
- func OnChange(fn func(index int, value string)) Option
- func PainterOpt(p Painter) Option
- func Placeholder(text string) Option
- func Selected(index int) Option
- func SelectedSignal(sig state.Signal[int]) Option
- func Signal(s state.Signal[int]) Optiondeprecated
- type Painter
- type TriggerPaintState
- type Widget
- func (w *Widget) A11yExpanded() bool
- func (w *Widget) A11yLabel() string
- func (w *Widget) A11yRole() string
- func (w *Widget) A11yValue() string
- func (w *Widget) Children() []widget.Widget
- func (w *Widget) Close(ctx widget.Context)
- func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas)
- func (w *Widget) Event(ctx widget.Context, e event.Event) bool
- func (w *Widget) IsFocusable() bool
- func (w *Widget) IsOpen() bool
- func (w *Widget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size
- func (w *Widget) Mount(ctx widget.Context)
- func (w *Widget) Open(ctx widget.Context)
- func (w *Widget) SelectedIndex() int
- func (w *Widget) SelectedValue() string
- func (w *Widget) SetSelectedIndex(index int)
- func (w *Widget) Unmount()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultPainter ¶
type DefaultPainter struct{}
DefaultPainter provides a minimal fallback painter for dropdowns. It draws simple rectangles and text without design system styling.
func (DefaultPainter) PaintMenu ¶
func (p DefaultPainter) PaintMenu(canvas widget.Canvas, st *MenuPaintState)
PaintMenu renders a minimal dropdown menu.
func (DefaultPainter) PaintTrigger ¶
func (p DefaultPainter) PaintTrigger(canvas widget.Canvas, st *TriggerPaintState)
PaintTrigger renders a minimal dropdown trigger.
type DropdownColorScheme ¶
type DropdownColorScheme struct {
Background widget.Color
Border widget.Color
FocusBorder widget.Color
TextColor widget.Color
PlaceholderText widget.Color
DisabledBg widget.Color
DisabledFg widget.Color
MenuBg widget.Color
MenuBorder widget.Color
ItemHover widget.Color
ItemSelected widget.Color
ItemDisabled widget.Color
ChevronColor widget.Color
FocusRing widget.Color
}
DropdownColorScheme provides theme-derived colors for dropdown painting. Zero value means the painter should use its built-in defaults.
type ItemDef ¶
type ItemDef struct {
// Value is the internal value associated with this item.
Value string
// Label is the display text. If empty, Value is used as the display text.
Label string
// Disabled prevents this item from being selected.
Disabled bool
}
ItemDef defines a single item in the dropdown list.
func (ItemDef) DisplayText ¶
DisplayText returns the text to display for this item. Returns Label if set, otherwise Value.
type MenuPaintState ¶
type MenuPaintState struct {
// Bounds is the menu's bounds in window coordinates.
Bounds geometry.Rect
// Items is the list of menu items.
Items []ItemDef
// HighlightedIndex is the index of the currently highlighted item (-1 for none).
HighlightedIndex int
// SelectedIndex is the index of the currently selected item (-1 for none).
SelectedIndex int
// ScrollOffset is the index of the first visible item.
ScrollOffset int
// VisibleCount is the number of items visible without scrolling.
VisibleCount int
// ItemHeight is the height of each item in the menu.
ItemHeight float32
// ColorScheme provides theme-derived colors. Zero value means use defaults.
ColorScheme DropdownColorScheme
}
MenuPaintState provides the current menu state to the painter.
type Option ¶
type Option func(*config)
Option configures a dropdown during construction.
func DisabledFn ¶
DisabledFn sets a dynamic function that determines whether the dropdown is disabled. When set, this takes precedence over the static value.
func Items ¶
Items sets the dropdown items from strings. Each string becomes both the value and label.
func MaxVisibleItems ¶
MaxVisibleItems sets the maximum number of items visible in the menu before scrolling is required. Defaults to 8.
func OnChange ¶
OnChange sets the callback invoked when the selection changes. The callback receives the new index and the selected item's value.
func PainterOpt ¶
PainterOpt sets the painter used to render the dropdown.
func Placeholder ¶
Placeholder sets the text shown when no item is selected.
func SelectedSignal ¶
SelectedSignal binds the dropdown's selected index to a reactive signal for two-way data binding. When the selection changes, the signal is updated. When the signal is set externally, the dropdown reflects the change.
type Painter ¶
type Painter interface {
// PaintTrigger draws the closed dropdown trigger (shows current selection).
PaintTrigger(canvas widget.Canvas, state *TriggerPaintState)
// PaintMenu draws the open dropdown menu.
PaintMenu(canvas widget.Canvas, state *MenuPaintState)
}
Painter draws the visual representation of a dropdown. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation. If no Painter is set, DefaultPainter is used.
type TriggerPaintState ¶
type TriggerPaintState struct {
// Bounds is the trigger widget's bounds.
Bounds geometry.Rect
// SelectedText is the display text of the currently selected item,
// or the placeholder if nothing is selected.
SelectedText string
// IsPlaceholder is true if SelectedText is the placeholder (nothing selected).
IsPlaceholder bool
// Open is true if the dropdown menu is currently visible.
Open bool
// Focused is true if the dropdown has keyboard focus.
Focused bool
// Hovered is true if the mouse is over the trigger.
Hovered bool
// Disabled is true if the dropdown is disabled.
Disabled bool
// ColorScheme provides theme-derived colors. Zero value means use defaults.
ColorScheme DropdownColorScheme
}
TriggerPaintState provides the current dropdown trigger state to the painter.
type Widget ¶
type Widget struct {
widget.WidgetBase
// contains filtered or unexported fields
}
Widget implements a dropdown/select widget with a trigger element and a floating menu overlay. It supports keyboard navigation, mouse selection, and mouse wheel scrolling within the menu.
Create with New using functional options.
func New ¶
New creates a new dropdown Widget with the given options.
The returned widget is visible, enabled, and focusable by default.
func (*Widget) A11yExpanded ¶
A11yExpanded returns true if the dropdown menu is currently visible. Maps to the aria-expanded attribute.
func (*Widget) A11yLabel ¶
A11yLabel returns the accessible label for the dropdown. It uses the configured accessibility hint if set, or falls back to "dropdown" as a generic label.
func (*Widget) A11yRole ¶
A11yRole returns the ARIA role for the dropdown widget. Returns "combobox" per WAI-ARIA 1.2 spec for dropdown/select controls.
func (*Widget) A11yValue ¶
A11yValue returns the currently displayed value for assistive technology. If no item is selected, it returns the placeholder text.
func (*Widget) Children ¶
Children returns nil; the dropdown trigger is a leaf widget. The menu is rendered in the overlay, not as a child.
func (*Widget) IsFocusable ¶
IsFocusable reports whether the dropdown can currently receive focus.
func (*Widget) Mount ¶
Mount creates signal bindings for push-based invalidation. Implements widget.Lifecycle.
func (*Widget) SelectedIndex ¶
SelectedIndex returns the currently selected item index, or -1 if none.
func (*Widget) SelectedValue ¶
SelectedValue returns the value of the currently selected item, or an empty string if nothing is selected.
func (*Widget) SetSelectedIndex ¶
SetSelectedIndex programmatically sets the selected item.
func (*Widget) Unmount ¶
func (w *Widget) Unmount()
Unmount is called when the dropdown is removed from the widget tree. Implements widget.Lifecycle.