🙌 OpenHands: AI-Driven Development
feat: click an image attachment to open it full size (#17179)
Vikas Palbb4ec4420e47
diff --git a/.github/pr-assets/pr-16268-01-thumbnail-in-message.png b/.github/pr-assets/pr-16268-01-thumbnail-in-message.pngnew file mode 100644index 0000000000..472bd003ecBinary files /dev/null and b/.github/pr-assets/pr-16268-01-thumbnail-in-message.png differdiff --git a/.github/pr-assets/pr-16268-02-lightbox-open.png b/.github/pr-assets/pr-16268-02-lightbox-open.pngnew file mode 100644index 0000000000..1d3c039170Binary files /dev/null and b/.github/pr-assets/pr-16268-02-lightbox-open.png differdiff --git a/.github/pr-assets/pr-16268-03-lightbox-closed.png b/.github/pr-assets/pr-16268-03-lightbox-closed.pngnew file mode 100644index 0000000000..05656fa316Binary files /dev/null and b/.github/pr-assets/pr-16268-03-lightbox-closed.png differdiff --git a/__tests__/components/image-preview.test.tsx b/__tests__/components/image-preview.test.tsxindex 7e40d4b925..2eb9fc2800 100644--- a/__tests__/components/image-preview.test.tsx+++ b/__tests__/components/image-preview.test.tsx@@ -1,37 +1,88 @@import { ImagePreview } from "#/components/features/images/image-preview";-import { render, screen } from "@testing-library/react";+import { render, screen, within } from "@testing-library/react";import userEvent from "@testing-library/user-event";import { describe, expect, it, vi } from "vitest";+const SRC = "https://example.com/image.jpg";+describe("ImagePreview", () => {it("should render an image", () => {- render(- <ImagePreview src="https://example.com/image.jpg" onRemove={vi.fn} />,- );- const img = screen.getByRole("img");+ render(<ImagePreview src={SRC} onRemove={vi.fn} />);expect(screen.getByTestId("image-preview")).toBeInTheDocument();- expect(img).toHaveAttribute("src", "https://example.com/image.jpg");+ expect(screen.getByRole("img")).toHaveAttribute("src", SRC);});it("should call onRemove when the close button is clicked", async () => {const user = userEvent.setup();const onRemoveMock = vi.fn();- render(- <ImagePreview- src="https://example.com/image.jpg"- onRemove={onRemoveMock}- />,- );+ render(<ImagePreview src={SRC} onRemove={onRemoveMock} />);- const closeButton = screen.getByRole("button");- await user.click(closeButton);+ await user.click(+ screen.getByRole("button", { name: "BUTTON$REMOVE_IMAGE" }),+ );expect(onRemoveMock).toHaveBeenCalledOnce();});it("should not display the close button when onRemove is not provided", () => {- render(<ImagePreview src="https://example.com/image.jpg" />);- expect(screen.queryByRole("button")).not.toBeInTheDocument();+ render(<ImagePreview src={SRC} />);++ expect(+ screen.queryByRole("button", { name: "BUTTON$REMOVE_IMAGE" }),+ ).not.toBeInTheDocument();+ });++ it("should not open the lightbox until the thumbnail is clicked", () => {+ render(<ImagePreview src={SRC} />);++ expect(screen.getByTestId("expand-image-button")).toBeInTheDocument();+ expect(screen.queryByTestId("image-lightbox")).not.toBeInTheDocument();+ });++ it("should open a full-size lightbox when the thumbnail is clicked", async () => {+ const user = userEvent.setup();+ render(<ImagePreview src={SRC} />);++ await user.click(screen.getByTestId("expand-image-button"));++ const lightbox = screen.getByTestId("image-lightbox");+ expect(lightbox).toBeInTheDocument();+ // The full-size image renders the same source as the thumbnail — the+ // thumbnail is only a CSS downscale, so no refetch is needed.+ expect(within(lightbox).getByRole("img")).toHaveAttribute("src", SRC);+ });++ it("should keep the lightbox out of the remove flow", async () => {+ const user = userEvent.setup();+ const onRemoveMock = vi.fn();+ render(<ImagePreview src={SRC} onRemove={onRemoveMock} />);++ await user.click(+ screen.getByRole("button", { name: "BUTTON$REMOVE_IMAGE" }),+ );++ expect(onRemoveMock).toHaveBeenCalledOnce();+ expect(screen.queryByTestId("image-lightbox")).not.toBeInTheDocument();+ });++ it("should close the lightbox via its close button", async () => {+ const user = userEvent.setup();+ render(<ImagePreview src={SRC} />);++ await user.click(screen.getByTestId("expand-image-button"));+ await user.click(screen.getByTestId("image-lightbox-close"));++ expect(screen.queryByTestId("image-lightbox")).not.toBeInTheDocument();+ });++ it("should close the lightbox when Escape is pressed", async () => {+ const user = userEvent.setup();+ render(<ImagePreview src={SRC} />);++ await user.click(screen.getByTestId("expand-image-button"));+ await user.keyboard("{Escape}");++ expect(screen.queryByTestId("image-lightbox")).not.toBeInTheDocument();});});diff --git a/src/components/features/images/image-lightbox.tsx b/src/components/features/images/image-lightbox.tsxnew file mode 100644index 0000000000..5357a1d648--- /dev/null+++ b/src/components/features/images/image-lightbox.tsx@@ -0,0 +1,52 @@+import React from "react";+import { useTranslation } from "react-i18next";+import { ModalBackdrop } from "#/components/shared/modals/modal-backdrop";+import { ModalCloseButton } from "#/components/shared/modals/modal-close-button";+import { I18nKey } from "#/i18n/declaration";++interface ImageLightboxProps {+ src: string;+ onClose: () => void;+}++/**+ * Full-size overlay for an image attachment. The thumbnail is a CSS downscale+ * of the same source, so no refetch is needed.+ */+export function ImageLightbox({ src, onClose }: ImageLightboxProps) {+ const { t } = useTranslation("openhands");++ // HeroUI consumes Escape on a document capture listener, so it never reaches+ // ModalBackdrop's window listener. Capture it here instead.+ React.useEffect(() => {+ const handleEscape = (event: KeyboardEvent) => {+ if (event.key !== "Escape") return;+ event.stopPropagation();+ onClose();+ };++ document.addEventListener("keydown", handleEscape, true);+ return () => document.removeEventListener("keydown", handleEscape, true);+ }, [onClose]);++ return (+ <ModalBackdrop+ onClose={onClose}+ closeOnEscape={false}+ aria-label={t(I18nKey.IMAGE$FULL_SIZE_PREVIEW)}+ >+ <div data-testid="image-lightbox" className="relative">+ <ModalCloseButton+ onClose={onClose}+ testId="image-lightbox-close"+ className="bg-black/60 text-white hover:bg-black/80"+ />+ <img+ src={src}+ alt={t(I18nKey.IMAGE$FULL_SIZE_PREVIEW)}+ className="max-h-[90vh] max-w-[90vw] rounded-lg object-contain"+ />+ </div>+ </ModalBackdrop>+ );+}diff --git a/src/components/features/images/image-preview.tsx b/src/components/features/images/image-preview.tsxindex 606f9be514..365c4dac76 100644--- a/src/components/features/images/image-preview.tsx+++ b/src/components/features/images/image-preview.tsx@@ -1,4 +1,8 @@+import React from "react";+import { useTranslation } from "react-i18next";import { RemoveButton } from "#/components/shared/buttons/remove-button";+import { I18nKey } from "#/i18n/declaration";+import { ImageLightbox } from "./image-lightbox";import { Thumbnail } from "./thumbnail";interface ImagePreviewProps {@@ -12,15 +16,30 @@ export function ImagePreview({onRemove,size = "small",}: ImagePreviewProps) {+ const { t } = useTranslation("openhands");+ const [isExpanded, setIsExpanded] = React.useState(false);+return (<div data-testid="image-preview" className="relative w-fit shrink-0 py-1">- <Thumbnail src={src} size={size} />+ <button+ type="button"+ data-testid="expand-image-button"+ aria-label={t(I18nKey.BUTTON$VIEW_FULL_SIZE_IMAGE)}+ onClick={() => setIsExpanded(true)}+ className="block cursor-zoom-in rounded-sm transition-opacity hover:opacity-80"+ >+ <Thumbnail src={src} size={size} />+ </button>{onRemove && (<RemoveButtononClick={onRemove}+ aria-label={t(I18nKey.BUTTON$REMOVE_IMAGE)}className="absolute right-[3px] top-[3px] cursor-pointer"/>)}+ {isExpanded && (+ <ImageLightbox src={src} onClose={() => setIsExpanded(false)} />+ )}</div>);}diff --git a/src/components/shared/buttons/remove-button.tsx b/src/components/shared/buttons/remove-button.tsxindex 12e4d61ae5..02d7f73e71 100644--- a/src/components/shared/buttons/remove-button.tsx+++ b/src/components/shared/buttons/remove-button.tsx@@ -4,13 +4,19 @@ import CloseIcon from "#/icons/close.svg?react";interface RemoveButtonProps {onClick: () => void;className?: React.HTMLAttributes<HTMLDivElement>["className"];+ "aria-label"?: string;}-export function RemoveButton({ onClick, className }: RemoveButtonProps) {+export function RemoveButton({+ onClick,+ className,+ "aria-label": ariaLabel,+}: RemoveButtonProps) {return (<buttontype="button"onClick={onClick}+ aria-label={ariaLabel}className={cn("bg-[var(--oh-muted)] rounded-full w-5 h-5 flex items-center justify-center cursor-pointer",className,diff --git a/src/i18n/translation.json b/src/i18n/translation.jsonindex 728c4e75a2..13362705cd 100644--- a/src/i18n/translation.json+++ b/src/i18n/translation.json@@ -41701,5 +41701,56 @@"uk": "Підключення \"{{name}}\" оновлено","zh-CN": "已更新连接“{{name}}”","zh-TW": "已更新連線「{{name}}」"+ },+ "BUTTON$VIEW_FULL_SIZE_IMAGE": {+ "ar": "عرض الصورة بالحجم الكامل",+ "ca": "Mostra la imatge a mida completa",+ "de": "Bild in voller Größe anzeigen",+ "en": "View full-size image",+ "es": "Ver imagen a tamaño completo",+ "fr": "Afficher l'image en taille réelle",+ "it": "Visualizza immagine a dimensione originale",+ "ja": "フルサイズの画像を表示",+ "ko-KR": "원본 크기 이미지 보기",+ "no": "Vis bildet i full størrelse",+ "pt": "Ver imagem em tamanho original",+ "tr": "Görseli tam boyutta görüntüle",+ "uk": "Переглянути зображення в повному розмірі",+ "zh-CN": "查看原始尺寸图片",+ "zh-TW": "檢視原始尺寸圖片"+ },+ "BUTTON$REMOVE_IMAGE": {+ "ar": "إزالة الصورة",+ "ca": "Elimina la imatge",+ "de": "Bild entfernen",+ "en": "Remove image",+ "es": "Eliminar imagen",+ "fr": "Supprimer l'image",+ "it": "Rimuovi immagine",+ "ja": "画像を削除",+ "ko-KR": "이미지 제거",+ "no": "Fjern bilde",+ "pt": "Remover imagem",+ "tr": "Görseli kaldır",+ "uk": "Видалити зображення",+ "zh-CN": "移除图片",+ "zh-TW": "移除圖片"+ },+ "IMAGE$FULL_SIZE_PREVIEW": {+ "ar": "معاينة الصورة بالحجم الكامل",+ "ca": "Previsualització de la imatge a mida completa",+ "de": "Vorschau des Bildes in voller Größe",+ "en": "Full-size image preview",+ "es": "Vista previa de la imagen a tamaño completo",+ "fr": "Aperçu de l'image en taille réelle",+ "it": "Anteprima dell'immagine a dimensione originale",+ "ja": "フルサイズの画像プレビュー",+ "ko-KR": "원본 크기 이미지 미리보기",+ "no": "Forhåndsvisning av bilde i full størrelse",+ "pt": "Pré-visualização da imagem em tamanho original",+ "tr": "Tam boyutlu görsel önizlemesi",+ "uk": "Попередній перегляд зображення в повному розмірі",+ "zh-CN": "原始尺寸图片预览",+ "zh-TW": "原始尺寸圖片預覽"}}