chip

package
v0.1.48 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package chip provides a compact chip widget for tags, filters, and inline actions, following the Material 3 chip guidelines.

A chip is a small, interactive, pill-shaped element. It can act as a simple action (assist/suggestion chip) or as a toggleable filter (filter chip).

Construction uses functional options for immutable configuration, while fluent methods handle mutable styling:

c := chip.New(
    chip.Label("In stock"),
    chip.Selectable(true),
    chip.OnSelectedChanged(func(sel bool) { applyFilter(sel) }),
).Padding(2)

Modes

  • Action chip (default): clicking invokes the OnClick handler. Use for assist or suggestion chips.
  • Filter chip (Selectable is true): clicking toggles the selected state and invokes OnSelectedChanged. The chip renders a filled background when selected and an outlined background when not.

Selection write-back

On activation a selectable chip toggles its selected state and writes the new value back to the appropriate source, matching the checkbox widget:

  • With a writable SelectedSignal, the new value is written to the signal (two-way binding).
  • With only a static value, the chip updates its own internal state.
  • With a read-only source (SelectedFn or SelectedReadonlySignal), the chip leaves the source untouched; the owner is responsible for updating it in response to OnSelectedChanged.

Visual Style

The visual rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) may supply its own painter. If no painter is set, DefaultPainter is used.

Signal Binding

Chip properties can be bound to reactive signals from the state package:

Accessibility

A chip is focusable when visible, enabled, and not disabled. It activates on the Enter and Space keys, matching the button widget.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChipColorScheme

type ChipColorScheme struct {
	Background         widget.Color // unselected fill color
	Border             widget.Color // unselected outline color
	Label              widget.Color // unselected label color
	SelectedBackground widget.Color // selected fill color
	SelectedLabel      widget.Color // selected label color
	DisabledBackground widget.Color // fill color when disabled
	DisabledLabel      widget.Color // label color when disabled
}

ChipColorScheme provides theme-derived colors for chip painting. Zero value means the painter should use its built-in defaults.

type DefaultPainter

type DefaultPainter struct{}

DefaultPainter provides a minimal fallback painter with Material-3-flavored styling: an outlined pill when unselected and a filled pill when selected, with a translucent state layer for hover and press.

func (DefaultPainter) ChipFontSize added in v0.1.37

func (DefaultPainter) ChipFontSize() float32

ChipFontSize returns the default font size.

func (DefaultPainter) ChipMinWidth added in v0.1.37

func (DefaultPainter) ChipMinWidth() float32

ChipMinWidth returns the default minimum chip width.

func (DefaultPainter) ChipPadding added in v0.1.37

func (DefaultPainter) ChipPadding() float32

ChipPadding returns the default horizontal padding.

func (DefaultPainter) ChipRadius added in v0.1.37

func (DefaultPainter) ChipRadius() float32

ChipRadius returns the default corner radius.

func (DefaultPainter) PaintChip

func (p DefaultPainter) PaintChip(canvas widget.Canvas, ps PaintState)

PaintChip renders the chip. If state.ColorScheme is non-zero, its colors are used instead of the built-in defaults.

type LayoutMetrics added in v0.1.37

type LayoutMetrics interface {
	// ChipFontSize returns the font size for the chip label.
	ChipFontSize() float32

	// ChipMinWidth returns the minimum chip width in logical pixels.
	ChipMinWidth() float32

	// ChipPadding returns the horizontal padding inside the chip.
	ChipPadding() float32

	// ChipRadius returns the corner radius for the chip.
	ChipRadius() float32
}

LayoutMetrics allows theme painters to provide spatial metrics used by the widget's Layout method to compute chip dimensions.

Painters that implement this interface provide custom metrics. Painters that do not implement it get default values from DefaultPainter.

type Option

type Option func(*config)

Option configures a chip during construction.

func ColorSchemeOpt

func ColorSchemeOpt(cs ChipColorScheme) Option

ColorSchemeOpt sets the color scheme for painting, overriding defaults.

func Disabled

func Disabled(d bool) Option

Disabled sets the chip's disabled state.

func DisabledFn

func DisabledFn(fn func() bool) Option

DisabledFn sets a dynamic function for the disabled state. When set, this takes precedence over the static value but not over a signal set via DisabledSignal.

func DisabledReadonlySignal

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

DisabledReadonlySignal binds the disabled state to a read-only signal. Takes highest precedence over all other disabled sources.

func DisabledSignal

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

DisabledSignal binds the disabled state to a reactive signal.

func Label

func Label(s string) Option

Label sets the chip's static label text.

func LabelFn

func LabelFn(fn func() string) Option

LabelFn sets a dynamic label function evaluated on each draw. When set, this takes precedence over the static label but not over a signal set via LabelSignal or LabelReadonlySignal.

func LabelReadonlySignal

func LabelReadonlySignal(sig state.ReadonlySignal[string]) Option

LabelReadonlySignal binds the chip's label to a read-only signal. When set, this takes highest precedence over all other label sources.

func LabelSignal

func LabelSignal(sig state.Signal[string]) Option

LabelSignal binds the chip's label to a reactive signal. When set, the signal value takes precedence over both LabelFn and Label but not over LabelReadonlySignal.

func OnClick

func OnClick(fn func()) Option

OnClick sets the handler invoked when the chip is activated by click or keyboard. It fires for both action and filter chips.

func OnSelectedChanged

func OnSelectedChanged(fn func(bool)) Option

OnSelectedChanged sets the handler invoked when a selectable chip toggles. The argument is the new selected state. Only fires when Selectable is set.

func PainterOpt

func PainterOpt(p Painter) Option

PainterOpt sets the painter used to render the chip. If not set, DefaultPainter is used.

func Selectable

func Selectable(enabled bool) Option

Selectable makes the chip toggle a selected state on activation (a filter chip). When false (the default), the chip is an action chip that only invokes its OnClick handler.

func Selected

func Selected(sel bool) Option

Selected sets the chip's initial selected state. Only meaningful when Selectable is enabled.

func SelectedFn

func SelectedFn(fn func() bool) Option

SelectedFn sets a dynamic selected-state function evaluated on each draw. Using a function (or signal) places the chip in controlled mode: the owner must update the source in response to OnSelectedChanged.

func SelectedReadonlySignal

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

SelectedReadonlySignal binds the selected state to a read-only signal (controlled mode). Takes highest precedence over all other selected sources.

func SelectedSignal

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

SelectedSignal binds the selected state to a reactive signal (controlled mode).

type PaintState

type PaintState struct {
	Label       string          // chip label text
	Bounds      geometry.Rect   // total widget bounds (excludes outer padding)
	Radius      float32         // corner radius
	FontSize    float32         // label font size in logical pixels (0 = painter default)
	Selectable  bool            // chip toggles a selected state
	Selected    bool            // chip is currently selected
	Hovered     bool            // pointer is over the chip
	Pressed     bool            // chip is being pressed
	Focused     bool            // chip has keyboard focus
	Disabled    bool            // chip is disabled
	ColorScheme ChipColorScheme // theme-derived colors (zero = use defaults)
}

PaintState provides the current chip state to the painter.

type Painter

type Painter interface {
	PaintChip(canvas widget.Canvas, state PaintState)
}

Painter draws the visual representation of a chip. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation. If no Painter is set, the chip uses DefaultPainter.

type Widget

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

Widget implements a compact, interactive chip.

A chip is created with New using functional options:

c := chip.New(
    chip.Label("Tag"),
    chip.OnClick(handleTag),
)

Fluent styling methods may be chained after construction:

c.Padding(2)

func New

func New(opts ...Option) *Widget

New creates a new chip Widget with the given options.

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

func (*Widget) Children

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

Children returns nil because a chip is a leaf widget.

func (*Widget) Draw

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

Draw renders the chip to the canvas.

func (*Widget) Event

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

Event handles an input event and returns true if consumed.

func (*Widget) IsFocusable

func (w *Widget) IsFocusable() bool

IsFocusable reports whether the chip can currently receive focus. Implements widget.Focusable.

func (*Widget) Layout

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

Layout calculates the chip's preferred size within the given constraints.

func (*Widget) Mount

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

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

func (*Widget) Padding

func (w *Widget) Padding(v float32) *Widget

Padding sets the outer padding around the chip content. Returns the widget for method chaining.

func (*Widget) Selected

func (w *Widget) Selected() bool

Selected returns the chip's current resolved selected state.

func (*Widget) SetSelected

func (w *Widget) SetSelected(sel bool)

SetSelected updates the chip's selected state.

If a writable SelectedSignal is bound, the new value is written back to it (two-way binding). With only a static value, the chip's own state is updated. When the selected state is driven by a read-only source (SelectedFn or SelectedReadonlySignal), this is a no-op: the owner controls that source.

func (*Widget) Unmount

func (w *Widget) Unmount()

Unmount is called when the chip 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