How to Add A React Lightbox to Your Project or Website (in Just 3 Steps)
Tags: react, javascript, webdev, tutorial
Published: 2022-09-28 13:35:46

Want to add a React lightbox to your project? You've come to the right place!

In this guide, we're going to be taking a look at how to add a lightbox to your React project in just 3 simple steps. 

This is great if you want to include a lightbox in your:

- eCommerce store
- blog
- portfolio or personal website
- and so much more

Let's get started!

#### Demo
Here's a demo of what we'll be creating:


![A demo of Lightbox.js with an image carousel featuring keyboard images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lue6qbl7upq6sspa0u8k.gif)

Pretty cool, right? And what's even better is that it can be set-up in a matter of minutes.

## Step 1: Install Lightbox.js
Firstly, install [Lightbox.js](https://www.getlightboxjs.com), which is a fully responsive React lightbox that has a great set of features, perfect for desktop and mobile devices. 

Features include:

- Image zooming
- Mobile zoom
- Thumbnails
- Slideshow
- Theming and customization options

We'll be taking a look at these features below also, just to show some of the extended functionality provided with the lightbox.

To install the lightbox, simply open your terminal of choice and run:

```bash
npm install lightbox.js-react --save
```

Once the package is installed, it's time to get started with implementing it!

## Step 2: Initialization
Open the file of the React component where you wish to integrate the lightbox and add the following imports:

```js
import React, {useEffect} from "react";
import 'lightbox.js-react/dist/index.css'
import {SlideshowLightbox, initLightboxJS} from 'lightbox.js-react'
```

### Licensing:
Lightbox.js is free if you're using it for an open source project. Otherwise, a commercial license can be purchased, which can be done so on the [pricing page here](https://www.getlightboxjs.com/pricing/).

Readers of this tutorial can also avail of an exclusive 35% off discount, using the code **OFFER35**.

- **Open source project**: Contact Lightbox.js to get a free license key through the [contact form here](https://www.getlightboxjs.com/contact/).
- **Commercial project**: Purchase a commercial license on the [official website here](https://www.getlightboxjs.com/pricing/). Use the exclusive code OFFER35 to get a discount too!

If you're an individual developer, the Individual commercial license is recommended, while a Team & Company plan can be purchased for teams or companies wishing to use the library. 

Once you have your license key, pass it to the `initLightboxJS` function you imported previously, along with the name of the plan your license uses. The two plan types are `team` and `individual`.

```js
useEffect(() => {
  initLightboxJS("Insert License key", "Insert plan type here");
});
```

## Step 3: Adding the Lightbox
Next, wrap the images you wish to include in a `SlideshowLightbox` component, as shown in the example snippet below.

Add the following to your JSX:

```html
<SlideshowLightbox theme="day" showThumbnails={true} className="images" roundedImages={true}> 
    <img alt='Mechanical keyboard with white keycaps.' src={"https://source.unsplash.com/sQZ_A17cufs/549x711"} />
    <img alt="Mechanical keyboard with white, pastel green and red keycaps."  src={"https://source.unsplash.com/rsAeSMzOX9Y/768x512"} />  
    <img alt="Mechanical keyboard with white, pastel pink, yellow and red keycaps."  src={"https://source.unsplash.com/Z6SXt1v5tP8/768x512"} />
    <img alt="Mechanical keyboard with white keycaps." src={"https://source.unsplash.com/2WcghjtPodU/549x711"} />       
                                    
</SlideshowLightbox>
```

While no extra CSS is required to style the images in the lightbox itself, if you'd like to style the initial images displayed outside (the images which are clicked to display the lightbox), you could use flexbox so that the images are positioned in a row. This step is optional however, as it may depend on your use case and how you'd like the images to be displayed and so forth.

Add the following CSS:

```css

.images {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
}

.images img {
    flex-basis: 33%;
    padding: 2px;
}

```

## The Result
If you've followed the steps above, you should now have a lightbox that displays whenever any of the images are clicked! Here's a demo:

![A demo of Lightbox.js with an image carousel featuring keyboard images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lue6qbl7upq6sspa0u8k.gif)

### Full Example
```jsx
import React, {useEffect} from "react";
import { SlideshowLightbox, initLightboxJS } from "lightbox.js-react";
import "lightbox.js-react/dist/index.css";

export function Demo() {
  useEffect(() => {
    initLightboxJS("Insert License key", "Insert plan type here");
  });

  return (
    <SlideshowLightbox
      theme="day"
      showThumbnails={true}
      className="images"
      roundedImages={true}
    >
      <img
        alt="Mechanical keyboard with white keycaps."
        src={"https://source.unsplash.com/sQZ_A17cufs/549x711"}
      />
      <img
        alt="Mechanical keyboard with white, pastel green and red keycaps."
        src={"https://source.unsplash.com/rsAeSMzOX9Y/768x512"}
      />
      <img
        alt="Mechanical keyboard with white, pastel pink, yellow and red keycaps."
        src={"https://source.unsplash.com/Z6SXt1v5tP8/768x512"}
      />
      <img
        alt="Mechanical keyboard with white keycaps."
        src={"https://source.unsplash.com/2WcghjtPodU/549x711"}
      />
    </SlideshowLightbox>
  );
}
```

### Bonus Step: Theming & Customization
This step is optional, but if you'd like to update the lightbox's theme, this is possible through passing a value to the `theme` prop. The three themes available are:

- **day**: A white background with gray icons
- **night**: A dark background with white icons
- **lightbox**: A semi-transparent gray background with gray icons

For further customization, you can also adjust the background color of the lightbox by passing a color to the `backgroundColor` prop as an RGB, RGBA, HEX code, or CSS color name value:

```js
<SlideshowLightbox backgroundColor="gray">
...
</SlideshowLightbox>
```

Here's an example with the `lightbox` theme:

![Keyboard with white keycaps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svznuwsgvu06nq9ea6e2.gif)

### Exploring More Demos
If you'd like to see more examples and demos of Lightbox.js in action, be sure to take a look at our demos page, which lists lots of example demos to get you started.

**[Online café shop](https://www.getlightboxjs.com/demo/ProductListingDemo)**


![Photos displayed in a React image carousel of macarons and cakes ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qz20qeuch7mdcy3ysdrd.gif)


**[Photo showcase](https://www.getlightboxjs.com/demo/PhotoShowcaseDemo)**

![Lightbox with nature imagery, including those of a desert and mountains](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrzf6jqxko1p6ilbtd1p.gif)

## Conclusion
Thanks very much for reading this tutorial, I hope you've found it useful! If you have any questions or comments, be sure to let me know below! 

If you have any issues with setting up the lightbox or need any other assistance, don't hesitate to let me know or use the [contact form provided on the official website](https://www.getlightboxjs.com/contact/), where all support queries are answered. 

Thanks again, and wishing you the very best!