1
1
---
2
- import { Image } from ' astro:assets' ;
3
- import { getAssetImagePath } from ' @src/utils/general' ;
4
-
2
+ import { Image } from " astro:assets" ;
3
+ import { getAssetImagePath } from " @src/utils/general" ;
5
4
6
5
interface Props {
7
- images? : any ;
6
+ images? : any [] ;
8
7
}
9
8
10
9
const { images } = Astro .props ;
10
+ const maxDimension = 256 ; // max size of preview image (256x256)
11
+
12
+ if (images ?.length ) {
13
+ await Promise .all (
14
+ images .map (async (imageInfo ) => {
15
+ const { default : resolvedPath } = await getAssetImagePath (imageInfo .path );
16
+ const { width : originalWidth, height : originalHeight, src } = resolvedPath ;
17
+
18
+ const scale = Math .min (maxDimension / originalWidth , maxDimension / originalHeight , 1 );
19
+
20
+ Object .assign (imageInfo , {
21
+ resolvedPath ,
22
+ src ,
23
+ width: Math .round (originalWidth * scale ),
24
+ height: Math .round (originalHeight * scale ),
25
+ });
26
+ }),
27
+ );
28
+ }
11
29
---
12
30
13
- { images && images .length > 0 && (
31
+ { images ? .length > 0 && (
14
32
<div class = " preview-images" >
15
- { images .map ((imageInfo : any ) => (
16
- <Image width = { imageInfo .width || undefined }
17
- height = { imageInfo .height || undefined }
18
- src = { getAssetImagePath (imageInfo .path )}
19
- alt = { imageInfo .description || " Preview" } />
33
+ { images .map (({ src , description , resolvedPath , width , height }) => (
34
+ <a href = { src } class = " glightbox" data-description = { description } data-gallery = " gallery" >
35
+ <Image
36
+ width = { width }
37
+ height = { height }
38
+ src = { resolvedPath }
39
+ alt = { description || " Preview" }
40
+ loading = " lazy"
41
+ decoding = " async"
42
+ />
43
+ </a >
20
44
))}
21
45
</div >
22
- )}
46
+ )}
47
+
48
+ <link rel =" stylesheet" href =" https://cdn.jsdelivr.net/npm/glightbox/dist/css/glightbox.min.css" />
49
+
50
+ <style is:global >
51
+ .gslide-description {
52
+ background: var(--sl-color-bg-nav) !important;
53
+ }
54
+
55
+ .gslide-desc {
56
+ color: var(--sl-color-text) !important;
57
+ }
58
+ </style >
59
+
60
+ <script is:client =" load" type =" module" >
61
+ import "https://cdn.jsdelivr.net/gh/mcstudios/glightbox/dist/js/glightbox.min.js";
62
+
63
+ const lightbox = GLightbox({ selector: ".glightbox" });
64
+
65
+ // Fix aria-hidden console warning
66
+ lightbox.on("open", () => {
67
+ document.querySelector(".glightbox-container")?.setAttribute("tabindex", "-1").focus();
68
+ });
69
+ </script >
70
+
0 commit comments