Vue Academy · Lesson

Notification and Toast Plugin Example

Building a toaster plugin: reactive queue, install logic, programmatic API via provide/inject.

Lesson 3 of 413 steps

Notification and Toast Plugin Example is a free Vue Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.

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 ref array holds the toast queue
  • An incrementing counter gives collision-free unique ids
  • notify is the public API; dismiss removes by id
  • install provides the service via app.provide
  • A useToast composable wraps inject for ergonomic use
Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
42
Lessons
156

Frequently Asked Questions

Is the “Notification and Toast Plugin Example” lesson free?

Yes — any 3 lessons of the Vue Academy learning path, including “Notification and Toast Plugin Example”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. Plugin Architecture and app.use()
  2. Global Properties and provide/inject in Plugins
  3. Notification and Toast Plugin Example
  4. Publishing and Typing Vue Plugins
← Back to Vue Academy