From e77b231822ee76d80a193f057f35e7701294148d Mon Sep 17 00:00:00 2001 From: gjulivan Date: Wed, 4 Nov 2026 13:49:32 +0100 Subject: [PATCH 1/3] fix: image lightbox inherit image sizing --- .../pluggableWidgets/image-web/CHANGELOG.md | 4 ++ .../image-web/src/components/Image/Image.tsx | 37 ++++++++++++++++++- .../src/components/__tests__/Image.spec.tsx | 27 ++++++++++++++ .../image-web/src/ui/Image.scss | 9 +++++ 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/packages/pluggableWidgets/image-web/CHANGELOG.md b/packages/pluggableWidgets/image-web/CHANGELOG.md index 97b0e045d3..47756efb43 100644 --- a/packages/pluggableWidgets/image-web/CHANGELOG.md +++ b/packages/pluggableWidgets/image-web/CHANGELOG.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Fixed + +- We fixed an issue where an image enlarged on click was shown smaller than the image on the page when its width was a percentage or a maximum height was set. The enlarged image now shows at its natural size, scaled down to fit the screen. + ## [1.6.1] - 2026-08-14 ### Fixed diff --git a/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx b/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx index a9c134b5cf..6b9a951095 100644 --- a/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx +++ b/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx @@ -1,4 +1,4 @@ -import { cloneElement, CSSProperties, FunctionComponent, ReactNode, useCallback } from "react"; +import { CSSProperties, FunctionComponent, ReactNode, useCallback } from "react"; import { ImageContentProps, ImageUi } from "./ui"; import { DisplayAsEnum, HeightUnitEnum, OnClickTypeEnum, WidthUnitEnum } from "../../../typings/ImageProps"; import { useLightboxState } from "../../utils/lightboxState"; @@ -40,6 +40,26 @@ function processImageLink(imageLink: string | undefined, displayAs: DisplayAsEnu return url.href; } +/** + * The enlarged image must not inherit the sizing of the image on the page: a percentage width would resolve + * against the shrink-to-fit lightbox and a max height would cap it. Its size comes from `Image.scss` instead. + */ +function getLightboxImageStyle(style: CSSProperties | undefined): CSSProperties | undefined { + if (!style) { + return style; + } + const { + width: _width, + height: _height, + minWidth: _minWidth, + minHeight: _minHeight, + maxWidth: _maxWidth, + maxHeight: _maxHeight, + ...rest + } = style; + return rest; +} + export const Image: FunctionComponent = ({ class: className, style, @@ -135,7 +155,20 @@ export const Image: FunctionComponent = ({ {content} {!previewMode && lightboxIsOpen && ( - {type === "image" ? cloneElement(content, { image, onClick: undefined }) : content} + {type === "image" ? ( + + ) : ( + content + )} )} diff --git a/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx b/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx index eb5a5e9d2e..a14b84b8f7 100644 --- a/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx +++ b/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx @@ -163,6 +163,33 @@ describe("Image", () => { expect(lightbox).toBeInTheDocument(); }); + it("does not apply the sizing of the image on the page to the lightbox image", async () => { + const { getByRole, getAllByRole } = render( + + ); + const thumbnail = getByRole("button"); + expect(thumbnail).toHaveStyle({ width: "50%", maxHeight: "250px" }); + + await user.click(thumbnail); + + // The thumbnail has role "button"; the only image is the one in the lightbox. + const [lightboxImage] = getAllByRole("img") as HTMLImageElement[]; + expect(lightboxImage.tagName).toBe("IMG"); + expect(lightboxImage.style.width).toBe(""); + expect(lightboxImage.style.height).toBe(""); + expect(lightboxImage.style.maxHeight).toBe(""); + expect(lightboxImage.style.minHeight).toBe(""); + expect(lightboxImage.style.opacity).toBe("0.9"); + }); + it("closes the lightbox when the user clicks on the close button after opening it", async () => { const { container, getByRole } = render(); const image = getByRole("button"); diff --git a/packages/pluggableWidgets/image-web/src/ui/Image.scss b/packages/pluggableWidgets/image-web/src/ui/Image.scss index 6fbec8e994..6561f8e359 100644 --- a/packages/pluggableWidgets/image-web/src/ui/Image.scss +++ b/packages/pluggableWidgets/image-web/src/ui/Image.scss @@ -44,6 +44,15 @@ top: 50%; left: 50%; transform: translate(-50%, -50%); + + // Show the image at its natural size, scaled down to fit the viewport. + img { + display: block; + width: auto; + height: auto; + max-width: 90vw; + max-height: 85vh; + } } .mx-image-viewer.mx-image-background { From 855f6ea76278c2cd8d8d5465ad2d24a7c052e227 Mon Sep 17 00:00:00 2001 From: gjulivan Date: Fri, 13 Nov 2026 14:24:52 +0000 Subject: [PATCH 2/3] fix(image-web): lightbox size for thumbnails, small images and aspect-ratio styles - Give the lightbox image its own class so its max width and height win over Atlas's `img.img-thumbnail { max-width: 100% }`, which capped a thumbnail at half the window. - Never show the enlarged image smaller than the image on the page: measure the image on click and, when that is larger than the natural size, set a width that keeps the aspect ratio and still fits the viewport. - Drop aspect-ratio, object-fit and object-position from the inline style passed to the lightbox image, so it is not cropped. Co-Authored-By: Claude Opus 5.5 --- .../pluggableWidgets/image-web/CHANGELOG.md | 2 +- .../image-web/src/components/Image/Image.tsx | 35 ++------ .../src/components/Image/LightboxImage.tsx | 82 +++++++++++++++++++ .../src/components/__tests__/Image.spec.tsx | 44 +++++++++- .../__tests__/LightboxImage.spec.tsx | 38 +++++++++ .../image-web/src/ui/Image.scss | 13 ++- 6 files changed, 181 insertions(+), 33 deletions(-) create mode 100644 packages/pluggableWidgets/image-web/src/components/Image/LightboxImage.tsx create mode 100644 packages/pluggableWidgets/image-web/src/components/__tests__/LightboxImage.spec.tsx diff --git a/packages/pluggableWidgets/image-web/CHANGELOG.md b/packages/pluggableWidgets/image-web/CHANGELOG.md index 47756efb43..d9f88bd993 100644 --- a/packages/pluggableWidgets/image-web/CHANGELOG.md +++ b/packages/pluggableWidgets/image-web/CHANGELOG.md @@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Fixed -- We fixed an issue where an image enlarged on click was shown smaller than the image on the page when its width was a percentage or a maximum height was set. The enlarged image now shows at its natural size, scaled down to fit the screen. +- We fixed an issue where an image enlarged on click was shown smaller than the image on the page when its width was a percentage or a maximum height was set. The enlarged image now shows at its natural size, or at its size on the page when that is larger, scaled down to fit the screen. ## [1.6.1] - 2026-08-14 diff --git a/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx b/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx index 6b9a951095..0505262957 100644 --- a/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx +++ b/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx @@ -1,4 +1,5 @@ -import { CSSProperties, FunctionComponent, ReactNode, useCallback } from "react"; +import { CSSProperties, FunctionComponent, ReactNode, useCallback, useState } from "react"; +import { ImageSize, LightboxImage } from "./LightboxImage"; import { ImageContentProps, ImageUi } from "./ui"; import { DisplayAsEnum, HeightUnitEnum, OnClickTypeEnum, WidthUnitEnum } from "../../../typings/ImageProps"; import { useLightboxState } from "../../utils/lightboxState"; @@ -40,26 +41,6 @@ function processImageLink(imageLink: string | undefined, displayAs: DisplayAsEnu return url.href; } -/** - * The enlarged image must not inherit the sizing of the image on the page: a percentage width would resolve - * against the shrink-to-fit lightbox and a max height would cap it. Its size comes from `Image.scss` instead. - */ -function getLightboxImageStyle(style: CSSProperties | undefined): CSSProperties | undefined { - if (!style) { - return style; - } - const { - width: _width, - height: _height, - minWidth: _minWidth, - minHeight: _minHeight, - maxWidth: _maxWidth, - maxHeight: _maxHeight, - ...rest - } = style; - return rest; -} - export const Image: FunctionComponent = ({ class: className, style, @@ -81,6 +62,7 @@ export const Image: FunctionComponent = ({ backgroundImageContent }) => { const { lightboxIsOpen, openLightbox, closeLightbox } = useLightboxState(); + const [sizeOnPage, setSizeOnPage] = useState(); const onCloseLightbox = useCallback( event => { @@ -96,6 +78,8 @@ export const Image: FunctionComponent = ({ if (onClickType === "action") { onClick?.(); } else if (onClickType === "enlarge") { + const { width, height } = event.currentTarget.getBoundingClientRect(); + setSizeOnPage({ width, height }); openLightbox(); } }, @@ -156,15 +140,12 @@ export const Image: FunctionComponent = ({ {!previewMode && lightboxIsOpen && ( {type === "image" ? ( - ) : ( content diff --git a/packages/pluggableWidgets/image-web/src/components/Image/LightboxImage.tsx b/packages/pluggableWidgets/image-web/src/components/Image/LightboxImage.tsx new file mode 100644 index 0000000000..03df0bab8a --- /dev/null +++ b/packages/pluggableWidgets/image-web/src/components/Image/LightboxImage.tsx @@ -0,0 +1,82 @@ +import classNames from "classnames"; +import { CSSProperties, ReactElement, SyntheticEvent, useCallback, useState } from "react"; + +export interface ImageSize { + width: number; + height: number; +} + +export interface LightboxImageProps { + image: string | undefined; + className?: string; + style?: CSSProperties; + altText?: string; + /** The size of the image on the page. The enlarged image is never shown smaller than this. */ + minSize?: ImageSize; +} + +/** + * Style properties of the image on the page that only make sense for the box it has on the page. The enlarged + * image gets its size from `Image.scss` and `getLightboxImageWidth` instead. + */ +const PAGE_ONLY_STYLE_PROPERTIES = [ + "width", + "height", + "minWidth", + "minHeight", + "maxWidth", + "maxHeight", + "aspectRatio", + "objectFit", + "objectPosition" +] as const; + +export function getLightboxImageStyle(style: CSSProperties | undefined): CSSProperties | undefined { + if (!style) { + return style; + } + const rest: CSSProperties = { ...style }; + for (const property of PAGE_ONLY_STYLE_PROPERTIES) { + delete rest[property]; + } + return rest; +} + +/** + * The enlarged image shows at its natural size, but at least at the size of the image on the page. It is scaled + * down to fit the viewport, keeping the aspect ratio of the image. Returns `undefined` when the natural size is + * large enough, so that `Image.scss` alone sizes the image. + */ +export function getLightboxImageWidth(naturalSize: ImageSize, minSize: ImageSize | undefined): string | undefined { + if (!minSize || naturalSize.width <= 0 || naturalSize.height <= 0) { + return undefined; + } + const ratio = naturalSize.width / naturalSize.height; + const width = Math.ceil(Math.max(minSize.width, minSize.height * ratio)); + if (width <= naturalSize.width) { + return undefined; + } + return `min(${width}px, var(--mx-image-viewer-lightbox-max-width), calc(var(--mx-image-viewer-lightbox-max-height) * ${ratio}))`; +} + +export function LightboxImage({ image, className, style, altText, minSize }: LightboxImageProps): ReactElement { + const [naturalSize, setNaturalSize] = useState(); + + const onLoad = useCallback((event: SyntheticEvent) => { + const { naturalWidth, naturalHeight } = event.currentTarget; + setNaturalSize({ width: naturalWidth, height: naturalHeight }); + }, []); + + const width = naturalSize ? getLightboxImageWidth(naturalSize, minSize) : undefined; + + return ( + {altText} + ); +} diff --git a/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx b/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx index a14b84b8f7..6014a05705 100644 --- a/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx +++ b/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx @@ -1,5 +1,5 @@ import "@testing-library/jest-dom"; -import { render } from "@testing-library/react"; +import { fireEvent, render } from "@testing-library/react"; import userEvent, { UserEvent } from "@testing-library/user-event"; import { forwardRef } from "react"; import { ModalProps } from "react-overlays/esm/Modal"; @@ -190,6 +190,48 @@ describe("Image", () => { expect(lightboxImage.style.opacity).toBe("0.9"); }); + it("does not apply the aspect ratio or object fit of the image on the page to the lightbox image", async () => { + const { getByRole, getAllByRole } = render( + + ); + await user.click(getByRole("button")); + + const [lightboxImage] = getAllByRole("img") as HTMLImageElement[]; + expect(lightboxImage.style.aspectRatio).toBe(""); + expect(lightboxImage.style.objectFit).toBe(""); + expect(lightboxImage.style.objectPosition).toBe(""); + }); + + it("keeps the thumbnail class and adds the lightbox image class", async () => { + const { getByRole, getAllByRole } = render( + + ); + expect(getByRole("button").getAttribute("src")).toContain("thumb=true"); + await user.click(getByRole("button")); + + const [lightboxImage] = getAllByRole("img") as HTMLImageElement[]; + expect(lightboxImage).toHaveClass("img-thumbnail", "mx-image-viewer-lightbox-image"); + expect(lightboxImage.getAttribute("src")).not.toContain("thumb=true"); + }); + + it("shows the lightbox image at least at the size of the image on the page", async () => { + const { getByRole, getAllByRole } = render(); + const imageOnPage = getByRole("button"); + jest.spyOn(imageOnPage, "getBoundingClientRect").mockReturnValue({ width: 560, height: 420 } as DOMRect); + await user.click(imageOnPage); + + const [lightboxImage] = getAllByRole("img") as HTMLImageElement[]; + Object.defineProperty(lightboxImage, "naturalWidth", { value: 400 }); + Object.defineProperty(lightboxImage, "naturalHeight", { value: 300 }); + fireEvent.load(lightboxImage); + + expect(lightboxImage.getAttribute("style")).toContain("min(560px"); + }); + it("closes the lightbox when the user clicks on the close button after opening it", async () => { const { container, getByRole } = render(); const image = getByRole("button"); diff --git a/packages/pluggableWidgets/image-web/src/components/__tests__/LightboxImage.spec.tsx b/packages/pluggableWidgets/image-web/src/components/__tests__/LightboxImage.spec.tsx new file mode 100644 index 0000000000..ff889a69aa --- /dev/null +++ b/packages/pluggableWidgets/image-web/src/components/__tests__/LightboxImage.spec.tsx @@ -0,0 +1,38 @@ +import { getLightboxImageStyle, getLightboxImageWidth } from "../Image/LightboxImage"; + +describe("getLightboxImageWidth", () => { + it("returns undefined when the natural size is at least the size on the page", () => { + expect(getLightboxImageWidth({ width: 400, height: 300 }, { width: 200, height: 150 })).toBeUndefined(); + expect(getLightboxImageWidth({ width: 400, height: 300 }, { width: 400, height: 300 })).toBeUndefined(); + expect(getLightboxImageWidth({ width: 400, height: 300 }, undefined)).toBeUndefined(); + }); + + it("uses the width on the page when it is larger than the natural width", () => { + expect(getLightboxImageWidth({ width: 400, height: 300 }, { width: 560, height: 420 })).toBe( + "min(560px, var(--mx-image-viewer-lightbox-max-width), calc(var(--mx-image-viewer-lightbox-max-height) * 1.3333333333333333))" + ); + }); + + it("uses the height on the page when the image on the page is taller than its aspect ratio", () => { + expect(getLightboxImageWidth({ width: 160, height: 120 }, { width: 200, height: 450 })).toMatch(/^min\(600px,/); + }); + + it("does not enlarge a cropped image whose box on the page fits inside the natural size", () => { + // aspect-ratio: 1 / 1 with object-fit: cover shows a 400x300 image at 300x300. + expect(getLightboxImageWidth({ width: 400, height: 300 }, { width: 300, height: 300 })).toBeUndefined(); + }); +}); + +describe("getLightboxImageStyle", () => { + it("removes the properties that size the image on the page and keeps the others", () => { + expect( + getLightboxImageStyle({ + width: "50%", + maxHeight: "250px", + aspectRatio: "1 / 1", + objectFit: "cover", + opacity: 0.9 + }) + ).toEqual({ opacity: 0.9 }); + }); +}); diff --git a/packages/pluggableWidgets/image-web/src/ui/Image.scss b/packages/pluggableWidgets/image-web/src/ui/Image.scss index 6561f8e359..eba9e84524 100644 --- a/packages/pluggableWidgets/image-web/src/ui/Image.scss +++ b/packages/pluggableWidgets/image-web/src/ui/Image.scss @@ -45,13 +45,18 @@ left: 50%; transform: translate(-50%, -50%); - // Show the image at its natural size, scaled down to fit the viewport. - img { + --mx-image-viewer-lightbox-max-width: 90vw; + --mx-image-viewer-lightbox-max-height: 85vh; + + // Show the image at its natural size, scaled down to fit the viewport. The class selector is needed to win + // over Atlas rules with the same specificity, such as `img.img-thumbnail { max-width: 100% }`. + // `LightboxImage` sets a width when the image on the page is larger than the natural size. + .mx-image-viewer-lightbox-image { display: block; width: auto; height: auto; - max-width: 90vw; - max-height: 85vh; + max-width: var(--mx-image-viewer-lightbox-max-width); + max-height: var(--mx-image-viewer-lightbox-max-height); } } From 4acea0e66ef8b30ad886dbd9182dcd236cd0aa74 Mon Sep 17 00:00:00 2001 From: gjulivan Date: Mon, 16 Nov 2026 23:05:16 +0100 Subject: [PATCH 3/3] fix(image-web): apply height in viewport units ContentImage and BackgroundImage overwrote the height from constructStyleObject with getStyle(), which only handled pixels and percentage and returned "" for viewport, so a height in vh was dropped. Co-Authored-By: Claude Opus 5.5 --- .../pluggableWidgets/image-web/CHANGELOG.md | 2 ++ .../image-web/src/components/Image/ui.tsx | 2 ++ .../src/components/__tests__/Image.spec.tsx | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/packages/pluggableWidgets/image-web/CHANGELOG.md b/packages/pluggableWidgets/image-web/CHANGELOG.md index d9f88bd993..ea7e972f4d 100644 --- a/packages/pluggableWidgets/image-web/CHANGELOG.md +++ b/packages/pluggableWidgets/image-web/CHANGELOG.md @@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - We fixed an issue where an image enlarged on click was shown smaller than the image on the page when its width was a percentage or a maximum height was set. The enlarged image now shows at its natural size, or at its size on the page when that is larger, scaled down to fit the screen. +- We fixed an issue where a height in viewport units was not applied to the image. + ## [1.6.1] - 2026-08-14 ### Fixed diff --git a/packages/pluggableWidgets/image-web/src/components/Image/ui.tsx b/packages/pluggableWidgets/image-web/src/components/Image/ui.tsx index 5868c11315..fac1f3276f 100644 --- a/packages/pluggableWidgets/image-web/src/components/Image/ui.tsx +++ b/packages/pluggableWidgets/image-web/src/components/Image/ui.tsx @@ -11,6 +11,8 @@ function getStyle(value: string | number, type: WidthUnitEnum | HeightUnitEnum): return value; } else if (type === "percentage") { return value + "%"; + } else if (type === "viewport") { + return value + "vh"; } return ""; diff --git a/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx b/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx index 6014a05705..66865cbd7a 100644 --- a/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx +++ b/packages/pluggableWidgets/image-web/src/components/__tests__/Image.spec.tsx @@ -91,6 +91,24 @@ describe("Image", () => { expect(image.asFragment()).toMatchSnapshot(); }); + it("applies a height in viewport units to the image", () => { + const { getByRole } = render(); + expect(getByRole("img")).toHaveStyle({ height: "30vh" }); + }); + + it("applies a height in viewport units to the background image", () => { + const { container } = render( + Image content} + /> + ); + expect(container.querySelector(".mx-image-background")).toHaveStyle({ height: "30vh" }); + }); + it("renders the structure with a glyph icon", () => { const image = render(); expect(image.asFragment()).toMatchSnapshot();