dropdown

package
v0.1.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2026 License: MIT Imports: 5 Imported by: 0

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

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 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

func (d ItemDef) DisplayText() string

DisplayText returns the text to display for this item. Returns Label if set, otherwise Value.

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 A11yHint

func A11yHint(hint string) Option

A11yHint sets the accessibility hint text for the dropdown.

func Disabled

func Disabled(d bool) Option

Disabled sets the dropdown's disabled state.

func DisabledFn

func DisabledFn(fn func() bool) Option

DisabledFn sets a dynamic function that determines whether the dropdown is disabled. When set, this takes precedence over the static value.

func ItemDefs

func ItemDefs(items []ItemDef) Option

ItemDefs sets the dropdown items from ItemDef definitions.

func Items

func Items(items ...string) Option

Items sets the dropdown items from strings. Each string becomes both the value and label.

func MaxVisibleItems

func MaxVisibleItems(n int) Option

MaxVisibleItems sets the maximum number of items visible in the menu before scrolling is required. Defaults to 8.

func OnChange

func OnChange(fn func(index int, value string)) Option

OnChange sets the callback invoked when the selection changes. The callback receives the new index and the selected item's value.

func PainterOpt

func PainterOpt(p Painter) Option

PainterOpt sets the painter used to render the dropdown.

func Placeholder

func Placeholder(text string) Option

Placeholder sets the text shown when no item is selected.

func Selected

func Selected(index int) Option

Selected sets the initially selected item index. Use -1 for no selection.

func SelectedSignal

func SelectedSignal(sig state.Signal[int]) Option

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.

func Signal deprecated

func Signal(s state.Signal[int]) Option

Deprecated: Use SelectedSignal instead.

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

func New(opts ...Option) *Widget

New creates a new dropdown Widget with the given options.

The returned widget is visible, enabled, and focusable by default.

func (*Widget) A11yExpanded

func (w *Widget) A11yExpanded() bool

A11yExpanded returns true if the dropdown menu is currently visible. Maps to the aria-expanded attribute.

func (*Widget) A11yLabel

func (w *Widget) A11yLabel() string

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

func (w *Widget) A11yRole() string

A11yRole returns the ARIA role for the dropdown widget. Returns "combobox" per WAI-ARIA 1.2 spec for dropdown/select controls.

func (*Widget) A11yValue

func (w *Widget) A11yValue() string

A11yValue returns the currently displayed value for assistive technology. If no item is selected, it returns the placeholder text.

func (*Widget) Children

func (w *Widget) Children() []widget.Widget

Children returns nil; the dropdown trigger is a leaf widget. The menu is rendered in the overlay, not as a child.

func (*Widget) Close

func (w *Widget) Close(ctx widget.Context)

Close closes the dropdown menu overlay.

func (*Widget) Draw

func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the dropdown trigger.

func (*Widget) Event

func (w *Widget) Event(ctx widget.Context, e event.Event) bool

Event handles input events for the dropdown trigger.

func (*Widget) IsFocusable

func (w *Widget) IsFocusable() bool

IsFocusable reports whether the dropdown can currently receive focus.

func (*Widget) IsOpen

func (w *Widget) IsOpen() bool

IsOpen returns true if the dropdown menu is currently visible.

func (*Widget) Layout

func (w *Widget) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the dropdown trigger's preferred size.

func (*Widget) Mount

func (w *Widget) Mount(ctx widget.Context)

Mount creates signal bindings for push-based invalidation. Implements widget.Lifecycle.

func (*Widget) Open

func (w *Widget) Open(ctx widget.Context)

Open opens the dropdown menu overlay.

func (*Widget) SelectedIndex

func (w *Widget) SelectedIndex() int

SelectedIndex returns the currently selected item index, or -1 if none.

func (*Widget) SelectedValue

func (w *Widget) SelectedValue() string

SelectedValue returns the value of the currently selected item, or an empty string if nothing is selected.

func (*Widget) SetSelectedIndex

func (w *Widget) SetSelectedIndex(index int)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL