Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/media-srcset-lightbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@simple-photo-gallery/common": minor
"@simple-photo-gallery/theme-modern": minor
"simple-photo-gallery": minor
---

Add media-level `srcset` support for responsive lightbox images.
2 changes: 2 additions & 0 deletions common/src/gallery/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const MediaFileSchema = z.looseObject({
width: z.number(),
height: z.number(),
thumbnail: ThumbnailSchema.optional(),
srcset: z.string().optional(),
lastMediaTimestamp: z.string().optional(),
});

Expand All @@ -33,6 +34,7 @@ export const MediaFileDeprecatedSchema = z.looseObject({
width: z.number(),
height: z.number(),
thumbnail: ThumbnailSchema.optional(),
srcset: z.string().optional(),
lastMediaTimestamp: z.string().optional(),
});

Expand Down
1 change: 1 addition & 0 deletions common/src/theme/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function resolveImage(image: MediaFile, mediaBaseUrl?: string, thumbsBaseUrl?: s
imagePath,
thumbnailPath,
thumbnailSrcSet,
srcset: image.srcset,
thumbnailWidth: image.thumbnail?.width,
thumbnailHeight: image.thumbnail?.height,
blurHash: image.thumbnail?.blurHash,
Expand Down
1 change: 1 addition & 0 deletions common/src/theme/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ResolvedImage {
imagePath: string;
thumbnailPath: string;
thumbnailSrcSet?: string;
srcset?: string;
thumbnailWidth?: number;
thumbnailHeight?: number;
blurHash?: string;
Expand Down
14 changes: 14 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Each item in the `images` array of a section represents a media file (image or v
- `url` - A custom URL for the media file. If provided, this URL will be used directly as the source, regardless of the `mediaBaseUrl` setting or base path configuration. This is useful when you want to host specific images on different CDNs or use external URLs.
- `alt` - A caption/description for the media (supports Markdown formatting)
- `thumbnail` - Thumbnail metadata object with `path`, `pathRetina`, `width`, `height`, optional `blurHash`, and optional `baseUrl`
- `srcset` - Responsive full-size image candidates used by themes for the lightbox, formatted like a standard image `srcset` value
- `lastMediaTimestamp` - Timestamp metadata (automatically generated)

### URL resolution
Expand Down Expand Up @@ -115,6 +116,19 @@ If `mediaBaseUrl` is set to `"https://cdn.example.com/images"`, the final URL wi

The final URL will always be `"https://special-cdn.example.com/custom-path/photo-001.jpg"`, regardless of the `mediaBaseUrl` setting.

**Example with responsive full-size candidates:**

```json
{
"type": "image",
"filename": "photo-001.jpg",
"url": "https://cdn.example.com/images/photo-001.jpg",
"width": 4000,
"height": 3000,
"srcset": "https://cdn.example.com/images/photo-001-1280.jpg 1280w, https://cdn.example.com/images/photo-001-1920.jpg 1920w"
}
```

### Thumbnail URL resolution

By default, thumbnail URLs are constructed by combining the `thumbsBaseUrl` (if set at the gallery level) with the thumbnail `path` or `pathRetina`. However, you can override this for individual thumbnails by setting a `baseUrl` property directly on the thumbnail object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const captionHtml = parsedCaption ? `<div class="image-caption markdown-content"
class="gallery-section__item"
href={image.imagePath}
data-pswp-src={image.imagePath}
data-pswp-srcset={image.srcset}
data-pswp-width={image.width}
data-pswp-height={image.height}
data-pswp-type={image.type}
Expand Down
35 changes: 35 additions & 0 deletions gallery/tests/lightbox-srcset.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { GalleryDataSchema } from '../../common/src/gallery';
import { resolveGalleryData } from '../../common/src/theme';

describe('lightbox responsive images', () => {
test('validates and resolves media srcset data', async () => {
const srcset = 'https://cdn.example/photo-1280.avif 1280w, https://cdn.example/photo-1920.avif 1920w';
const galleryData = GalleryDataSchema.parse({
title: 'Gallery',
description: '',
headerImage: 'hero.jpg',
metadata: {},
subGalleries: {
title: '',
galleries: [],
},
sections: [
{
images: [
{
type: 'image',
filename: 'photo.jpg',
width: 4000,
height: 3000,
srcset,
},
],
},
],
});

const resolved = await resolveGalleryData(galleryData);

expect(resolved.sections[0].images[0].srcset).toBe(srcset);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const captionHtml = parsedCaption ? `<div class="image-caption markdown-content"
class="gallery-section__item"
href={image.imagePath}
data-pswp-src={image.imagePath}
data-pswp-srcset={image.srcset}
data-pswp-width={image.width}
data-pswp-height={image.height}
data-pswp-type={image.type}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const captionHtml = parsedCaption ? `<div class="image-caption markdown-content"
class="gallery-section__item"
href={image.imagePath}
data-pswp-src={image.imagePath}
data-pswp-srcset={image.srcset}
data-pswp-width={image.width}
data-pswp-height={image.height}
data-pswp-type={image.type}
Expand Down
Loading