3.0.1 • Published 5 months ago

react-photoswipe-gallery v3.0.1

Weekly downloads
298
License
MIT
Repository
github
Last release
5 months ago

react-photoswipe-gallery

codecov npm

A configurable and flexible React component wrapper around PhotoSwipe.

Basic Usage

import 'photoswipe/dist/photoswipe.css'
import 'photoswipe/dist/default-skin/default-skin.css'

import { Gallery, Item } from 'react-photoswipe-gallery'

const MyGallery = () => (
  <Gallery>
    <Item
      original="https://placekitten.com/1024/768?image=1"
      thumbnail="https://placekitten.com/80/60?image=1"
      width="1024"
      height="768"
    >
      {({ ref, open }) => (
        <img ref={ref} onClick={open} src="https://placekitten.com/80/60?image=1" />
      )}
    </Item>
    <Item
      original="https://placekitten.com/1024/768?image=2"
      thumbnail="https://placekitten.com/80/60?image=2"
      width="1024"
      height="768"
    >
      {({ ref, open }) => (
        <img ref={ref} onClick={open} src="https://placekitten.com/80/60?image=2" />
      )}
    </Item>
  </Gallery>
)

<Gallery /> component ships with default PhotoSwipeUI and Layout. Such a setup is suitable for most cases. If you want more control, jump to advanced usage example.

Demo

Check out storybook with source code examples.

Installation

yarn add photoswipe react-photoswipe-gallery

or

npm install photoswipe react-photoswipe-gallery --save

Advanced Usage

If you want to customize PhotoSwipe layout or use your PhotoSwipe UI, you should take <CustomGallery /> component.

We also provide configurable <DefaultLayout />. It's suitable for most cases, and provide props for configuring all captions and removing unneeded UI elements.

Also, if you have more than one gallery instance in your view, we recommend reusing <Layout /> between several <CustomGallery />.

import PhotoswipeUIDefault from 'photoswipe/dist/photoswipe-ui-default'
import { CustomGallery, Item, DefaultLayout } from 'react-photoswipe-gallery'

const MyGallery = () => {
  const layoutRef = useRef()

  return (
    <CustomGallery layoutRef={layoutRef} ui={PhotoswipeUIDefault}>
      {/*...*/}
    </CustomGallery>

    <CustomGallery layoutRef={layoutRef} ui={PhotoswipeUIDefault}>
      {/*...*/}
    </CustomGallery>

    <DefaultLayout
      shareButton={false}
      fullscreenButton={false}
      zoomButton={false}
      ref={layoutRef}
    />
  )
}

Hash Navigation

You should pass a unique id prop to <Gallery /> or <CustomGallery /> component, to enable hash navigation.

Optionally, you can also pass the id to <Item /> component. Otherwise, the index will be used.

const MyGallery = () => {
  <Gallery id="my-gallery">
    <Item
      id="first-pic"
      {/*...*/}
    />
    <Item
      id="second-pic"
      {/*...*/}
    />
  </Gallery>
}

Props

Gallery

You can pass any of DefaultLayout props to Gallery.

PropTypeRequiredDescription
idNumber or String✓ (for hash navigation)Item ID, for hash navigation
optionsObjectPhotoSwipe options
onOpenFunctionTriggers after PhotoSwipe.init() call. Use it for accessing PhotoSwipe API. It will receive PhotoSwipe instance as the first argument: (photoswipe: PhotoSwipe) => void

Item

Should be children of the Gallery.

PropTypeRequiredDescription
childrenFunctionRender prop for exposing Gallery API
originalStringUrl of original image
thumbnailStringUrl of thumbnail
widthNumber or StringWidth of original image
heightNumber or StringHeight of original image
titleStringTitle for Default UI
htmlStringHtml content, if you need to use it as modal
idNumber or StringItem ID, for hash navigation

Note about Item's children render prop.

Item accepts only function as children.

type RenderItem = (props: {
  /**
   * Required `ref` object to any html node of item
   *
   * Can be omitted if there is only one item in the gallery
   */
  ref: React.MutableRefObject;

  /**
   * Function that opens the gallery at the current item's index
   */
  open: () => void;
}) => JSX.Element

<Item>
  {({ ref, open }) => (
    <img ref={ref} onClick={open} />
  ) as RenderItem}
</Item>

<Item>
  {({ ref, open }) => (
    <span ref={ref}>Open gallery</span>
  ) as RenderItem}
</Item>

CustomGallery

PropTypeRequiredDescription
layoutRefReact.MutableRefObjectRef to your layout element
uiPhotoSwipeUIPhotoSwipe UI class
idNumber or String✓ (for hash navigation)Item ID, for hash navigation
optionsObjectPhotoSwipe options
onOpenFunctionTriggers after PhotoSwipe.init() call. Use it for accessing PhotoSwipe API. It will receive PhotoSwipe instance as the first argument: (photoswipe: PhotoSwipe) => void

DefaultLayout

All props are optional.

PropTypeDefaultDescription
closeButtonCaptionObject'Close (Esc)'.pswp__button--close caption
shareButtonCaptionObject'Share'.pswp__button--share caption
toggleFullscreenButtonCaptionObject'Toggle fullscreen'.pswp__button--fs caption
zoomButtonCaptionObject'Zoom in/out'.pswp__button--zoom caption
prevButtonCaptionObject'Previous (arrow left)'.pswp__button--arrow--left caption
nextButtonCaptionObject'Next (arrow right)'.pswp__button--arrow--right caption
shareButtonBooleantrueShow .pswp__button--share
fullscreenButtonBooleantrueShow .pswp__button--fs
zoomButtonBooleantrueShow .pswp__button--zoom

Hooks

useGallery

The useGallery hook returns an object with some useful methods.

PropertyTypeDescription
open(index: number) => voidThis function allows programmatically open Photoswipe UI at index

Requirements

Development

yarn storybook

or

yarn start

License

MIT

3.0.1

5 months ago

3.0.0-alpha.0

6 months ago

3.0.0

5 months ago

2.2.3

1 year ago

2.2.5

1 year ago

2.2.4

1 year ago

2.2.7

1 year ago

2.2.6

1 year ago

2.2.2

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

1.3.7

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

1.3.10

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

2.0.0-alpha.2

3 years ago

2.0.0-alpha.0

3 years ago

2.0.0-alpha.1

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.3.4

3 years ago

1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-rc.3

4 years ago

1.0.0-rc.4

4 years ago

1.0.0-rc.2

4 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago