Socket
Socket
Sign inDemoInstall

lightbox-react

Package Overview
Dependencies
13
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    lightbox-react

Lightbox for React components or images


Version published
Weekly downloads
5.3K
decreased by-6.87%
Maintainers
1
Install size
759 kB
Created
Weekly downloads
 

Readme

Source

React Component & Image Lightbox

NPM

See it in action on Skilled.dev - Coding Interview Course and gitconnected - The Developer Community

A lightbox for React components or images. String passed as arguments are assumed to be the src for an image. Otherwise, it will check if the argument is able to be rended as a child React component of the lightbox.

DEMO

Originally forked from fritz-c's library

Features

  • Keyboard shortcuts (with rate limiting)
  • Image Zoom
  • Flexible rendering using src values assigned on the fly
  • Image preloading for smoother viewing
  • Mobile friendly, with pinch to zoom and swipe (Thanks, @webcarrot!)
  • No external CSS

Example

import React, { Component } from 'react';
import Lightbox from 'lightbox-react';
import 'lightbox-react/style.css'; // This only needs to be imported once in your app

import VideoIframe from 'components/video';

const images = [
  VideoIframe,
  '//placekitten.com/1500/500',
  '//placekitten.com/4000/3000',
  '//placekitten.com/800/1200',
  '//placekitten.com/1500/1500',
];

export default class LightboxExample extends Component {
  constructor(props) {
    super(props);

    this.state = {
      photoIndex: 0,
      isOpen: false,
    };
  }

  render() {
    const { photoIndex, isOpen } = this.state;

    return (
      <div>
        <button type="button" onClick={() => this.setState({ isOpen: true })}>
          Open Lightbox
        </button>

        {isOpen && (
          <Lightbox
            mainSrc={images[photoIndex]}
            nextSrc={images[(photoIndex + 1) % images.length]}
            prevSrc={images[(photoIndex + images.length - 1) % images.length]}
            onCloseRequest={() => this.setState({ isOpen: false })}
            onMovePrevRequest={() =>
              this.setState({
                photoIndex: (photoIndex + images.length - 1) % images.length,
              })
            }
            onMoveNextRequest={() =>
              this.setState({
                photoIndex: (photoIndex + 1) % images.length,
              })
            }
          />
        )}
      </div>
    );
  }
}

Deprecation Notice

All unprefixed classes (listed below) will be removed in v4.0.0. Use their ril- prefixed alternatives instead. close, closing, download-blocker, image-current, image-next, image-prev, inner, next-button, not-loaded, outer, prev-button, toolbar, toolbar-left, toolbar-right, zoom-in, zoom-out

Options

PropertyTypeDefaultRequiredDescription
mainSrcstringyesMain display image url or React component
prevSrcstringPrevious display image url or component (displayed to the left). If left undefined, onMovePrevRequest will not be called, and the button not displayed
nextSrcstringNext display image url or component (displayed to the right). If left undefined, onMoveNextRequest will not be called, and the button not displayed
mainSrcThumbnailstringThumbnail image url corresponding to props.mainSrc. Displayed as a placeholder while the full-sized image loads.
prevSrcThumbnailstringThumbnail image url corresponding to props.prevSrc. Displayed as a placeholder while the full-sized image loads.
nextSrcThumbnailstringThumbnail image url corresponding to props.nextSrc. Displayed as a placeholder while the full-sized image loads.
onCloseRequestfuncyesClose window event. Should change the parent state such that the lightbox is not rendered
onMovePrevRequestfuncempty functionMove to previous image event. Should change the parent state such that props.prevSrc becomes props.mainSrc, props.mainSrc becomes props.nextSrc, etc.
onMoveNextRequestfuncempty functionMove to next image event. Should change the parent state such that props.nextSrc becomes props.mainSrc, props.mainSrc becomes props.prevSrc, etc.
onImageLoadErrorfuncempty functionCalled when an image fails to load.
<code>(imageSrc: string, srcType: string, errorEvent: object): void</code>
discourageDownloadsboolfalseEnable download discouragement (prevents [right-click -> Save Image As...])
animationDisabledboolfalseDisable all animation
animationOnKeyInputboolfalseDisable animation on actions performed with keyboard shortcuts
animationDurationnumber300Animation duration (ms)
keyRepeatLimitnumber180Required interval of time (ms) between key actions (prevents excessively fast navigation of images)
keyRepeatKeyupBonusnumber40Amount of time (ms) restored after each keyup (makes rapid key presses slightly faster than holding down the key to navigate images)
imageTitlenodeImage title (Descriptive element above image)
imageCaptionnodeImage caption (Descriptive element below image)
toolbarButtonsnode[]Array of custom toolbar buttons
reactModalStyleObject{}Set z-index style, etc., for the parent react-modal (react-modal style format)
imagePaddingnumber10Padding (px) between the edge of the window and the lightbox
clickOutsideToClosebooltrueWhen true, clicks outside of the image close the lightbox
enableZoombooltrueSet to false to disable zoom functionality and hide zoom buttons

Browser Compatibility

BrowserWorks?
ChromeYes
FirefoxYes
SafariYes
IE >= 10Yes
IE 9Everything works, but no animations

Contributing

After cloning the repository and running npm install inside, you can use the following commands to develop and build the project.

# Starts a webpack dev server that hosts a demo page with the lightbox.
# It uses react-hot-loader so changes are reflected on save.
npm start

# Lints the code with eslint and my custom rules.
npm run lint

# Lints and builds the code, placing the result in the dist directory.
# This build is necessary to reflect changes if you're
#  `npm link`-ed to this repository from another local project.
npm run build

Pull requests are welcome!

License

MIT

Keywords

FAQs

Last updated on 05 Aug 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc