Frontend School

Real-time Toast Notifications System

Machine Coding
medium

Real-time Toast Notifications System

Medium
React
TypeScript
UI/UX
State Management
Accessibility

Overview

In modern web applications, providing immediate, non-intrusive feedback to users about the status of operations (e.g., "Item added to cart", "Settings saved successfully", "Error fetching data") is crucial for a smooth user experience. This problem challenges you to build a robust and reusable Toast Notification system that can display temporary messages, informing users without interrupting their workflow. Your solution should be architected to be easily integrated into any part of a React application.

What you need to do / Task

Your goal is to implement a flexible and accessible Toast Notification system. This involves creating a ToastProvider to manage the state of notifications and a Toast component to render individual messages.

  1. ToastProvider Component: Create a React Context Provider (ToastProvider) that will wrap your application. This provider should manage the state (a queue) of active toasts.
  2. Toast Component: Develop a Toast component responsible for rendering a single notification message. It should display the message, type (e.g., success, error, info), and a close button.
  3. Toast Management Logic:
    • Adding Toasts: Provide a clear API (e.g., a custom hook useToast or a context function) to add new toasts to the queue from any component within the ToastProvider's scope.
    • Automatic Dismissal: Each toast should automatically dismiss after a default duration (e.g., 5 seconds), which should also be configurable per toast.
    • Manual Dismissal: Implement a visible close button (X icon) on each toast, allowing users to manually dismiss it immediately.
    • Hover-to-Pause: When a user hovers their mouse over an active toast, its auto-dismiss timer should pause. When the mouse leaves, the timer should resume.
  4. Display & Stacking: Toasts should appear in a fixed position on the screen (e.g., top-right corner) and stack vertically when multiple toasts are active. New toasts should appear at the top of the stack, pushing older ones down.

UI/UX expectations

  • Layout: Toasts should be fixed to the top-right corner of the viewport. Multiple toasts should stack vertically, with new toasts appearing above older ones.
  • Interactions:
    • Clicking a close button dismisses the toast instantly.
    • Toasts disappear automatically after their duration.
    • Hovering over a toast should pause its auto-dismiss countdown.
  • Accessibility:
    • Toasts should be announced to screen readers using aria-live="polite" or aria-live="assertive" as appropriate for the toast type.
    • The close button should have an aria-label for screen reader users.

Acceptance criteria

  • New toasts appear correctly in the top-right corner.
  • Multiple toasts stack vertically, with the newest toast at the top.
  • Toasts automatically disappear after their configured duration (default 5 seconds).
  • Clicking the close button on a toast immediately removes it.
  • Hovering the mouse over a toast pauses its auto-dismiss timer; moving the mouse away resumes it.
  • The application can trigger different types of toasts (e.g., 'success', 'error', 'info') with distinct visual cues (minimal styling expected, but type should be conveyed).
  • Toasts are announced to screen readers, and the close button is accessible.

Constraints

Constraints & assumptions

  • You must use React and TypeScript.
  • Do not use any third-party toast notification libraries or complex animation libraries. Build the core functionality from scratch.
  • Focus on functionality, architecture, and basic styling for clarity, not pixel-perfect design.
  • Default toast duration: 5000 milliseconds (5 seconds).
  • A reasonable maximum number of simultaneously visible toasts (e.g., 3-5) is acceptable to prevent screen clutter.
  • No persistent storage for toasts (they are ephemeral).

Out of scope

  • Advanced animations (e.g., complex slide-in/slide-out transitions beyond basic CSS).
  • Customization of toast positions (stick to top-right).
  • Drag-and-drop functionality for toasts.
  • Complex theming or highly customizable styling beyond basic type differentiation.
  • Implementing a queue that prevents duplicate toasts or prioritizes certain types.

What interviewers evaluate

  • System Design & Architecture: How effectively the ToastProvider and Toast components are designed for reusability, modularity, and extensibility using React Context and hooks.
  • State Management: Competent use of React state and effects to manage the toast queue, timers, and interactions (auto-dismissal, hover-to-pause).
  • UI Implementation & Interaction: Correct rendering, stacking, and handling of user interactions (dismiss, hover) for a smooth user experience.
  • Code Quality & Readability: Clean, well-structured, type-safe (TypeScript), and maintainable code with appropriate separation of concerns.
  • Basic Accessibility: Inclusion of fundamental accessibility attributes like aria-live and aria-label for screen reader compatibility.

Loading editor...