useToast

Um composable para exibir notificações toast no seu app.

Uso

Use o composable useToast, importado automaticamente, para exibir notificações Toast.

  • The useToast composable uses Nuxt's useState to manage the toast state, ensuring reactivity across your application.
  • A maximum of 5 toasts are displayed at a time. When adding a new toast that would exceed this limit, the oldest toast is automatically removed.
  • When removing a toast, there's a 200ms delay before it's actually removed from the state, allowing for exit animations.
Certifique-se de envolver seu app com o componente App, que usa nosso componente Toaster, que por sua vez usa o componente ToastProvider do Reka UI.
Aprenda a personalizar a aparência e o comportamento dos toasts na documentação do componente Toast.

API

useToast()

O composable useToast fornece métodos para gerenciar as notificações toast globalmente.

add()

add(toast: Partial<Toast>): Toast

Adds a new toast notification.

Parâmetros

toast
Partial<Toast> required
A partial Toast object with the following properties:

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: 'success'
  })
}
</script>

update()

update(id: string | number, toast: Partial<Toast>): void

Updates an existing toast notification.

Parâmetros

id
string | number required
The unique identifier of the toast to update.
toast
Partial<Toast> required
A partial Toast object with the properties to update.
<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.

Parâmetros

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>