Enhancing the Notification System with Centralized State in Svelte

Introduction to Notification Systems

Welcome to the first lesson of our Advanced UI Features and Theming course! In this course, we'll enhance a Kanban board application built with Svelte to add professional-quality UI features and a comprehensive theming system.

User feedback is a critical component of any modern web application. When users perform actions like creating, updating, or deleting data, they need immediate feedback to confirm their actions were successful or to be informed of any errors. This is where notification systems come in.

Our Kanban application already has a basic notification system that shows a success message when a task is added:

Svelte
// Current implementation in NotificationManager.svelte
<script>
  import Notification from './Notification.svelte';
  import { getLatestTask } from '$lib/taskStore.svelte.js';

  let taskNotification = $state(null);
  
  $effect(() => {
    const latestTask = getLatestTask();
    if (latestTask && new Date(latestTask.createdAt) > new Date(Date.now() - 2000)) {
      taskNotification = {
        message: `Added task: "${latestTask.title}"`,
        type: 'success'
      };
    }
  });
</script>

{#if taskNotification}
  <Notification message={taskNotification.message} type={taskNotification.type} />
{/if}

Current Limitations

While this works for the specific case of adding a task, it has several limitations:

  • Single notification issue: It only handles one notification at a time (imagine if Gmail could only show one "Message sent" at a time)
  • Tight coupling: It's tightly coupled to the task creation process
  • Limited scope: It doesn't provide a way to show notifications from other parts of the application
  • Limited types: It lacks support for different types of notifications (error, warning, info)
  • No feedback for other operations: Deleting, moving, or updating tasks provides no user feedback

What We'll Build

In this lesson, we'll build a more robust notification system that addresses these limitations. Our enhanced system will:

  1. Centralize notification management in a dedicated store
  2. Support multiple notification types with appropriate styling
  3. Allow showing multiple notifications simultaneously
  4. Provide convenient methods for adding different types of notifications
  5. Handle automatic dismissal with configurable durations
  6. Integrate with our existing task operations to provide comprehensive feedback

Let's get started by creating a centralized notification store.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal