Photoswiper
An plugin for easy and accessible PhotoSwipe initialization.
Usage
Photoswiper comes with three versions: a Node.js version, an ES module version, and a standalone version that can be used in the browser. All versions will automatically set up event listeners to open the PhotoSwipe instance on click.
Node
- Import Photoswiper
- Instantiate Photoswiper with a gallery element
const Photoswiper = ; const myGallery = document;
ES Module
- Add the script to your document
- Import it and instantiate Photoswiper
<!-- ...other head stuff... --> <!-- ...body... -->
Main.js:
; const myGallery = document;
Browser
- Add the script to your document
- Instantiate Photoswiper with a gallery element
<!-- ...other head stuff... --> <!-- ...body... -->
Main.js:
"use strict" var myGallery = document;
API
A few helpful hooks are available on the Photoswiper instance.
#disable()
Disable the event listeners so that the PhotoSwipe instance isn't triggered on click.
#enable()
Enable the event listeners so that the PhotoSwipe instance is triggered on click. Note that this is automatically set during instantiation. You only need to call it if you've explicitly disabled the instance.
#toggle()
Switch between enabled/disabled.
#open(index: number, triggerEl: HTMLElement, fromUrl: boolean = false)
Open the PhotoSwipe instance. This is what is called on click.
For example, you can use this to open the PhotoSwipe instance when clicking another element:
// this assumes a button with aria-controls:// <button id="some-button" aria-controls="id-of-pswp-item">...</button>// and a gallery item with a data-pid corresponding to its index in the gallery:// <figure id="id-of-pswp-item" data-pid="2">...</figure> const triggerBtn = document;triggerBtn { const related = document; const i = ; myGallery;}
Options
Photoswiper provides a few extra options in addition to all the PhotoSwipe options.
Option | Type | Default |
---|---|---|
bemRoot |
string |
undefined |
onInit |
function |
noop |
onOpen |
function |
noop |
PhotoSwipeUI |
function |
The default PhotoSwipe UI. |
selectors |
object |
See the Selectors documentation. |
bemRoot
Use this to customize your selectors according to BEM BEM conventions. For instance, passing myImages
would cause the figure element selector to become .myImages__figure
instead of .pswp-gallery__figure
.
onInit(figures: NodeList)
This is called at the end of Photoswiper construction, but before the PhotoSwipe gallery has been opened. Note that PhotoSwipe has an onInit hook that is called when the PhotoSwipe instance opens.
const myGallery = galleryEl { // do something with the list of figures, such as setting data-pid indices figures; };
onOpen(pswp: PhotoSwipe instance)
This is called when PhotoSwipe opens. It corresponds to PhotoSwipe's own onInit
hook.
const myGallery = galleryEl { // do something with the photoswipe instance console; };
photoswipeUI
Specify your PhotoSwipe ui function here. Defaults to the default PhotoSwipe UI.
Selectors
NOTE: this was renamed from "Structure" in v3.0
Photoswiper defaults to semantic element selectors, but all selectors can be explicitly overriden. Optionally use BEM by supplying a bemRoot
class name in the options (e.g. { bemRoot: 'pswp-gallery' }
).
Elements include:
Option Name | Default | BEM Output | Description |
---|---|---|---|
GALLERY | figure |
.${bemRoot} |
The root gallery element. |
TITLE | figcaption |
.${bemRoot}__title |
The title of the whole gallery. There can only be one of these per gallery, and it must be a direct child of the GALLERY. |
FIGURE | figure |
.${bemRoot}__figure |
The figure container |
LINK | a |
.${bemRoot}__link |
The anchor element that contains the full resolution image. |
THUMB | img |
.${bemRoot}__thumbnail |
The img thumbnail. |
CAPTION | figcaption |
.${bemRoot}__caption |
Captions for each image. |
PSWP | .pswp |
N/A | The PhotoSwipe element. |
Example Markup
This example assumes a bemRoot
of "pswp-gallery":
const myGallery = galleryEl bemRoot: 'pswp-gallery' ;
Kittens Kittens are tiny cats. <!-- ...other content... --> <!-- http://photoswipe.com/documentation/getting-started.html#init-add-pswp-to-dom -->...