Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Toast for Svelte

Toast notifications for Svelte on Ark UI: success, error and promise toasts that leave on their own, read by screen readers without moving focus.

On this page9 sections
UI library: Ark UI

A toast is a short notice at the edge of the screen after something happens, such as "Event created" with an Undo button, and it leaves on its own a few seconds later. Focus stays where it was, so people keep working while a screen reader reads it out.

Here it's built from Ark UI's toast parts, with a toaster you call from anywhere. For a message that has to stay until someone deals with it, put an Alert in the page instead.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Toast anatomy: root, title, description, action trigger, close trigger

Usage

<script lang="ts">
import { Button } from "@/components/ui/button";
import { Toaster, toast } from "@/components/ui/toast";
</script>
<>
  <Toaster />
  <Button
    variant="outline"
    onClick={() =>
      toast.create({
        title: "Event created",
        description: "Sunday, December 3 at 9:00 AM",
      })
    }
  >
    Show Toast
  </Button>
</>

The toast in this file is a shared toaster, and Toaster shows it when you pass no toaster of your own.

Accessibility

Toaster renders a region labeled with "Notifications", its placement and its hotkey, such as "Notifications, bottom-end (alt+T)", and the region is a polite live region. Each toast in it is a focusable status element named by its title and described by its description, so a screen reader reads it while focus stays put. Pointing at the region, focusing inside it or hiding the tab pauses every toast.

A toast leaves on its own, so never make its button the only way to do something.

Alt+T
Moves focus to the toast region, which pauses every toast and, where they overlap, spreads them out.
Tab
Moves focus to the next toast, then through its action and close buttons. When focus leaves the region while the pointer is elsewhere, it goes back to the element that had it before.
Shift+Tab
Moves focus to the previous toast or button.
Enter
Activates the focused action or close button.
Space
Activates the focused action or close button.
Escape
When focus is on a toast or a button inside it, dismisses that toast.

Demos

Types

Five buttons, Default, Success, Info, Warning and Error, each calling toast.create with a one-line description, and all but Default with the matching type, which picks the icon. Each closes after 5 seconds, the duration the demo's toaster sets for every type. Use a typed toast when the outcome matters, and keep the words specific, since the icon alone doesn't say what happened.

Preview width

Promise

Create Event calls toast.promise with a request that resolves after two seconds. One toast reads "Creating event…" beside a spinner, then becomes "Event created." in place, and the error message would show only if the promise rejected, which this one never does. Use it for a save or an upload someone is waiting on, so the same toast carries the result.

Preview width

FAQ

How do I show a toast in Svelte?

Render Toaster once near the root of the app and call toast.create from any handler with a title and a description. The toast exported from components/ui/toast is a shared toaster that keeps up to 3 on screen at the bottom end, and Toaster shows it when you pass none.

For other settings, make your own toaster with createToaster and pass it to Toaster as toaster, as the demos on this page do. Keep it in a module of its own, so every file that calls it shares one instance.

How do I show a success or error toast with Ark UI?

Pass type to toast.create, one of success, error, warning, info or loading, or call the shortcut of the same name, such as toast.success, with the same options. The type picks the icon in ToastItem, with the error one in the destructive color, as the Types demo shows.

A loading toast stays until you update it. The other types leave after 5 seconds on the shared toast, which sets duration for all of them, while a toaster created without one closes a success toast after 2.

How do I show a loading toast that updates when a request finishes?

Call toast.promise with the promise and a loading, success and error option, each an object of toast options such as title and description, of which loading is required. One toast shows it and stays up, then turns into the success or error toast when the promise settles, as the Promise demo does.

success can be a function that receives the resolved data. A fetch Response that isn't ok counts as an error, and the call returns an object whose unwrap gives you the promise's result.

How do I close a toast from code?

Keep the id that toast.create returns and pass it to toast.dismiss, which plays the exit animation. With no id, every toast closes.

An action button dismisses its own toast once its onClick has run, which is why the Undo in the demo at the top of the page makes no dismiss call. toast.remove takes a toast out at once, without the exit, and toast.update changes one in place. Set closable to false to hide the close button on a toast your code will dismiss.

How do I change where toasts appear?

Set placement when you create the toaster with createToaster: top-start, top, top-end, bottom-start, bottom or bottom-end. The shared toast in your file uses bottom-end.

max caps how many show at once, 3 on the shared toaster and 24 on one you create without it, and the rest wait in a queue. overlap piles toasts into a stack that spreads out on hover, and gap and offsets set the spacing.

How long does a toast stay on screen, and how do I change it?

Five seconds on the shared toast, which passes duration to createToaster, while a loading toast stays until you update or dismiss it. Pass duration in milliseconds to one toast, or to createToaster for all of them, and Infinity keeps a toast up until it's dismissed. A toaster created without duration keeps Ark UI's defaults, 2 seconds for success and 5 for the others.

The time pauses while the pointer is over the toasts, while focus is inside them and while the tab is hidden.

Give a toast with an Undo button longer than the default, since reading it and reaching the button both take time.

Can keyboard and screen reader users reach a toast?

Yes. A new toast is read out from a polite live region without moving focus, and Alt+T moves focus to the toasts and pauses them, so there's time to reach Undo. Escape dismisses the focused toast, and when focus leaves the region with the pointer elsewhere, it returns to where it was.

Keep the title short, since it's read first, and keep anything a toast offers available somewhere else on the page too. The Accessibility section lists each key.

Should I use Toast or Sonner?

Toast is the one to use on Svelte. Sonner is built on the sonner package, which is React only, while Toast runs on Ark UI's state machine on every framework, with its queue, placements and promise toasts.

API Reference

ToastItem

<div> data-slot="toast"

PropType
parentService<ToastGroupSchema>

The parent toast group service. Required when using toast as a child of a group.

Type
Service<ToastGroupSchema>
toastArkToast.Options

The toast item data.

action?object

The action of the toast.

Type
{ label: string; onClick: VoidFunction }
closable?boolean

Whether the toast is closable.

description?T

The description of the toast.

dir?"ltr" | "rtl"

The direction of the toast.

Type
"ltr" | "rtl"
duration?number

The duration the toast will be visible.

gap?number

The gap of the toast.

id?string

The unique id of the toast.

index?number

The index of the toast in the group.

meta?Record<string, any>

The metadata of the toast.

Type
Record<string, any>
onStatusChange?(details: StatusChangeDetails) → void

Function called when the toast is visible.

Type
(details: StatusChangeDetails) → void
Parameters

details – { status: Status; src?: string }

Returns
void
priority?1 | 2 | 3 | 4 | 5 | 6 | 7 | 8

The priority of the toast (1 = highest, 8 = lowest).

Type
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8
removeDelay?number

The duration for the toast to kept alive before it is removed. Useful for exit transitions.

title?T

The title of the toast.

translations?object

Specifies the localized strings that identifies the accessibility elements and their states.

Type
{ closeTriggerLabel?: string }
type?"success" | "error" | "loading" | "info" | "warning" | (string & {})

The type of the toast.

Type
"success" | "error" | "loading" | "info" | "warning" | (string & {})
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How the content aligns to its trigger.

Type
"start" | "center" | "end"
data-first?string

Present when frontmost is true.

data-mounted?string

Present when mounted.

data-overlap?string

Present when overlapping.

data-paused?string

Present when paused.

data-placementstring

The placement of the toast.

data-sibling?string

Present when frontmost is false.

data-side"top" | "right" | "bottom" | "left"

The side of the trigger that the toast is positioned on.

Type
"top" | "right" | "bottom" | "left"
data-stack?string

Present when stacked is true.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
data-typestring

The type of the item.

CSS variableType
--opacitystring
--scalestring
--viewport-offset-leftstring
--widthstring

The measured width, for animating open and closed.

--xstring

The horizontal position.

--ystring

The vertical position.

--z-indexstring

The stacking order.

ToastTitle

<div> data-slot="toast-title"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
CSS variableType
--opacitystring
--scalestring
--viewport-offset-leftstring
--widthstring

The measured width, for animating open and closed.

--xstring

The horizontal position.

--ystring

The vertical position.

--z-indexstring

The stacking order.

ToastDescription

<div> data-slot="toast-description"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
CSS variableType
--opacitystring
--scalestring
--viewport-offset-leftstring
--widthstring

The measured width, for animating open and closed.

--xstring

The horizontal position.

--ystring

The vertical position.

--z-indexstring

The stacking order.

ToastActionTrigger

<button> data-slot="toast-action-trigger"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
CSS variableType
--opacitystring
--scalestring
--viewport-offset-leftstring
--widthstring

The measured width, for animating open and closed.

--xstring

The horizontal position.

--ystring

The vertical position.

--z-indexstring

The stacking order.

ToastCloseTrigger

<button> data-slot="toast-close-trigger"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
CSS variableType
--opacitystring
--scalestring
--viewport-offset-leftstring
--widthstring

The measured width, for animating open and closed.

--xstring

The horizontal position.

--ystring

The vertical position.

--z-indexstring

The stacking order.

Toaster

data-slot="toaster"

PropType
dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
label?string

The human-readable label for the toast region.

Default
"Notifications"
toaster?CreateToasterReturn<any>

Toaster instance.

Type
CreateToasterReturn<any>
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How the content aligns to its trigger.

Type
"start" | "center" | "end"
data-first?string

Present when frontmost is true.

data-mounted?string

Present when mounted is true.

data-overlap?string

Present when stacked is false.

data-paused?string

Present when paused is true.

data-placementstring

The content's placement on its trigger, such as bottom-start.

data-sibling?string

Present when frontmost is false.

data-side"top" | "right" | "bottom" | "left"

The side of the trigger the content sits on.

Type
"top" | "right" | "bottom" | "left"
data-stack?string

Present when stacked is true.

data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
data-typestring

Mirrors type.

CSS variableType
--viewport-offset-leftstring
--viewport-offset-rightstring
--widthstring

The measured width, for animating open and closed.

useToast