# Toast

<FrameworkCodeSwitcher>
  <div slot="astro">
```astro
---
import { Button } from "@/components/starwind/button";
---

<div class="flex flex-wrap gap-2">
  <Button variant="outline" id="toast-demo-default">Default</Button>
  <Button variant="outline" id="toast-demo-success">Success</Button>
  <Button variant="outline" id="toast-demo-error">Error</Button>
</div>

<script>
  import { toast } from "@starwind-ui/astro/toast";

  document.getElementById("toast-demo-default")?.addEventListener("click", () => {
    toast("Default Toast");
  });

  document.getElementById("toast-demo-success")?.addEventListener("click", () => {
    toast.success("Success!", { description: "Your changes have been saved." });
  });

  document.getElementById("toast-demo-error")?.addEventListener("click", () => {
    toast.error("Error", { description: "Something went wrong." });
  });
</script>
```
  </div>
  <div slot="react">
```tsx
import { Button } from "@/components/starwind/button";
import { toast } from "@starwind-ui/react/toast";

export function Example() {
  return (
    <div className="flex flex-wrap gap-2">
      <Button variant="outline" onClick={() => toast("Default Toast")}>Default</Button>
      <Button variant="outline" onClick={() => toast.success("Success!", { description: "Your changes have been saved." })}>Success</Button>
      <Button variant="outline" onClick={() => toast.error("Error", { description: "Something went wrong." })}>Error</Button>
    </div>
  );
}
```
  </div>
</FrameworkCodeSwitcher>

> **Info:** The styled package supplies the Toaster markup, while imperative notifications now come from the framework primitive entrypoint: `@starwind-ui/astro/toast`.

## Installation

```bash
npx starwind@latest add toast
```

## 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.

```astro title="src/layouts/Layout.astro"
<!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.

```astro
<Button id="show-toast">Show Toast</Button>

<script>
  import { toast } from "@starwind-ui/astro/toast";

  document.getElementById("show-toast")?.addEventListener("click", () => {
    toast("Hello world!");
  });
</script>
```

> **Info:** Import `toast` from `@starwind-ui/astro/toast`; the CLI-installed styled folder no longer ships a separate toast manager.

### Variants

The toast system supports multiple variants for different message types.

```astro
---
import { Button } from "@/components/starwind/button";
---

<div class="flex flex-wrap gap-2">
  <Button variant="outline" id="toast-default">Default</Button>
  <Button variant="outline" id="toast-success">Success</Button>
  <Button variant="outline" id="toast-error">Error</Button>
  <Button variant="outline" id="toast-warning">Warning</Button>
  <Button variant="outline" id="toast-info">Info</Button>
  <Button variant="outline" id="toast-loading">Loading</Button>
</div>

<script>
  import { toast } from "@starwind-ui/astro/toast";

  document.getElementById("toast-default")?.addEventListener("click", () => {
    toast("Default Toast");
  });

  document.getElementById("toast-success")?.addEventListener("click", () => {
    toast.success("Success!", {
      description: "Your changes have been saved.",
    });
  });

  document.getElementById("toast-error")?.addEventListener("click", () => {
    toast.error("Error", {
      description: "Something went wrong.",
    });
  });

  document.getElementById("toast-warning")?.addEventListener("click", () => {
    toast.warning("Warning", {
      description: "Please review your input.",
    });
  });

  document.getElementById("toast-info")?.addEventListener("click", () => {
    toast.info("Info", {
      description: "Here's some helpful information.",
    });
  });

  document.getElementById("toast-loading")?.addEventListener("click", () => {
    toast.loading("Loading...", {
      description: "Please wait while we process your request.",
    });
  });
</script>
```

> **Info:** Variant helpers are provided by the Runtime toast API imported from `@starwind-ui/astro/toast` and rendered by the installed `Toaster` templates.

### Promise Toast

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

```astro
---
import { Button } from "@/components/starwind/button";
---

<div class="flex flex-wrap gap-2">
  <Button variant="primary" id="toast-promise-success">Promise (Success)</Button>
  <Button variant="error" id="toast-promise-error">Promise (Error)</Button>
</div>

<script>
  import { toast } from "@starwind-ui/astro/toast";

  document.getElementById("toast-promise-success")?.addEventListener("click", () => {
    const fakeApiCall = async () => {
      await new Promise((resolve) => setTimeout(resolve, 2000));
      return { name: "John" };
    };

    toast.promise(fakeApiCall(), {
      loading: { title: "Saving...", description: "Please wait" },
      success: (data) => ({ title: "Saved!", description: `Welcome, ${data.name}!` }),
      error: { title: "Error", description: "Failed to save" },
    });
  });

  document.getElementById("toast-promise-error")?.addEventListener("click", () => {
    const fakeApiCall = async () => {
      await new Promise((resolve) => setTimeout(resolve, 2000));
      throw new Error("Network error");
    };

    toast
      .promise(fakeApiCall(), {
        loading: "Processing...",
        success: "Done!",
        error: (err) => ({
          title: err.message || "Contact support for assistance",
          description: "Pardon our dust while we fix this issue.",
        }),
      })
      .catch(() => {});
  });
</script>
```

> **Info:** Promise lifecycle updates now use the shared Runtime toast manager from the Astro primitive package.

### Updating & Dismissing

You can update existing toasts or dismiss them programmatically.

```astro
---
import { Button } from "@/components/starwind/button";
---

<div class="flex flex-wrap gap-2">
  <Button variant="outline" id="toast-update">Create & Update</Button>
  <Button variant="outline" id="toast-dismiss-all">Dismiss All</Button>
</div>

<script>
  import { toast } from "@starwind-ui/astro/toast";

  document.getElementById("toast-update")?.addEventListener("click", () => {
    const id = toast("Processing...", { description: "Step 1 of 3" });

    setTimeout(() => {
      toast.update(id, { title: "Still working...", description: "Step 2 of 3" });
    }, 1500);

    setTimeout(() => {
      toast.update(id, {
        title: "Complete!",
        description: "All steps finished",
        variant: "success",
      });
    }, 3000);
  });

  document.getElementById("toast-dismiss-all")?.addEventListener("click", () => {
    toast.dismiss();
  });
</script>
```

> **Info:** Updating and dismissing notifications uses the shared Runtime toast API rather than the removed component-local manager.

## Framework Availability
Toast is available for Astro and React.
Astro available; React available
| Framework | Status | Notes |
| --- | --- | --- |
| Astro | available | - |
| React | available | - |
## API Reference
### Toaster
| Prop | Type | Required | Default | Kind | Description |
| --- | --- | --- | --- | --- | --- |
| `gap` | `string` | No | `"0.5rem"` | Wrapper prop | Sets the space between repeated items. |
| `peek` | `string` | No | `"1rem"` | Wrapper prop | Sets how much of an adjacent item remains visible. |
- Inherits div attributes.

### ToastTemplate
- Inherits div attributes.

### ToastItem
| Prop | Type | Required | Default | Kind | Description |
| --- | --- | --- | --- | --- | --- |
| `variant` | `"default" \| "success" \| "error" \| "warning" \| "info"` | No | `"default"` | Styled variant | Selects the component's visual variant. |
- Inherits div attributes.

### ToastContent
- Inherits div attributes.

### ToastTitle
| Prop | Type | Required | Default | Kind | Description |
| --- | --- | --- | --- | --- | --- |
| `variant` | `"default" \| "success" \| "error" \| "warning" \| "info" \| "loading"` | No | `"default"` | Styled variant | Selects the component's visual variant. |
- Inherits div attributes.

### ToastDescription
- Inherits div attributes.

### ToastAction
- Inherits button attributes.

### ToastClose
| Prop | Type | Required | Default | Kind | Description |
| --- | --- | --- | --- | --- | --- |
| `showIcon` | `boolean` | No | `true` | Wrapper prop | Shows the component's generated icon. |
- Inherits button attributes.
### Primitive And Runtime API
Behavior, state, events, form participation, and imperative methods are documented in the lower-level references.
- Primitive: [Toast Primitive](/docs/primitives/toast/)
- Runtime factory: [`createToastManager`](/docs/runtime/#create-toast-manager) from `@starwind-ui/runtime/toast`

## Changelog

### v2.0.0

- Rebuilt Toast on Starwind Runtime for manager state, templates, updates, dismissal, and lifecycle.
- See the [Toast Primitive](/docs/primitives/toast/) for the underlying unstyled anatomy and behavior API.