Usage
Use the auto-imported useToast composable to display Toast notifications.
- The
useToastcomposable uses Nuxt'suseStateto manage the toast state, ensuring reactivity across your application. - A maximum of 5 toasts are displayed at a time by default. When adding a new toast that would exceed this limit, the oldest toast is automatically removed. Change it with the
toaster.maxprop on theAppcomponent. - When removing a toast, there's a 200ms delay before it's actually removed from the state, allowing for exit animations.
Make sure to wrap your app with the
App component which uses our Toaster component which uses the ToastProvider component from Reka UI.API
useToast()
The useToast composable provides methods to manage toast notifications globally.
add()
add(toast: Partial<Toast>): Toast
Adds a new toast notification.
Parameters
toast
Partial<Toast> required
A partial
Toast object with the following properties:id
string | number
A unique identifier for the toast. If not provided, a unique id is generated. Reusing an existing id merges into that toast instead of adding a new one.
open
boolean
Whether the toast is open. Defaults to
true.title
string | VNode | (() => VNode)
The title displayed in the toast.
description
string | VNode | (() => VNode)
The description displayed in the toast.
icon
IconComponent
The icon displayed in the toast.
avatar
AvatarProps
The avatar displayed in the toast. See Avatar.
color
string
The color of the toast. Defaults to
air-secondary.orientation
'horizontal' | 'vertical'
The orientation between the content and the actions. Defaults to
vertical.close
boolean | Omit<ButtonProps, LinkPropsKeys>
Customize or hide the close button (with
false value). Defaults to true.closeIcon
IconComponent
The icon displayed in the close button.
actions
ButtonProps[]
The actions displayed in the toast. See Button.
progress
boolean | Pick<ProgressProps, 'color' | 'b24ui'>
Customize or hide the progress bar (with
false value). Defaults to true.duration
number
The duration in milliseconds before the toast auto-closes. Defaults to
5000. Set to 0 to keep the toast open until it's manually closed. Can also be set globally on the App component.onClick
(toast: Toast) => void
A callback function invoked when the toast is clicked.
onUpdateOpen
(open: boolean) => void
A callback function invoked when the toast open state changes. Useful to perform an action when the toast closes (expired or dismissed).
type
'foreground' | 'background'
How assistive technologies announce the toast. Use
background for toasts that aren't the result of a direct user action.as
any
The element or component the toast renders as. Defaults to
li.Returns: The complete Toast object that was added.
<script setup lang="ts">
const toast = useToast()
function showToast() {
toast.add({
title: 'Success',
description: 'Your action was completed successfully.',
color: 'air-primary-success'
})
}
</script>
update()
update(id: string | number, toast: Omit<Partial<Toast>, 'id'>): void
Updates an existing toast notification.
Parameters
id
string | number required
The unique identifier of the toast to update.
toast
Omit<Partial<Toast>, 'id'> required
A partial
Toast object with the properties to update. The id cannot be changed, the toast is reopened, and duration is reset unless you pass it again. The id cannot be changed, the toast is reopened, and duration is reset unless you pass it again.<script setup lang="ts">
const toast = useToast()
function updateToast(id: string | number) {
toast.update(id, {
title: 'Updated Toast',
description: 'This toast has been updated.'
})
}
</script>
remove()
remove(id: string | number): void
Removes a toast notification.
Parameters
id
string | number required
The unique identifier of the toast to remove.
<script setup lang="ts">
const toast = useToast()
function removeToast(id: string | number) {
toast.remove(id)
}
</script>
clear()
clear(): void
Removes all toast notifications.
<script setup lang="ts">
const toast = useToast()
function clearAllToasts() {
toast.clear()
}
</script>
toasts
toasts: Ref<Toast[]>
A reactive array containing all current toast notifications.
<script setup lang="ts">
const { toasts } = useToast()
</script>
<template>
<div>
<pre>{{ toasts }}</pre>
</div>
</template>