Introduction
Vue Toasted is One of the Best Toast plugin available for VueJS. it is used by VueJS, Laravel, NuxtJS and trusted by many more organizations it is responsive, touch compatible, easy to use, attractive and feature rich with icons, actions etc...
Interactive Demo
Checkout the Interactive Demo here.
Installation
Install using npm
npm install vue-toasted --save
Install using yarn
yarn add vue-toasted
Direct usage with html
<script src="https://unpkg.com/vue-toasted"></script>
<script>
Vue.use(Toasted)
</script>
Nuxt 💓 Officially uses vue-toasted
for their toast module.
installation guide 👉 @nuxtjs/toast
Usage
It is simple. couple of lines all what you need.
import Toasted from 'vue-toasted';
Vue.use(Toasted)
Vue.use(Toasted, Options)
this.$toasted.show('hello billo')
Vue.toasted.show('hola billo');
All Good Now you have this cool toast in your project..
Icons :fire:
Material Icons, Fontawesome and Material Design Icons are supported. you will have to import the icon packs into your project. example using icons
{
icon : 'check'
icon : {
name : 'watch',
after : true
}
}
Actions :fire:
You can have single or multiple actions in the toast. take a look at the example below
Check below Vue Router section for router integration
Parameters | Type's | Default | Description |
---|
text* | String | - | name of action |
href | String | null | url of action |
icon | String | null | name of material for action |
target | String | null | target of url |
class | String/Array | null | custom css class for the action |
push | Object | null | Vue Router push parameters |
onClick | Function(e,toastObject) | null | onClick Function of action |
Examples
{
action : {
text : 'Cancel',
onClick : (e, toastObject) => {
toastObject.goAway(0);
}
},
action : [
{
text : 'Cancel',
onClick : (e, toastObject) => {
toastObject.goAway(0);
}
},
{
text : 'Undo',
push : {
name : 'somewhere',
dontClose : true
}
}
]
}
API
Options
below are the options you can pass to create a toast
Option | Type's | Default | Description |
---|
position | String | 'top-right' | Position of the toast container ['top-right', 'top-center', 'top-left', 'bottom-right', 'bottom-center', 'bottom-left'] |
duration | Number | null | Display time of the toast in millisecond |
keepOnHover | Boolean | false | When mouse is over a toast's element, the corresponding duration timer is paused until the cursor leaves the element |
action | Object, Array | null | Add single or multiple actions to toast explained here |
fullWidth | Boolean | false | Enable Full Width |
fitToScreen | Boolean | false | Fits to Screen on Full Width |
className | String, Array | null | Custom css class name of the toast |
containerClass | String, Array | null | Custom css classes for toast container |
iconPack | String | 'material' | Icon pack type to be used ['material', 'fontawesome', 'mdi', 'custom-class', 'callback'] |
Icon | String, Object | null | Material icon name as string. explained here |
type | String | 'default' | Type of the Toast ['success', 'info', 'error'] |
theme | String | 'toasted-primary' | Theme of the toast you prefer ['toasted-primary', 'outline', 'bubble'] |
onComplete | Function | null | Trigger when toast is completed |
closeOnSwipe | Boolean | true | Closes the toast when the user swipes it |
singleton | Boolean | false | Only allows one toast at a time. |
Methods
Methods available on Toasted...
Vue.toasted.show( 'my message', { })
Method | Parameter's | Description |
---|
show | message, options | show a toast with default style |
success | message, options | show a toast with success style |
info | message, options | show a toast with info style |
error | message, options | show a toast with error style |
register | name, message, options | register your own toast with options explained here |
clear | - | clear all toasts |
Toast Object
Each Toast Returns a Toast Object where you can manipulate the toast.
el : HtmlElement
text : Function(text)
goAway : Function(delay = 800)
using the toast object
let myToast = this.$toasted.show("Holla !!");
myToast.text("Changing the text !!!").goAway(1500);
Vue Router
Adding vue-router to vue-toasted where you can use it on toast actions.
var router = new VueRouter({
mode: 'history',
routes: [{
path: '/foo',
name : 'foo-name'
}],
linkActiveClass: "active"
});
Vue.use(Toasted, {
router
});
Custom Toast Registration
You can register your own toasts like below and it will be available all over the application.
Toasted.register
methods api details...
Parameters | Type's | Default | Description |
---|
name* | String | - | name of your toast |
message* | String/Function(payload) | - | Toast Body Content |
options | String/Object | {} | Toast Options as Object or either of these strings ['success', 'info', 'error'] |
Take a look at the below examples
Simple Example
import Toasted from 'vue-toasted';
Vue.use(Toasted);
Vue.toasted.register('my_app_error', 'Oops.. Something Went Wrong..', {
type : 'error',
icon : 'error_outline'
})
After Registering your toast you can easily access it in the vue component like below
this.$toasted.global.my_app_error();
Advanced Example
You can also pass message as a function. which will give you more power.
Lets think you need to pass a custom message to the error notification we built above.
this.$toasted.global.my_app_error({
message : 'Not Authorized to Access'
});
you can register a toast with payload like below on the example.
import Toasted from 'vue-toasted';
Vue.use(Toasted);
let options = {
type : 'error',
icon : 'error_outline'
};
Vue.toasted.register('my_app_error',
(payload) => {
if(! payload.message) {
return "Oops.. Something Went Wrong.."
}
return "Oops.. " + payload.message;
},
options
)
Browsers support
IE / Edge | Firefox | Chrome | Safari | Opera | iOS Safari | Chrome for Android |
---|
IE10, IE11, Edge | last 7 versions | last 7 versions | last 7 versions | last 7 versions | last 3 versions | last 3 versions |
Please Report If You have Found any Issues.
Mobile Responsiveness
On Mobile Toasts will be on full width. according to the position the toast will either be on top or bottom.
Credits
- Inspired and developed from materialize-css toast.
- Uses hammerjs for touch events
- Uses lightweight and fast animejs for animations.
- Whoever contributes to this project :wink:
Enjoy Toasting !!