Skip to main content

Starwind UI v1.15 is now available! With new prose, sidebar, and more components

Toast

Installation

Usage

Setup

The Toaster component must be added once to your page, usually in a common layout file shared between pages so you can trigger toasts from anywhere.

src/layouts/Layout.astro
---
import { Toaster } from "@/components/starwind/toast";
---
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
</head>
<body>
<!-- navigation -->
<main>
<!-- content -->
</main>
<Toaster position="bottom-right" />
</body>
</html>

Basic Usage

Toasts are created using the toast function. Import it in a <script> tag and call it to show notifications.

<Button id="show-toast">Show Toast</Button>
<script>
import { toast } from "@/components/starwind/toast";
document.getElementById("show-toast")?.addEventListener("click", () => {
toast("Hello world!");
});
</script>

Variants

The toast system supports multiple variants for different message types.

Promise Toast

Handle async operations with automatic loading, success, and error states.

Updating & Dismissing

You can update existing toasts or dismiss them programmatically.

API Reference

Toaster

The container component that renders toast notifications. Add this once to your layout.

PropTypeDefault
position"top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right""bottom-right"
limitnumber3
durationnumber5000
gapstring"0.5rem"
peekstring"1rem"
classstring-
<Toaster position="bottom-right" limit={3} duration={5000} />

Additional Notes:

  • position: Where toasts appear on screen
  • limit: Maximum number of visible toasts (older ones stack behind)
  • duration: Default auto-dismiss time in milliseconds
  • gap: Spacing between expanded toasts
  • peek: How much stacked toasts peek out from behind

toast()

The main function to create toast notifications.

import { toast } from "@/components/starwind/toast";
// Simple string
toast("Hello world");
// With options
toast("Title", { description: "Description text" });
// Full options object
toast({
title: "Title",
description: "Description",
variant: "success",
duration: 3000,
});

toast.success() / error() / warning() / info() / loading()

Shorthand methods for creating variant-specific toasts.

toast.success("Saved successfully");
toast.error("Something went wrong", { description: "Please try again" });
toast.warning("Check your input");
toast.info("New update available");
toast.loading("Processing..."); // Does not auto-dismiss

toast.promise()

Handle async operations with automatic state transitions.

toast.promise(asyncOperation(), {
loading: "Saving...",
success: "Saved!",
error: "Failed to save",
});
// With dynamic messages
toast.promise(fetchUser(), {
loading: { title: "Loading...", description: "Fetching user data" },
success: (user) => ({ title: "Welcome!", description: `Hello, ${user.name}` }),
error: (err) => `Error: ${err.message}`,
});

toast.update()

Update an existing toast by ID.

const id = toast("Processing...");
toast.update(id, { title: "Done!", variant: "success" });

toast.dismiss()

Dismiss toasts programmatically.

const id = toast("Hello");
toast.dismiss(id); // Dismiss specific toast
toast.dismiss(); // Dismiss all toasts

ToastOptions

PropertyTypeDescription
idstringCustom ID for the toast
titlestringMain toast message
descriptionstringSecondary description text
variant"default" | "success" | "error" | "warning" | "info" | "loading"Visual variant
durationnumberAuto-dismiss time in ms (0 for infinite)
onClose() => voidCallback when close animation starts
onRemove() => voidCallback when toast is removed from DOM

Changelog

v1.0.0

  • Initial release with starwind v1.13.0