---
title: Toast
description: A succinct message that is displayed temporarily.
---

```templ
package examples

import "github.com/axadrn/shadcn-templ/v2/components/button"

templ ToastDemo() {
	@button.Button(button.Props{
		Variant:    button.VariantOutline,
		Attributes: templ.Attributes{"id": "toast-demo-trigger"},
	}) {
		Show Toast
	}
	<script nonce={ templ.GetNonce(ctx) }>
		document.getElementById("toast-demo-trigger").addEventListener("click", () => {
			const id = window.tui.toast.add({
				title: "Event created",
				description: "Sunday, December 3 at 9:00 AM",
				actionProps: {
					children: "Undo",
					onClick() {
						window.tui.toast.close(id);
					},
				},
			});
		});
	</script>
}
```

## Installation

<CodeTabs>

<TabsList>
  <TabsTrigger value="cli">Command</TabsTrigger>
  <TabsTrigger value="manual">Manual</TabsTrigger>
</TabsList>
<TabsContent value="cli">

```bash
shadcn-templ add toast
```

</TabsContent>

<TabsContent value="manual">

<Steps className="mb-0 pt-2">

<Step>Copy and paste the following code into your project.</Step>

<ComponentSource name="toast" title="components/toast/toast.templ" />

<ComponentSource name="toast" title="components/toast/toast.js" />

Component scripts are loaded through the shared script bundle, see [JavaScript](/docs/installation#javascript).

<Step>Update the import paths to match your project setup.</Step>

</Steps>

</TabsContent>

</CodeTabs>

Add the `Toaster` component to your base layout.

```templ showLineNumbers title="layout.templ"
import "github.com/axadrn/shadcn-templ/v2/components/toast"

templ Layout() {
	<html lang="en">
		<body>
			<main>{ children... }</main>
			@toast.Toaster()
		</body>
	</html>
}
```

## Usage

```js showLineNumbers
const id = window.tui.toast.add({
	title: "Event created",
	description: "Sunday, December 3 at 9:00 AM",
})
```

## Types

Set the `type` option to render a status icon. The built-in renderer recognizes `success`, `info`, `warning`, `error`, and `loading`.

```templ
package examples

import "github.com/axadrn/shadcn-templ/v2/components/button"

type toastTypeDemo struct {
	Label       string
	Type        string
	Description string
}

var toastTypeDemos = []toastTypeDemo{
	{"Default", "", "Event has been created."},
	{"Success", "success", "Event has been created."},
	{"Info", "info", "Arrive 10 minutes before the event."},
	{"Warning", "warning", "The event is almost full."},
	{"Error", "error", "Could not create event."},
	{"Loading", "loading", "Creating event..."},
}

templ ToastTypes() {
	<div class="flex flex-wrap gap-2" id="toast-types">
		for _, demo := range toastTypeDemos {
			@button.Button(button.Props{
				Variant: button.VariantOutline,
				Attributes: templ.Attributes{
					"data-toast-type":        demo.Type,
					"data-toast-description": demo.Description,
				},
			}) {
				{ demo.Label }
			}
		}
	</div>
	<script nonce={ templ.GetNonce(ctx) }>
		document.getElementById("toast-types").addEventListener("click", (e) => {
			const btn = e.target.closest("[data-toast-description]");
			if (!btn) return;
			window.tui.toast.add({
				type: btn.getAttribute("data-toast-type") || undefined,
				description: btn.getAttribute("data-toast-description"),
			});
		});
	</script>
}
```

## Action

Pass button props with `actionProps` to render an action.

```js showLineNumbers
const id = window.tui.toast.add({
	title: "Event created",
	actionProps: {
		children: "Undo",
		onClick() {
			window.tui.toast.close(id)
		},
	},
})
```

## Promise

Use `toast.promise` to update one toast as an asynchronous task moves through loading, success, and error states.

```templ
package examples

import "github.com/axadrn/shadcn-templ/v2/components/button"

templ ToastPromise() {
	@button.Button(button.Props{
		Variant:    button.VariantOutline,
		Attributes: templ.Attributes{"id": "toast-promise-trigger"},
	}) {
		Create Event
	}
	<script nonce={ templ.GetNonce(ctx) }>
		document.getElementById("toast-promise-trigger").addEventListener("click", () => {
			window.tui.toast.promise(
				new Promise((resolve) => {
					window.setTimeout(() => resolve({ name: "Event" }), 2000);
				}),
				{
					loading: "Creating event…",
					success: (data) => `${data.name} created.`,
					error: "Could not create event.",
				},
			);
		});
	</script>
}
```

## API Reference

### Toaster

The `toast.Toaster` component hosts the toasts, mount it once in your layout.

| Prop      | Type     | Default |
| --------- | -------- | ------- |
| `Timeout` | `int`    | `5000`  |
| `Limit`   | `int`    | `3`     |
| `Class`   | `string` | -       |

### toast

The `window.tui.toast` object is the toast manager pendant.

| Function  | Signature                                    | Description                                              |
| --------- | -------------------------------------------- | -------------------------------------------------------- |
| `add`     | `(options) => id`                            | Shows a toast, options carry `title`, `description`, `type`, `timeout` and `actionProps`. |
| `close`   | `(id) => void`                               | Closes a toast.                                          |
| `promise` | `(promise, { loading, success, error }) => id` | Shows a loading toast that morphs with the promise.      |

### Toast

The `toast.Toast` component renders a toast server side, e.g. swapped in via htmx.

| Prop          | Type                                                                | Default   |
| ------------- | ------------------------------------------------------------------- | --------- |
| `Title`       | `string`                                                            | -         |
| `Description` | `string`                                                            | -         |
| `Type`        | `TypeSuccess \| TypeInfo \| TypeWarning \| TypeError \| TypeLoading` | -         |
| `Timeout`     | `int`                                                               | inherited |
