VSonnerDemo.mp4
Stackable toast component for Vuetify.
Important
Snackbars should appear one at a time. This component breaks the Material spec.
npm install vuetify-sonner
Add <VSonner />
to your app, it will be the place where all your toasts will be rendered. After that you can use toast()
from anywhere in your app.
<script setup lang="ts">
import { VSonner, toast } from 'vuetify-sonner'
// Required for snackbar background and text color
import 'vuetify-sonner/style.css'
</script>
<template>
<VApp>
<VSonner />
<VBtn @click="toast('My first toast')">
Give me a toast
</VBtn>
</VApp>
</template>
Most basic toast. You can customize it by passing an options object as the second argument.
toast('My first toast')
With description:
toast('Event has been created', {
description: 'Monday, January 3rd at 6:00pm',
})
Renders a button.
toast('Event has been created', {
action: {
label: 'Undo',
onClick: () => console.log('Undo'),
buttonProps: {
// v-btn props
}
},
})
Behind the scenes, the toast component uses Vuetify Cards, as the snackbar component has its own overlay logic.
You can change the position through the position
prop on the component. Default is bottom-center
.
<VSonner position="top-center" />
Toasts can also be expanded by default through the expand
prop. You can also change the amount of visible toasts which is 3 by default.
<VSonner expand :visible-toasts="9" />
toast('Event has been created', {
cardProps: {
color: 'success',
class: 'my-toast',
},
cardTextProps: {
// v-card-text props
},
cardActionsProps: {
// v-card-actions props
},
prependIcon: 'mdi-check-circle',
prependIconProps: {
// v-icon props
}
})
To remove a toast programmatically use toast.dismiss(id)
.
const toastId = toast('Event has been created')
toast.dismiss(toastId)
You can also use the dismiss method without the id to dismiss all toasts.
// Removes all toasts
toast.dismiss()
You can change the duration of each toast by using the duration
property, or change the duration of all toasts like this:
<VSonner :duration="10000" />
toast('Event has been created', {
duration: 10000,
})
// Persisent toast
toast('Event has been created', {
duration: Number.POSITIVE_INFINITY,
})
You can pass onDismiss
and onAutoClose
callbacks. onDismiss
gets fired when either the close button gets clicked or the toast is swiped. onAutoClose
fires when the toast disappears automatically after it's timeout (duration
prop).
toast('Event has been created', {
onDismiss: t => console.log(`Toast with id ${t.id} has been dismissed`),
onAutoClose: t => console.log(`Toast with id ${t.id} has been closed automatically`),
})
You can focus on the toast area by pressing ⌥/alt + T. You can override it by providing an array of event.code values for each key.
<VSonner :hotkey="['KeyC']" />
export default defineNuxtConfig({
build: {
transpile: ['vue-sonner']
}
})
<script setup lang="ts">
import { VSonner, toast } from 'vuetify-sonner'
</script>
<template>
<VApp>
<ClientOnly>
<VSonner />
</ClientOnly>
<VBtn @click="toast('My first toast')">
Give me a toast
</VBtn>
</VApp>
</template>
MIT