Frontend School

Reusable Confirmation Modal Component

Machine Coding
easy

Reusable Confirmation Modal Component

Easy
React
Component
Modal
UI
State Management
Accessibility

Overview

In Shiprocket's dynamic logistics platform, users frequently perform critical actions such as confirming an order, updating shipment details, or deleting a manifest. To prevent accidental operations and enhance user experience, a clear confirmation step is crucial. This problem challenges you to build a reusable and accessible Confirmation Modal component that can be easily integrated into various parts of the application.

What you need to do / Task

Your primary task is to create a Modal React component that facilitates user confirmation for critical actions. This component should be flexible and easy to use.

  1. Create a Modal Component: Develop a functional React component named Modal.
  2. Display Content: The modal should display a title, a message (e.g., "Are you sure you want to delete this shipment?"), and two action buttons: one for Confirm and one for Cancel.
  3. Control Visibility: Implement props to control the modal's isOpen state from a parent component.
  4. Handle Actions: The Modal component should accept onConfirm and onCancel callback functions, which are triggered when the respective buttons are clicked.
  5. Integrate with App: Use your Modal component within the provided App component, triggered by a button click.

UI/UX expectations

  • Overlay and Dimming: When open, the modal should overlay the entire screen, dimming the background content to focus user attention.
  • Centering: The modal dialog itself should be visually centered on the screen.
  • Closing Mechanisms: The modal must be closable by:
    • Clicking the 'Cancel' button.
    • Clicking on the dimmed backdrop area (outside the modal content).
    • Pressing the Escape key.
  • Button Styling: The 'Confirm' button should have a distinct visual style (e.g., a primary color or a destructive red for a deletion action), and the 'Cancel' button should have a neutral style.
  • Accessibility: Implement basic accessibility considerations, such as keyboard navigation (e.g., focus management for buttons) and appropriate ARIA attributes for screen readers (e.g., aria-modal).

Acceptance criteria

  • The Modal component is correctly implemented and reusable.
  • A trigger button in the App component successfully opens the modal.
  • The modal closes when the 'Cancel' button is clicked.
  • The modal closes when the backdrop (outside the modal content) is clicked.
  • The modal closes when the Escape key is pressed.
  • Clicking 'Confirm' executes the onConfirm callback (e.g., logs a message to the console).
  • Clicking 'Cancel' executes the onCancel callback (e.g., logs a message to the console).
  • The modal's title, message, and button texts are configurable via props from the parent component.
  • The background content is visibly dimmed when the modal is open.
  • The modal is visually centered and appears above other content.

Constraints

Constraints & assumptions

  • You must use React (functional components and hooks).
  • No external UI component libraries (e.g., Material UI, Ant Design) are allowed. Use pure CSS for styling.
  • Focus on implementing the core functionality and basic styling; pixel-perfect design is not required.
  • For this problem, do not use React Portals; render the modal directly within the DOM structure of the App component.

Out of scope

  • Complex animations for modal opening/closing.
  • Advanced state management solutions (e.g., Redux, Zustand).
  • Server-side API interactions or complex data fetching.

What interviewers evaluate

  • Correct and idiomatic implementation of a reusable React component.
  • Effective use of React state management for UI interactions.
  • Attention to basic accessibility principles (keyboard interaction, ARIA).
  • Clean, readable, and maintainable code structure.

Loading editor...