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: lvt-click
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
- func GetTypeIcon(t Type) string
- func Templates() *base.TemplateSet
- type Container
- func (c *Container) Add(msg Message)
- func (c *Container) AddError(title, body string)
- func (c *Container) AddInfo(title, body string)
- func (c *Container) AddSuccess(title, body string)
- func (c *Container) AddWarning(title, body string)
- func (c *Container) Count() int
- func (c *Container) Dismiss(id string)
- func (c *Container) DismissAll()
- func (c *Container) GetPositionClasses() string
- func (c *Container) GetTypeClasses(t interface{}) string
- func (c *Container) HasMessages() bool
- func (c *Container) Styles() styles.ToastStyles
- func (c *Container) TakePendingJSON() string
- func (c *Container) VisibleMessages() []Message
- type ContainerOption
- type Message
- type MessageOption
- type Position
- type Type
Constants ¶
const DefaultAutoDismissMS = 5000
DefaultAutoDismissMS is the default auto-dismiss duration for success/info toasts.
Variables ¶
This section is empty.
Functions ¶
func GetTypeIcon ¶
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) AddSuccess ¶
AddSuccess adds a success toast that auto-dismisses after 5 seconds.
func (*Container) AddWarning ¶
AddWarning adds a warning toast (no auto-dismiss).
func (*Container) GetPositionClasses ¶
GetPositionClasses returns CSS classes for the container position.
func (*Container) GetTypeClasses ¶
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 ¶
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 ¶
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 ¶
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 WithDismissible ¶
func WithDismissible(dismissible bool) MessageOption
WithDismissible sets whether the toast can be dismissed.
func WithType ¶
func WithType(t Type) MessageOption
WithType sets the toast type (info, success, warning, error).