31 - Modal Window for Alerts and Confirmations

Description

Create a modal window component for displaying alerts and confirmations, with the following features:

  • Displays messages and optional buttons for actions

  • Overlays the main content to focus user attention

  • Handles user interactions (e.g., button clicks) to close the modal or confirm actions

Algorithm

  1. Create a React component for the modal window

  2. Define the props for the modal window (message, buttons, onClose, onConfirm)

  3. Use CSS to style the modal window and its components

  4. Add JavaScript code to handle user interactions (button clicks, key presses)

Classes

  • Modal: The main modal window component

  • ModalContent: The content area of the modal window

  • ModalFooter: The footer area of the modal window with buttons

Code

Modal.js

ModalContent.js

ModalFooter.js

Modal.css

Explanation

The code creates a modal window component that displays a message and optional buttons for actions. The component uses the useState hook to manage the open/closed state of the modal window. The handleButtonClick function is called when a button is clicked, and it calls the onConfirm or onClose props depending on the button clicked.

Possible Future Enhancements

  • Add animation effects to the modal window

  • Support multiple modal windows at the same time

  • Add a backdrop component to overlay the main content

  • Use a library like React-Modal for a more robust implementation

Last updated