toast

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 6 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
	Priority Priority
}

Entry is a toast a Manager keeps.

type Manager

type Manager[K comparable, V any] struct {
	Motion Motion
	// Limit is how many toasts show at once; 0 shows every one.
	Limit int
	// 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.

A Limit makes the toasts a queue: the oldest of the highest priority show, and those beyond the limit wait in their place, their motion and their timeout not started until they show, as a list of notifications does. Waiting says how many wait.

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 or the app is in the background. A toast waiting behind Limit's does not move at all.

func (*Manager[K, V]) Dismiss

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

Dismiss begins a shown toast's exit, or removes a waiting toast directly without showing it, 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 shown toast's exit, removes waiting toasts directly without showing them, 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 a toast, and returns the value of the one with its id, which it merges into rather than adding another: that one keeps its place, and one leaving shows again. The timeout and priority are the newest ones'. A timeout of 0 keeps it until it is dismissed.

func (*Manager[K, V]) PushPriority

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

PushPriority is Push with a priority: a PriorityHigh toast shows before the toasts waiting of PriorityNormal.

func (*Manager[K, V]) Visible

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

Visible returns the toasts a Stack shows, oldest first: those Limit leaves room for, which are the highest priorities and then the oldest, and every ending one as it leaves.

func (*Manager[K, V]) Waiting

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

Waiting returns how many toasts wait behind the ones showing, for a stack that shows how many are to come.

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, and a negative Duration or ExitDuration disables that transition.

func SonnerMotion

func SonnerMotion() Motion

SonnerMotion returns Sonner's motion.

type Priority

type Priority int

Priority orders the toasts waiting to show: a higher one shows before every lower one, and the oldest of one priority before the newer.

const (
	// PriorityDefault leaves the priority to the caller's default:
	// PriorityNormal in a Manager, and PriorityHigh in ui.Toaster for an
	// error or a toast that stays until it is dismissed.
	PriorityDefault Priority = iota
	// PriorityNormal is a toast shown in turn, even on an error or a toast
	// that stays until it is dismissed.
	PriorityNormal
	// PriorityHigh shows before the toasts waiting of PriorityNormal, as
	// an error or the progress of a running job does.
	PriorityHigh
)

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()
	// Polite reads it as a status, a polite live region, rather than an
	// alert: for a toast that reports something done, not a failure. A
	// toast with OnPress stays a button.
	Polite bool
	// PoliteFunc is Polite when it changes while the toast shows, as when
	// a toast replaces it; it wins over Polite.
	PoliteFunc func() bool
	// 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