Skip to content

The Open Standard for Generative UI

Refactor image gallery (#77)

subhamdfec1702d3c2
diff --git a/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx b/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx
index a10b28673..3b25181af 100644
--- a/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx
+++ b/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx
@@ -2,9 +2,10 @@ import clsx from "clsx";
import { ChevronLeft, ChevronRight, X } from "lucide-react";
import React, { useCallback, useEffect, useRef, useState } from "react";
import { IconButton } from "../IconButton";
+import { ImageItem } from "./ImageGallery";
interface GalleryModalProps {
- images: string[];
+ images: ImageItem[];
selectedImageIndex: number;
setSelectedImageIndex: (index: number) => void;
onClose: () => void;
@@ -19,49 +20,56 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
const [scrollButtons, setScrollButtons] = useState({ showLeft: false, showRight: false });
const carouselRef = useRef<HTMLDivElement>(null);
- // Memoize scroll check function
+ // Check if scrolling is needed
const checkScroll = useCallback(() => {
- if (carouselRef.current) {
- const { scrollLeft, scrollWidth, clientWidth } = carouselRef.current;
- setScrollButtons({
- showLeft: scrollLeft > 0,
- showRight: scrollLeft < scrollWidth - clientWidth - 1,
- });
- }
+ if (!carouselRef.current) return;
+
+ const container = carouselRef.current;
+ setScrollButtons({
+ showLeft: container.scrollLeft > 0,
+ showRight: container.scrollLeft + container.offsetWidth < container.scrollWidth,
+ });
}, []);
- // Check if scrolling is needed
useEffect(() => {
+ if (!carouselRef.current) {
+ return;
+ }
+
+ const container = carouselRef.current;
// Initial check
checkScroll();
- const currentRef = carouselRef.current;
- if (currentRef) {
- currentRef.addEventListener("scroll", checkScroll);
+ const resizeObserver = new ResizeObserver(checkScroll);
+ resizeObserver.observe(container);
- const resizeObserver = new ResizeObserver(checkScroll);
- resizeObserver.observe(currentRef);
+ container.addEventListener("scroll", checkScroll);
- return () => {
- currentRef.removeEventListener("scroll", checkScroll);
- resizeObserver.disconnect();
- };
- }
- return;
+ return () => {
+ container.removeEventListener("scroll", checkScroll);
+ resizeObserver.disconnect();
+ };
}, [checkScroll]);
- // Memoize scroll handlers
- const scrollLeft = useCallback(() => {
+ const scroll = (direction: "left" | "right") => {
if (carouselRef.current) {
- carouselRef.current.scrollBy({ left: -140, behavior: "smooth" });
- }
- }, []);
+ const container = carouselRef.current;
+ const containerRect = container.getBoundingClientRect();
+ const scrollAmount = containerRect.width * 0.28;
- const scrollRight = useCallback(() => {
- if (carouselRef.current) {
- carouselRef.current.scrollBy({ left: 140, behavior: "smooth" });
+ if (direction === "left") {
+ container.scrollBy({
+ left: -scrollAmount,
+ behavior: "smooth",
+ });
+ } else {
+ container.scrollBy({
+ left: scrollAmount,
+ behavior: "smooth",
+ });
+ }
}
- }, []);
+ };
// Memoize thumbnail click handler
const handleThumbnailClick = useCallback(
@@ -74,10 +82,13 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
<div className="crayon-gallery__modal-content">
<div className="crayon-gallery__modal-header">
<span className="crayon-gallery__modal-heading">All Photos</span>
- <IconButton size="extra-small" variant="secondary" icon={<X />} onClick={onClose} />
+ <IconButton size="small" variant="secondary" icon={<X />} onClick={onClose} />
</div>
<div className="crayon-gallery__modal-main">
- <img src={images[selectedImageIndex]} alt={`Gallery image ${selectedImageIndex + 1}`} />
+ <img
+ src={images[selectedImageIndex]?.src}
+ alt={images[selectedImageIndex]?.alt || `Gallery image ${selectedImageIndex + 1}`}
+ />
</div>
<div className="crayon-gallery__modal-carousel-container">
{scrollButtons.showLeft && (
@@ -86,11 +97,11 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
"crayon-gallery__carousel-button",
"crayon-gallery__carousel-button--left",
)}
- onClick={scrollLeft}
+ onClick={() => scroll("left")}
aria-label="Scroll images left"
icon={<ChevronLeft />}
variant="secondary"
- size="small"
+ size="extra-small"
/>
)}
@@ -104,7 +115,7 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
)}
onClick={handleThumbnailClick(index)}
>
- <img src={image} alt={`Gallery thumbnail ${index + 1}`} />
+ <img src={image.src} alt={image.alt || `Gallery thumbnail ${index + 1}`} />
</div>
))}
</div>
@@ -115,11 +126,11 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
"crayon-gallery__carousel-button",
"crayon-gallery__carousel-button--right",
)}
- onClick={scrollRight}
+ onClick={() => scroll("right")}
aria-label="Scroll images right"
icon={<ChevronRight />}
variant="secondary"
- size="small"
+ size="extra-small"
/>
)}
</div>
diff --git a/js/packages/react-ui/src/components/ImageGallery/ImageGallery.tsx b/js/packages/react-ui/src/components/ImageGallery/ImageGallery.tsx
index c1c7dde64..fd2debc7a 100644
--- a/js/packages/react-ui/src/components/ImageGallery/ImageGallery.tsx
+++ b/js/packages/react-ui/src/components/ImageGallery/ImageGallery.tsx
@@ -3,8 +3,14 @@ import React, { useCallback, useMemo, useState } from "react";
import { Button } from "../Button";
import { GalleryModal } from "./GalleryModal";
+export interface ImageItem {
+ src: string;
+ alt?: string;
+ details?: string;
+}
+
interface CrayonGalleryProps {
- images: string[];
+ images: ImageItem[];
}
const MAX_GRID_IMAGES = 5;
@@ -59,7 +65,7 @@ export const ImageGallery: React.FC<CrayonGalleryProps> = ({ images }) => {
className={clsx("crayon-gallery__image", index === 0 && "crayon-gallery__image--main")}
onClick={() => handleImageClick(index)}
>
- <img src={image} alt={`Gallery image ${index + 1}`} />
+ <img src={image.src} alt={image.alt || `Gallery image ${index + 1}`} />
</div>
))}
{shouldShowButton && (
diff --git a/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss b/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
index 050287c01..00b697ad9 100644
--- a/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
+++ b/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
@@ -4,14 +4,15 @@
display: flex;
flex-direction: column;
width: 100%;
- border-radius: cssUtils.$rounded-m;
overflow: hidden;
position: relative;
+ border-radius: cssUtils.$rounded-m;
&__grid {
display: grid;
- grid-gap: cssUtils.$spacing-s;
width: 100%;
+ max-height: 376px;
+ grid-gap: cssUtils.$spacing-s;
}
// Layout variations
@@ -33,8 +34,25 @@
}
&--quad &__grid {
- grid-template-columns: repeat(2, 1fr);
+ grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(2, 1fr);
+ grid-template-areas:
+ "img1 img1 img2 img2 "
+ "img1 img1 img3 img4";
+ .crayon-gallery__image {
+ &:nth-child(1) {
+ grid-area: img1;
+ }
+ &:nth-child(2) {
+ grid-area: img2;
+ }
+ &:nth-child(3) {
+ grid-area: img3;
+ }
+ &:nth-child(4) {
+ grid-area: img4;
+ }
+ }
}
&--default &__grid {
@@ -55,7 +73,6 @@
height: 100%;
object-fit: cover;
transition: transform 0.3s ease;
- aspect-ratio: 1/1;
&:hover {
transform: scale(1.05);
@@ -95,39 +112,31 @@
}
&__modal-content {
- position: relative;
width: 100%;
max-width: 720px;
- max-height: 560px;
- border: 1px solid cssUtils.$stroke-default;
- box-sizing: border-box;
+ max-height: 640px;
+ // position: relative;
overflow-y: auto;
- background-color: cssUtils.$bg-container;
- border-radius: cssUtils.$rounded-xl;
display: flex;
flex-direction: column;
+ box-sizing: border-box;
+ border: 1px solid cssUtils.$stroke-default;
+ background-color: cssUtils.$bg-container;
+ border-radius: cssUtils.$rounded-xl;
gap: cssUtils.$spacing-xl;
padding-top: cssUtils.$spacing-xl;
padding-bottom: cssUtils.$spacing-xl;
}
-
&__modal-header {
- width: 100%;
display: flex;
- justify-content: space-between;
align-items: center;
+ justify-content: space-between;
padding-left: cssUtils.$spacing-xl;
padding-right: cssUtils.$spacing-xl;
}
- &__modal-heading {
- @include cssUtils.typography("title", "medium");
- color: cssUtils.$primary-text;
- }
-
&__modal-main {
- width: 100%;
- height: 312px;
+ height: 390px;
display: flex;
justify-content: center;
align-items: center;
@@ -140,6 +149,11 @@
object-fit: contain;
border-radius: cssUtils.$rounded-s;
}
+ &:hover {
+ .crayon-gallery__modal-details {
+ display: flex;
+ }
+ }
}
&__modal-carousel-container {
@@ -151,13 +165,13 @@
}
&__modal-carousel {
- width: 100%;
display: flex;
gap: cssUtils.$spacing-s;
overflow-x: auto;
scroll-behavior: smooth;
scrollbar-width: none;
- padding: cssUtils.$spacing-s 0;
+ padding-left: cssUtils.$spacing-xl;
+ padding-right: cssUtils.$spacing-xl;
&::-webkit-scrollbar {
display: none;
@@ -181,11 +195,12 @@
&__modal-thumbnail {
flex: 1;
- height: 112px;
- min-width: 170px;
+ height: 116px;
+ min-width: 174px;
cursor: pointer;
opacity: 0.6;
border-radius: cssUtils.$rounded-s;
+ border: 1px solid transparent;
overflow: hidden;
transition: opacity 0.2s ease;
box-sizing: border-box;
@@ -196,9 +211,8 @@
&--active {
opacity: 1;
- border: 1px solid cssUtils.$stroke-interactive-el-selected;
+ border-color: cssUtils.$stroke-interactive-el-selected;
border-radius: cssUtils.$rounded-s;
- box-sizing: border-box;
}
img {
diff --git a/js/packages/react-ui/src/components/ImageGallery/stories/imsgeGallery.stories.tsx b/js/packages/react-ui/src/components/ImageGallery/stories/imsgeGallery.stories.tsx
index a65ed6d84..5a818b1b8 100644
--- a/js/packages/react-ui/src/components/ImageGallery/stories/imsgeGallery.stories.tsx
+++ b/js/packages/react-ui/src/components/ImageGallery/stories/imsgeGallery.stories.tsx
@@ -19,7 +19,7 @@ const meta: Meta<typeof ImageGallery> = {
description: "The images to display in the gallery",
table: {
category: "Content",
- type: { summary: "string[]" },
+ type: { summary: "Array<{ src: string, alt: string, details?: string }>" },
},
},
},
@@ -31,19 +31,43 @@ type Story = StoryObj<typeof ImageGallery>;
export const ImageGalleryStory: Story = {
render: (args) => (
- <Card style={{ width: "600px" }}>
- <ImageGallery
- {...args}
- images={[
- "https://images.unsplash.com/photo-1526481280693-3bfa7568e0f3?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGphcGFufGVufDB8fDB8fHww",
- "https://plus.unsplash.com/premium_photo-1675610853926-6d69a0a99ea7?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTd8fGphcGFufGVufDB8fDB8fHww",
- "https://images.unsplash.com/photo-1554797589-7241bb691973?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTJ8fGphcGFufGVufDB8fDB8fHww",
- "https://images.unsplash.com/photo-1492571350019-22de08371fd3?q=80&w=3906&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
- "https://plus.unsplash.com/premium_photo-1661878091370-4ccb8763756a?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8NXx8amFwYW58ZW58MHx8MHx8fDA%3D",
- "https://images.unsplash.com/photo-1740412662676-a3b16d74ee86?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHwzfHx8ZW58MHx8fHx8",
- "https://images.unsplash.com/photo-1740386072835-938733c974e1?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHwyNzh8fHxlbnwwfHx8fHw%3D",
- ]}
- ></ImageGallery>
- </Card>
+ <div
+ style={{
+ width: "800px",
+ height: "700px",
+ display: "flex",
+ justifyContent: "center",
+ alignItems: "center",
+ }}
+ >
+ <Card style={{ width: "600px" }}>
+ <ImageGallery
+ {...args}
+ images={[
+ {
+ src: "https://images.unsplash.com/photo-1526481280693-3bfa7568e0f3?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTB8fGphcGFufGVufDB8fDB8fHww",
+ },
+ {
+ src: "https://plus.unsplash.com/premium_photo-1675610853926-6d69a0a99ea7?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTd8fGphcGFufGVufDB8fDB8fHww",
+ },
+ {
+ src: "https://images.unsplash.com/photo-1554797589-7241bb691973?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTJ8fGphcGFufGVufDB8fDB8fHww",
+ },
+ {
+ src: "https://images.unsplash.com/photo-1492571350019-22de08371fd3?q=80&w=3906&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
+ },
+ {
+ src: "https://plus.unsplash.com/premium_photo-1661878091370-4ccb8763756a?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8NXx8amFwYW58ZW58MHx8MHx8fDA%3D",
+ },
+ {
+ src: "https://images.unsplash.com/photo-1740412662676-a3b16d74ee86?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHwzfHx8ZW58MHx8fHx8",
+ },
+ {
+ src: "https://images.unsplash.com/photo-1740386072835-938733c974e1?w=900&auto=format&fit=crop&q=60&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxmZWF0dXJlZC1waG90b3MtZmVlZHwyNzh8fHxlbnwwfHx8fHw%3D",
+ },
+ ]}
+ />
+ </Card>
+ </div>
),
};