Usage
This component acts as a Singleton. Implement a single instance of <Toast.Group> in the root scope your application, then trigger it anywhere on demand using a shared createToaster() instance.
import { toaster } from '/some/path/toaster.ts';
import { Toast } from '@skeletonlabs/skeleton-react';
import type { PropsWithChildren } from 'react';
export default function Layout(props: PropsWithChildren) {
return (
<>
{props.children}
<Toast.Group toaster={toaster}>
{(toast) => (
<Toast toast={toast} key={toast.id}>
<Toast.Message>
<Toast.Title>{toast.title}</Toast.Title>
<Toast.Description>{toast.description}</Toast.Description>
</Toast.Message>
<Toast.CloseTrigger />
</Toast>
)}
</Toast.Group>
</>
);
}<script lang="ts">
import { toaster } from '/some/path/toaster.ts';
import { Toast } from '@skeletonlabs/skeleton-svelte';
import type { Snippet } from 'svelte';
interface Props {
children?: Snippet;
}
const props: Props = $props();
</script>
{@render props.children?.()}
<Toast.Group {toaster}>
{#snippet children(toast)}
<Toast toast={toast}>
<Toast.Message>
<Toast.Title>{toast.title}</Toast.Title>
<Toast.Description>{toast.description}</Toast.Description>
</Toast.Message>
<Toast.CloseTrigger />
</Toast>
{/snippet}
</Toast.Group>import { toaster } from '/some/path/toaster.ts';
export default function Page() {
function onClickHandler() {
toaster.info({ title: 'Example', description: 'This is a toast message.' });
}
return ();
}<script lang="ts">
import { toaster } from '/some/path/toaster.ts';
function onClickHandler() {
toaster.info({ title: 'Example', description: 'This is a toast message.' });
}
</script>Triggers
Shorthand methods are available for toast triggers, including: info(), success(), warning(), and error().
Or use the generic create() method to trigger toasts of any type: info|success|warning|error.
toaster.create({
type: 'info',
title: 'This is a toast',
description: 'This is a toast description.',
});Placement
Set the placement value in your createToaster instance to define the global screen position.
Action
Use the action to define a custom button for each toast instance.
Closable
Use the closable prop to toggle display of the close button.
Meta
Use the meta key to pass arbitrary data along with the toast instance. This can be used to display an icon for example.
Promise
Toasts support asynchronous updates using promises.
API Reference
Root
card p-3 w-full md:w-md ring flex items-center gap-2 preset-filled-surface-50-950 ring-surface-200-800 data-[type=success]:preset-filled-success-500 data-[type=warning]:preset-filled-warning-500 data-[type=error]:preset-filled-error-500| Prop | Default | Type |
|---|---|---|
toast | — | Omit<Options<any>, "id" | "parent"> |
element | — | ((attributes: HTMLAttributes<"div">) => Element) | undefined Render the element yourself |
Context
| Prop | Default | Type |
|---|---|---|
children | — | (toast: ToastApi<PropTypes, any>) => ReactNode |
Group
| Prop | Default | Type |
|---|---|---|
toaster | — | ToastStore<any> |
children | — | ((toast: ToastProps<any>) => Element | null) | undefined |
element | — | ((attributes: HTMLAttributes<"div">) => Element) | undefined Render the element yourself |
Message
flex-1| Prop | Default | Type |
|---|---|---|
element | — | ((attributes: HTMLAttributes<"div">) => Element) | undefined Render the element yourself |
Title
font-medium text-sm| Prop | Default | Type |
|---|---|---|
element | — | ((attributes: HTMLAttributes<"div">) => Element) | undefined Render the element yourself |
Description
text-sm| Prop | Default | Type |
|---|---|---|
element | — | ((attributes: HTMLAttributes<"div">) => Element) | undefined Render the element yourself |
ActionTrigger
btn preset-filled| Prop | Default | Type |
|---|---|---|
element | — | ((attributes: HTMLAttributes<"button">) => Element) | undefined Render the element yourself |
CloseTrigger
btn-icon hover:preset-tonal| Prop | Default | Type |
|---|---|---|
element | — | ((attributes: HTMLAttributes<"button">) => Element) | undefined Render the element yourself |
Root
card p-3 w-full md:w-md ring flex items-center gap-2 preset-filled-surface-50-950 ring-surface-200-800 data-[type=success]:preset-filled-success-500 data-[type=warning]:preset-filled-warning-500 data-[type=error]:preset-filled-error-500| Prop | Default | Type |
|---|---|---|
toast | — | Options<any> |
element | — | Snippet<[HTMLAttributes<"div">]> | undefined Render the element yourself |
Context
| Prop | Default | Type |
|---|---|---|
children | — | Snippet<[() => ToastApi<PropTypes, any>]> |
Group
| Prop | Default | Type |
|---|---|---|
toaster | — | ToastStore<any> |
children | — | Snippet<[ToastProps<any>]> | undefined |
element | — | Snippet<[HTMLAttributes<"div">]> | undefined Render the element yourself |
Message
flex-1| Prop | Default | Type |
|---|---|---|
element | — | Snippet<[HTMLAttributes<"div">]> | undefined Render the element yourself |
Title
font-medium text-sm| Prop | Default | Type |
|---|---|---|
element | — | Snippet<[HTMLAttributes<"div">]> | undefined Render the element yourself |
Description
text-sm| Prop | Default | Type |
|---|---|---|
element | — | Snippet<[HTMLAttributes<"div">]> | undefined Render the element yourself |
ActionTrigger
btn preset-filled| Prop | Default | Type |
|---|---|---|
element | — | Snippet<[HTMLAttributes<"button">]> | undefined Render the element yourself |
CloseTrigger
btn-icon hover:preset-tonal| Prop | Default | Type |
|---|---|---|
element | — | Snippet<[HTMLAttributes<"button">]> | undefined Render the element yourself |