Notification and Toast Plugin Example
Building a toaster plugin: reactive queue, install logic, programmatic API via provide/inject.
Notification and Toast Plugin Example is a free Vue Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Goal: A Toast Plugin
We will build a toast notification plugin. It maintains a reactive queue of messages, exposes a notify API, and renders toasts globally. Any component can trigger a toast through inject or a composable.
The Reactive Queue
The core state is a reactive ref holding an array of toast objects. When we push/remove items, every component rendering the queue updates automatically.
import { ref } from 'vue'
const toasts = ref([])
// each toast: { id, message, type }Unique IDs via a Counter
Each toast needs a unique id for keying and removal. Use an incrementing counter, not Date.now() — two toasts fired in the same millisecond would collide with a timestamp.
let counter = 0
function nextId() {
// guaranteed unique even for rapid-fire toasts
return ++counter
}The notify Function
notify is the public API. It pushes a new toast with a fresh id and schedules its removal after a duration.
function notify(message, type = 'info', duration = 3000) {
const id = nextId()
toasts.value.push({ id, message, type })
setTimeout(() => dismiss(id), duration)
return id
}Dismissing a Toast
dismiss removes a toast by id by filtering it out of the reactive array. Reassigning toasts.value keeps reactivity intact.
function dismiss(id) {
toasts.value = toasts.value.filter(t => t.id !== id)
}Bundling Into a Service
Group the state and functions into one service object. This object is what the plugin provides to the app.
const toastService = {
toasts, // reactive ref (read in the UI)
notify, // create a toast
dismiss // remove a toast
}The Plugin install Method
The plugin install provides the service via app.provide so any component can inject it. We export a Symbol key for safety.
export const toastKey = Symbol('toast')
export default {
install(app) {
app.provide(toastKey, toastService)
app.config.globalProperties.$toast = toastService
}
}Registering the Toast Container
The plugin can also register a global ToastContainer component that renders the queue, so the consumer just drops it once near the app root.
import ToastContainer from './ToastContainer.vue'
install(app) {
app.provide(toastKey, toastService)
app.component('ToastContainer', ToastContainer)
}The Container Template
The container reads the injected queue and renders each toast, with a click-to-dismiss handler keyed by the unique id.
<script setup>
import { inject } from 'vue'
import { toastKey } from './toast'
const { toasts, dismiss } = inject(toastKey)
</script>
<template>
<div class="toasts">
<div v-for="t in toasts" :key="t.id" :class="t.type"
@click="dismiss(t.id)">
{{ t.message }}
</div>
</div>
</template>A Convenience Composable
Wrap the inject in a useToast composable so components get a clean API and a helpful error if the plugin was not installed.
import { inject } from 'vue'
import { toastKey } from './toast'
export function useToast() {
const service = inject(toastKey)
if (!service) throw new Error('Toast plugin not installed')
return service
}Using It in a Component
Finally, any component triggers notifications through the composable. Register app.use(toastPlugin) and place <ToastContainer /> once near the root.
<script setup>
import { useToast } from './toast'
const { notify } = useToast()
function save() {
notify('Saved successfully', 'success')
}
</script>Quick Check
Test your understanding of the toast plugin design.
Recap
You built a toast plugin:
- A reactive
refarray holds the toast queue - An incrementing counter gives collision-free unique ids
notifyis the public API;dismissremoves by idinstallprovides the service viaapp.provide- A
useToastcomposable wrapsinjectfor ergonomic use
Frequently asked questions
Is the “Notification and Toast Plugin Example” lesson free?
Yes — the full text of “Notification and Toast Plugin Example” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “Notification and Toast Plugin Example”?
Building a toaster plugin: reactive queue, install logic, programmatic API via provide/inject. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Notification and Toast Plugin Example” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Plugin Architecture and app.use()
- Global Properties and provide/inject in Plugins
- Notification and Toast Plugin Example
- Publishing and Typing Vue Plugins