GithubHelp home page GithubHelp logo

littleboy1988 / react-images Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jossmac/react-images

0.0 1.0 0.0 5.32 MB

A simple lightbox component for displaying an array of images

Home Page: http://jossmac.github.io/react-images

License: MIT License

JavaScript 100.00%

react-images's Introduction

React Images

A simple, responsive lightbox component for displaying an array of images.

Quick start

import React from 'react';
import Lightbox from 'react-images';

export default class Sample extends React.Component {
  ...
  render() {
    return (
      <Lightbox
        images={[{ src: 'http://example.com/img1.jpg' }, { src: 'http://example.com/img2.jpg' }]}
        isOpen={this.state.lightboxIsOpen}
        onClickPrev={this.gotoPrevious}
        onClickNext={this.gotoNext}
        onClose={this.closeLightbox}
      />
    );
  }
}

Demo & Examples

Live demo: jossmac.github.io/react-images

To build the examples locally, run:

npm install
npm start

Then open localhost:8000 in a browser.

Using srcset

Example using srcset:

<Lightbox
  images={LIGHTBOX_IMAGE_SET}
  ...
/>

const LIGHTBOX_IMAGE_SET = [
  {
    src: 'http://example.com/example/img1.jpg',
    srcset: [
      'http://example.com/example/img1_1024.jpg 1024w',
      'http://example.com/example/img1_800.jpg 800w',
      'http://example.com/example/img1_500.jpg 500w',
      'http://example.com/example/img1_320.jpg 320w',
    ],
  },
  {
    src: 'http://example.com/example/img2.jpg',
    srcset: [
      'http://example.com/example/img2_1024.jpg 1024w',
      'http://example.com/example/img2_800.jpg 800w',
      'http://example.com/example/img2_500.jpg 500w',
      'http://example.com/example/img2_320.jpg 320w',
    ],
  }
];

Notes on srcset support:

The srcset attribute is supported by some modern browsers. Results of browser implementation and behaviour may vary. The sizes attribute uses the default maxWidth CSS property set to the image. By default this is 80% so 80vw.

Another thing to note is that 'h' or height in the srcset attribute does not yet exist. Because of the nature of the fixed height of a Lightbox this is problematic for portrait sized images. You will need to calculate what the best 'w' size for a portrait size ought to be given the height of the fixed viewport otherwise unnecessarily large images will be fetched. See issue: ResponsiveImagesCG/picture-element#86

Read more about the srcset and sizes attributes here: https://ericportis.com/posts/2014/srcset-sizes/.

Adding Captions

Example using caption for the first image:

<Lightbox
  images={LIGHTBOX_IMAGE_SET}
  ...
/>

const LIGHTBOX_IMAGE_SET = [
  {
    src: 'http://example.com/example/img1.jpg',
    caption: 'Sydney, Australia - Photo by Jill Smith',
  },
  {
    src: 'http://example.com/example/img2.jpg',
  }
];

Note that the caption is an entirely optional property, as can be seen in the first gallery on the example page. The first image has a single line caption, the second demonstrates multiline, and the remaining images are without captions, entirely.

Options

Property Type Default Description
backdropClosesModal bool false Allow users to exit the lightbox by clicking the backdrop
enableKeyboardInput bool true Supports keyboard input - esc, arrow left, and arrow right
currentImage number 0 The index of the image to display initially
images array undefined Required. An array of objects containing valid src and srcset values of img element
isOpen bool false Whether or not the lightbox is displayed
onClickPrev func undefined Required. Fired on request of the previous image
onClickNext func undefined Required. Fired on request of the next image
onClose func undefined Required. Handle closing of the lightbox
showCloseButton bool true Optionally display a close "X" button in top right corner
showImageCount bool true Optionally display image index, e.g., "3 of 20"
width number 900 Maximum width of the carousel; defaults to 900px

react-images's People

Contributors

jossmac avatar jedwatson avatar ko avatar shmuga avatar neptunian avatar sapphire64 avatar flode avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.