Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-grid-gallery

benhowell92.6kMIT1.0.1TypeScript support: included

Justified gallery component for React.

react, react-component, image, images, photo, photos, gallery, select, selectable, justified, tags, tagging, EXIF, image gallery, photo gallery

readme

React Grid Gallery

Justified image gallery component for React inspired by Google Photos.

:tada: v1.0.0 is out!

There are breaking changes with v0.5.x, check out the migration guide to learn more. Documentation for v0.5.x is here.

Live Demo & Examples

https://benhowell.github.io/react-grid-gallery/

Installation

Using npm:

npm install --save react-grid-gallery

Quick Start

import { Gallery } from "react-grid-gallery";

const images = [
   {
      src: "https://c2.staticflickr.com/9/8817/28973449265_07e3aa5d2e_b.jpg",
      width: 320,
      height: 174,
      isSelected: true,
      caption: "After Rain (Jeshu John - designerspics.com)",
   },
   {
      src: "https://c2.staticflickr.com/9/8356/28897120681_3b2c0f43e0_b.jpg",
      width: 320,
      height: 212,
      tags: [
         { value: "Ocean", title: "Ocean" },
         { value: "People", title: "People" },
      ],
      alt: "Boats (Jeshu John - designerspics.com)",
   },
   {
      src: "https://c4.staticflickr.com/9/8887/28897124891_98c4fdd82b_b.jpg",
      width: 320,
      height: 212,
   },
];

<Gallery images={images} />

Image Options

Property Type Description
src string Required. A string referring to any valid image resource (file, url, etc).
width number Required. Width of the image.
height number Required. Height of the image.
nano string:base64 Optional. Thumbnail Base64 image will be injected to background under the main image. This provides a base64, 4x4 generated image whilst the image is being loaded.
alt string Optional. Image alt attribute.
tags array Optional. An array of objects containing tag attributes (value, title and key if value is element). e.g. {value: "foo", title: "bar"} or {value: <a href={tag.url}>{tag.name}</a>, title: tag.title, key: tag.key}
isSelected bool Optional. The selected state of the image.
caption string | ReactNode Optional. Image caption.
customOverlay element Optional. A custom element to be rendered as a thumbnail overlay on hover.
thumbnailCaption string | ReactNode Optional. A thumbnail caption shown below thumbnail.
orientation number Optional. Orientation of the image. Many newer digital cameras (both dSLR and Point & Shoot digicams) have a built-in orientation sensor. The output of this sensor is used to set the EXIF orientation flag in the image file's metatdata to reflect the positioning of the camera with respect to the ground (See EXIF Orientation Page for more info).
Property Type Description
images array Required. An array of objects containing image properties (see Image Options above).
id string Optional, default ReactGridGallery. id attribute for <Gallery> tag. This prop may be useful for those who wish to discriminate between multiple galleries.
enableImageSelection bool Optional, default true. Allow images to be selectable. Setting this option to false whilst supplying images with isSelected: true will result in those images being permanently selected.
onSelect func Optional. Function to execute when an image is selected. Optional args: index (index of selected image in images array), image (the selected image), event. This function is only executable when enableImageSelection: true.
rowHeight number Optional, default 180. The height of each row in the gallery.
maxRows number Optional. The maximum number of rows to show in the gallery.
margin number Optional, default 2. The margin around each image in the gallery.
onClick func Optional. Function to execute when gallery image clicked. Optional args: index (index of selected image in images array), image (the clicked image), event (the click event).
tagStyle func | CSSProperties Optional. Style or function that returns style to pass to tag elements. Optional args: item (the image item in images). Overrides internal tag style.
tileViewportStyle func | CSSProperties Optional. Style or function to style the image tile viewport. Optional args: item (the image item in images). Overrides internal tileViewportStyle function.
thumbnailStyle func | CSSProperties Optional. Style or function to style the image thumbnail. Optional args: item (the image item in images). Overrides internal thumbnailStyle function.
thumbnailImageComponent React component Optional. Substitute in a React component that would get passed imageProps (the props that would have been passed to the <img> tag) and item (the original item in images) to be used to render thumbnails; useful for lazy loading.
defaultContainerWidth number Optional. Set default width for the container. This option is useful during server-side rendering when we want to generate an initial markup before we can detect the actual container width.

General Notes

  • react-grid-gallery is built for modern browsers and therefore IE support is limited to IE 11 and newer.

  • As the inspiration for this component comes from Google Photos, very small thumbnails may not be the most aesthetically pleasing due to the border size applied when selected. A sensible rowHeight default of 180px has been chosen, but rowHeights down to 100px are still reasonable.

  • Gallery width is determined by the containing element. Therefore your containing element must have a width (%, em, px, whatever) before the gallery is loaded!

  • If you don't know your width and height values, you can find these out using any number of javascript hacks, bearing in mind the load penalty associated with these methods.

Contributing

All contributions to react-grid-gallery are very welcome. Feature requests, issue reports and pull requests are greatly appreciated. Please follow the contribution guidelines

License

React Grid Gallery is free to use for personal and commercial projects under the MIT License. Attribution is not required, but appreciated.

Acknowledgements

  • ScottMRafferty for preloadNextImage not propagating to Lightbox fix PR 78

  • Approximator for currentImageWillChange (Function to execute before lightbox image change) PR 97.

  • Vadimuz for nano image props and functionality PR 101.

  • pxpeterxu for adding functionality to inject a custom thumbnail image component (for lazy-loading) PR 104.

  • lryta for fixing crash when this.props.images.length - 1 < this.state.currentImage PR #111.

  • jimishf for lightBoxProps option to assign any prop directly to lightbox PR #121.

  • kym6464 for replacing deprecated defaultProps and for clearing of rollup cache on build PR #298

changelog

CHANGELOG

The most recent changelog is available on the releases page.

v0.5.6 / 2022-08-23

  • Fixed grid breakdown when container width is decimal #170, closes #84
  • TypeScript declaration file added #173, closes #145
  • Added defaultContainerWidth option for SSR rendering support #175

v0.5.5 / 2019-06-20

  • Added new lightBoxProps option to assign any prop directly to lightbox PR #121. Thanks jimishf.

v0.5.4 / 2019-03-10

  • CSS class names are prefixed with ReactGridGallery_
  • Add way to inject a custom thumbnail image component (for lazy-loading) PR 104. Thanks pxpeterxu.
  • Fix crash when this.props.images.length - 1 < this.state.currentImage PR #111. Thanks lryta.

v0.5.3 / 2018-09-30

  • Added nano prop and functionality for base64 4x4 image placeholders PR 101. Thanks Vadimuz.

v0.5.2 / 2018-09-15

  • Added currentImageWillChange PR 97. Function to execute before lightbox image change. Useful for tracking current image shown in lightbox. Thanks Approximator.

v0.5.1 / 2018-06-29

  • Moved prop-types dependency from dev dependencies to dependencies

v0.5.0 / 2018-06-26

v0.4.11 / 2018-04-29

v0.4.10 / 2018-04-29

v0.4.9 / 2018-04-27

  • Added optional alt tag to image props (defaults to empty string).

v0.4.8 / 2018-01-20

  • Added image rotation/transformation functionality based upon EXIF orientation passed in the image orientation prop PR 67. Thanks mis94.

v0.4.7 / 2017-11-20

  • Added className to custom overlay.

v0.4.6 / 2017-10-02

  • Added vendor specific prefixes to userSelect styling.

v0.4.5 / 2017-10-02

v0.4.4 / 2017-09-29

  • Added optional thumbnailCaption functionality PR 42. Thanks jakub-tucek.

  • Updated acknowledgements.

  • Updated documentation.

v0.4.3 / 2017-09-15

  • Fixed resize event not triggering on scroll bar presence change PR 40. Thanks SimeonC.

  • Updated acknowledgements.

v0.4.2 / 2017-07-23

  • Added optional id prop for the id attribute of the <Gallery> tag.

  • Added className attribute for the <Gallery> tag.

  • Updated documentation.

v0.4.1 / 2017-07-20

  • Fixed maxRows not updating bug PR 35. Thanks SimeonC.

  • Updated documentation.

v0.4.0 / 2017-06-29

v0.3.7 / 2017-06-26

  • Added optional tagStyle prop to style thumbnail tags.

v0.3.6 / 2017-06-07

  • Added optional lightbox prop showLightboxThumbnails to display thumbnails beneath the Lightbox image.

  • Added optional lightbox prop onClickLightboxThumbnail as a fn to execute when lightbox thumbnail clicked. Overrides internal function: gotoImage.

v0.3.5 / 2017-06-04

  • Refactored for react 16 (moved from PropTypes to prop-types package).

  • Bumped react-images to 0.5.4 which has been refactored for react 16.

  • Added theme pass-though prop PR 27. Thanks danalloway.

  • Updated acknowledgements.

v0.3.4 / 2017-05-05

  • Bumped to react 15.5.4

  • Refactored to use new prop-types package (React.PropTyes deprecated).

v0.3.3 / 2017-04-22

  • Added customOverlay functionality PR 22. Thanks ValYouW.

  • Added demo to project page for customOverlay.

  • Updated documentation

  • Updated acknowledgements

v0.3.2 / 2017-04-07

v0.3.1 / 2017-04-04

  • Added lightboxWillOpen and lightBoxWillClose functionality PR 20. Thanks ValYouW.

  • Updated documentation for onClickThumbnail fn issue #19

  • Updated acknowledgements

v0.3.0 / 2017-01-14

  • Fixed bug where lightboxWidth does not exceed 1024px

  • Bumped react-images to 0.5.2

v0.2.10 / 2017-01-13

  • Fixed bug in passing lightboxWidth prop

v0.2.9 / 2016-12-21

  • Added prop to set maximum width of lightbox. Defaults to 1024px.

v0.2.8 / 2016-12-10

  • Updated documentation including correction of onSelectImage prop documentation.

  • More descriptive package keywords.

  • Grammatical tweaks.

v0.2.7 / 2016-11-04

Breaking Changes

  • Consistent naming scheme implemented both internally and externally. External breaking change to the onImageSelected prop which has been renamed onSelectImage. All internal instances of Func refactored to Fn. handleClickImage refactored to onClickImage. handleResize refactored to onResize.

The following changes in v0.2.7 allow react-grid-gallery to be used in an (optionally) stateless way.

  • Added optional prop onClickImage. This prop takes a function and is triggered when a lightbox displayed image is clicked. Supplying this prop will override the default onClickImage function.

  • Added optional prop onClickPrev. This prop takes a function and is triggered when the left arrow in lightbox is clicked. Supplying this prop will override the default onClickPrev function.

  • Added optional prop onClickNext. This prop takes a function and is triggered when the right arrow in lightbox is clicked. Supplying this prop will override the default onClickNext function.

  • Added explicit closeLightbox function to the lightbox onClose prop.

v0.2.6 / 2016-10-25

v0.2.5 / 2016-09-26

  • Added image tagging functionality. Optional tags prop takes an array of objects containing tag attributes. value prop is the text shown on the tag and title prop is the text shown when hovering over the tag. e.g. tags: [{value: "Ocean", title: "Ocean"}, {value: "People", title: "People"}]

v0.2.4 / 2016-09-17

  • onImageSelected prop function now takes two optional args, index (index of selected image in images array) and image (the selected image object).

v0.2.3 / 2016-09-16

Breaking changes

  • Image selection state now handled within image object by optional boolean prop isSelected. This greatly reduces complexity both within and outside the component as the image itself carries it's selected state. Therefore selectedImages prop has been removed.

  • onSelectedImagesChange prop removed due to the changes outlined above.

  • Optional onImageSelected prop added. This prop takes a function and an optional image object as a parameter.

  • isSelected removed as first class prop on Image (now a prop on the image item passed in)

  • Image onToggleSelected renamed to onImageSelected.

v0.2.2 / 2016-09-11

  • Fixes bug on small edge case whereby duplicate images causes an error (two children cannot have the same key) and subsequently only the first of any repeated image src can be rendered.

v0.2.1 / 2016-09-11

  • Fixes Bug where updating an image caused wrong aspect due to thumb not resizing. Bug caused by using array index as react key rather than something unique to the image. Thanks to cust0dian for the pull request which fixes this issue by assigning src attribute as key.

  • Fixes bug where only thumbnails are updated when images props changes, meaning re-render doesn't happen until window is resized. Thanks again to cust0dian for the pull request which fixes this issue.

v0.2.0 / 2016-09-03

  • Construction of thumbnail images and image rows removed from render. Thumbnails and rows now only rebuilt when container size changes.

  • selectedImages state now set via props change.

  • onSelectedImagesChange callback now called directly from onToggleSelected. Previously, a combination of setting selectedImages state and triggering onSelectedImagesChange inside componentWillUpdate caused a double render.

  • Internal image access now via state instead of props.

  • Thumbnail generation now atomic function rather than whole array at once.


v0.1.14 / 2016-08-22

  • selectedImages state set on componentWillReceiveProps allowing selections from outside component to trigger state update.

v0.1.13 / 2016-08-22

  • Replaced legacy ref string with ref callback. Fixes multiple react owner issue when using react-grid-gallery inside a reagent project :)

v0.1.12 / 2016-08-22

  • Replaced ReactDOM.findDOMNode(this) with ref, removed react-dom deps
  • Added conditional to ensure image onClick not fired when no function specified
  • Moved CheckButton styling (color, hoverColor, selectedColor) to props

v0.1.11 / 2016-08-21

  • Fixed react-dom typo

v0.1.10 / 2016-08-21

  • Added option to allow disabling of lightbox image display. enableLightbox (PropType.bool, default true)

  • Added option to allow passing in of function to execute on thumbnail click. onClickThumbnail (PropType.func, default openLightbox)

v0.1.9 / 2016-08-19

  • Removed darkening effect on thumbnail hover when enableImageSelection: false

v0.1.8 / 2016-08-17

  • Handful of code samples and demos added to project page.
  • PropType bugs fixed on Gallery and Image

v0.1.7 / 2016-08-16

  • Gulp task ensenble to clean/build/deploy lib, web (gh-pages) and hacked up cljs js lib
  • Project page with examples, docs etc.
  • Updated options documentation

v0.1.6 / 2016-08-15

v0.1.5 / 2016-08-13

  • Removed commentary and dead code
  • Replaced simple functions with anonymous inline functions

v0.1.4 / 2016-08-13

  • Added support for disabling image selection (optional)
  • Updated options documentation

v0.1.3 / 2016-08-13

  • Added support for disabling image selection (optional)
  • Updated options documentation

v0.1.2 / 2016-08-13

  • Added support for onSelectedImagesChange function (optional)
  • Updated options documentation

v0.1.1 / 2016-08-11

  • Added support for all functional lightbox options
  • Updated README with options documentation

v0.1.0 / 2016-08-11

  • Simplified thumbnail viewport
  • Fixed aspect bug on shrinkage effect on thumbnail selection

v0.0.4 / 2016-08-10

  • Shrinkage effect on thumbnail selection

v0.0.3 / 2016-08-09

  • Darkening effect on thumbnail hover (increases visibility of check button)
  • Pointer cursor on thumbnail hover

v0.0.2 / 2016-08-08

  • Full lightbox functionality provided by react-images by @jossmac
  • Auto scaled, clipped and justified images to fit rowHeight prop
  • Image selection and gallery level reference to list of selected images