Frontend School

Build a Flexible Toast Notification System

Machine Coding
medium

Build a Flexible Toast Notification System

Medium
React
UI
Frontend
Notifications
Context API
Hooks

Overview

In a dynamic platform like Livspace, providing immediate and clear feedback to users is crucial for a smooth user experience. Whether it's confirming a successful action (e.g., 'Design saved!'), alerting to an error ('Failed to load data'), or providing general information, toast notifications serve as a non-intrusive way to communicate with the user. This problem challenges you to build a reusable and robust toast notification system, demonstrating your skills in component architecture, state management, and UI implementation.

What you need to do / Task

Your primary goal is to build a ToastProvider and a useToast hook that allows any component within the application to trigger and manage toast notifications.

  1. ToastProvider Component: Create a top-level component that will manage the state of all active toasts and render them in a designated area of the screen.
  2. useToast Hook: Implement a custom React hook that provides methods to addToast (trigger a new toast) and dismissToast (manually remove a toast by ID).
  3. Toast Component: Design a individual Toast component responsible for displaying a single notification, including its message, type, and a close button.
  4. Toast Types: Support at least four distinct toast types: success, error, info, and warning. Each type should have a visually distinct appearance (e.g., different background colors).
  5. Automatic Dismissal: Ensure toasts automatically disappear after a configurable duration (default to 5 seconds).
  6. Manual Dismissal: Each toast should include a close button that allows the user to dismiss it immediately.
  7. Consistent Positioning: All toasts should appear stacked in the top-right corner of the viewport.

UI/UX expectations

  • Layout: Toasts should appear in the top-right corner of the screen, stacking vertically with a small gap between them.
  • Appearance: Each toast should be a distinct, visually appealing rectangle. Different types (success, error, info, warning) must be clearly distinguishable through color or other visual cues.
  • Interactions: Toasts should have a prominent message and a clear 'X' or close button for manual dismissal.
  • Responsiveness: The toast container should adapt gracefully to different screen sizes (though complex responsiveness is not the primary focus).

Acceptance criteria

  • The application must render a ToastProvider at a high level in the component tree.
  • Any child component can use the useToast hook to trigger new notifications.
  • Calling addToast with different types (success, error, info, warning) results in visually distinct toasts.
  • New toasts appear in the top-right corner, stacking correctly without overlapping.
  • Each toast displays its message and a functional close button.
  • Toasts automatically disappear after the default (or specified) duration.
  • Clicking the close button on any toast dismisses it immediately.
  • Multiple toasts can be displayed simultaneously, and they maintain their stacking order.

Constraints

Constraints & assumptions

  • You must use React for building the UI components.
  • Avoid using external UI component libraries (e.g., Material UI, Ant Design). Basic CSS styling is expected.
  • The default auto-dismissal duration for toasts should be 5 seconds.
  • Toasts should appear in the top-right corner of the viewport.

Out of scope

  • Complex entry/exit animations beyond basic CSS transitions.
  • Advanced accessibility features (e.g., ARIA live regions).
  • Support for custom icons or images within toasts.
  • Toast queueing or limiting the number of visible toasts.

What interviewers evaluate

  • Architectural Design: How well the ToastProvider, useToast hook, and Toast components are structured, demonstrating clear separation of concerns.
  • State Management: Effective use of React Context API and useState/useRef/useEffect hooks to manage toast lifecycle and state.
  • UI Implementation: Correct rendering, styling, and positioning of toasts, including type-specific visual variations.
  • Event Handling & Timers: Proper implementation of automatic and manual dismissal mechanisms.
  • Code Quality: Readability, maintainability, and adherence to React best practices.

Loading editor...