Skip to content

Commit 3e42b15

Browse files
committed
react-grid-gallery mostly works
1 parent 4ded8e8 commit 3e42b15

File tree

7 files changed

+66
-70
lines changed

7 files changed

+66
-70
lines changed

src/components/Gallery.astro

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
import { Image } from 'astro:assets';
2+
import { getImage } from 'astro:assets';
33
44
import ReactGallery from '@/components/react/Gallery';
55
import { IMAGE_SIZES } from '@/constants/image';
66
import { cn } from '@/utils/styles';
77
8-
import type { AstroImageProps, ImageProps } from '@/types/common';
8+
import type { ImageProps } from '@/types/common';
99
import type { ImageMetadata } from 'astro';
1010
1111
export interface Props extends astroHTML.JSX.HTMLAttributes {}
@@ -31,31 +31,52 @@ const getAllImagesMetadata = (): ImageMetadata[] => {
3131
3232
const imagesMetadata = getAllImagesMetadata();
3333
34-
const imageMetadataToAstroImageProps = (imagesMetadata: ImageMetadata): AstroImageProps => ({
35-
src: imagesMetadata,
36-
...IMAGE_SIZES.FIXED.MDX_XXS_16_9,
37-
alt: 'Gallery image',
38-
});
34+
const imageMetadataToReactImageProps = async (
35+
imagesMetadata: ImageMetadata
36+
): Promise<ImageProps> => {
37+
const astroImageProps = {
38+
src: imagesMetadata,
39+
format: 'webp',
40+
};
41+
42+
const thumbnailAstroImageProps = {
43+
...astroImageProps,
44+
...IMAGE_SIZES.FIXED.MDX_XXS_16_9,
45+
alt: 'Thumbnail image',
46+
};
47+
48+
const lightboxAstroImageProps = {
49+
...astroImageProps,
50+
...IMAGE_SIZES.FIXED.MDX_XL_16_9,
51+
alt: 'Lightbox image',
52+
};
53+
54+
const optimizedThumbnailAstroImageProps = await getImage(thumbnailAstroImageProps);
55+
const { src, attributes } = optimizedThumbnailAstroImageProps;
3956
40-
const astroImagePropsToReactImageProps = (astroImageProps: AstroImageProps): ImageProps => {
41-
const astroImageSrc = astroImageProps.src as ImageMetadata;
57+
const optimizedLightboxAstroImageProps = await getImage(lightboxAstroImageProps);
58+
const { src: originalSrc } = optimizedLightboxAstroImageProps;
4259
43-
return {
44-
src: astroImageSrc.src,
45-
originalSrc: astroImageSrc.src,
46-
width: parseInt(String(astroImageProps.width)),
47-
height: parseInt(String(astroImageProps.height)),
60+
const reactImageProps = {
61+
src,
62+
originalSrc,
63+
// width and height only for thumbnails
64+
width: parseInt(String(attributes.width)),
65+
height: parseInt(String(attributes.height)),
4866
};
67+
68+
return reactImageProps;
4969
};
5070
51-
const astroImages = imagesMetadata.map((metadata) => imageMetadataToAstroImageProps(metadata));
52-
const reactImages = astroImages.map((astroProps) => astroImagePropsToReactImageProps(astroProps));
71+
const reactImages = await Promise.all(
72+
imagesMetadata.map((metadata) => imageMetadataToReactImageProps(metadata))
73+
);
74+
75+
// console.log('reactImages', reactImages);
5376
5477
const { class: className } = Astro.props;
5578
---
5679

5780
<div class={cn('', className)}>
58-
<ReactGallery client:only="react" images={reactImages}>
59-
<Image {...IMAGE_SIZES.FIXED.MDX_XXS_16_9} src="https://google.com" alt="default" />
60-
</ReactGallery>
81+
<ReactGallery client:only="react" images={reactImages} />
6182
</div>

src/components/react/Gallery.tsx

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
import { cloneElement, useState } from 'react';
1+
import { useState } from 'react';
22

33
import { Gallery as ReactGridGallery } from 'react-grid-gallery';
44
import Lightbox from 'react-image-lightbox';
55

66
import type { ImageProps } from '@/types/common';
7-
import type { ReactElement } from 'react';
8-
import type { ThumbnailImageComponentImageProps } from 'react-grid-gallery';
97

108
import 'react-image-lightbox/style.css';
119

12-
import ThumbnailImage from '@/components/react/ThumbnailImage';
13-
1410
interface Props {
15-
children?: ReactElement;
1611
images: ImageProps[];
1712
}
1813

19-
const Gallery: React.FC<Props> = ({ images, children: thumbnailImageComponent }) => {
14+
// global polyfill for react-image-lightbox
15+
window.global = window.global || window;
16+
17+
const Gallery: React.FC<Props> = ({ images }) => {
2018
const [index, setIndex] = useState(-1);
2119

2220
const currentImage = images[index];
@@ -30,31 +28,9 @@ const Gallery: React.FC<Props> = ({ images, children: thumbnailImageComponent })
3028
const handleMovePrev = () => setIndex(prevIndex);
3129
const handleMoveNext = () => setIndex(nextIndex);
3230

33-
console.log('currentImage', currentImage);
34-
3531
return (
3632
<div>
37-
<ReactGridGallery
38-
images={images}
39-
thumbnailImageComponent={(props) => {
40-
const imageProps: ThumbnailImageComponentImageProps = props.imageProps;
41-
42-
// key, src, alt, title, style
43-
const { key: _, ...astroImageProps } = imageProps;
44-
45-
console.log('imageProps', imageProps);
46-
47-
return (
48-
<ThumbnailImage {...props}>
49-
{cloneElement(thumbnailImageComponent ?? <></>, {
50-
...astroImageProps,
51-
})}
52-
</ThumbnailImage>
53-
);
54-
}}
55-
onClick={handleClick}
56-
enableImageSelection={false}
57-
/>
33+
<ReactGridGallery images={images} onClick={handleClick} enableImageSelection={false} />
5834
{!!currentImage && (
5935
<Lightbox
6036
imageTitle={currentImage.caption}

src/components/react/ThumbnailImage.tsx

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/layouts/Centered.astro

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
---
22
import Base from '@/layouts/Base.astro';
3+
import { cn } from '@/utils/styles';
34
45
import type { BaseProps } from '@/layouts/Base.astro';
56
6-
export interface Props extends BaseProps {}
7+
export interface Props extends BaseProps, astroHTML.JSX.HTMLAttributes {}
78
89
const props = Astro.props;
10+
11+
// targets <main /> tag
12+
const { class: className } = Astro.props;
913
---
1014

1115
{/* flex here to take full height for pagination, flex-row align-stretch */}
1216
{/* h-full dosent work without all parents h-full */}
1317

1418
<Base {...props}>
15-
<main id="main" class="flex-grow flex flex-col centered-px max-w-4xl py-4 lg:py-8">
19+
<main
20+
id="main"
21+
class={cn('flex-grow flex flex-col centered-px max-w-4xl py-4 lg:py-8', className)}
22+
>
1623
<slot />
1724
</main>
1825
</Base>

src/layouts/Page.astro

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
---
22
import Centered from '@/layouts/Centered.astro';
33
import { getOpenGraphImagePath } from '@/libs/api/open-graph/image-path';
4+
import { cn } from '@/utils/styles';
45
56
export interface Content {
67
title: string;
78
description: string;
89
readingTime: number;
910
file: string;
1011
url: string;
12+
/** set max-w-xxx */
13+
class: string;
1114
}
1215
1316
export interface Props {
@@ -16,7 +19,7 @@ export interface Props {
1619
1720
// metadata from frontmatter
1821
const { content } = Astro.props;
19-
const { title, description } = content;
22+
const { title, description, class: className } = content;
2023
2124
// available in Layouts
2225
const { pathname } = Astro.url;
@@ -28,7 +31,7 @@ const image = getOpenGraphImagePath(path);
2831
const metadata = { title, description, image };
2932
---
3033

31-
<Centered {metadata}>
34+
<Centered {metadata} class={cn(className)}>
3235
<article class="my-prose">
3336
<slot />
3437
</article>

src/pages/gallery.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
layout: '../layouts/Page.astro'
33
title: 'Gallery'
44
description: 'Image gallery'
5+
class: 'max-w-5xl'
56
---
67

78
import Gallery from '../components/Gallery.astro';
89

10+
# Gallery
11+
912
<Gallery class="not-prose" />

src/utils/navigation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export const getActiveNavItemPath = (routePathname: string): NavigationItem['pat
1515
case routePathname === ROUTES.ABOUT:
1616
activeNavItem = getNavItem(ROUTES.ABOUT);
1717
break;
18+
case routePathname === ROUTES.GALLERY:
19+
activeNavItem = getNavItem(ROUTES.GALLERY);
20+
break;
1821
// unused
1922
case routePathname === ROUTES.RESUME:
2023
activeNavItem = getNavItem(ROUTES.RESUME);

0 commit comments

Comments
 (0)