On this page

Toast

Live examples of Toast, with the exact Vue file that renders each one.

Every example is a complete Vue file. Copy it into an app that has the registry installed, or open it in StackBlitz.

Success, information, warning and error

Each role has its icon, so the role is not colour alone.

SonnerRoles.vue
<script setup lang="ts">
import { toast } from 'vue-sonner'
import { Button } from '@/components/ui/button'
import { ButtonGroup } from '@/components/ui/button-group'
import { Toaster } from '@/components/ui/sonner'

// The id keeps this example's toasts apart from the others on the page. An app mounts one Toaster, with no id.
const id = 'sonner-roles'
</script>

<template>
  <Toaster :id="id" container-aria-label="Notifications" />
  <ButtonGroup variant="spaced" aria-label="Show a toast">
    <Button variant="outline" @click="toast.success('Product saved', { toasterId: id })">Success</Button>
    <Button variant="outline" @click="toast.info('Sync paused for maintenance', { toasterId: id, description: 'Orders resume at 22:00.' })">Info</Button>
    <Button variant="outline" @click="toast.warning('Stock is 2 hours old', { toasterId: id, description: 'The warehouse connection is slow.' })">Warning</Button>
    <Button variant="outline" @click="toast.error('Order sync failed', { toasterId: id, description: 'The ERP rejected the API key.' })">Error</Button>
  </ButtonGroup>
</template>

With an action

A toast with an action stays longer, so there is time to reach it.

  • Order status
  • Tax class
  • Warehouse
SonnerUndo.vue
<script setup lang="ts">
import { ref } from 'vue'
import { toast } from 'vue-sonner'
import { Button } from '@/components/ui/button'
import { Toaster, toastWaitMs } from '@/components/ui/sonner'

const id = 'sonner-undo'
const mappings = ref(['Order status', 'Tax class', 'Warehouse'])

// A toast with an action stays for motion.wait.toast.action (10 s), so there is time to reach it.
function remove(name: string) {
  const index = mappings.value.indexOf(name)
  mappings.value.splice(index, 1)
  toast(`Mapping “${name}” removed`, {
    toasterId: id,
    duration: toastWaitMs(true),
    action: {
      label: 'Undo',
      onClick: () => mappings.value.splice(index, 0, name),
    },
  })
}
</script>

<template>
  <Toaster :id="id" container-aria-label="Notifications" />
  <ul class="m-0 flex w-full max-w-80 list-none flex-col p-0 [font:var(--bh-font-body-default)]">
    <li
      v-for="name in mappings"
      :key="name"
      class="flex items-center justify-between gap-(--bh-space-100) border-b-(length:--bh-border-width-default) border-(--bh-color-border) py-(--bh-space-100) text-(--bh-color-text)"
    >
      {{ name }}
      <Button variant="ghost-destructive" size="sm" @click="remove(name)">Remove</Button>
    </li>
  </ul>
</template>

A task in the background

One toast follows the task: loading, then the result.

SonnerPromise.vue
<script setup lang="ts">
import { toast } from 'vue-sonner'
import { Button } from '@/components/ui/button'
import { Toaster } from '@/components/ui/sonner'

const id = 'sonner-promise'

// One toast follows a task that runs in the background: loading, then the result.
function exportOrders() {
  const job = new Promise<number>(resolve => setTimeout(() => resolve(1240), 2500))
  toast.promise(job, {
    toasterId: id,
    loading: 'Exporting orders…',
    success: (count: number) => `${count.toLocaleString('en')} orders exported`,
    error: 'Export failed. Try again.',
  })
}
</script>

<template>
  <Toaster :id="id" container-aria-label="Notifications" />
  <Button variant="outline" @click="exportOrders">Export orders</Button>
</template>