toast

package
v0.1.4 Latest Latest
Warning

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

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

Documentation

Overview

Package toast provides notification/toast components for displaying messages.

Available variants:

  • New() creates a toast container (template: "lvt:toast:container:v1")
  • NewMessage() creates a single toast message (template: "lvt:toast:message:v1")

Required lvt-* attributes: name

Example usage:

// In your controller/state
Toasts: toast.New("notifications")

// To show a toast
state.Toasts.Add(toast.NewMessage(
    toast.WithTitle("Success"),
    toast.WithBody("Your changes have been saved."),
    toast.WithType(toast.Success),
))

// In your template
{{template "lvt:toast:container:v1" .Toasts}}

Index

Constants

View Source
const DefaultAutoDismissMS = 5000

DefaultAutoDismissMS is the default auto-dismiss duration for success/info toasts.

Variables

This section is empty.

Functions

func GetTypeIcon

func GetTypeIcon(t Type) string

GetTypeIcon returns a default icon for the toast type.

func Templates

func Templates() *base.TemplateSet

Templates returns the toast component's template set for registration with the LiveTemplate framework.

Example usage in main.go:

import "github.com/livetemplate/lvt/components/toast"

tmpl, err := livetemplate.New("app",
    livetemplate.WithComponentTemplates(toast.Templates()),
)

Available templates:

  • "lvt:toast:container:v1" - Toast container with position
  • "lvt:toast:message:v1" - Individual toast message

Types

type Container

type Container struct {
	base.Base

	// Messages is the list of active toasts
	Messages []Message

	// Position determines where toasts appear
	Position Position

	// MaxVisible limits how many toasts are shown at once (0 = unlimited)
	MaxVisible int

	// Counter for generating unique IDs
	Counter int `json:"counter"`
	// contains filtered or unexported fields
}

Container holds and manages multiple toast notifications. Use template "lvt:toast:container:v1" to render.

func New

func New(id string, opts ...ContainerOption) *Container

New creates a toast container.

Example:

toasts := toast.New("notifications",
    toast.WithPosition(toast.TopRight),
    toast.WithMaxVisible(5),
)

func (*Container) Add

func (c *Container) Add(msg Message)

Add adds a new toast message.

func (*Container) AddError

func (c *Container) AddError(title, body string)

AddError adds an error toast (no auto-dismiss).

func (*Container) AddInfo

func (c *Container) AddInfo(title, body string)

AddInfo adds an info toast that auto-dismisses after 5 seconds.

func (*Container) AddSuccess

func (c *Container) AddSuccess(title, body string)

AddSuccess adds a success toast that auto-dismisses after 5 seconds.

func (*Container) AddWarning

func (c *Container) AddWarning(title, body string)

AddWarning adds a warning toast (no auto-dismiss).

func (*Container) Count

func (c *Container) Count() int

Count returns the number of active toasts.

func (*Container) Dismiss

func (c *Container) Dismiss(id string)

Dismiss removes a toast by ID.

func (*Container) DismissAll

func (c *Container) DismissAll()

DismissAll removes all toasts.

func (*Container) GetPositionClasses

func (c *Container) GetPositionClasses() string

GetPositionClasses returns CSS classes for the container position.

func (*Container) GetTypeClasses

func (c *Container) GetTypeClasses(t interface{}) string

GetTypeClasses returns CSS classes for a toast type using the style adapter. Accepts Type or string to handle values that may lose their named type after JSON round-trip (e.g., state cloning via marshal/unmarshal in mount.go:cloneStateTyped). The alternative — normalizing at deserialization — would require framework-level changes to the generic state cloner.

func (*Container) HasMessages

func (c *Container) HasMessages() bool

HasMessages returns true if there are any toasts.

func (*Container) Styles

func (c *Container) Styles() styles.ToastStyles

Styles returns the resolved ToastStyles for this component.

func (*Container) TakePendingJSON

func (c *Container) TakePendingJSON() string

TakePendingJSON returns JSON-encoded pending messages and clears the queue. Safe to call multiple times per render cycle: the first call drains the queue and caches the result; subsequent calls within the same cycle return the cached value. This handles LiveTemplate's double-evaluation pattern where dynamic expressions are evaluated once for HTML rendering and once for the diff tree. Returns an empty string when there are no pending messages.

Note: the first call drains c.Messages. After template execution completes, Count() and HasMessages() will return zero even though toasts are displaying client-side. This is by design — the server's role ends once the client receives the pending JSON.

func (*Container) VisibleMessages

func (c *Container) VisibleMessages() []Message

VisibleMessages returns the messages to display (respects MaxVisible).

type ContainerOption

type ContainerOption func(*Container)

ContainerOption is a functional option for configuring toast containers.

func WithMaxVisible

func WithMaxVisible(max int) ContainerOption

WithMaxVisible limits how many toasts are shown at once.

func WithPosition

func WithPosition(pos Position) ContainerOption

WithPosition sets the position of the toast container.

func WithStyled

func WithStyled(styled bool) ContainerOption

WithStyled enables Tailwind CSS styling for the component.

type Message

type Message struct {
	ID            string `json:"id"`
	Title         string `json:"title"`
	Body          string `json:"body"`
	Type          Type   `json:"type"`
	Dismissible   bool   `json:"dismissible"`
	Icon          string `json:"icon,omitempty"`
	AutoDismissMS int    `json:"dismissMS"`
}

Message represents a single toast notification.

func NewMessage

func NewMessage(opts ...MessageOption) Message

NewMessage creates a new toast message with options.

type MessageOption

type MessageOption func(*Message)

MessageOption is a functional option for configuring toast messages.

func WithAutoDismiss

func WithAutoDismiss(ms int) MessageOption

WithAutoDismiss sets the auto-dismiss duration in milliseconds. Set to 0 to disable auto-dismiss. Negative values are clamped to 0.

func WithBody

func WithBody(body string) MessageOption

WithBody sets the toast body text.

func WithDismissible

func WithDismissible(dismissible bool) MessageOption

WithDismissible sets whether the toast can be dismissed.

func WithIcon

func WithIcon(icon string) MessageOption

WithIcon sets a custom icon for the toast.

func WithTitle

func WithTitle(title string) MessageOption

WithTitle sets the toast title.

func WithType

func WithType(t Type) MessageOption

WithType sets the toast type (info, success, warning, error).

type Position

type Position string

Position represents where toasts appear on screen.

const (
	TopRight     Position = "top-right"
	TopLeft      Position = "top-left"
	TopCenter    Position = "top-center"
	BottomRight  Position = "bottom-right"
	BottomLeft   Position = "bottom-left"
	BottomCenter Position = "bottom-center"
)

type Type

type Type string

Type represents the visual style/severity of a toast.

const (
	Info    Type = "info"
	Success Type = "success"
	Warning Type = "warning"
	Error   Type = "error"
)

Jump to

Keyboard shortcuts

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