File size: 10,869 Bytes
e903a32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
---
// @ts-ignore - types provided by Astro at runtime
import Image from "./Image.astro";

interface ImageItem {
    /** Source image imported via astro:assets */
    src: any;
    /** Alt text for accessibility */
    alt: string;
    /** Individual caption for this image */
    caption?: string;
    /** Optional individual image ID for referencing */
    id?: string;
    /** Enable zoom on this specific image (defaults to parent zoomable setting) */
    zoomable?: boolean;
    /** Enable download on this specific image (defaults to parent downloadable setting) */
    downloadable?: boolean;
}

interface Props {
    /** Array of images to display */
    images: ImageItem[];
    /** Global caption for the entire figure */
    caption?: string;
    /** Layout mode: number of columns or 'auto' for responsive */
    layout?: "2-column" | "3-column" | "4-column" | "auto";
    /** Enable medium-zoom behavior on all images (can be overridden per image) */
    zoomable?: boolean;
    /** Show download buttons on all images (can be overridden per image) */
    downloadable?: boolean;
    /** Optional class to apply on the wrapper */
    class?: string;
    /** Optional global ID for the multi-image figure */
    id?: string;
}

const {
    images,
    caption,
    layout = "3-column",
    zoomable = false,
    downloadable = false,
    class: className,
    id,
} = Astro.props as Props;

const hasCaptionSlot = Astro.slots.has("caption");
const hasCaption =
    hasCaptionSlot || (typeof caption === "string" && caption.length > 0);
const uid = `mi_${Math.random().toString(36).slice(2)}`;

// Generate CSS grid columns based on layout
const getGridColumns = () => {
    switch (layout) {
        case "2-column":
            return "repeat(2, 1fr)";
        case "3-column":
            return "repeat(3, 1fr)";
        case "4-column":
            return "repeat(4, 1fr)";
        case "auto":
            return "repeat(auto-fit, minmax(200px, 1fr))";
        default:
            return "repeat(3, 1fr)";
    }
};

const gridColumns = getGridColumns();
---

<div
    class={`multi-image ${className || ""}`}
    data-mi-root={uid}
    data-layout={layout}
    {id}
>
    {
        hasCaption ? (
            <figure class="multi-image-figure">
                <div
                    class="multi-image-grid"
                    style={`grid-template-columns: ${gridColumns}`}
                >
                    {images.map((image, index) => (
                        <div class="multi-image-item">
                            <Image
                                src={image.src}
                                alt={image.alt}
                                zoomable={image.zoomable ?? zoomable}
                                downloadable={
                                    image.downloadable ?? downloadable
                                }
                                class="multi-image-img"
                            />
                            {image.caption && (
                                <div class="multi-image-subcaption">
                                    {image.caption}
                                </div>
                            )}
                            {image.id && (
                                <span
                                    id={image.id}
                                    style="position: absolute;"
                                />
                            )}
                        </div>
                    ))}
                </div>
                <figcaption class="multi-image-caption">
                    {hasCaptionSlot ? (
                        <slot name="caption" />
                    ) : (
                        caption && <span set:html={caption} />
                    )}
                </figcaption>
            </figure>
        ) : (
            <div
                class="multi-image-grid"
                style={`grid-template-columns: ${gridColumns}`}
            >
                {images.map((image, index) => (
                    <div class="multi-image-item">
                        <Image
                            src={image.src}
                            alt={image.alt}
                            zoomable={image.zoomable ?? zoomable}
                            downloadable={image.downloadable ?? downloadable}
                            class="multi-image-img"
                        />
                        {image.caption && (
                            <div class="multi-image-subcaption">
                                {image.caption}
                            </div>
                        )}
                        {image.id && (
                            <span id={image.id} style="position: absolute;" />
                        )}
                    </div>
                ))}
            </div>
        )
    }
</div>

<style>
    .multi-image {
        margin: var(--block-spacing-y) 0;
    }

    .multi-image-figure {
        margin: 0;
    }

    .multi-image-grid {
        display: grid;
        gap: 1rem;
        align-items: start;
    }

    .multi-image-item {
        display: flex;
        flex-direction: column;
        text-align: center;
        position: relative;
        z-index: var(--z-content);
        transition: z-index 0.3s ease;
    }

    /* Quand medium-zoom est actif, masquer temporairement les autres images du multi-image */
    :global(.medium-zoom--opened) .multi-image-item {
        opacity: 0;
        z-index: calc(var(--z-base) - 1);
        transition:
            opacity 0.3s ease,
            z-index 0.3s ease;
    }

    /* Specifically hide captions during zoom - radical approach */
    :global(.medium-zoom--opened) .multi-image-subcaption {
        display: none !important;
    }

    /* Completely hide all multi-image elements during zoom */
    :global(.medium-zoom--opened) .multi-image {
        z-index: -1 !important;
    }

    /* Hide all texts from all multi-images */
    :global(.medium-zoom--opened) .multi-image-caption {
        display: none !important;
    }

    /* The currently zoomed image remains visible */
    :global(.medium-zoom--opened)
        .multi-image-item:has(:global(.medium-zoom--opened)) {
        opacity: 1;
        z-index: var(--z-overlay);
    }

    /* Fallback for browsers without :has() support */
    :global(.medium-zoom--opened) .multi-image-item.zoom-active {
        opacity: 1 !important;
        z-index: var(--z-overlay) !important;
    }

    /* Garder la caption de l'image active visible */
    :global(.medium-zoom--opened)
        .multi-image-item.zoom-active
        .multi-image-subcaption {
        opacity: 1 !important;
        z-index: var(--z-overlay) !important;
    }

    .multi-image-item :global(.ri-root) {
        margin: 0;
    }

    .multi-image-item :global(figure) {
        margin: 0;
    }

    .multi-image-img {
        width: 100%;
        height: auto;
        object-fit: contain;
    }

    .multi-image-subcaption {
        font-size: 0.85rem;
        color: var(--muted-color);
        margin-top: 0.5rem;
        line-height: 1.4;
    }

    .multi-image-caption {
        text-align: left;
        font-size: 0.9rem;
        color: var(--muted-color);
        margin-top: 1rem;
        line-height: 1.4;
    }

    /* Responsive behavior */
    @media (max-width: 768px) {
        .multi-image-grid[style*="repeat(3, 1fr)"],
        .multi-image-grid[style*="repeat(4, 1fr)"] {
            grid-template-columns: 1fr !important;
            gap: 1.5rem;
        }

        .multi-image-grid[style*="repeat(2, 1fr)"] {
            grid-template-columns: 1fr !important;
            gap: 1.5rem;
        }
    }

    @media (min-width: 769px) and (max-width: 1024px) {
        .multi-image-grid[style*="repeat(4, 1fr)"] {
            grid-template-columns: repeat(2, 1fr) !important;
        }
    }

    /* Images maintain natural aspect ratio */
    .multi-image[data-layout*="column"] .multi-image-item :global(img) {
        height: auto;
        object-fit: contain;
    }

    /* Auto layout gets flexible heights */
    .multi-image[data-layout="auto"] .multi-image-item :global(img) {
        height: auto;
    }

    /* Ensure images maintain aspect ratio */
    .multi-image-item :global(img) {
        max-width: 100%;
        display: block;
        margin: 0 auto;
    }
</style>

<script>
    // Enhanced medium-zoom integration for MultiFigure
    document.addEventListener("DOMContentLoaded", () => {
        // Improve MultiFigure behavior with medium-zoom
        const multiImages = document.querySelectorAll(".multi-image");

        multiImages.forEach((multiImage) => {
            const items = multiImage.querySelectorAll(".multi-image-item");
            const zoomableImages = multiImage.querySelectorAll(
                'img[data-zoomable="1"]',
            );

            zoomableImages.forEach((img) => {
                img.addEventListener("click", () => {
                    // Find the parent item of the clicked image and the ri-root
                    const activeItem = img.closest(".multi-image-item");
                    const riRoot = img.closest(".ri-root");

                    // Nettoyer TOUS les zoom-active (MultiFigure items et Figure)
                    document
                        .querySelectorAll(
                            ".multi-image-item.zoom-active, .ri-root.zoom-active",
                        )
                        .forEach((el) => el.classList.remove("zoom-active"));

                    // Add zoom-active to active elements
                    if (activeItem) {
                        activeItem.classList.add("zoom-active");
                    }
                    if (riRoot) {
                        riRoot.classList.add("zoom-active");
                    }
                });
            });
        });

        // Nettoyer TOUTES les classes lors de la fermeture du zoom
        document.addEventListener("click", (e) => {
            if (e.target.classList.contains("medium-zoom-overlay")) {
                // Zoom closed, clean up all zoom-active classes
                document
                    .querySelectorAll(
                        ".multi-image-item.zoom-active, .ri-root.zoom-active",
                    )
                    .forEach((item) => item.classList.remove("zoom-active"));
            }
        });

        // Listen for keyboard events to close zoom
        document.addEventListener("keydown", (e) => {
            if (e.key === "Escape") {
                document
                    .querySelectorAll(
                        ".multi-image-item.zoom-active, .ri-root.zoom-active",
                    )
                    .forEach((item) => item.classList.remove("zoom-active"));
            }
        });
    });
</script>