Skip to content

The Open Standard for Generative UI

Crayon image gallery (#55)

subhamba7ee060825b
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index 76475922c..cd79a0f54 100644
--- a/js/packages/react-ui/package.json
+++ b/js/packages/react-ui/package.json
@@ -2,7 +2,7 @@
"type": "module",
"name": "@crayonai/react-ui",
"license": "MIT",
- "version": "0.5.14",
+ "version": "0.5.15",
"description": "Component library for Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
diff --git a/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx b/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx
new file mode 100644
index 000000000..a10b28673
--- /dev/null
+++ b/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx
@@ -0,0 +1,129 @@
+import clsx from "clsx";
+import { ChevronLeft, ChevronRight, X } from "lucide-react";
+import React, { useCallback, useEffect, useRef, useState } from "react";
+import { IconButton } from "../IconButton";
+
+interface GalleryModalProps {
+ images: string[];
+ selectedImageIndex: number;
+ setSelectedImageIndex: (index: number) => void;
+ onClose: () => void;
+}
+
+export const GalleryModal: React.FC<GalleryModalProps> = ({
+ images,
+ selectedImageIndex,
+ setSelectedImageIndex,
+ onClose,
+}) => {
+ const [scrollButtons, setScrollButtons] = useState({ showLeft: false, showRight: false });
+ const carouselRef = useRef<HTMLDivElement>(null);
+
+ // Memoize scroll check function
+ const checkScroll = useCallback(() => {
+ if (carouselRef.current) {
+ const { scrollLeft, scrollWidth, clientWidth } = carouselRef.current;
+ setScrollButtons({
+ showLeft: scrollLeft > 0,
+ showRight: scrollLeft < scrollWidth - clientWidth - 1,
+ });
+ }
+ }, []);
+
+ // Check if scrolling is needed
+ useEffect(() => {
+ // Initial check
+ checkScroll();
+
+ const currentRef = carouselRef.current;
+ if (currentRef) {
+ currentRef.addEventListener("scroll", checkScroll);
+
+ const resizeObserver = new ResizeObserver(checkScroll);
+ resizeObserver.observe(currentRef);
+
+ return () => {
+ currentRef.removeEventListener("scroll", checkScroll);
+ resizeObserver.disconnect();
+ };
+ }
+ return;
+ }, [checkScroll]);
+
+ // Memoize scroll handlers
+ const scrollLeft = useCallback(() => {
+ if (carouselRef.current) {
+ carouselRef.current.scrollBy({ left: -140, behavior: "smooth" });
+ }
+ }, []);
+
+ const scrollRight = useCallback(() => {
+ if (carouselRef.current) {
+ carouselRef.current.scrollBy({ left: 140, behavior: "smooth" });
+ }
+ }, []);
+
+ // Memoize thumbnail click handler
+ const handleThumbnailClick = useCallback(
+ (index: number) => () => setSelectedImageIndex(index),
+ [setSelectedImageIndex],
+ );
+
+ return (
+ <div className="crayon-gallery__modal">
+ <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} />
+ </div>
+ <div className="crayon-gallery__modal-main">
+ <img src={images[selectedImageIndex]} alt={`Gallery image ${selectedImageIndex + 1}`} />
+ </div>
+ <div className="crayon-gallery__modal-carousel-container">
+ {scrollButtons.showLeft && (
+ <IconButton
+ className={clsx(
+ "crayon-gallery__carousel-button",
+ "crayon-gallery__carousel-button--left",
+ )}
+ onClick={scrollLeft}
+ aria-label="Scroll images left"
+ icon={<ChevronLeft />}
+ variant="secondary"
+ size="small"
+ />
+ )}
+
+ <div className="crayon-gallery__modal-carousel" ref={carouselRef}>
+ {images.map((image, index) => (
+ <div
+ key={index}
+ className={clsx(
+ "crayon-gallery__modal-thumbnail",
+ index === selectedImageIndex && "crayon-gallery__modal-thumbnail--active",
+ )}
+ onClick={handleThumbnailClick(index)}
+ >
+ <img src={image} alt={`Gallery thumbnail ${index + 1}`} />
+ </div>
+ ))}
+ </div>
+
+ {scrollButtons.showRight && (
+ <IconButton
+ className={clsx(
+ "crayon-gallery__carousel-button",
+ "crayon-gallery__carousel-button--right",
+ )}
+ onClick={scrollRight}
+ aria-label="Scroll images right"
+ icon={<ChevronRight />}
+ variant="secondary"
+ size="small"
+ />
+ )}
+ </div>
+ </div>
+ </div>
+ );
+};
diff --git a/js/packages/react-ui/src/components/ImageGallery/ImageGallery.tsx b/js/packages/react-ui/src/components/ImageGallery/ImageGallery.tsx
new file mode 100644
index 000000000..c1c7dde64
--- /dev/null
+++ b/js/packages/react-ui/src/components/ImageGallery/ImageGallery.tsx
@@ -0,0 +1,84 @@
+import clsx from "clsx";
+import React, { useCallback, useMemo, useState } from "react";
+import { Button } from "../Button";
+import { GalleryModal } from "./GalleryModal";
+
+interface CrayonGalleryProps {
+ images: string[];
+}
+
+const MAX_GRID_IMAGES = 5;
+const getLayoutClassName = (imageCount: number): string => {
+ switch (imageCount) {
+ case 1:
+ return "crayon-gallery--single";
+ case 2:
+ return "crayon-gallery--double";
+ case 3:
+ return "crayon-gallery--triple";
+ case 4:
+ return "crayon-gallery--quad";
+ default:
+ return "crayon-gallery--default";
+ }
+};
+
+export const ImageGallery: React.FC<CrayonGalleryProps> = ({ images }) => {
+ const [showAll, setShowAll] = useState(false);
+ const [selectedImageIndex, setSelectedImageIndex] = useState(0);
+
+ // Memoize layout class to prevent recalculation
+ const layoutClass = useMemo(() => getLayoutClassName(images.length), [images.length]);
+
+ // Memoize whether to show button
+ const shouldShowButton = useMemo(() => images.length > MAX_GRID_IMAGES, [images.length]);
+
+ // Memoize visible images
+ const visibleImages = useMemo(() => images.slice(0, MAX_GRID_IMAGES), [images]);
+
+ // Memoize callbacks
+ const toggleShowAll = useCallback(() => {
+ setShowAll((prev) => !prev);
+ }, []);
+
+ const handleImageClick = useCallback((index: number) => {
+ setSelectedImageIndex(index);
+ setShowAll(true);
+ }, []);
+
+ const setSelectedImageIndexMemoized = useCallback((index: number) => {
+ setSelectedImageIndex(index);
+ }, []);
+
+ return (
+ <div className={clsx("crayon-gallery", layoutClass)}>
+ <div className="crayon-gallery__grid">
+ {visibleImages.map((image, index) => (
+ <div
+ key={index}
+ className={clsx("crayon-gallery__image", index === 0 && "crayon-gallery__image--main")}
+ onClick={() => handleImageClick(index)}
+ >
+ <img src={image} alt={`Gallery image ${index + 1}`} />
+ </div>
+ ))}
+ {shouldShowButton && (
+ <div className="crayon-gallery__show-all-button">
+ <Button variant="primary" size="small" onClick={toggleShowAll}>
+ Show All
+ </Button>
+ </div>
+ )}
+ </div>
+
+ {showAll && (
+ <GalleryModal
+ images={images}
+ selectedImageIndex={selectedImageIndex}
+ setSelectedImageIndex={setSelectedImageIndexMemoized}
+ onClose={toggleShowAll}
+ />
+ )}
+ </div>
+ );
+};
diff --git a/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss b/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
new file mode 100644
index 000000000..050287c01
--- /dev/null
+++ b/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
@@ -0,0 +1,267 @@
+@use "../../cssUtils" as cssUtils;
+
+.crayon-gallery {
+ display: flex;
+ flex-direction: column;
+ width: 100%;
+ border-radius: cssUtils.$rounded-m;
+ overflow: hidden;
+ position: relative;
+
+ &__grid {
+ display: grid;
+ grid-gap: cssUtils.$spacing-s;
+ width: 100%;
+ }
+
+ // Layout variations
+ &--single &__grid {
+ grid-template-columns: 1fr;
+ }
+
+ &--double &__grid {
+ grid-template-columns: repeat(2, 1fr);
+ }
+
+ &--triple &__grid {
+ grid-template-columns: 1fr 1fr;
+ grid-template-rows: 1fr 1fr;
+
+ .crayon-gallery__image:first-child {
+ grid-row: 1 / -1;
+ }
+ }
+
+ &--quad &__grid {
+ grid-template-columns: repeat(2, 1fr);
+ grid-template-rows: repeat(2, 1fr);
+ }
+
+ &--default &__grid {
+ grid-template-columns: 2fr 1fr 1fr;
+ grid-template-rows: 1fr 1fr;
+
+ .crayon-gallery__image:first-child {
+ grid-row: 1 / -1;
+ }
+ }
+
+ &__image {
+ position: relative;
+ overflow: hidden;
+
+ img {
+ width: 100%;
+ height: 100%;
+ object-fit: cover;
+ transition: transform 0.3s ease;
+ aspect-ratio: 1/1;
+
+ &:hover {
+ transform: scale(1.05);
+ }
+ }
+
+ &--main {
+ img {
+ height: 100%;
+ }
+ }
+ }
+
+ &__show-all-button {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ position: absolute;
+ bottom: 5px;
+ right: 5px;
+ }
+
+ // Modal styles
+ &__modal {
+ position: fixed;
+ top: 0;
+ left: 0;
+ width: 100%;
+ height: 100%;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ z-index: 1000;
+ animation: fadeIn 0.3s ease;
+ backdrop-filter: blur(3px);
+ background-color: cssUtils.$bg-overlay;
+ }
+
+ &__modal-content {
+ position: relative;
+ width: 100%;
+ max-width: 720px;
+ max-height: 560px;
+ border: 1px solid cssUtils.$stroke-default;
+ box-sizing: border-box;
+ overflow-y: auto;
+ background-color: cssUtils.$bg-container;
+ border-radius: cssUtils.$rounded-xl;
+ display: flex;
+ flex-direction: column;
+ 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;
+ 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;
+ display: flex;
+ justify-content: center;
+ align-items: center;
+ padding-left: cssUtils.$spacing-xl;
+ padding-right: cssUtils.$spacing-xl;
+
+ img {
+ max-width: 100%;
+ max-height: 100%;
+ object-fit: contain;
+ border-radius: cssUtils.$rounded-s;
+ }
+ }
+
+ &__modal-carousel-container {
+ position: relative;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ width: 100%;
+ }
+
+ &__modal-carousel {
+ width: 100%;
+ display: flex;
+ gap: cssUtils.$spacing-s;
+ overflow-x: auto;
+ scroll-behavior: smooth;
+ scrollbar-width: none;
+ padding: cssUtils.$spacing-s 0;
+
+ &::-webkit-scrollbar {
+ display: none;
+ }
+ }
+
+ &__carousel-button {
+ position: absolute;
+ top: 50%;
+ transform: translateY(-50%);
+ z-index: 1;
+
+ &--left {
+ left: 24px;
+ }
+
+ &--right {
+ right: 24px;
+ }
+ }
+
+ &__modal-thumbnail {
+ flex: 1;
+ height: 112px;
+ min-width: 170px;
+ cursor: pointer;
+ opacity: 0.6;
+ border-radius: cssUtils.$rounded-s;
+ overflow: hidden;
+ transition: opacity 0.2s ease;
+ box-sizing: border-box;
+
+ &:hover {
+ opacity: 0.8;
+ }
+
+ &--active {
+ opacity: 1;
+ border: 1px solid cssUtils.$stroke-interactive-el-selected;
+ border-radius: cssUtils.$rounded-s;
+ box-sizing: border-box;
+ }
+
+ img {
+ width: 100%;
+ height: 100%;
+ object-fit: cover;
+ }
+ }
+
+ // Responsive adjustments
+ @media (max-width: 768px) {
+ &--default &__grid {
+ grid-template-columns: repeat(6, 1fr);
+ grid-template-rows: repeat(2, 1fr);
+ grid-template-areas:
+ "img1 img1 img1 img2 img2 img2 "
+ "img3 img3 img4 img4 img5 img5";
+
+ .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;
+ }
+ &:nth-child(5) {
+ grid-area: img5;
+ }
+ }
+ }
+ &--triple &__grid {
+ grid-template-columns: repeat(2, 1fr);
+ grid-template-rows: repeat(2, 1fr);
+ grid-template-areas:
+ "img1 img1"
+ "img2 img3";
+
+ .crayon-gallery__image {
+ &:nth-child(1) {
+ grid-area: img1;
+ }
+ &:nth-child(2) {
+ grid-area: img2;
+ }
+ &:nth-child(3) {
+ grid-area: img3;
+ }
+ }
+ }
+ }
+
+ @keyframes fadeIn {
+ from {
+ opacity: 0;
+ }
+ to {
+ opacity: 1;
+ }
+ }
+}
diff --git a/js/packages/react-ui/src/components/ImageGallery/index.ts b/js/packages/react-ui/src/components/ImageGallery/index.ts
new file mode 100644
index 000000000..ba7656e63
--- /dev/null
+++ b/js/packages/react-ui/src/components/ImageGallery/index.ts
@@ -0,0 +1 @@
+export * from "./ImageGallery";
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
new file mode 100644
index 000000000..a65ed6d84
--- /dev/null
+++ b/js/packages/react-ui/src/components/ImageGallery/stories/imsgeGallery.stories.tsx
@@ -0,0 +1,49 @@
+import type { Meta, StoryObj } from "@storybook/react";
+import { Card } from "../../Card";
+import { ImageGallery } from "../ImageGallery";
+
+const meta: Meta<typeof ImageGallery> = {
+ title: "Components/ImageGallery",
+ component: ImageGallery,
+ parameters: {
+ layout: "centered",
+ docs: {
+ description: {
+ component: "```tsx\nimport { ImageGallery } from '@crayon-ui/react-ui';\n```",
+ },
+ },
+ },
+ argTypes: {
+ images: {
+ control: false,
+ description: "The images to display in the gallery",
+ table: {
+ category: "Content",
+ type: { summary: "string[]" },
+ },
+ },
+ },
+ tags: ["autodocs", "!dev"],
+};
+
+export default meta;
+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>
+ ),
+};
diff --git a/js/packages/react-ui/src/components/index.scss b/js/packages/react-ui/src/components/index.scss
index 21c785d30..fb8792fbe 100644
--- a/js/packages/react-ui/src/components/index.scss
+++ b/js/packages/react-ui/src/components/index.scss
@@ -34,3 +34,4 @@
@forward "./TextContent/textContent.scss";
@forward "./Steps/steps.scss";
@forward "./Callout/callout.scss";
+@forward "./ImageGallery/imageGallery.scss";
diff --git a/js/packages/react-ui/src/index.ts b/js/packages/react-ui/src/index.ts
index f024b6013..cde650ee1 100644
--- a/js/packages/react-ui/src/index.ts
+++ b/js/packages/react-ui/src/index.ts
@@ -16,6 +16,7 @@ export * from "./components/FollowUpItem";
export * from "./components/FormControl";
export * from "./components/IconButton";
export * from "./components/Image";
+export * from "./components/ImageGallery";
export * from "./components/Input";
export * from "./components/Label";
export * from "./components/ListBlock";