react-photoswipe-gallery
React component wrapper around PhotoSwipe.
Basic Usage
<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.
Installation
or
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 />
.
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.
Props
Gallery
You can pass any of
DefaultLayout
props toGallery
.
Prop | Type | Required | Description |
---|---|---|---|
id |
Number or String | ✓ (for hash navigation) | Item ID, for hash navigation |
options |
Object | PhotoSwipe options | |
onOpen |
Function | Triggers 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
.
Prop | Type | Required | Description |
---|---|---|---|
children |
Function | ✓ | Render prop for exposing Gallery API |
original |
String | Url of original image | |
thumbnail |
String | Url of thumbnail | |
width |
Number or String | Width of original image | |
height |
Number or String | Height of original image | |
title |
String | Title for Default UI | |
html |
String | Html content, if you need to use it as modal | |
id |
Number or String | Item ID, for hash navigation |
Note about Item's children
render prop.
Item accepts only function as children.
CustomGallery
Prop | Type | Required | Description |
---|---|---|---|
layoutRef |
React.MutableRefObject<HTMLElement> |
✓ | Ref to your layout element |
ui |
PhotoSwipeUI | ✓ | PhotoSwipe UI class |
id |
Number or String | ✓ (for hash navigation) | Item ID, for hash navigation |
options |
Object | PhotoSwipe options | |
onOpen |
Function | Triggers 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.
Prop | Type | Default | Description |
---|---|---|---|
closeButtonCaption |
Object | 'Close (Esc)' | .pswp__button--close caption |
shareButtonCaption |
Object | 'Share' | .pswp__button--share caption |
toggleFullscreenButtonCaption |
Object | 'Toggle fullscreen' | .pswp__button--fs caption |
zoomButtonCaption |
Object | 'Zoom in/out' | .pswp__button--zoom caption |
prevButtonCaption |
Object | 'Previous (arrow left)' | .pswp__button--arrow--left caption |
nextButtonCaption |
Object | 'Next (arrow right)' | .pswp__button--arrow--right caption |
shareButton |
Boolean | true |
Show .pswp__button--share |
fullscreenButton |
Boolean | true |
Show .pswp__button--fs |
zoomButton |
Boolean | true |
Show .pswp__button--zoom |
Hooks
useGallery
The useGallery
hook returns an object with some useful methods.
Property | Type | Description |
---|---|---|
open |
(index: number) => void | This function allows programmatically open Photoswipe UI at index |
Requirements
Development
then
or