Skip to content

The Open Standard for Generative UI

Refactor image gallery (#165)

subham704a284f0e0e
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index 18a7eed27..295ae1554 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.8.14",
+ "version": "0.8.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
index bfd34b94c..5e1c1a116 100644
--- a/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx
+++ b/js/packages/react-ui/src/components/ImageGallery/GalleryModal.tsx
@@ -1,7 +1,9 @@
import clsx from "clsx";
import { ChevronLeft, ChevronRight, X } from "lucide-react";
import React, { useCallback, useEffect, useRef, useState } from "react";
+import { createPortal } from "react-dom";
import { IconButton } from "../IconButton";
+import { useTheme } from "../ThemeProvider";
import { ImageItem } from "./ImageGallery";
export interface GalleryModalProps {
@@ -19,6 +21,8 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
}) => {
const [scrollButtons, setScrollButtons] = useState({ showLeft: false, showRight: false });
const carouselRef = useRef<HTMLDivElement>(null);
+ const modalContentRef = useRef<HTMLDivElement>(null);
+ const { portalThemeClassName } = useTheme();
// Check if scrolling is needed
const checkScroll = useCallback(() => {
@@ -51,6 +55,34 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
};
}, [checkScroll]);
+ // Handle modal lifecycle events (scroll lock, close on escape, close on outside click)
+ useEffect(() => {
+ // Disable body scroll
+ document.body.style.overflow = "hidden";
+
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if (e.key === "Escape") {
+ onClose();
+ }
+ };
+
+ const handleOutsideClick = (e: MouseEvent) => {
+ if (modalContentRef.current && !modalContentRef.current.contains(e.target as Node)) {
+ onClose();
+ }
+ };
+
+ document.addEventListener("keydown", handleKeyDown);
+ document.addEventListener("mousedown", handleOutsideClick);
+
+ // Cleanup
+ return () => {
+ document.body.style.overflow = "auto";
+ document.removeEventListener("keydown", handleKeyDown);
+ document.removeEventListener("mousedown", handleOutsideClick);
+ };
+ }, [onClose]);
+
const scroll = (direction: "left" | "right") => {
if (carouselRef.current) {
const container = carouselRef.current;
@@ -77,9 +109,9 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
[setSelectedImageIndex],
);
- return (
- <div className="crayon-gallery__modal">
- <div className="crayon-gallery__modal-content">
+ return createPortal(
+ <div className={clsx("crayon-gallery__modal", portalThemeClassName)}>
+ <div className="crayon-gallery__modal-content" ref={modalContentRef}>
<div className="crayon-gallery__modal-header">
<span className="crayon-gallery__modal-heading">All Photos</span>
<IconButton size="small" variant="secondary" icon={<X />} onClick={onClose} />
@@ -92,17 +124,24 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
</div>
<div className="crayon-gallery__modal-carousel-container">
{scrollButtons.showLeft && (
- <IconButton
+ <div
className={clsx(
- "crayon-gallery__carousel-button",
- "crayon-gallery__carousel-button--left",
+ "crayon-gallery__modal-carousel-button-container",
+ "crayon-gallery__modal-carousel-button-container-left",
)}
- onClick={() => scroll("left")}
- aria-label="Scroll images left"
- icon={<ChevronLeft />}
- variant="secondary"
- size="extra-small"
- />
+ >
+ <IconButton
+ className={clsx(
+ "crayon-gallery__carousel-button",
+ "crayon-gallery__carousel-button--left",
+ )}
+ onClick={() => scroll("left")}
+ aria-label="Scroll images left"
+ icon={<ChevronLeft />}
+ variant="secondary"
+ size="extra-small"
+ />
+ </div>
)}
<div className="crayon-gallery__modal-carousel" ref={carouselRef}>
@@ -121,20 +160,28 @@ export const GalleryModal: React.FC<GalleryModalProps> = ({
</div>
{scrollButtons.showRight && (
- <IconButton
+ <div
className={clsx(
- "crayon-gallery__carousel-button",
- "crayon-gallery__carousel-button--right",
+ "crayon-gallery__modal-carousel-button-container",
+ "crayon-gallery__modal-carousel-button-container-right",
)}
- onClick={() => scroll("right")}
- aria-label="Scroll images right"
- icon={<ChevronRight />}
- variant="secondary"
- size="extra-small"
- />
+ >
+ <IconButton
+ className={clsx(
+ "crayon-gallery__carousel-button",
+ "crayon-gallery__carousel-button--right",
+ )}
+ onClick={() => scroll("right")}
+ aria-label="Scroll images right"
+ icon={<ChevronRight />}
+ variant="secondary"
+ size="extra-small"
+ />
+ </div>
)}
</div>
</div>
- </div>
+ </div>,
+ document.body,
);
};
diff --git a/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss b/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
index 00b697ad9..9680e92c8 100644
--- a/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
+++ b/js/packages/react-ui/src/components/ImageGallery/imageGallery.scss
@@ -112,11 +112,11 @@
}
&__modal-content {
- width: 100%;
+ width: calc(100% - 80px);
+ height: calc(100% - 80px);
max-width: 720px;
max-height: 640px;
- // position: relative;
- overflow-y: auto;
+ overflow: hidden;
display: flex;
flex-direction: column;
box-sizing: border-box;
@@ -134,9 +134,14 @@
padding-left: cssUtils.$spacing-xl;
padding-right: cssUtils.$spacing-xl;
}
+ &__modal-heading {
+ @include cssUtils.typography(heading, extra-small);
+ color: cssUtils.$primary-text;
+ }
&__modal-main {
- height: 390px;
+ flex: 1;
+ min-height: 0;
display: flex;
justify-content: center;
align-items: center;
@@ -178,17 +183,21 @@
}
}
- &__carousel-button {
+ &__modal-carousel-button-container {
position: absolute;
top: 50%;
transform: translateY(-50%);
z-index: 1;
-
- &--left {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ background-color: cssUtils.$bg-container;
+ border-radius: cssUtils.$rounded-xs;
+ overflow: hidden;
+ &-left {
left: 24px;
}
-
- &--right {
+ &-right {
right: 24px;
}
}
@@ -279,3 +288,13 @@
}
}
}
+
+@media (max-width: 768px) {
+ .crayon-gallery__modal-content {
+ width: 100%;
+ height: 100%;
+ border-radius: 0;
+ max-height: none;
+ max-width: none;
+ }
+}