### Install Vue Toastification Source: https://github.com/maronato/vue-toastification/blob/next/README.md Installs the Vue Toastification library using either Yarn or npm. Ensure you are installing the '@next' version for Vue 3 compatibility. ```bash $ yarn add vue-toastification@next $ npm install --save vue-toastification@next ``` -------------------------------- ### Toast Options Object Example Source: https://github.com/maronato/vue-toastification/blob/next/README.md Provides a comprehensive example of the options object that can be passed when creating a toast. This covers various configurations like position, timeout, and more. ```javascript toast('My toast message', { position: POSITION.TOP_CENTER, timeout: 5000, closeOnClick: true, pauseOnHover: true, draggable: true, draggablePercent: 0.6, showCloseButtonOnHover: false, hideProgressBar: false, closeButton: 'button', icon: true, rtl: false, transition: 'bounce', toastClassName: 'my-toast-class', bodyClassName: ['my-toast-body-class-1', 'my-toast-body-class-2'], progressClassName: 'my-progress-class', // ... other options }) ``` -------------------------------- ### Install Vue Toastification (npm/yarn) Source: https://github.com/maronato/vue-toastification/blob/next/README.md Installation instructions for Vue Toastification using npm or yarn package managers. This is the first step to integrate the library into your Vue 3 project. ```bash npm install vue-toastification@next # or ``` ```bash yarn add vue-toastification@next ``` -------------------------------- ### Create and Use Toasts in Vue Components Source: https://github.com/maronato/vue-toastification/blob/next/README.md Demonstrates how to obtain and use the toast interface within a Vue component's setup function or methods. Allows for basic toasts, styled toasts with options, and integration into component methods. ```html ``` -------------------------------- ### Custom Transitions for Toasts Source: https://github.com/maronato/vue-toastification/blob/next/README.md Example of defining and applying custom CSS transitions for toast appearance and disappearance. This allows for unique animation effects. ```javascript import { useToast } from "vue-toastification" const toast = useToast() t-oast("Toast with custom transition", { transition: "custom-fade", // Assumes you have a CSS transition named 'custom-fade' transitionDuration: 500 // Duration in ms }) ``` -------------------------------- ### Simulating Plugin Initialization and Adding Toasts with loadPlugin Source: https://github.com/maronato/vue-toastification/blob/next/CONTRIBUTING.md This example demonstrates how to use the `loadPlugin` utility to simulate `app.use(Toast)`. It shows how to initialize the plugin with custom options, access the toast interface and position wrappers, and verify the addition and content of toasts using Jest and Vue Test Utils. ```typescript import { ToastOptionsAndRequiredContent } from "src/types"; import { POSITION } from "src/ts/constants"; import loadPlugin from "tests/utils/plugin"; describe("test plugin", () => { it("adds toast", async () => { // Load plugin with default value for position const options = { position: POSITION.BOTTOM_LEFT }; const { toastInterface, bottomLeft, containerWrapper } = loadPlugin(options); // Get way to access list of toasts inside containerWrapper const vm = (containerWrapper.vm as unknown) as { toastArray: ToastOptionsAndRequiredContent[] } // Initially there should be no toasts expect(vm.toastArray.length).toBe(0); expect(bottomLeft.getToasts().length).toBe(0); // Create a toast const content = "I'm a toast"; toastInterface(content); // Wait for render await containerWrapper.vm.$nextTick(); // Toast should be created with correct content expect(vm.toastArray.length).toBe(1); expect(bottomLeft.getToasts().length).toBe(1); expect(bottomLeft.getToasts().at(0).props().content).toBe(content); }); }); ``` -------------------------------- ### Custom Toast Classes Source: https://github.com/maronato/vue-toastification/blob/next/README.md Example of applying custom CSS classes to individual toast messages for specific styling. This allows for fine-grained control over the appearance of toasts. ```javascript import { useToast } from "vue-toastification" const toast = useToast() t-oast("Toast with custom class", { // Add a class to the toast container toastClassName: "my-custom-toast-class" }) ``` -------------------------------- ### Create a Toast Notification (Vue Component) Source: https://github.com/maronato/vue-toastification/blob/next/README.md Example of how to create and display a simple toast notification within a Vue 3 component using the injected `useToast` composable. This is the primary method for showing user feedback. ```javascript import { useToast } from "vue-toastification" export default { setup() { // Get toast interface const toast = useToast() // Use toast toast("Hello world!") // Simple content toast.success("Toast success message", { // You can also use the specific types timeout: 5000 }) toast.error("Toast error message") toast.warning('Toast warning message') toast.info('Toast info message') return {} } } ``` -------------------------------- ### Manually Provide Toast Interface to Subtree Source: https://github.com/maronato/vue-toastification/blob/next/README.md Illustrates how to create and provide a new Vue Toastification instance to a specific component subtree using `provideToast`. Parent components use `provideToast` in their setup, making the toast instance available to child components which can then access it via `useToast`. This method is useful for localized toast management. ```html ``` -------------------------------- ### Vue 3 Composition API Toast Notification with TypeScript Source: https://context7.com/maronato/vue-toastification/llms.txt Demonstrates a complete Vue 3 component using the Composition API with full TypeScript type safety. The setup function creates a reusable showNotification method that accepts typed notification data and applies customizable toast options including position, timeout, and interaction behaviors. ```typescript import { defineComponent } from "vue"; import { useToast } from "vue-toastification"; import { ToastInterface, PluginOptions, ToastOptions, TYPE, POSITION } from "vue-toastification"; interface NotificationData { title: string; message: string; type: TYPE; } export default defineComponent({ setup() { const toast: ToastInterface = useToast(); const showNotification = (data: NotificationData): void => { const options: ToastOptions = { type: data.type, position: POSITION.TOP_RIGHT, timeout: 5000, closeOnClick: true, pauseOnHover: true, draggable: true }; toast(`${data.title}: ${data.message}`, options); }; return { showNotification }; } }); ``` -------------------------------- ### Use Existing HTML Element as Toast Container (JavaScript) Source: https://github.com/maronato/vue-toastification/blob/next/README.md Demonstrates how to specify an existing HTML element as the container for toasts using the `container` option. This example shows selecting an element with the ID `my-container` and passing it directly or via a function to the plugin options. ```javascript // App.js const myContainer = document.querySelector('#my-container'); app.use(Toast, { container: myContainer }); ``` ```javascript // App.js const getContainer = () => document.querySelector('#my-container'); app.use(Toast, { container: getContainer }); ``` -------------------------------- ### Use Async Node as Toast Container with MutationObserver (JavaScript) Source: https://github.com/maronato/vue-toastification/blob/next/README.md Provides an example of using an asynchronously available HTML element as the toast container. It employs the `MutationObserver` API to detect when an element with the ID `toast-container` is added to the DOM, resolving the promise with the found element. ```javascript // App.js function asyncGetContainer() { return new Promise(resolve => { const observer = new MutationObserver(function(mutations, me) { const myContainer = document.getElementById("toast-container"); if (myContainer) { me.disconnect(); resolve(myContainer); } }); observer.observe(document, { childList: true, subtree: true }); }); } app.use(Toast, { container: asyncGetContainer }); ``` -------------------------------- ### Override SCSS Variables for Styling Source: https://github.com/maronato/vue-toastification/blob/next/README.md Illustrates how to override the default SCSS variables of Vue Toastification to customize its appearance. This requires a build setup that supports SCSS pre-processing. ```scss // Import Vue Toastification and override variables $toast-border-radius: 10px; $toast-background-color: #333; $toast-text-color: #fff; @import "vue-toastification/src/scss/index.scss"; ``` -------------------------------- ### Enable Right-to-Left (RTL) Support (Vue) Source: https://context7.com/maronato/vue-toastification/llms.txt Explains how to enable Right-to-Left (RTL) layout support for toast content. This can be configured on a per-toast basis using the `rtl: true` option or globally for all toasts by passing the option during plugin installation. ```javascript import { useToast } from "vue-toastification"; const toast = useToast(); // Per-toast RTL ttoast.success("نجح العملية!", { rtl: true }); // Global RTL app.use(Toast, { rtl: true }); ``` -------------------------------- ### Mount Toasts to Custom DOM Elements (Vue) Source: https://context7.com/maronato/vue-toastification/llms.txt Shows how to mount toast notifications to custom DOM elements instead of the default container. This includes examples for using an existing element, a function that returns an element, and an asynchronous function that waits for the element to exist. ```javascript import { createApp } from "vue"; import Toast from "vue-toastification"; // Mount to existing element const customContainer = document.querySelector("#toast-container"); createApp(App).use(Toast, { container: customContainer }); // Using function that returns container createApp(App).use(Toast, { container: () => document.querySelector("#notifications") }); // Async container (waits for element to exist) function asyncGetContainer() { return new Promise(resolve => { const observer = new MutationObserver((mutations, me) => { const container = document.getElementById("dynamic-container"); if (container) { me.disconnect(); resolve(container); } }); observer.observe(document, { childList: true, subtree: true }); }); } createApp(App).use(Toast, { container: asyncGetContainer }); ``` -------------------------------- ### Configure Accessibility Options (Vue) Source: https://context7.com/maronato/vue-toastification/llms.txt Details how to configure ARIA attributes for improved accessibility with screen readers. Custom accessibility options, including `toastRole` and `closeButtonLabel`, can be set per-toast or globally during plugin setup. ```javascript import { useToast } from "vue-toastification"; const toast = useToast(); // Custom accessibility options ttoast("Important alert", { accessibility: { toastRole: "alert", closeButtonLabel: "Close notification" } }); // Global accessibility config app.use(Toast, { accessibility: { toastRole: "status", closeButtonLabel: "Dismiss" } }); ``` -------------------------------- ### Disable Toast Icons Globally and Per Toast Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to disable toast icons in Vue Toastification. The first example disables icons for all toasts by default during plugin registration, while the second demonstrates disabling the icon for a single toast instance. ```javascript // Disable every toast's icon by default app.use(Toast, { icon: false }); // Disable icons per toast toast('No icon!', { icon: false }); ``` -------------------------------- ### Manually Provide Interfaces Source: https://github.com/maronato/vue-toastification/blob/next/README.md Details the process of manually providing the toast interface, often used when you need more control over instance creation or when not using the global `app.use` method. ```javascript // Assume 'toast' is an instance created elsewhere, e.g., new Toast({...}) // Using provide/inject in Vue 3 // In parent component or setup function: // import { provide } from 'vue'; // import { useToast } from 'vue-toastification'; // const toast = useToast(); // Or create a new instance // provide('myToastService', toast); // In child component: // import { inject } from 'vue'; // const toast = inject('myToastService'); // toast('Manual injection works'); ``` -------------------------------- ### Provide Interfaces to Existing Instances Source: https://github.com/maronato/vue-toastification/blob/next/README.md Explains how to make an existing Vue Toastification instance available throughout the application, typically via Vue's provide/inject API or by mounting it to the app instance. ```javascript // In your main.js or a setup file import Toast, { POSITION } from "vue-toastification" const toastInstance = new Toast({ position: POSITION.BOTTOM_LEFT }) // Provide the instance to the app app.provide('toast', toastInstance) // In a component: // import { inject } from 'vue' // const toast = inject('toast') // toast.info('Message injected') ``` -------------------------------- ### Hide Close Button in Toast Notifications Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to disable the close button on toast notifications by setting the closeButton option to false. This can be applied when calling the toast function or during plugin setup. ```javascript toast('No close button', { closeButton: false }); ``` -------------------------------- ### Create New Toastification Instances Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to create separate, independent instances of Vue Toastification. This is useful for managing different sets of toasts with distinct configurations or in complex application structures. ```javascript import Toast from "vue-toastification" // Create a new instance with custom options const customToastInstance = new Toast({ position: "TOP_CENTER", timeout: 3000 }) // You can then use customToastInstance to manage toasts // customToastInstance.success("Message via custom instance") // To use it within a component's setup function: // import { getCurrentInstance } from 'vue' // const { proxy } = getCurrentInstance() // const customToast = proxy.customToastInstance // Assuming it was provided globally ``` -------------------------------- ### Update from v1.x to v2 (Vue 3) Source: https://github.com/maronato/vue-toastification/blob/next/README.md Guidance on updating from Vue Toastification v1.x to v2, specifically addressing breaking changes and updated usage patterns required for Vue 3 compatibility. ```markdown ## Updating from v1.x Vue Toastification v2 is **only** compatible with Vue 3. If you are using Vue 2, check out [Vue Toastification v1](https://github.com/Maronato/vue-toastification/tree/master). ### Plugin registration ```javascript // Vue 2 // import Vue from 'vue'; // import Toast from 'vue-toastification'; // import 'vue-toastification/dist/index.css'; // Vue.use(Toast); // Vue 3 import { createApp } from 'vue' import App from './App.vue' import Toast from "vue-toastification" import 'vue-toastification/dist/index.css' const app = createApp(App) app.use(Toast) app.mount('#app') ``` ### Creating toasts ```javascript // Vue 2 // this.$toast('message'); // Vue 3 import { useToast } from "vue-toastification" const toast = useToast() // or t-oast('message') // if registered globally and imported ``` ``` -------------------------------- ### Register Vue Toastification Plugin (main.js) Source: https://github.com/maronato/vue-toastification/blob/next/README.md Demonstrates how to register the Vue Toastification plugin globally in your main application entry point (e.g., main.js). This makes toast functionalities available throughout your Vue 3 application. ```javascript import { createApp } from 'vue' import App from './App.vue' import Toast, { POSITION } from "vue-toastification" // Import the CSS import 'vue-toastification/dist/index.css' const app = createApp(App) app.use(Toast, { // Set default options position: POSITION.BOTTOM_RIGHT }) app.mount('#app') ``` -------------------------------- ### Use Existing Toast Instance with useToast Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to obtain an interface to an existing Vue Toastification instance from another file using `useToast`. This is useful for sharing toast instances across modules or components defined in separate files. It requires importing `useToast` and the `eventBus` from its source file. ```javascript import { useToast } from "vue-toastification"; import { myEventBus } from "./otherFile.js"; const toast = useToast(myEventBus); ``` -------------------------------- ### Create Reusable Toast Interfaces with EventBus Source: https://github.com/maronato/vue-toastification/blob/next/README.md Demonstrates creating multiple interfaces to a single Vue Toastification instance using the EventBus. This allows for shared toast management across different parts of an application. It requires importing `createToastInterface` and `EventBus` from 'vue-toastification'. ```javascript import { createToastInterface, EventBus } from "vue-toastification"; // Create a new event bus const myEventBus = new EventBus(); // Generate the first interface, passing your eventBus as a parameter const toast = createToastInterface({ timeout: 1000, eventBus: myEventBus, }); // Later, generate another interface to the same instance const otherToast = createToastInterface(myEventBus); ``` -------------------------------- ### Create Multiple Independent Toast Instances Source: https://context7.com/maronato/vue-toastification/llms.txt Demonstrates how to create and manage multiple independent toast instances using `createToastInterface`. Each instance can have its own configuration and control its own set of toasts. It also shows how to share an event bus between instances for synchronized control. ```javascript import { createToastInterface, EventBus } from "vue-toastification"; // Create independent toast instance const adminToast = createToastInterface({ position: POSITION.TOP_RIGHT, timeout: 3000, containerClassName: "admin-toasts" }); const userToast = createToastInterface({ position: POSITION.BOTTOM_LEFT, timeout: 5000, containerClassName: "user-toasts" }); adminToast.success("Admin notification"); userToast.info("User notification"); // Share event bus between instances const sharedEventBus = new EventBus(); const toast1 = createToastInterface({ eventBus: sharedEventBus, timeout: 3000 }); const toast2 = createToastInterface(sharedEventBus); // Both interfaces control the same toasts const id = toast1("Shared toast"); ttoast2.dismiss(id); ``` -------------------------------- ### Preset Default Toast Options per Type Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to set default options specifically for each toast type (e.g., success, error, info). This avoids repetition when configuring common toast behaviors. ```javascript import { useToast } from "vue-toastification" const toast = useToast() // Default options applied globally when registering the plugin app.use(Toast, { toastDefaults: { // Defaults for all types timeout: 5000, // Default for success toasts success: { position: POSITION.TOP_CENTER, timeout: 7000 }, // Default for error toasts error: { timeout: 0, // Non-dismissible by default showCloseButtonOnHover: true } } }) // Usage: t-oast.success("This success toast will last 7 seconds") t-oast.error("This error toast will not auto-dismiss") ``` -------------------------------- ### Use Vue Toastification Outside Vue Components Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to use the `useToast` function to create toasts when working outside of Vue components, such as within Vuex stores. This relies on the global event bus created during plugin registration. ```javascript // store.js import { createStore } from 'vuex' import { useToast } from 'vue-toastification' const toast = useToast() const store = createStore({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { increment (context) { context.commit('increment') toast.success("incremented!") } } }) ``` -------------------------------- ### Create Independent Toast Interface (JavaScript) Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to create a separate, independent toast interface using `createToastInterface`. This allows for managing multiple sets of toasts distinctly. The function can also accept standard plugin options to configure the new interface. ```javascript import { createToastInterface } from "vue-toastification"; const myInterface = createToastInterface(); ``` ```javascript import { createToastInterface } from "vue-toastification"; const myInterface = createToastInterface({ timeout: 1000 }); ``` -------------------------------- ### Preset Default Toast Options Per Toast Type Source: https://github.com/maronato/vue-toastification/blob/next/README.md Shows how to configure default options for different toast types (ERROR, SUCCESS, etc.) during plugin registration. This approach reduces code repetition by centralizing toast behavior configuration for each type. ```javascript import Toast, { TYPE } from "vue-toastification"; const options = { toastDefaults: { // ToastOptions object for each type of toast [TYPE.ERROR]: { timeout: 10000, closeButton: false, }, [TYPE.SUCCESS]: { timeout: 3000, hideProgressBar: true, } } }; app.use(Toast, options); ``` -------------------------------- ### Utilize Different Toast Types Source: https://github.com/maronato/vue-toastification/blob/next/README.md Demonstrates how to display toasts with different semantic types such as info, success, error, and warning. This can be done by calling specific methods like `toast.success()` or by programmatically setting the type using `TYPE` constants. ```javascript toast("Default toast"); tost.info("Info toast"); tost.success("Success toast"); tost.error("Error toast"); tost.warning("Warning toast"); // You can also set the type programmatically when calling the default toast import { TYPE } from "vue-toastification"; tost("Also a success toast", { type: TYPE.SUCCESS // or "success", "error", "default", "info" and "warning" }); ``` -------------------------------- ### Custom Fade Transition CSS for Vue Toastification Source: https://github.com/maronato/vue-toastification/blob/next/README.md Provides CSS keyframe animations and transition classes ('fade-enter-active', 'fade-leave-active', 'fade-move') to create a custom fade transition effect for Vue Toastification. This requires implementing specific animation and transition properties. ```css @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } } .fade-enter-active { animation-name: fadeIn; animation-duration: 750ms; animation-fill-mode: both; } .fade-leave-active { animation-name: fadeOut; animation-duration: 750ms; animation-fill-mode: both; } .fade-move { transition-timing-function: ease-in-out; transition-property: all; transition-duration: 400ms; } ``` -------------------------------- ### Define Custom Transitions for Toasts (JavaScript & CSS) Source: https://context7.com/maronato/vue-toastification/llms.txt Illustrates how to implement custom animations for toast appearances in vue-toastification. This includes using built-in transitions, applying named custom transitions, and defining custom enter, leave, and move transition classes with CSS keyframes. ```javascript import { useToast } from "vue-toastification"; const toast = useToast(); // Built-in transitions app.use(Toast, { transition: "Vue-Toastification__bounce" // default }); app.use(Toast, { transition: "Vue-Toastification__fade" }); app.use(Toast, { transition: "Vue-Toastification__slideBlurred" }); // Named custom transition app.use(Toast, { transition: "my-custom-fade" }); // Custom transition classes app.use(Toast, { transition: { enter: "my-enter-active", leave: "my-leave-active", move: "my-move" } }); ``` ```css /* Custom transition CSS */ @keyframes customFadeIn { from { opacity: 0; transform: translateY(-20px); } to { opacity: 1; transform: translateY(0); } } .my-enter-active { animation: customFadeIn 0.5s ease-out; } .my-leave-active { animation: customFadeIn 0.5s ease-in reverse; } .my-move { transition: all 0.4s ease; } ``` -------------------------------- ### Use Mixed Custom Transition Classes in Vue Toastification Source: https://github.com/maronato/vue-toastification/blob/next/README.md Illustrates configuring Vue Toastification with a mix of custom transition classes for entering and moving animations ('fade-enter-active', 'fade-move'), while using the default 'bounce' transition for leaving animations. This requires specifying all three properties if custom classes are used. ```javascript app.use(Toast, { transition: { enter: "fade-enter-active", leave: "Vue-Toastification__bounce-leave-active", move: "fade-move" } }); ``` -------------------------------- ### Updating Default Options Source: https://github.com/maronato/vue-toastification/blob/next/README.md Demonstrates how to update the globally configured default toast options after the plugin has been registered. This allows for dynamic changes to default behaviors. ```javascript import { useToast } from "vue-toastification" const toast = useToast() // Access and update defaults (example, exact API might vary based on version) t-oast.defaults.timeout = 10000 t-oast.defaults.position = POSITION.TOP_LEFT // You might need to re-register or use a specific method provided by the library // Consult the library's documentation for the most accurate way to update defaults dynamically. ``` -------------------------------- ### Render a JSX Component as Toast Content Source: https://github.com/maronato/vue-toastification/blob/next/README.md Demonstrates rendering a JSX component within a toast notification. This is useful for developers who prefer JSX syntax for their UI elements. ```javascript import { useToast } from "vue-toastification" const toast = useToast() t-oast({ component: { render() { return (
This toast uses JSX.
{{ $t('message.success') }}