Frontend School

Build a Reusable Twitter-Style Toast Notification System

Machine Coding
medium

Build a Reusable Twitter-Style Toast Notification System

Medium
Frontend
React
UI
Notifications
Machine Coding
Context API
Hooks

Overview

Toast notifications are transient, non-disruptive messages that provide immediate feedback to users about the outcome of an action (e.g., "Tweet sent successfully!", "Settings saved"). They are crucial for a responsive and intuitive user experience on platforms like Twitter, confirming user actions without interrupting their workflow. This problem challenges you to build a robust, reusable, and accessible toast notification system.

What you need to do / Task

Your goal is to implement a flexible toast notification system using React, consisting of:

  1. ToastProvider Component: This component should wrap your application (or a part of it), managing the state of all active toasts.
  2. useToast Hook: This custom hook should provide a showToast function, allowing any component within the ToastProvider's scope to easily display a toast notification.
  3. Display and Stack Toasts: Toasts should appear in a designated, consistent area of the screen (e.g., top-right corner) and stack vertically. Newer toasts should appear above older ones.
  4. Auto-Dismissal: Each toast must automatically disappear after a specified duration (e.g., 3-5 seconds).
  5. Manual Dismissal: Each toast should include a clear, clickable 'X' or dismiss button, enabling users to close it manually before auto-dismissal.
  6. Toast Types: Support at least three distinct toast types (success, error, info), each with a unique visual indicator (e.g., different background color, border color, or icon).

UI/UX expectations

  • Positioning: All toast notifications should consistently appear in the top-right corner of the viewport.
  • Stacking: When multiple toasts are active, new toasts should appear at the top of the stack, pushing older toasts downwards. Maintain reasonable vertical spacing between toasts.
  • Visual Cues: Each toast type (success, error, info) must have a distinct visual style (e.g., a specific background color, border highlight, or icon) to convey its urgency or status.
  • Dismiss Button: A clearly visible and clickable 'X' icon or button should be present on each toast for manual dismissal.
  • Accessibility: Consider basic accessibility by including appropriate ARIA attributes for live regions to announce toasts to screen reader users.

Acceptance criteria

  • A toast notification appears on screen when the showToast function is called.
  • Multiple toasts stack correctly in the top-right corner, with newer toasts appearing above older ones.
  • Toasts automatically disappear after their configured duration.
  • Clicking the 'X' button on any toast dismisses it immediately.
  • Toasts display distinct visual styles for success, error, and info types.
  • The system can handle showing multiple toasts concurrently while respecting the maximum visible toast limit.
  • Basic accessibility attributes for live regions are present on toast elements.

Constraints

Constraints & assumptions

  • The solution must be implemented using React functional components and hooks.
  • Do not use any external UI component libraries (e.g., Material UI, Ant Design); build all components from scratch.
  • Focus on core functionality and clean architecture; pixel-perfect styling is not the primary evaluation metric, but the UI should be functional and visually distinct.
  • The default auto-dismissal duration for toasts should be between 3 and 5 seconds.
  • A maximum of 3-5 toasts should be visible on screen at any given time; if this limit is exceeded, the oldest toast should be automatically removed to make space for the newest one.

Out of scope

  • Complex animations (e.g., advanced entrance/exit transitions beyond basic fade-in/out).
  • Internationalization (i18n) for toast messages.
  • Persisting toasts across page navigations or reloads.
  • Advanced keyboard navigation or focus trapping within the toast container (beyond basic dismiss button interaction).

What interviewers evaluate

  • Architectural Design: Effective use of React Context and Hooks to create a reusable and scalable toast system.
  • State Management: Correctly managing the lifecycle and state of multiple concurrent toast notifications, including auto-dismissal and the maximum visible toast limit.
  • Code Quality: Readability, maintainability, type safety (if TypeScript is used), and adherence to React best practices.
  • UI/UX Implementation: Accurate rendering, stacking, and interaction behavior of toasts as per requirements, including distinct visual types.
  • Basic Accessibility: Inclusion of appropriate ARIA attributes for live regions to enhance screen reader compatibility.

Loading editor...