docs/getting-started.md
PhotoSwipe v6 is under development Please leave feedback in GitHub Discussion.
Before you start:
The PhotoSwipe consists of three parts:
photoswipe.esm.js).photoswipe-lightbox.esm.js) - loads Core and chooses when PhotoSwipe should be opened. Its file size is significantly smaller. It also loads the first image (in parallel with Core).photoswipe.css) - a single file that controls all the styling. There are no external assets for icons - all of them are dynamically generated via JS and very tiny. Refer to styling for more info.JS files are separated, so you can dynamically load Core only when the user needs it, thus reducing the size of your main bundle.
The recommended way to use PhotoSwipe is using a single <script type="module"></script>, for example:
<script type="module">
import PhotoSwipeLightbox from 'photoswipe/dist/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#my-gallery',
children: 'a',
pswpModule: () => import('photoswipe/dist/photoswipe.esm.js')
});
lightbox.init();
</script>
Don't forget to include the CSS too:
<link rel="stylesheet" href="photoswipe/dist/photoswipe.css">
Alternatively, you may install PhotoSwipe via NPM or Yarn:
npm i photoswipe --save
import PhotoSwipeLightbox from 'photoswipe/lightbox';
import 'photoswipe/style.css';
const lightbox = new PhotoSwipeLightbox({
gallery: '#my-gallery',
children: 'a',
pswpModule: () => import('photoswipe')
});
lightbox.init();
Playgrounds: StackBlitz, CodeSandbox, CodePen (with unpkg).
If you are unable to use ES modules, the transpiled version can be found in dist/umd/ folder of the repository.
import { gettingStartedTemplate } from '@site/src/components/PswpCodePreview/gallery-templates/getting-started.js';
<PswpCodePreview galleryID="getting-started" numItems="6" displayHTML templateFn={gettingStartedTemplate}>// Include Lightbox
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const lightbox = new PhotoSwipeLightbox({
// may select multiple "galleries"
gallery: '#gallery--getting-started',
// Elements within gallery (slides)
children: 'a',
// setup PhotoSwipe Core dynamic import
pswpModule: () => import('/photoswipe/photoswipe.esm.js')
});
lightbox.init();
You don't have to dynamically import pswpModule, especially if PhotoSwipe is one of the primary features of your page.
<PswpCodePreview galleryID="no-dynamic-import">import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
// highlight-next-line
import PhotoSwipe from '/photoswipe/photoswipe.esm.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery--no-dynamic-import',
children: 'a',
// highlight-next-line
pswpModule: PhotoSwipe
});
lightbox.init();
Each element that matches the selector should be or should contain link <a> element. The link must have such attributes:
href or data-pswp-src attribute (latter has higher priority).data-pswp-width.data-pswp-height.And optionally:
thumbSelector).data-cropped="true" attribute if thumbnail is cropped. See also Animating from Cropped Thumbnail.PhotoSwipe API supports almost any markup and any data source, read more about it here.
import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const options = {
// highlight-next-line
gallery: '#gallery--individual a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js')
};
const lightbox = new PhotoSwipeLightbox(options);
lightbox.init();
data-pswp-srcset attribute. It supports the same markup as native srcset.data-pswp-width and data-pswp-height should define the largest image size.sizes attribute will be generated automatically based on actual width of the image. For example, if user zooms-in - sizes will be adjusted to the new zoom level.import { srcsetTemplate } from '@site/src/components/PswpCodePreview/gallery-templates/srcset-test.js';
<PswpCodePreview galleryID="responsive-images" templateFn={srcsetTemplate}>import PhotoSwipeLightbox from '/photoswipe/photoswipe-lightbox.esm.js';
const options = {
gallery: '#gallery--responsive-images',
children: 'a',
pswpModule: () => import('/photoswipe/photoswipe.esm.js')
};
const lightbox = new PhotoSwipeLightbox(options);
lightbox.init();
script type="nomodule".