🌐
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

13 - Dynamic Image Gallery with Lightbox

Description

Create a React application that displays a dynamic image gallery with a lightbox feature. The gallery should display a grid of image thumbnails, and when a thumbnail is clicked, the corresponding image should be displayed in a lightbox.

Algorithm

  1. Initialize state variables to store the images data and the currently selected image.

  2. Render a grid of image thumbnails using a mapping function.

  3. Define a function to handle image selection and update the selected image state.

  4. Render a lightbox component with the selected image.

  5. Define a function to handle lightbox closing and update the selected image state.

Classes

  • ImageGallery: A component that manages the images state and renders the gallery and lightbox.

  • Lightbox: A component that displays the selected image in a full-screen view.

Code

ImageGallery.js

import React, { useState } from 'react';
import Lightbox from './Lightbox';

function ImageGallery() {
  const [images, setImages] = useState([
    { id: 1, url: 'image1.jpg' },
    { id: 2, url: 'image2.jpg' },
    { id: 3, url: 'image3.jpg' },
  ]);
  const [selectedImage, setSelectedImage] = useState(null);

  const handleImageSelect = (image) => {
    setSelectedImage(image);
  };

  const handleLightboxClose = () => {
    setSelectedImage(null);
  };

  return (
    <div>
      <ul>
        {images.map((image) => (
          <li key={image.id}>
            <img
              src={image.url}
              alt={image.url}
              onClick={() => handleImageSelect(image)}
            />
          </li>
        ))}
      </ul>
      {selectedImage && (
        <Lightbox
          image={selectedImage}
          onClose={handleLightboxClose}
        />
      )}
    </div>
  );
}

export default ImageGallery;

Lightbox.js

import React from 'react';

function Lightbox({ image, onClose }) {
  return (
    <div className="lightbox">
      <img src={image.url} alt={image.url} />
      <button onClick={onClose}>Close</button>
    </div>
  );
}

export default Lightbox;

index.css

.lightbox {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
}

.lightbox button {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #fff;
  border: none;
  padding: 10px;
  cursor: pointer;
}

Explanation

The ImageGallery component initializes the images state and renders a grid of thumbnails. When a thumbnail is clicked, it updates the selected image state and renders the Lightbox component. The Lightbox component displays the selected image in a full-screen view and provides a close button to update the selected image state.

Possible Future Enhancements

  • Add image captions and descriptions.

  • Implement image zoom and pan functionality.

  • Add a loading indicator for images.

  • Integrate with a backend service to fetch images data.

  • Display image metadata (e.g., camera model, aperture).

Previous12 - Unit ConverterNext14 - Weather Widget with Temperature and Description

Last updated 10 months ago