🔥Vue Toastify🔥
Simple and dependency free notification plugin.
Installation
npm i vue-toastify
import { createApp } from 'vue';
import plugin from 'vue-toastify';
import 'vue-toastify/index.css';
import 'vue-toastify/themes/dark.css';
import type { Settings } from 'vue-toastify';
const app = createApp({ });
app.use<Settings>(plugin, { });
app.mount('#app');
Usage with Nuxt
Options:
Custom styling
Styles include a 'dark'
(default) and a 'light'
theme. If you would like to create your own styles you may use the following helpers:
import { createVtTheme, getCssRules } from 'vue-toastify';
createVtTheme('myThemeName', '#8f6b42');
getCssRules('myThemeName', '#8f6b42').forEach(rule => {...});
Custom notifications
You may create some methods on the useToast()
so it will shortcut any repetition you may have in your app. To register them add a customNotifications
key to the settings when registering the plugin.
app.use<Settings>(plugin, {
customNotifications: {
authenticationError: {
body: 'Authentication error',
}
}
});
useToast().authenticationError();
Ambient declaration for custom notifications
import type { ToastPluginAPI, CustomMethods } from 'vue-toastify';
declare module 'vue-toastify' {
interface MyMethods extends CustomMethods {
authenticationError(): string;
}
function useToast(): ToastPluginAPI & MyMethods;
}
Events
The plugin emits events that you can listen to which allows for using callbacks at different points in the toast's lifecycle.
import { useVtEvents, useToast } from 'vue-toastify';
const toast = useToast().success({ body: 'Hello world', canTimeout: true });
useVtEvents().once('vtPaused', payload => {
if (payload.id === toast.id) {
}
})
Usage with Nuxt
The recommended way to install is by creating a plugin. As notifications are expected to be responses to user actions, we can lazy load the plugin to reduce the initial bundle size.
Be sure
to familiarise yourself with the Nuxt plugin documentation.
import type { Settings } from 'vue-toastify';
export default defineNuxtPlugin({
name: 'toast',
parallel: true,
setup: nuxt => {
void import('vue-toastify').then(exports => {
nuxt.vueApp.use<Settings>(exports.default, {
pauseOnHover: true,
theme: 'light',
position: 'top-right'
});
});
}
});
Then specify the auto-imported preset in your configuration.
export default defineNuxtConfig({
css: [
'vue-toastify/index.css',
'vue-toastify/themes/light.css'
],
imports: {
presets: [
{
from: 'vue-toastify',
imports: [
'useToast',
]
}
]
},
})