From 0efa39b8254bfe9cc69aaefc86d601cb8877b52c Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Thu, 11 Apr 2024 17:45:19 +0800 Subject: [PATCH] Sometimes it returns a preview image without dimenions --- src/components/media.jsx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/components/media.jsx b/src/components/media.jsx index 17aa466..6694413 100644 --- a/src/components/media.jsx +++ b/src/components/media.jsx @@ -511,6 +511,22 @@ function Media({ height={height} data-orientation={orientation} loading="lazy" + onLoad={(e) => { + if (!hasDimensions) { + const $media = e.target.closest('.media'); + if ($media) { + $media.dataset.orientation = + e.target.naturalWidth > e.target.naturalHeight + ? 'landscape' + : 'portrait'; + $media.style['--width'] = `${e.target.naturalWidth}px`; + $media.style[ + '--height' + ] = `${e.target.naturalHeight}px`; + $media.style.aspectRatio = `${e.target.naturalWidth}/${e.target.naturalHeight}`; + } + } + }} /> ) : (