Skip to content

Commit 5e4815e

Browse files
committed
Fix PhotoSwipe captions with official dynamic caption plugin
Root Cause: - PhotoSwipe v5 removed built-in caption support from v4 - Custom captionContent callback (lines 223-232) doesn't exist in v5 API - Captions silently failed because option was ignored Solution: - Install official photoswipe-dynamic-caption-plugin@1.2.7 - Replace broken callback with plugin initialization - Plugin uses v5's ui.registerElement() API internally - Type 'auto' = aside on desktop, below on mobile Technical Changes: - Import plugin + CSS (lines 193-195) - Remove broken captionContent callback - Add plugin init before lightbox.init() (lines 230-236) - Update CSS to target .pswp__dynamic-caption class - Egyptian design system colors applied (!important for plugin override) Testing: - Click any photo in /portfolio/statsbomb gallery - Caption should display with TODO text - Verify dark semi-transparent background - Check mobile (caption below) vs desktop (caption aside) Next Step: Replace 13 TODO captions with personal context from docs/plans/statsbomb-photo-captions-context-needed.md Story Points: 3 (library-first solution, reversible)
1 parent 5d2cd83 commit 5e4815e

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

bun.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"headroom.js": "^0.12.0",
3535
"motion": "^12.23.24",
3636
"photoswipe": "^5.4.4",
37+
"photoswipe-dynamic-caption-plugin": "^1.2.7",
3738
"shiki": "^3.13.0",
3839
"zod": "^4.1.12"
3940
}

src/components/PhotoGallery.astro

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -174,23 +174,28 @@ try {
174174
}
175175
}
176176

177-
/* PhotoSwipe Caption Styling */
178-
:global(.pswp__caption-content) {
179-
padding: var(--spacing-md);
180-
background: rgba(15, 23, 42, 0.9); /* text-text with alpha */
181-
color: var(--color-background);
182-
font-size: 14px;
183-
line-height: 1.5;
184-
text-align: center;
185-
font-style: italic;
186-
border-radius: var(--radius-sm) var(--radius-sm) 0 0;
177+
/* PhotoSwipe Dynamic Caption Plugin - Egyptian Design System Override */
178+
:global(.pswp__dynamic-caption) {
179+
background: rgba(15, 23, 42, 0.9) !important; /* text-text with alpha */
180+
color: var(--color-background) !important;
181+
font-size: 14px !important;
182+
line-height: 1.5 !important;
183+
padding: var(--spacing-md) !important;
184+
font-style: italic !important;
185+
}
186+
187+
/* Ensure caption text is readable */
188+
:global(.pswp__dynamic-caption a) {
189+
color: var(--color-primary) !important;
187190
}
188191
</style>
189192

190193
<script>
191194
import PhotoSwipeLightbox from "photoswipe/lightbox";
192195
import PhotoSwipe from "photoswipe";
196+
import PhotoSwipeDynamicCaption from 'photoswipe-dynamic-caption-plugin';
193197
import "photoswipe/style.css";
198+
import 'photoswipe-dynamic-caption-plugin/photoswipe-dynamic-caption-plugin.css';
194199

195200
// Check for reduced motion preference
196201
function prefersReducedMotion(): boolean {
@@ -217,18 +222,6 @@ try {
217222
children: "a",
218223
pswpModule: PhotoSwipe,
219224

220-
// Enable captions from data-pswp-caption attribute
221-
captionContent: (slide) => {
222-
const caption = slide.data.element?.getAttribute('data-pswp-caption');
223-
if (!caption) return '';
224-
225-
// Create caption element with proper styling
226-
const captionEl = document.createElement('div');
227-
captionEl.className = 'pswp__caption-content';
228-
captionEl.textContent = caption;
229-
return captionEl;
230-
},
231-
232225
// Animation durations (will be overridden by CSS if reduced motion)
233226
showAnimationDuration: prefersReducedMotion() ? 0 : 400,
234227
hideAnimationDuration: prefersReducedMotion() ? 0 : 400,
@@ -237,6 +230,14 @@ try {
237230
closeOnVerticalDrag: true,
238231
});
239232

233+
// Initialize official caption plugin
234+
const captionPlugin = new PhotoSwipeDynamicCaption(lightbox, {
235+
type: 'auto', // 'auto' = aside on desktop, below on mobile
236+
captionContent: (slide) => {
237+
return slide.data.element?.getAttribute('data-pswp-caption') || '';
238+
},
239+
});
240+
240241
lightbox.init();
241242
console.log("[PhotoGallery] Lightbox initialized for:", galleryId);
242243
});

0 commit comments

Comments
 (0)