toast

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advance

type Advance[K comparable, V any] struct {
	Changed   bool
	Presented []K
	Ending    []K
	Removed   []Entry[K, V]
}

Advance is what an Advance changed: the toasts it presented, the ones whose timeout ran out, and the ones it removed.

type Anchor

type Anchor uint8

Anchor is the edge of the viewport a Stack lays out from: the newest toast is nearest it.

const (
	AnchorTopRight Anchor = iota
	AnchorTopLeft
	AnchorTopCenter
	AnchorBottomLeft
	AnchorBottomCenter
	AnchorBottomRight
	AnchorLeftCenter
	AnchorRightCenter
)

type Entry

type Entry[K comparable, V any] struct {
	ID     K
	Value  V
	Status Status
}

Entry is a toast a Manager keeps.

type Manager

type Manager[K comparable, V any] struct {
	Motion Motion
	// contains filtered or unexported fields
}

Manager keeps toasts in order with their lifecycle, as gpui-base's: each one starts, is present once the motion's Duration has passed, counts its timeout down while present and not paused, and stays ending for the ExitDuration before it is removed. It renders nothing and runs no timer: the app pushes toasts, renders Visible with a Stack and calls Advance on a timer while any is shown, passing whether the stack is expanded. Times are the app's clock's, Scope.Now, so a test's manual clock moves them. The zero value has no toasts and Sonner's motion.

func (*Manager[K, V]) Advance

func (m *Manager[K, V]) Advance(now time.Time, paused bool) Advance[K, V]

Advance moves the lifecycle on to now; paused holds present toasts' timeouts, as while the stack is expanded.

func (*Manager[K, V]) Dismiss

func (m *Manager[K, V]) Dismiss(id K, now time.Time) bool

Dismiss begins a toast's exit and reports whether it was not leaving already.

func (*Manager[K, V]) DismissAll

func (m *Manager[K, V]) DismissAll(now time.Time) []K

DismissAll begins every toast's exit and returns the ids of those that were not leaving already.

func (*Manager[K, V]) Entries

func (m *Manager[K, V]) Entries() []Entry[K, V]

Entries returns every toast, oldest first.

func (*Manager[K, V]) Get

func (m *Manager[K, V]) Get(id K) (v V, ok bool)

Get returns the value of the toast with id.

func (*Manager[K, V]) Len

func (m *Manager[K, V]) Len() int

Len returns how many toasts it keeps.

func (*Manager[K, V]) Push

func (m *Manager[K, V]) Push(id K, value V, timeout time.Duration, now time.Time) (replaced V, ok bool)

Push adds the newest toast, replacing one with the same id, whose value it returns. A timeout of 0 keeps it until it is dismissed.

func (*Manager[K, V]) Visible

func (m *Manager[K, V]) Visible(limit int) []Entry[K, V]

Visible returns the newest limit toasts that are not ending and every ending one, oldest first: what a Stack shows.

type Motion

type Motion struct {
	// Duration is how long a toast enters, and the stack's springs'
	// response.
	Duration time.Duration
	// ExitDuration is how long a toast leaves.
	ExitDuration time.Duration
	// CollapsedPeek is how far apart collapsed toasts overlap, and
	// ExpandedGap the space between expanded ones.
	CollapsedPeek, ExpandedGap core.Points
	// CollapsedScaleStep is how much narrower each collapsed toast behind
	// the newest is, as a fraction of its width.
	CollapsedScaleStep float32
	// CollapsedVisible is how many collapsed toasts show.
	CollapsedVisible int
}

Motion are the tokens of a Manager and a Stack; a zero field keeps Sonner's, which SonnerMotion returns.

func SonnerMotion

func SonnerMotion() Motion

SonnerMotion returns Sonner's motion.

type Stack

type Stack[T any, K comparable] struct {
	Toasts           func() []T
	Key              func(T) K
	Toast            func(cx *core.Scope, item func() T) core.Node
	Motion           Motion
	Placement        Anchor
	Reverse          bool
	NoCloseOnEscape  bool
	OnExpandedChange func(expanded bool)
	OnCancel         func()
	Class            string
	Style            core.Style
	StyleFunc        func() core.Style
	TestID           string
}

Stack lays toasts out, as gpui-base's: Toasts are its items, oldest first, each rendered by Toast and matched by Key as core.Each matches rows. From Placement's edge, collapsed toasts overlap CollapsedPeek apart, each further one narrower by CollapsedScaleStep, and only CollapsedVisible show; expanded, while the pointer or a finger is on it, they are a list ExpandedGap apart, so the app pauses its manager (OnExpandedChange). Reverse turns the stack around: the newest toast is the farthest from the edge and the oldest the nearest, as a list growing away from it. Arrivals, departures and expansion move them with critically damped springs of the motion's Duration; a Toast that is ending stays where it shows and takes no room, so the others close up as it leaves. While it holds toasts it hears Escape and back (OnCancel: dismiss the newest), unless NoCloseOnEscape. It has no look: its style gives its width, and the app places it, in an Overlay at that edge inside the safe area, centered at a center anchor.

func (Stack[T, K]) Render

func (s Stack[T, K]) Render(cx *core.Scope) core.Node

Render implements core.Node.

type Status

type Status uint8

Status is a toast's phase in its lifecycle.

const (
	// Starting is a toast entering, for the motion's Duration.
	Starting Status = iota
	// Present is a toast shown, counting its timeout down.
	Present
	// Ending is a toast leaving, for the motion's ExitDuration,
	// before it is removed.
	Ending
)

type SwipeDirection

type SwipeDirection uint8

SwipeDirection is the direction a toast is swiped away in.

const (
	SwipeRight SwipeDirection = iota
	SwipeLeft
	SwipeUp
	SwipeDown
	// SwipeOff keeps the toast from being swiped.
	SwipeOff
)

type Toast

type Toast struct {
	// Status is its phase, Starting when nil.
	Status         func() Status
	SwipeDirection SwipeDirection
	SwipeThreshold core.Points
	StartingClass  string
	StartingStyle  core.Style
	EndingClass    string
	EndingStyle    core.Style
	SwipingClass   string
	SwipingStyle   core.Style
	Label          string
	// LabelFunc is a label that changes while the toast shows, as when a toast replaces it; it wins over Label, and the screen reader reads each new one.
	LabelFunc func() string
	// OnPress makes the toast itself a button. Child controls such as action buttons keep their own presses.
	OnPress func()
	// OnDismissRequest: the user swiped it away; dismiss it. It stays
	// where the swipe took it.
	OnDismissRequest func()
	Class            string
	Style            core.Style
	StyleFunc        func() core.Style
	Children         []core.Node
	TestID           string
	// OnLayout and Tooltip are its view's, as a core.View's.
	OnLayout func(core.Bounds)
	Tooltip  core.Node
}

Toast is an unstyled toast's root, as gpui-base's: the alert role around the app's content. Its look follows Status, a Manager's, through StartingStyle in its first frame while starting (so a Transition in its style animates its entry from it), EndingStyle while ending, and SwipingStyle while a pointer swipes it; they merge over Style and StyleFunc, and their classes are important, over both. A finger or the mouse dragged toward SwipeDirection moves it once it moved 10 points, so its buttons stay buttons; let go past SwipeThreshold (50 when 0) or flicked, it moves on out and asks to be dismissed with OnDismissRequest, and short of it goes back. While ending it takes no presses. The screen reader reads Label as it shows. A Stack lays toasts out.

The swipe moves a view around the root so its animated translation composes with StartingStyle, EndingStyle and the app's own Transform.

func (Toast) Render

func (t Toast) Render(cx *core.Scope) core.Node

Render implements core.Node.

Jump to

Keyboard shortcuts

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