🌐
50 React Coding Puzzles
  • 50 React Coding Puzzles
  • Contact
  • Reviews
  • Table of Contents
  • Introduction
  • 1 - Simple Counter App
  • 2 - Dynamic List of Items with Strikethrough
  • 3 - Color Picker with Hex Code Display
  • 4 - Password Strength Checker
  • 5 - Countdown Timer with Audio
  • 6 - Dynamic Movie List with Filtering and Sorting
  • 7 - Text Animation Component (Fade/Scroll)
  • 8 - Dynamically Generated Text Art
  • 9 - Meme Generator with Custom Text
  • 10 - "What Should I Eat?" Random Recipe Generator
  • 11 - Simple Quiz App with Multiple Choice
  • 12 - Unit Converter
  • 13 - Dynamic Image Gallery with Lightbox
  • 14 - Weather Widget with Temperature and Description
  • 15 - Contact Form with Email and Message Validation
  • 16 - Color Palette Generator
  • 17 - Drag-and-Drop Feature for Rearranging Items
  • 18 - Interactive Quiz with Dynamic Results and Feedback
  • 19 - To-Do List with Task Categorization
  • 20 - Note-Taking App with Markdown Support
  • 21 - Simple Calculator with Basic Operations
  • 22 - Word Scramble Game with Timer and Score
  • 23 - Number Guessing Game
  • 24 - Digital Clock with Time Zone Support
  • 25 - Interactive Time Zone Converter
  • 26 - Weather Forecast App with Location Detection
  • 27 - Real-Time Stock Ticker with Updates
  • 28 - Virtual Dice Roller with Multiple Dice
  • 29 - Responsive Navigation Menu with Dropdowns
  • 30 - Progress Bar for Loading or Task Completion
  • 31 - Modal Window for Alerts and Confirmations
  • 32 - Infinite Scroll for Loading More Content
  • 33 - Form Validation with Error Messages
  • 34 - Search Bar with Filtering and Suggestions
  • 35 - Drag-and-Drop File Upload
  • 36 - Interactive Color Picker with Sliders
  • 37 - Image Carousel with Autoplay
  • 38 - Rating System with Stars or Thumbs Up/Down
  • 39 - Comment Section with Nested Replies
  • 40 - Pagination for Long Lists of Data
  • 41 - Stopwatch app that tracks elapsed time
  • 42 - Responsive E-commerce Product Grid
  • 43 - Random Movie/Book Recommender
  • 44 - Kanban board for managing tasks with drag-and-drop
  • 45 - Chat application with real-time messaging using WebSockets
  • 46 - Calendar that displays events and allows scheduling
  • 47 - Tic-Tac-Toe Game
  • 48 - Hangman Game
  • 49 - Notes App
  • 50 - Expense Tracker
  • Afterword
  • Appendix
Powered by GitBook
On this page
  • Description
  • Algorithm
  • Classes
  • Code
  • Explanation
  • Possible Future Enhancements

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

import React from 'react';
import './Modal.css';
import ModalContent from './ModalContent';
import ModalFooter from './ModalFooter';

const Modal = ({
  message,
  buttons,
  onClose,
  onConfirm
}) => {
  const [isOpen, setIsOpen] = React.useState(false);

  const handleButtonClick = (button) => {
    if (button === 'confirm') {
      onConfirm();
    } else {
      onClose();
    }
  };

  return (
    <div className="modal" aria-hidden={!isOpen}>
      <ModalContent>
        <h5>{message}</h5>
      </ModalContent>
      <ModalFooter>
        {buttons.map((button, index) => (
          <button
            key={index}
            onClick={() => handleButtonClick(button)}
          >
            {button}
          </button>
        ))}
      </ModalFooter>
    </div>
  );
};

export default Modal;

ModalContent.js

import React from 'react';

const ModalContent = ({ children }) => {
  return <div className="modal-content">{children}</div>;
};

export default ModalContent;

ModalFooter.js

import React from 'react';

const ModalFooter = ({ children }) => {
  return <div className="modal-footer">{children}</div>;
};

export default ModalFooter;

Modal.css

.modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #888;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  width: 50%;
}

.modal-footer {
  background-color: #f9f9f9;
  padding: 10px;
  border-top: 1px solid #888;
  border-radius: 0 0 5px 5px;
}

.modal-footer button {
  background-color: #007bff;
  color: #fff;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

.modal-footer button:hover {
  background-color: #0069d9;
}

.modal-content h5 {
  margin-top: 0;
}

.modal-content p {
  margin-bottom: 20px;
}

/* Media queries for responsive design */

@media (max-width: 768px) {
  .modal-content {
    width: 80%;
  }
}

@media (max-width: 480px) {
  .modal-content {
    width: 100%;
  }
  .modal-footer {
    flex-direction: column;
  }
  .modal-footer button {
    width: 100%;
  }
}

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

Previous30 - Progress Bar for Loading or Task CompletionNext32 - Infinite Scroll for Loading More Content

Last updated 10 months ago