-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from nemanjam/feature/add-gallery-page
Add gallery page
- Loading branch information
Showing
17 changed files
with
363 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
import { Image } from 'astro:assets'; | ||
import ReactGallery from '@/components/react/Gallery'; | ||
import { IMAGE_SIZES } from '@/constants/image'; | ||
import { cn } from '@/utils/styles'; | ||
import type { AstroImageProps, ImageProps } from '@/types/common'; | ||
import type { ImageMetadata } from 'astro'; | ||
export interface Props extends astroHTML.JSX.HTMLAttributes {} | ||
// filenames | ||
const EXCLUDE_IMAGES = ['avatar1.jpg']; | ||
const getAllImagesMetadata = (): ImageMetadata[] => { | ||
const imageModules = import.meta.glob<{ default: ImageMetadata }>( | ||
// cant be even variable | ||
'/src/assets/images/all-images/*.jpg', | ||
{ eager: true } | ||
); | ||
// convert map to array | ||
const imagesMetadata = Object.keys(imageModules) | ||
// filter excluded filenames | ||
.filter((path) => !EXCLUDE_IMAGES.some((excludedFileName) => path.endsWith(excludedFileName))) | ||
// return metadata array | ||
.map((path) => imageModules[path].default); | ||
return imagesMetadata; | ||
}; | ||
const imagesMetadata = getAllImagesMetadata(); | ||
const imageMetadataToAstroImageProps = (imagesMetadata: ImageMetadata): AstroImageProps => ({ | ||
src: imagesMetadata, | ||
...IMAGE_SIZES.FIXED.MDX_XXS_16_9, | ||
alt: 'Gallery image', | ||
}); | ||
const astroImagePropsToReactImageProps = (astroImageProps: AstroImageProps): ImageProps => { | ||
// console.log('astroImageProps', JSON.stringify(astroImageProps, null, 2)); | ||
const astroImageSrc = astroImageProps.src as ImageMetadata; | ||
return { | ||
src: astroImageSrc.src, | ||
originalSrc: astroImageSrc.src, | ||
width: parseInt(String(astroImageProps.width)), | ||
height: parseInt(String(astroImageProps.height)), | ||
}; | ||
}; | ||
const astroImages = imagesMetadata.map((metadata) => imageMetadataToAstroImageProps(metadata)); | ||
const reactImages = astroImages.map((astroProps) => astroImagePropsToReactImageProps(astroProps)); | ||
console.log('images', JSON.stringify(reactImages, null, 2)); | ||
const { class: className } = Astro.props; | ||
--- | ||
|
||
<div class={cn('', className)}> | ||
<ReactGallery | ||
client:only="react" | ||
images={reactImages} | ||
thumbnailImageComponent={(<Image {...IMAGE_SIZES.FIXED.MDX_XXS_16_9} src="" alt="default" />)} | ||
/> | ||
</div> | ||
{ | ||
/* | ||
/@fs/home/username/Desktop/nemanjam.github.io/src/assets/images/all-images/cyco5.jpg?origWidth=4608&origHeight=2592&origFormat=jpg | ||
<ul class={cn('grid gap-4 grid-cols-1 sm:grid-cols-2 md:grid-cols-3', className)}> | ||
{ | ||
imagesMetadata.map((image) => ( | ||
<li> | ||
<Image {...IMAGE_SIZES.FIXED.MDX_XXS_16_9} src={image} alt="my image" /> | ||
</li> | ||
)) | ||
} | ||
</ul> | ||
interface Image { | ||
key?: Key; | ||
src: string; | ||
width: number; | ||
height: number; | ||
nano?: string; | ||
alt?: string; | ||
tags?: ImageTag[]; | ||
isSelected?: boolean; | ||
caption?: ReactNode; | ||
customOverlay?: ReactNode; | ||
thumbnailCaption?: ReactNode; | ||
orientation?: number; | ||
} | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
import { getImage } from 'astro:assets'; | ||
import ReactGallery from '@/components/react/Gallery'; | ||
import { IMAGE_SIZES } from '@/constants/image'; | ||
import { cn } from '@/utils/styles'; | ||
import type { ImageProps } from '@/types/common'; | ||
import type { ImageMetadata } from 'astro'; | ||
export interface Props extends astroHTML.JSX.HTMLAttributes {} | ||
// filenames | ||
const EXCLUDE_IMAGES = ['avatar1.jpg']; | ||
const getAllImagesMetadata = (): ImageMetadata[] => { | ||
const imageModules = import.meta.glob<{ default: ImageMetadata }>( | ||
// cant be even variable | ||
'/src/assets/images/all-images/*.jpg', | ||
{ eager: true } | ||
); | ||
// convert map to array | ||
const imagesMetadata = Object.keys(imageModules) | ||
// filter excluded filenames | ||
.filter((path) => !EXCLUDE_IMAGES.some((excludedFileName) => path.endsWith(excludedFileName))) | ||
// return metadata array | ||
.map((path) => imageModules[path].default); | ||
return imagesMetadata; | ||
}; | ||
const imagesMetadata = getAllImagesMetadata(); | ||
const imageMetadataToReactImageProps = async ( | ||
imagesMetadata: ImageMetadata | ||
): Promise<ImageProps> => { | ||
const astroImageProps = { | ||
src: imagesMetadata, | ||
format: 'webp', | ||
}; | ||
const thumbnailAstroImageProps = { | ||
...astroImageProps, | ||
...IMAGE_SIZES.FIXED.MDX_XXS_16_9, | ||
alt: 'Thumbnail image', | ||
}; | ||
const lightboxAstroImageProps = { | ||
...astroImageProps, | ||
...IMAGE_SIZES.FIXED.MDX_XL_16_9, | ||
alt: 'Lightbox image', | ||
}; | ||
const optimizedThumbnailAstroImageProps = await getImage(thumbnailAstroImageProps); | ||
const { src, attributes } = optimizedThumbnailAstroImageProps; | ||
const optimizedLightboxAstroImageProps = await getImage(lightboxAstroImageProps); | ||
const { src: originalSrc } = optimizedLightboxAstroImageProps; | ||
const reactImageProps = { | ||
src, | ||
originalSrc, | ||
// width and height only for thumbnails | ||
width: parseInt(String(attributes.width)), | ||
height: parseInt(String(attributes.height)), | ||
}; | ||
return reactImageProps; | ||
}; | ||
const reactImages = await Promise.all( | ||
imagesMetadata.map((metadata) => imageMetadataToReactImageProps(metadata)) | ||
); | ||
// console.log('reactImages', reactImages); | ||
const { class: className } = Astro.props; | ||
--- | ||
|
||
<div class={cn('', className)}> | ||
<ReactGallery client:only="react" images={reactImages} /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useState } from 'react'; | ||
|
||
import { Gallery as ReactGridGallery } from 'react-grid-gallery'; | ||
import Lightbox from 'react-image-lightbox'; | ||
|
||
import type { ImageProps } from '@/types/common'; | ||
|
||
import 'react-image-lightbox/style.css'; | ||
|
||
interface Props { | ||
images: ImageProps[]; | ||
} | ||
|
||
// global polyfill for react-image-lightbox | ||
window.global = window.global || window; | ||
|
||
const Gallery: React.FC<Props> = ({ images }) => { | ||
const [index, setIndex] = useState(-1); | ||
|
||
const currentImage = images[index]; | ||
const nextIndex = (index + 1) % images.length; | ||
const nextImage = images[nextIndex] || currentImage; | ||
const prevIndex = (index + images.length - 1) % images.length; | ||
const prevImage = images[prevIndex] || currentImage; | ||
|
||
const handleClick = (index: number, _item: ImageProps) => setIndex(index); | ||
const handleClose = () => setIndex(-1); | ||
const handleMovePrev = () => setIndex(prevIndex); | ||
const handleMoveNext = () => setIndex(nextIndex); | ||
|
||
return ( | ||
<div> | ||
<ReactGridGallery images={images} onClick={handleClick} enableImageSelection={false} /> | ||
{!!currentImage && ( | ||
<Lightbox | ||
imageTitle={currentImage.caption} | ||
mainSrc={currentImage.originalSrc} | ||
mainSrcThumbnail={currentImage.src} | ||
nextSrc={nextImage.originalSrc} | ||
nextSrcThumbnail={nextImage.src} | ||
prevSrc={prevImage.originalSrc} | ||
prevSrcThumbnail={prevImage.src} | ||
onCloseRequest={handleClose} | ||
onMovePrevRequest={handleMovePrev} | ||
onMoveNextRequest={handleMoveNext} | ||
/> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Gallery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
--- | ||
import Base from '@/layouts/Base.astro'; | ||
import { cn } from '@/utils/styles'; | ||
import type { BaseProps } from '@/layouts/Base.astro'; | ||
export interface Props extends BaseProps {} | ||
export interface Props extends BaseProps, astroHTML.JSX.HTMLAttributes {} | ||
const props = Astro.props; | ||
// targets <main /> tag | ||
const { class: className } = Astro.props; | ||
--- | ||
|
||
{/* flex here to take full height for pagination, flex-row align-stretch */} | ||
{/* h-full dosent work without all parents h-full */} | ||
|
||
<Base {...props}> | ||
<main id="main" class="flex-grow flex flex-col centered-px max-w-4xl py-4 lg:py-8"> | ||
<main | ||
id="main" | ||
class={cn('flex-grow flex flex-col centered-px max-w-4xl py-4 lg:py-8', className)} | ||
> | ||
<slot /> | ||
</main> | ||
</Base> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
layout: '../layouts/Page.astro' | ||
title: 'Gallery' | ||
description: 'Image gallery' | ||
class: 'max-w-5xl' | ||
--- | ||
|
||
import Gallery from '../components/Gallery.astro'; | ||
|
||
# Gallery | ||
|
||
<Gallery class="not-prose" /> |
Oops, something went wrong.