Toast for Astro
Astro notifications with status icons, actions, and promise states.
Installation
bunx --bun shadcn@latest add @gnostic/toastnpx shadcn@latest add @gnostic/toastpnpm dlx shadcn@latest add @gnostic/toastInstall component packages
bun add @data-slot/toastnpm install @data-slot/toastpnpm add @data-slot/toastCopy the files
src/components/ui/toast/index.tssrc/components/ui/toast/toast-action.astrosrc/components/ui/toast/toast-close.astrosrc/components/ui/toast/toast-content.astrosrc/components/ui/toast/toast-description.astrosrc/components/ui/toast/toast-icon.astrosrc/components/ui/toast/toast-item.astrosrc/components/ui/toast/toast-template.astrosrc/components/ui/toast/toast-title.astrosrc/components/ui/toast/toast-viewport.astrosrc/components/ui/toast/toast.astrosrc/components/ui/toast/types.ts
Usage
Place one Toast in your layout. It supplies the template, status icons, close button, and notification viewport. Its script initializes the data-slot controller.
---
import { Toast } from "@/components/ui/toast"
---
<Toast data-app-toast />
<script>
import { createToast } from "@data-slot/toast"
const root = document.querySelector("[data-app-toast]")!
const toaster = createToast(root)
toaster.show({
title: "Changes saved",
description: "Your project settings are up to date.",
type: "success",
})
</script>
Call show() from your application event handler after the root is in the document. createToast(root) returns the existing controller when the root is already initialized. Each root has an independent stack. With Astro navigation, find the current root before you use the controller; the component destroys its old controllers before a page swap.
Examples
With an action
An action dismisses its toast after the callback runs. Call event.preventDefault() in the callback to keep it open.
Promise states
Use promise() to change one notification from loading to success or error. The examples simulate a request. Call the returned handle’s unwrap() when your application also needs the result or error.
Position
Use position to select one of six viewport positions. Notifications expand when hovered or focused. Older notifications remain mounted but become hidden and inert when the visible limit is exceeded.
Custom template
The default slot replaces both the template and viewport. Keep one ToastTemplate and one ToastViewport inside the root. ToastTemplate has its own default content, which you can replace with the exported parts:
<Toast>
<ToastTemplate>
<ToastItem>
<ToastContent>
<ToastTitle />
<ToastDescription />
</ToastContent>
<ToastAction />
<ToastClose aria-label="Dismiss notification" />
</ToastItem>
</ToastTemplate>
<ToastViewport aria-label="Project notifications" />
</Toast>
Import these parts from @/components/ui/toast. The primitive fills the title, description, and action with text. It hides an empty description or action. Keep these slots free of child markup that must survive an update. Avoid fixed IDs inside the template because each notification clones it.
API reference
ToastProps
position?ToastPosition
Position of the stack in the page viewport.
ToastPosition"bottom-right"limit?number
Maximum number of visible notifications.
number3duration?number
Time before dismissal, in milliseconds. Use 0 to keep a toast open.
number5000pauseOnHover?boolean
Pause dismissal while the pointer is over the stack.
booleantruepauseOnFocus?boolean
Pause dismissal while focus is inside the stack.
booleantrueportal?boolean
Move the viewport to document.body to escape clipping containers.
booleantrueToastShowOptions
id?string
stringtitlestring
stringdescription?string
stringtype?ToastType
ToastTypeduration?number
numberaction?ToastAction
ToastActiondismissible?boolean
booleancloseButtonAriaLabel?string
stringtestId?string
stringToastUpdateOptions
title?string
stringdescription?string
stringtype?ToastType
ToastTypeduration?number
numberaction?ToastAction
ToastActiondismissible?boolean
booleancloseButtonAriaLabel?string
stringtestId?string
stringToastAction
labelstring
stringonClick?(event: ToastActionEvent) => void
Call `event.preventDefault()` to keep the toast open after the action.
(event: ToastActionEvent) => voidvalue?string
stringToastPromiseOptions
loadingstring | ToastPromiseState
string | ToastPromiseStatesuccess?ToastPromiseStateValue<T>
ToastPromiseStateValue<T>error?ToastPromiseErrorValue
ToastPromiseErrorValuedescription?string
stringOther parts accept their native HTML attributes and class. ToastViewport is an ordered list. ToastItem is a list item. ToastAction and ToastClose are buttons. ToastIcon supplies the status icons, including the loading indicator.
Events
Dispatch these events on the toast root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
toast:change |
{ id: string, action: "show" | "dismiss" } |
toast:action |
{ id: string, value: string | undefined } |
Inbound events
| Event | Detail |
|---|---|
toast:show |
ToastShowOptions |
toast:update |
{ id: string } & ToastUpdateOptions |
toast:dismiss |
{ id: string } or string |
toast:clear |
none |
Controller
Import createToast and controller types from @data-slot/toast. Use show, update, promise, dismiss, and dismissAll to control notifications. A repeated show() ID replaces the old toast; update(id, patch) changes it in place. Set duration: 0 to keep a notification open until dismissed.
The root also accepts toast:show, toast:update, toast:dismiss, and toast:clear custom events. It emits toast:change and toast:action. See the data-slot Toast API for options and event details.
Read the installed source for all exported parts.
Accessibility and limits
The primitive announces errors and warnings as alerts. Other notifications use polite status announcements. Keep essential errors next to the affected field as well. Timers pause on hover, focus, window blur, and a hidden document. Users can close a toast with its labeled button or a swipe. dismissible: false disables swiping; it does not remove the close button.
The styles adapt Coss Toast to Astro and data-slot. This component uses the data-slot API. Anchored toasts, Base UI providers, F6 viewport focus, and Escape dismissal are not supplied. Animation follows the reduced-motion preference.
portal defaults to true in this Astro component, so the viewport can escape clipping containers. The primitive itself defaults to false. Set portal={false} to retain the viewport in its original parent; it still uses fixed positioning unless you supply a different layout class.
Theme and layout
Toast styles use the project preset. Keep messages short enough to read at phone widths, and choose a viewport position that does not cover the main page action. Portalled toasts can escape clipping containers. Use an Alert for feedback that must remain in the page content.