Frontend School

Build a Reusable Toast Notification System

Machine Coding
easy

Build a Reusable Toast Notification System

Easy
React
UI Components
State Management
Context API
CSS

Overview

Toast notifications are a common UI pattern used to provide users with transient, non-blocking feedback about an operation's success, failure, or general information. They appear for a short period and then automatically disappear, or can be dismissed manually. Implementing a robust and reusable toast notification system is a fundamental skill for front-end developers, ensuring a good user experience without interrupting workflows.

What you need to do / Task

Your task is to build a reusable Toast Notification system for a React application. You are provided with a basic component structure; your goal is to fill in the missing logic and styling.

  1. Toast Provider: Complete the ToastProvider component to manage the state of all active toasts and make the addToast function available throughout the application via the ToastContext.
  2. useToast Hook: The useToast hook is already provided; ensure it correctly consumes the ToastContext.
  3. Toast Component: Complete the Toast component. Each toast should:
    • Display its message.
    • Apply distinct styling based on its type (e.g., 'success', 'error', 'info').
    • Include a close button to manually dismiss the toast.
  4. Automatic Dismissal: Implement logic so that each toast automatically disappears after a specified duration (e.g., 5 seconds).
  5. Manual Dismissal: Ensure clicking the close button on a toast immediately removes it.
  6. Display Area: All toasts should appear in a fixed position on the screen (e.g., the top-right corner) and stack vertically.

UI/UX expectations

  • Positioning: Toasts should appear in the top-right corner of the viewport, fixed, and not interfere with the page's scroll.
  • Stacking: New toasts should appear above existing ones (i.e., the most recent toast is at the top of the stack). You can achieve this by adding new toasts to the beginning of the toasts array in state.
  • Appearance:
    • Each toast should have a distinct background color or border based on its type (e.g., green for success, red for error, blue for info).
    • They should have a clear message and a visually distinct close button (e.g., an 'X' icon).
    • Maintain reasonable padding and margins for readability.
  • Accessibility: Use appropriate ARIA roles (e.g., role="alert", aria-live="assertive") for the toast elements to ensure screen readers announce the messages.

Acceptance criteria

  • A button in App.tsx successfully triggers new toasts via the useToast hook.
  • Toasts appear in the top-right corner and stack correctly (newest on top).
  • Each toast displays its message and has a type-specific style.
  • Toasts automatically disappear after approximately 5 seconds.
  • Clicking the 'X' button on a toast immediately removes it.
  • Multiple toasts can be active simultaneously, up to a reasonable limit (e.g., 3-5).
  • The toast system is reusable and encapsulated within the ToastProvider and useToast hook.

Constraints

Constraints & assumptions

  • You are working in a React environment.
  • Focus on functional correctness and clean component architecture. Ensure proper use of React hooks (useState, useEffect, useCallback, useContext).
  • Styling can be simple; advanced animations are not required. Basic CSS is sufficient.
  • The application does not need to handle more than 5 concurrent toasts for this exercise.
  • No external UI library (e.g., Material UI, Ant Design) is allowed; implement components from scratch.
  • Assume the ToastProvider wraps the entire application, as provided in the starter code.

Out of scope

  • Complex animations for toast entry/exit.
  • Customizable toast positions (e.g., bottom-left, center).
  • Drag-and-drop functionality for toasts.
  • Internationalization (i18n) for toast messages.

What interviewers evaluate

  • Component Design: How well the ToastProvider and Toast components are structured, how responsibilities are separated, and the effective use of the useToast hook.
  • State Management: Effective use of React state and context to manage the list of active toasts.
  • Side Effects: Correct handling of setTimeout and clearTimeout for automatic dismissal to prevent memory leaks and unexpected behavior.
  • UI/UX Implementation: Adherence to the specified visual and interactive requirements, including basic styling, positioning, and accessibility attributes.
  • Code Quality: Readability, maintainability, and understanding of React best practices.

Loading editor...