PhotoSwipe JavaScript Image Gallery
PhotoSwipe v5.4.4 is a highly customizable JavaScript image gallery and lightbox designed for touch devices. It supports common features like swiping, zooming, and pan gestures, and includes core and lightbox components for flexible integration. The library maintains an active release cadence, frequently publishing patch and minor versions to address bugs, enhance accessibility, and improve browser compatibility. Key differentiators include robust touch support, a modular architecture allowing for dynamic import of its core, extensive customization options via filters and options, and strong focus on accessibility (ARIA attributes, focus trapping) and offline functionality. It ships with TypeScript types, facilitating modern development workflows. It offers a significant rewrite compared to its v4 predecessor, focusing on modern web standards and modularity.
Common errors
-
Uncaught TypeError: PhotoSwipeLightbox is not a constructor
cause Attempting to use `PhotoSwipeLightbox` before it's properly imported or available in the global scope (e.g., CommonJS `require` in an ESM context, incorrect script tag order, or using the wrong import path).fixEnsure you are using `import PhotoSwipeLightbox from 'photoswipe/lightbox';` for ESM setups or loading the correct script files in the browser. Verify your bundler configuration if applicable. -
Uncaught SyntaxError: Unexpected token '??' or 'Uncaught SyntaxError: Unexpected token '.'
cause Running PhotoSwipe v5.0.0-v5.3.9 in an older browser environment that does not support ES2020 features like nullish coalescing (??) or optional chaining (?).fixUpgrade to PhotoSwipe v5.4.0 or newer, which includes fixes for these syntax issues. Alternatively, ensure your build pipeline transpiles modern JavaScript to an ES version supported by your target browsers. -
Gallery not opening or images not loading after click
cause Incorrect `gallery` or `children` selectors in `PhotoSwipeLightbox` initialization, or `pswpModule` callback not correctly returning the core PhotoSwipe module.fixDouble-check that the `gallery` and `children` selectors accurately match your HTML structure. Ensure the `pswpModule` option is correctly set up as a dynamic import: `pswpModule: () => import('photoswipe')`.
Warnings
- breaking PhotoSwipe v5 introduces a complete rewrite with a new API and modular structure, making it incompatible with v4. Projects migrating from v4 will require significant code changes.
- gotcha Early versions of PhotoSwipe v5 (pre-5.4.0) used modern JavaScript syntax like nullish coalescing (??) and optional chaining (?), which caused `SyntaxError`s in older browsers that do not support ES2020 features.
- gotcha Accessibility features like ARIA attributes and focus trapping were incrementally improved. Prior to v5.3.4, the gallery might have lacked full ARIA compliance or proper focus management for keyboard navigation.
- gotcha A regression in v5.3.5 caused the gallery to lazy-load the full image from `src` instead of correctly utilizing `srcset` for responsive image loading, potentially leading to increased bandwidth usage.
- deprecated The `dataSource` parameter for `PhotoSwipeLightbox.loadAndOpen` was made optional in v5.4.2. In previous versions, omitting it would lead to an error or unexpected behavior, as it was a required argument.
Install
-
npm install photoswipe -
yarn add photoswipe -
pnpm add photoswipe
Imports
- PhotoSwipeLightbox
const PhotoSwipeLightbox = require('photoswipe/lightbox');import PhotoSwipeLightbox from 'photoswipe/lightbox';
- PhotoSwipe
const PhotoSwipe = require('photoswipe');import PhotoSwipe from 'photoswipe';
- photoswipe.css
import 'photoswipe/photoswipe.css';
- PhotoSwipeOptions
import type { PhotoSwipeOptions } from 'photoswipe';
Quickstart
import PhotoSwipeLightbox from 'photoswipe/lightbox';
import 'photoswipe/photoswipe.css';
// 1. Define your gallery items in HTML with data attributes
// <div id="my-gallery">
// <a href="full-image-1.jpg" data-pswp-width="1200" data-pswp-height="800" target="_blank">
// <img src="thumbnail-1.jpg" alt="Image 1">
// </a>
// <a href="full-image-2.jpg" data-pswp-width="1000" data-pswp-height="1500" target="_blank">
// <img src="thumbnail-2.jpg" alt="Image 2">
// </a>
// </div>
// 2. Initialize the PhotoSwipe Lightbox
const lightbox = new PhotoSwipeLightbox({
gallery: '#my-gallery', // Selector for the gallery container
children: 'a', // Selector for individual gallery items
pswpModule: () => import('photoswipe'), // Dynamically import the core module
// Optional: Set options for the core PhotoSwipe instance
// easing: 'cubic-bezier(.175, .885, .32, 1.275)',
// bgOpacity: 0.9,
});
// 3. Initialize the lightbox
lightbox.init();
// To open programmatically:
// lightbox.loadAndOpen(0); // Opens the first item