useToast()

useToast() provides the calling interface for global lightweight toast notifications. You can call it from any component, composable, or async callback without managing state or display logic in templates. Toast messages are rendered by the SwToast component, typically mounted at the layout root.

Basic Usage

Four semantic shortcuts

加载中...
const { success, error, warning, info } = useToast()

success('Saved successfully')
error('Upload failed', { title: 'File Error' })
warning('Low storage space', { duration: 8000 })
info('A new version has been released — please refresh')

Full Options

Pill vs card forms

加载中...

duration: quick / default / sticky

加载中...

Use the add() method to pass the complete configuration:

const { add } = useToast()

add({
  message: 'Operation successful, data has been updated',
  title: 'Notice',
  type: 'success',
  duration: 4000, // auto-dismiss after 4 seconds; 0 means no auto-dismiss
})

Return Values

NameTypeDescription
toastsDeepReadonly<Ref<ToastItem[]>>Read-only reactive list of all currently visible toasts (no direct push/splice — use add/remove)
add(options: ToastOptions) => voidAdd a toast notification
remove(id: string) => voidRemove a specific toast by ID
success(message: string, opts?) => voidShow a success toast (green)
error(message: string, opts?) => voidShow an error toast (red)
warning(message: string, opts?) => voidShow a warning toast (orange)
info(message: string, opts?) => voidShow an info toast (blue)

ToastOptions

ParameterTypeRequiredDefaultDescription
messagestring✓—Toast body text
titlestring—Optional title shown above the body
type'success' | 'error' | 'warning' | 'info'✓—Toast type — controls icon and color (add() has no default; required. Shorthand methods set it for you)
durationnumber3000Auto-dismiss delay in ms; 0 disables auto-dismiss

Notes

  • SwToast must be in the layout: Toasts are rendered and queued by SwToast. Add <SwToast /> to your layout file (e.g. default.vue), otherwise there will be no visual feedback.
  • The shorthand methods success(), error(), warning(), info() all accept a second argument to override any option except type.
  • toasts is a reactive array. To implement a custom toast UI, read this array directly and use remove(id) for full custom rendering.