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:
ToastProvider Component: This component should wrap your application (or a part of it), managing the state of all active toasts.
useToast Hook: This custom hook should provide a showToast function, allowing any component within the ToastProvider's scope to easily display a toast notification.
- 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.
- Auto-Dismissal: Each toast must automatically disappear after a specified duration (e.g., 3-5 seconds).
- Manual Dismissal: Each toast should include a clear, clickable 'X' or dismiss button, enabling users to close it manually before auto-dismissal.
- 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.