1.0.4 • Published 4 months ago

react-custom-lightbox v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

ReactImageVideoLightbox

ReactImageVideoLightbox is a React component that displays a lightbox to view images and videos. It supports image navigation, video autoplay, and thumbnail previews. This component is useful for creating a gallery or media viewer within your React application.

Installation

To install the component, run the following command:

npm install react-custom-lightbox

or

yarn add react-custom-lightbox

Dependencies

  • React 16.8+ (for hooks support)

Component Props

Prop NameTypeDefault ValueDescription
startIndexnumber0The index of the first media item (image/video) to be displayed when the lightbox opens.
dataarray[]An array of objects representing the media resources (images/videos). Each object should have a type ('photo' or 'video') and url (media URL), and optionally altTag.
showResourceCountbooleanfalseIf true, displays the current image/video index and total count (e.g., '1 / 5').
onCloseCallbackfunctionnullCallback function to be called when the lightbox is closed.
onNavigationCallbackfunctionnullCallback function to be called when navigating (next/previous) between items.
showThumbnailsbooleanfalseIf true, shows thumbnails of all media items for navigation.
enableAutoPlaybooleanfalseIf true, automatically starts video playback when the video is displayed.
imageWidthnumber100%Maximum width for image resources.
imageHeightnumber100%Maximum height for image resources.
videoWidthnumber100%Width of the video player.
videoHeightnumber100%Height of the video player.
customCloseIconReactNodenullCustom close icon to replace the default close button.
customLeftArrowIconReactNodenullCustom left arrow icon for navigation.
customRightArrowIconReactNodenullCustom right arrow icon for navigation.
showCloseButtonbooleantrueIf true, displays a close icon in the lightbox; if false, the close icon is hidden.
showIndicatorsbooleantrueIf false, hides both bullet points and thumbnails from the UI.
activeBulletColorstringwhiteDefines the color of the active bullet point.
bulletColorstringgrayDefines the color of inactive bullet points.

Example Usage

import React from "react";
import ReactImageVideoLightbox from "react-custom-lightbox";

const mediaData = [
  { type: "photo", url: "https://example.com/image1.jpg", altTag: "Image 1" },
  { type: "video", url: "https://example.com/video1.mp4" },
  { type: "photo", url: "https://example.com/image2.jpg", altTag: "Image 2" },
];

const YourComponent = () => {
  const handleClose = () => {
    console.log("Lightbox closed");
  };

  const handleNavigation = (index) => {
    console.log(`Navigated to item at index ${index}`);
  };

  return (
    <ReactImageVideoLightbox
      startIndex={0}
      data={mediaData}
      showResourceCount={true}
      onCloseCallback={handleClose}
      onNavigationCallback={handleNavigation}
      showThumbnails={true}
      enableAutoPlay={true}
      imageWidth={800}
      imageHeight={600}
      videoWidth={800}
      videoHeight={450}
      customCloseIcon={<svg>...</svg>} // Optional custom close icon
      customLeftArrowIcon={<svg>...</svg>} // Optional custom left arrow icon
      customRightArrowIcon={<svg>...</svg>} // Optional custom right arrow icon
      showCloseButton={true}
      activeBulletColor="white"
      bulletColor="gray"
      showIndicators={true}
    />
  );
};

export default YourComponent;

Features

  • Image and Video Display: Displays images and videos based on the data prop.
  • Navigation: Users can navigate through media items using left and right arrows.
  • Thumbnails: Optionally shows a scrollable row of thumbnails.
  • Loading Spinner: A spinner is shown while the media is loading.
  • Escape Key Close: Pressing the Escape key closes the lightbox.
  • AutoPlay: Videos can be auto-played by setting enableAutoPlay to true.

Notes

  • Ensure the data array contains at least one media item of type 'photo' or 'video'.
  • The video elements respect the autoPlay behavior based on the enableAutoPlay prop.
  • Thumbnails are optional and can be toggled with the showThumbnails prop.
  • The onNavigationCallback is called every time the user navigates to a different media item.

License

MIT License. See LICENSE file for details.

1.0.4

4 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago