Toast Primitive
Use Toast to report a result without interrupting the current task. It shows the message for a limited time and can offer a way to dismiss it.
Anatomy
Use the Astro primitive adapter to render Toast anatomy with the Runtime wiring included.
---import { Toast } from "@starwind-ui/astro/toast";---
<Toast.Viewport> <Toast.Root> <Toast.Content> <Toast.Title>Saved</Toast.Title> <Toast.Description>Your changes were saved.</Toast.Description> </Toast.Content> <Toast.Close>Dismiss</Toast.Close> </Toast.Root></Toast.Viewport>Use the React primitive adapter when Toast state participates in React rendering.
import { Toast } from "@starwind-ui/react/toast";
export function Example() { return ( <Toast.Viewport> <Toast.Root> <Toast.Content> <Toast.Title>Saved</Toast.Title> <Toast.Description>Your changes were saved.</Toast.Description> </Toast.Content> <Toast.Close>Dismiss</Toast.Close> </Toast.Root> </Toast.Viewport> );}Use the Vue components to build Toast.
<script setup lang="ts">import Toast from "@starwind-ui/vue/toast";</script>
<template> <Toast.Viewport> <Toast.Root> <Toast.Content> <Toast.Title>Saved</Toast.Title> <Toast.Description>Your changes were saved.</Toast.Description> </Toast.Content> <Toast.Close>Dismiss</Toast.Close> </Toast.Root> </Toast.Viewport></template>Use the Svelte components to build Toast.
<script lang="ts"> import { Toast } from "@starwind-ui/svelte/toast";</script>
<Toast.Viewport> <Toast.Root> <Toast.Content> <Toast.Title>Saved</Toast.Title> <Toast.Description>Your changes were saved.</Toast.Description> </Toast.Content> <Toast.Close>Dismiss</Toast.Close> </Toast.Root></Toast.Viewport>Render the Toast data-sw-* contract yourself, then initialize createToastManager.
<div data-sw-toast-viewport role="region" aria-live="polite" aria-atomic="false" aria-relevant="additions text" aria-label="Notifications" tabindex="-1"> <div data-sw-toast-root> <div data-sw-toast-content> <div data-sw-toast-title>Saved</div> <div data-sw-toast-description>Your changes were saved.</div> </div> <button data-sw-toast-close type="button" aria-label="Close notification">Dismiss</button> </div></div>
<script type="module"> import { createToastManager } from "@starwind-ui/runtime/toast";
const root = document.querySelector("[data-sw-toast-viewport]"); if (root) { createToastManager(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
API Reference
Viewport
The visible viewport for Toast content.
- Default element
- div
- Discovery hook
- data-sw-toast-viewport
- Role
- region
Props
duration number 5000
- Description
- Configures the duration option for the Viewport part.
- Kind
- option
- Targets
- -
- Full type
- number
gap string "0.5rem"
- Description
- Configures the gap option for the Viewport part.
- Kind
- option
- Targets
- -
- Full type
- string
limit number 3
- Description
- Configures the limit option for the Viewport part.
- Kind
- option
- Targets
- -
- Full type
- number
peek string "1rem"
- Description
- Configures the peek option for the Viewport part.
- Kind
- option
- Targets
- -
- Full type
- string
position "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right" "bottom-right"
- Description
- Configures the position option for the Viewport part.
- Kind
- option
- Targets
- -
- Full type
- "top-left" | "top-center" | "top-right" | "bottom-left" | "bottom-center" | "bottom-right"
Data Attributes
Runtime hooks
Metadata
Template
Template markup used to create Toast items.
- Default element
- template
- Discovery hook
- data-sw-toast-template
- Role
- -
Props
variant "default" | "error" | "info" | "loading" | "success" | "warning" "default"
- Description
- Selects the visual variant for the Template part.
- Kind
- option
- Targets
- template, root
- Full type
- "default" | "error" | "info" | "loading" | "success" | "warning"
Data Attributes
Runtime hooks
Root
The main element that owns the Toast Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-toast-root
- Role
- -
Props
variant "default" | "error" | "info" | "loading" | "success" | "warning" "default"
- Description
- Selects the visual variant for the Root part.
- Kind
- option
- Targets
- template, root
- Full type
- "default" | "error" | "info" | "loading" | "success" | "warning"
Data Attributes
Runtime hooks
State
Metadata
Content
The content container rendered by Toast.
- Default element
- div
- Discovery hook
- data-sw-toast-content
- Role
- -
Data Attributes
Runtime hooks
Title
The accessible title for Toast.
- Default element
- div
- Discovery hook
- data-sw-toast-title
- Role
- -
Data Attributes
Runtime hooks
Title Text
Text content for the Toast title.
- Default element
- span
- Discovery hook
- data-sw-toast-title-text
- Role
- -
Data Attributes
Runtime hooks
Description
Supporting description text for Toast.
- Default element
- div
- Discovery hook
- data-sw-toast-description
- Role
- -
Data Attributes
Runtime hooks
Action
An optional action button shown inside Toast.
- Default element
- button
- Discovery hook
- data-sw-toast-action
- Role
- -
Data Attributes
Runtime hooks
Close
A control that closes Toast.
- Default element
- button
- Discovery hook
- data-sw-toast-close
- Role
- -
Data Attributes
Runtime hooks
Runtime API
- Factory
createToastManager- Import
@starwind-ui/runtime/toast- Root hook
- viewport
data-sw-toast-viewport - Option props
- -
Related Styled Components
Changelog
View version history v1.0.3 6 releases
v1.0.3
- Toast content follows its viewport or explicit local direction. Physical viewport positions and swipe directions keep their meanings.
- In Vue, this component now needs Vue 3.5.21 or newer.
v1.0.2
- In Astro, an attribute that you set on a Toast part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
v1.0.1
- Fixed toasts skipping their entrance animation or leaving incorrect spacing when updated immediately, including when a promise resolves.
- Reduced repeated JavaScript when Astro Toast is used with other Starwind components.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
v0.1.0
- First release. Toast has a manager for the list of toasts, with templates, updates, closing, and pausing. It controls when each toast shows and goes away.