Skip to content

Toast

A powerful toast notification system for Vue.js that displays user-friendly messages with customizable positioning, styling, and behavior options

TIP

This plugin has a composable for easier use, after installing it you can use useToast

Installation

ts
import { ToastPlugin } from 'maz-ui/plugins/toast'
import { createApp } from 'vue'

const app = createApp(App)

const toastOptions: ToastOptions = {
  position: 'bottom-right',
  timeout: 10_000,
  persistent: false,
}

app.use(ToastPlugin, toastOptions)
app.mount('#app')
ts
export default defineNuxtConfig({
  modules: ['@maz-ui/nuxt'],
  mazUi: {
    composables: {
      useToast: true,
    },
  },
})

Basic usage

Toast can have a link or an action

Close toast programmatically

You can close a toast programmatically by using the close method returned by the toast function

typescript
function showToast() {
  const toastMessage = toast.message('Toast message closed by code')

  setTimeout(() => {
    toastMessage.close()
  }, 2000)
}

Type

ts
interface ToastOptions {
  /**
   * The position of the toast on the screen
   * @default 'bottom-right'
   */
  position?: ToastPosition
  /**
   * The timeout is in ms, it's the time before the toast is automatically closed
   * if set to `false`, the toast will not be closed automatically
   * @default 10000
   */
  timeout?: number | boolean
  /**
   * If the toast is persistent, it can't be closed by user interaction (only on timeout or programmatically)
   * @default false
   */
  persistent?: boolean
  /**
   * Display an icon in the toast
   * @default true
   */
  icon?: boolean
  /**
   * The action will be displayed as a button in the toast
   * @default undefined
   */
  button?: MazBtnProps & {
    text: string
    onClick: () => unknown
    href?: string
    text?: string
    /** @default _self */
    target?: string
    /** @default false */
    closeToast?: boolean
  }
  buttons?: (MazBtnProps & {
    text: string
    onClick: () => unknown
    href?: string
    text?: string
    /** @default _self */
    target?: string
    /** @default false */
    closeToast?: boolean
  })[]
  /**
   * If the toast is queued, it will be displayed in the order of the toasts
   * @default false
   */
  queue?: boolean
  /**
   * The maximum number of toasts to display
   * @default false
   */
  maxToasts?: number | boolean
  /**
   * If the toast is paused on hover, it will be paused when the mouse is over the toast
   * @default true
   */
  pauseOnHover?: boolean
  /**
   * If the message is HTML
   * @default false
   */
  html?: boolean
}