Skip to content

Commit aa6d338

Browse files
committed
Preview images gallery
1 parent 0589943 commit aa6d338

File tree

2 files changed

+66
-14
lines changed

2 files changed

+66
-14
lines changed
Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,70 @@
11
---
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";
54
65
interface Props {
7-
images?: any;
6+
images?: any[];
87
}
98
109
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+
}
1129
---
1230

13-
{images && images.length > 0 && (
31+
{images?.length > 0 && (
1432
<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>
2044
))}
2145
</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+

web/src/pages/reference/[func].astro

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
100100
<!-- Description -->
101101
<ItemDescription description={funcInfo.description} incomplete={funcInfo.incomplete} />
102102

103-
<!-- Preview Images -->
104-
<PreviewImages images={funcInfo.preview_images} />
105-
106103
<!-- Notes -->
107104
{notesContent.length > 0 && (
108105
<div class="notes-section">
@@ -208,6 +205,13 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
208205

209206
<CodeExamplesSection codeExamples={funcExamples}, examplesRequired={true} theItem={func} />
210207

208+
<!-- Preview Images -->
209+
<div id="preview-images-gallery">
210+
<h4>Gallery</h4>
211+
<p>Click on the image to view the full preview along with the description.</p>
212+
<PreviewImages images={funcInfo.preview_images} />
213+
</div>
214+
211215
<ChangelogList entries={changelogEntries} />
212216

213217
<SeeAlsoSection seeAlsoLinks={getSeeAlsoLinksForItem(func)} currentId={func.id} />

0 commit comments

Comments
 (0)