Package Exports
- vue-toastification
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (vue-toastification) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Vue Toastification
Light, easy and beautiful toasts!
Inspired by React Toastify
Features
Easy to setup for real, you can make it work in less than 10sec!
Customize everything
Swipe to close 👌
Use your custom components or JSX as the toast body for endless possibilities!
Create custom experiences with the
onClose
andonClick
hooksRemove toasts programatically
Define behavior per toast
Pause toast when hovering and when window loses focus 👁
Fancy progress bar to display the remaining time
Use your themes and animations easily
Demo
Need some more convincing? Check out the demo
Installation
$ yarn add vue-toastification
$ npm install --save vue-toastification
Usage
Add it as a Vue plugin:
import Vue from "vue";
import Toast from "vue-toastification";
// Import the CSS or use your own!
import "vue-toastification/dist/index.css";
const options = {
// You can set your default options here
};
Vue.use(Toast, options);
And then just call it in your components with
this.$toast("I'm a toast!");
// Or with options
this.$toast("My toast content", {
timeout: 2000,
onClose: () => console.log("closed!")
});
// These options will override the options defined in the "Vue.use" plugin registration for this specific toast
Positioning the Toast
By default, the toasts will be displayed at the top right corner of your screen, but you can set it manually using the position
option.
The following values are allowed: top-right, top-center, top-left, bottom-right, bottom-center, bottom-left.
Vue.use(Toast, {
// Setting the global default position
position: "top-left"
});
// Or set it per toast
this.$toast("I'm a toast", { position: "bottom-left" });
Toast types
Depending on the context, you may want to use toasts of different colors. You can easily do that by setting the type of toast to be displayed.
this.$toast("Default toast");
this.$toast.info("Info toast");
this.$toast.success("Success toast");
this.$toast.error("Error toast");
this.$toast.warning("Warning toast");
// You can also set the type programatically when calling the default toast
this.$toast("Also a success toast", {
type: "success" // or "error", "default", "info" and "warning"
});
Setting the type only works when using this.$toast
, it won't work when registering the plugin with Vue.use
.
Setting the toast timeout
You can set for how long the toast will remain on the screen (in milliseconds) using the timeout
option, or disable it altogether by setting it to false
// 1 second
this.$toast("Quick toast", { timeout: 1000 });
// Or make the toast permanent until manually closed
this.$toast("Persistent toast", { timeout: false })
// Or set it when registering the plugin
Vue.use(Toast, { timeout: 2000 });
Render a component
Want to use some custom single file component inside the toast? You can! Just pass it as the toast content and it will be rendered.
import MyComponent from "./MyComponent.vue";
this.$toast(MyComponent);
Close the toast using a custom component
When using custom components it is also possible to close the toast from within.
To do that, just emit the close-toast
event
// MyComponent.vue
<template>
<button @click="$emit('close-toast')">Close Toast</button>
</template>
// OtherFile.vue
import MyComponent from "./MyComponent.vue";
// This toast will be closed when the button inside it is clicked
this.$toast(MyComponent);
Render a JSX component
Sometimes you won't want to create a whole component just for a toast. In those cases, you can pass a JSX template to the Toast for it to render as a component
Note: Read this to learn how to enable JSX inside of Vue
const myJSX = (
<div>
<h1>My Title</h1>
<span>My text</span>
</div>
);
// Vue Toastification will generate the appropriate render function automatically.
this.$toast(myJSX);
Render a component with props and events
Usually it is not enough to just render a simple component and you'll need to handle events or pass props. You can do that too!
Just pass the content in the format
{
component: Component,
props: {
propName: propValue // Optional
},
listeners: {
eventName: eventHandler // Optional
}
}
Props will be passed to the created component and the event listeners will be attached to it as well.
Note: Props passed are not reactive
const content = {
// Your component or JSX template
component: MyComponent,
// Props are just regular props, but these won't be reactive
props: {
myProp: "abc",
otherProp: 123
},
// Listeners will listen to and execute on event emission
listeners: {
click: () => console.log("Clicked!"),
myEvent: myEventHandler
}
};
this.$toast(content);
Dismiss toasts programatically
When a toast is created, an ID is assigned to it. You can use it later to programatically dismiss the toast.
You can also choose a custom ID (String or Number) for the toast during its creation.
// Get the toast ID on creation
const toastId = this.$toast("my toast");
// Dismiss it later
this.$toast.dismiss(toastId);
// Pass your custom ID to the toast
this.$toast("my other toast", { id: "my id" });
this.$toast.dismiss("my id");
Clear all toasts
You can also dismiss all toasts at once using clear
.
this.$toast("my toast A");
this.$toast("my toast B");
this.$toast("my toast C");
// Dismiss all toasts
this.$toast.clear();
API
Plugin registration (Vue.use)
Option | Type | Default | Description |
---|---|---|---|
position | String | top-right |
Position of the toast on the screen. Can be any of top-right, top-center, top-left, bottom-right, bottom-center, bottom-left. |
newestOnTop | Boolean | true |
Whether or not the newest toasts are placed on the top of the stack. |
maxToasts | Number | 20 |
Maximum number of toasts on each stack at a time. Overflows wait until older toasts are dismissed to appear. |
transition | String | bounce |
Name of the Vue Transition to use. Only enter-active , leave-active and move are applied. |
transitionDuration | Number or Object | 750 |
Duration of the transition. Can either be a positive integer for both enter and leave, or an object of shape {enter: Number, leave: Number} . |
draggable | Boolean | true |
Whether or not the toast can be dismissed by being dragged to the side. |
draggablePercent | Positive Number | 0.6 |
By how much of the toast width in percent (0 to 1 ) it must be dragged before being dismissed. |
pauseOnFocusLoss | Boolean | true |
Whether or not the toast is paused when the window loses focus. |
pauseOnHover | Boolean | true |
Whether or not the toast is paused when it is hovered by the mouse. |
closeOnClick | Boolean | true |
Whether or not the toast is closed when clicked. |
timeout | Positive Integer or false | 5000 |
How many miliseconds for the toast to be auto dismissed, or false to disable. |
container | HTMLElement | document.body |
Container where the toasts are mounted. |
toastClassName | String or Array of Strings | [] |
Custom classes applied to the toast. |
bodyClassName | String or Array of Strings | [] |
Custom classes applied to the body of the toast. |
hideProgressBar | Boolean | false |
Whether or not the progress bar is hidden. |
hideCloseButton | Boolean | false |
Whether or not the close button is hidden. |
icon | Boolean or String | true |
Custom icon class to be used. When set to true , the icon is set automatically depending on the toast type and false disables the icon. |
Toast (this.$toast)
Parameter | Type | Required | Description |
---|---|---|---|
content | String, Vue Component, JSX or Object | Yes | Toast contents. Can either be a string, a Vue Component, a JSX template or an Object. The shape of the object and its properties are described here |
options | Object | No | Toast options. Described here |
Toast Content Object
Prop | Type | Required | Description |
---|---|---|---|
component | Vue Component or JSX | Yes | Component that will be rendered. |
props | Object | No | propName: propValue pairs of props that will be passed to the component. These are not reactive |
listeners | Object | No | eventName: eventHandler pairs of events that the component can emit. |
Toast Options Object
Option | Type | Default | Description |
---|---|---|---|
id | Number or String | auto |
ID of the toast. |
type | String | default |
Type of the toast. Can be any of success, error, default, info and warning |
position | String | top-right |
Position of the toast on the screen. Can be any of top-right, top-center, top-left, bottom-right, bottom-center, bottom-left. |
draggable | Boolean | true |
Whether or not the toast can be dismissed by being dragged to the side. |
draggablePercent | Positive Number | 0.6 |
By how much of the toast width in percent (0 to 1 ) it must be dragged before being dismissed. |
pauseOnFocusLoss | Boolean | true |
Whether or not the toast is paused when the window loses focus. |
pauseOnHover | Boolean | true |
Whether or not the toast is paused when it is hovered by the mouse. |
closeOnClick | Boolean | true |
Whether or not the toast is closed when clicked. |
onClick | Function | NOOP |
Callback for when the toast is clicked. A closeToast callback is passed as argument to onClick when it is called. |
timeout | Positive Integer or false | 5000 |
How many miliseconds for the toast to be auto dismissed, or false to disable. |
toastClassName | String or Array of Strings | [] |
Custom classes applied to the toast. |
bodyClassName | String or Array of Strings | [] |
Custom classes applied to the body of the toast. |
hideProgressBar | Boolean | false |
Whether or not the progress bar is hidden. |
hideCloseButton | Boolean | false |
Whether or not the close button is hidden. |
icon | Boolean or String | true |
Custom icon class to be used. When set to true , the icon is set automatically depending on the toast type and false disables the icon. |
⚠️️ Toast options supersede Plugin Registration props ⚠️
License
Copyright (C) 2019 Maronato. Licensed under MIT