collapsible

package
v0.1.8 Latest Latest
Warning

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

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

Documentation

Overview

Package collapsible provides a collapsible/expandable section widget.

A collapsible section has a clickable header bar and a content area that shows or hides when the header is toggled. The expand/collapse transition can be animated using the animation engine.

Construction uses functional options for immutable configuration:

section := collapsible.New(
    collapsible.Title("CPU Usage"),
    collapsible.Content(chartWidget),
    collapsible.Expanded(true),
    collapsible.HeaderHeight(36),
    collapsible.Animated(true),
    collapsible.Duration(200*time.Millisecond),
)

Visual Style

The header rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) can supply its own painter. If no painter is set, DefaultPainter is used, which draws a minimal header with a background, title text, and an expand/collapse arrow indicator.

Animation

When Animated is true (the default), toggling the section smoothly animates the content height using animation.EaseInOutCubic easing. The animation progress drives content clipping via widget.Canvas.PushClip. Set Animated to false for instant expand/collapse.

Signal Binding

The expanded state can be bound to a reactive signal:

  • ExpandedSignal -- TWO-WAY binding: reads from signal AND writes back on toggle

Interaction

Clicking the header or pressing Enter/Space when focused toggles the section. The OnToggle callback is invoked with the new expanded state.

Focus

The collapsible section implements widget.Focusable and participates in tab navigation. A focus ring is drawn on the header when focused.

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 header painter. It draws a background, title text, and an arrow indicator.

func (DefaultPainter) PaintHeader

func (p DefaultPainter) PaintHeader(canvas widget.Canvas, s HeaderState)

PaintHeader renders a minimal collapsible header.

type HeaderState

type HeaderState struct {
	Title         string
	Expanded      bool
	Hovered       bool
	Pressed       bool
	Focused       bool
	Bounds        geometry.Rect
	ArrowProgress float32 // 0.0 = collapsed (right arrow), 1.0 = expanded (down arrow)

	// Styling overrides (zero value means use painter defaults).
	HeaderColor widget.Color
	ArrowColor  widget.Color
}

HeaderState provides the current state to the painter for header rendering.

type Option

type Option func(*config)

Option configures a collapsible section during construction.

func Animated

func Animated(b bool) Option

Animated enables or disables smooth expand/collapse animation. Default is true.

func ArrowColor

func ArrowColor(color widget.Color) Option

ArrowColor sets the color of the expand/collapse arrow indicator. Default is a dark gray.

func Content

func Content(w widget.Widget) Option

Content sets the child widget displayed when expanded.

func Duration

func Duration(d time.Duration) Option

Duration sets the animation duration for expand/collapse. Default is 200ms.

func Expanded

func Expanded(b bool) Option

Expanded sets the initial expanded state. When true, the content is visible by default.

func ExpandedReadonlySignal

func ExpandedReadonlySignal(sig state.ReadonlySignal[bool]) Option

ExpandedReadonlySignal binds the expanded state to a read-only signal. This is useful for computed signals created via state.NewComputed. When set, this takes highest precedence over all other expanded sources.

func ExpandedSignal

func ExpandedSignal(sig state.Signal[bool]) Option

ExpandedSignal binds the expanded state to a reactive signal. This is a TWO-WAY binding: the widget reads from the signal, and when the user toggles, the new state is written back. When set, the signal value takes precedence over Expanded.

func HeaderColor

func HeaderColor(color widget.Color) Option

HeaderColor sets the background color of the header bar. Default is a light gray.

func HeaderHeight

func HeaderHeight(h float32) Option

HeaderHeight sets the height of the header bar in logical pixels. Default is 36.

func OnToggle

func OnToggle(fn func(expanded bool)) Option

OnToggle sets the callback invoked when the section is toggled. The callback receives the new expanded state.

func PainterOpt

func PainterOpt(p Painter) Option

PainterOpt sets the painter used to render the header. Each design system provides its own painter. If not set, DefaultPainter is used.

func Title

func Title(s string) Option

Title sets the static header title text.

func TitleFn

func TitleFn(fn func() string) Option

TitleFn sets a dynamic title function that is evaluated on each draw. When set, this takes precedence over the static title.

type Painter

type Painter interface {
	PaintHeader(canvas widget.Canvas, state HeaderState)
}

Painter draws the visual representation of a collapsible section header. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation.

If no Painter is set, the section uses DefaultPainter.

type Widget

type Widget struct {
	widget.WidgetBase
	// contains filtered or unexported fields
}

Widget implements a collapsible section with a clickable header and expandable content area.

Create with New using functional options:

section := collapsible.New(
    collapsible.Title("Details"),
    collapsible.Content(detailWidget),
    collapsible.Expanded(true),
)

func New

func New(opts ...Option) *Widget

New creates a new collapsible section Widget with the given options.

The returned widget is visible and enabled by default. The content starts collapsed unless Expanded is set to true.

func (*Widget) Children

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

Children returns the content widget for tree traversal. The content is always returned even when collapsed, to allow the framework to manage lifecycle and focus traversal.

func (*Widget) Draw

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

Draw renders the collapsible section.

func (*Widget) Event

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

Event handles input events and returns true if consumed.

func (*Widget) IsAnimating

func (w *Widget) IsAnimating() bool

IsAnimating reports whether an expand/collapse animation is in progress.

func (*Widget) IsExpanded

func (w *Widget) IsExpanded() bool

IsExpanded returns the current expanded state.

func (*Widget) IsFocusable

func (w *Widget) IsFocusable() bool

IsFocusable reports whether the collapsible section can receive focus.

func (*Widget) Layout

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

Layout calculates the widget's size given constraints.

The height depends on the animation progress:

  • Collapsed: headerHeight only
  • Expanded: headerHeight + content height
  • Animating: headerHeight + content height * progress

func (*Widget) Mount

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

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

func (*Widget) Progress

func (w *Widget) Progress() float32

Progress returns the current animation progress (0.0 = collapsed, 1.0 = expanded). This is useful for testing and external monitoring.

func (*Widget) SetExpanded

func (w *Widget) SetExpanded(expanded bool)

SetExpanded sets the expanded state programmatically. If animated, triggers a smooth transition.

func (*Widget) Toggle

func (w *Widget) Toggle()

Toggle toggles the expanded state.

func (*Widget) Unmount

func (w *Widget) Unmount()

Unmount is called when the widget is removed from the tree. Implements widget.Lifecycle.

Jump to

Keyboard shortcuts

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