Trên trang này

Toast

Ví dụ trực tiếp của Toast, kèm đúng file Vue hiển thị từng ví dụ.

Mỗi ví dụ là một file Vue hoàn chỉnh. Chép vào ứng dụng đã cài registry, hoặc mở trong StackBlitz.

Thành công, thông tin, cảnh báo và lỗi

Mỗi loại có icon riêng, nên loại không chỉ thể hiện bằng màu.

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'

// id giữ toast của ví dụ này tách khỏi các ví dụ khác trên trang. Trong app chỉ gắn một Toaster, không cần id.
const id = 'sonner-roles'
</script>

<template>
  <Toaster :id="id" container-aria-label="Thông báo" />
  <ButtonGroup variant="spaced" aria-label="Hiện toast">
    <Button variant="outline" @click="toast.success('Đã lưu sản phẩm', { toasterId: id })">Thành công</Button>
    <Button variant="outline" @click="toast.info('Tạm dừng đồng bộ để bảo trì', { toasterId: id, description: 'Đơn hàng chạy lại lúc 22:00.' })">Thông tin</Button>
    <Button variant="outline" @click="toast.warning('Số tồn kho đã cũ 2 giờ', { toasterId: id, description: 'Kết nối kho đang chậm.' })">Cảnh báo</Button>
    <Button variant="outline" @click="toast.error('Đồng bộ đơn hàng thất bại', { toasterId: id, description: 'ERP từ chối API key.' })">Lỗi</Button>
  </ButtonGroup>
</template>

Có thao tác

Toast có thao tác hiện lâu hơn, để kịp bấm.

  • Trạng thái đơn hàng
  • Nhóm thuế
  • Kho hàng
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(['Trạng thái đơn hàng', 'Nhóm thuế', 'Kho hàng'])

// Toast có thao tác ở lại theo motion.wait.toast.action (10 giây), đủ thời gian để tới được nút.
function remove(name: string) {
  const index = mappings.value.indexOf(name)
  mappings.value.splice(index, 1)
  toast(`Đã xóa mapping “${name}”`, {
    toasterId: id,
    duration: toastWaitMs(true),
    action: {
      label: 'Hoàn tác',
      onClick: () => mappings.value.splice(index, 0, name),
    },
  })
}
</script>

<template>
  <Toaster :id="id" container-aria-label="Thông báo" />
  <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)">Xóa</Button>
    </li>
  </ul>
</template>

Tác vụ chạy nền

Một toast theo tác vụ: đang tải, rồi kết quả.

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'

// Một toast theo một việc chạy nền: đang chạy, rồi kết quả.
function exportOrders() {
  const job = new Promise<number>(resolve => setTimeout(() => resolve(1240), 2500))
  toast.promise(job, {
    toasterId: id,
    loading: 'Đang xuất đơn hàng…',
    success: (count: number) => `Đã xuất ${count.toLocaleString('vi')} đơn hàng`,
    error: 'Xuất thất bại. Thử lại.',
  })
}
</script>

<template>
  <Toaster :id="id" container-aria-label="Thông báo" />
  <Button variant="outline" @click="exportOrders">Xuất đơn hàng</Button>
</template>