diff --git a/projects/plugins/jetpack/changelog/fix-jetpack-slideshow-aspectRatio-fallback b/projects/plugins/jetpack/changelog/fix-jetpack-slideshow-aspectRatio-fallback new file mode 100644 index 000000000000..a1362228d50b --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-jetpack-slideshow-aspectRatio-fallback @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +Slideshow: Ensure slideshows do not stretch to parent container width on mobile if no list height is set. diff --git a/projects/plugins/jetpack/extensions/blocks/slideshow/swiper-callbacks.js b/projects/plugins/jetpack/extensions/blocks/slideshow/swiper-callbacks.js index 9245b5ed89d4..49cff5102a0b 100644 --- a/projects/plugins/jetpack/extensions/blocks/slideshow/swiper-callbacks.js +++ b/projects/plugins/jetpack/extensions/blocks/slideshow/swiper-callbacks.js @@ -47,7 +47,8 @@ function swiperResize( swiper ) { if ( ! img ) { return; } - const aspectRatio = img.naturalWidth / img.naturalHeight; + const hasNatural = img.naturalWidth > 0 && img.naturalHeight > 0; + const aspectRatio = hasNatural ? img.naturalWidth / img.naturalHeight : SIXTEEN_BY_NINE; const sanityAspectRatio = Math.max( Math.min( aspectRatio, SIXTEEN_BY_NINE ), 1 ); const sanityHeight = typeof window !== 'undefined'