toast

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package toast provides feedback notifications for Vango applications.

Since Vango uses persistent WebSocket connections, traditional HTTP flash cookies don't work. Instead, toasts use the generic ctx.Emit() mechanism to dispatch events to the client.

Zero Protocol Changes

This package uses the existing Custom Event mechanism (ctx.Emit) rather than adding new protocol opcodes. This keeps the thin client at ~9.5KB and allows users to choose their own toast UI library.

Client-Side Handler

The client-side handler is user-defined, allowing integration with any toast library (Toastify, Sonner, vanilla JS, etc.):

// user/app.js
window.addEventListener("vango:toast", (e) => {
    const { level, message, title } = e.detail;

    // Option 1: Use a library like Toastify
    Toastify({ text: message, className: level }).showToast();

    // Option 2: Use Sonner
    toast[level](message);

    // Option 3: Vanilla JS
    showCustomToast(level, message);
});

Server-Side Usage

In your Vango handlers:

func DeleteProject(ctx vango.Ctx, id int) error {
    if err := db.Projects.Delete(id); err != nil {
        toast.Error(ctx, "Failed to delete project")
        return err
    }

    toast.Success(ctx, "Project deleted")
    ctx.Navigate("/projects")
    return nil
}

With title:

toast.WithTitle(ctx, toast.TypeSuccess, "Settings", "Your changes have been saved.")

Index

Constants

View Source
const EventName = "vango:toast"

EventName is the event name dispatched for toasts. Client-side code should listen for this event.

Variables

This section is empty.

Functions

func Custom

func Custom(ctx server.Ctx, data map[string]any)

Custom shows a toast with custom data. Use this for advanced toast configurations.

func Error

func Error(ctx server.Ctx, message string)

Error shows an error toast.

toast.Error(ctx, "Failed to delete item")

func Info

func Info(ctx server.Ctx, message string)

Info shows an info toast.

toast.Info(ctx, "New features available")

func Show

func Show(ctx server.Ctx, level Type, message string)

Show displays a toast notification to the user. Uses ctx.Emit to send a custom event to the client.

The client receives a CustomEvent with:

  • event.type = "vango:toast"
  • event.detail = { level: "success|error|warning|info", message: "..." }

func Success

func Success(ctx server.Ctx, message string)

Success shows a success toast.

toast.Success(ctx, "Changes saved!")

func Warning

func Warning(ctx server.Ctx, message string)

Warning shows a warning toast.

toast.Warning(ctx, "This action cannot be undone")

func WithAction

func WithAction(ctx server.Ctx, level Type, message, actionLabel, actionID string)

WithAction shows a toast with an action button.

toast.WithAction(ctx, toast.TypeInfo, "Undo available", "Click to undo", "undo")

func WithTitle

func WithTitle(ctx server.Ctx, level Type, title, message string)

WithTitle shows a toast with a title and message.

toast.WithTitle(ctx, toast.TypeSuccess, "Settings", "Your changes have been saved.")

Types

type Type

type Type string

Type represents the toast notification type.

const (
	TypeSuccess Type = "success"
	TypeError   Type = "error"
	TypeWarning Type = "warning"
	TypeInfo    Type = "info"
)

Jump to

Keyboard shortcuts

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