diff --git a/packages/pluggableWidgets/image-web/CHANGELOG.md b/packages/pluggableWidgets/image-web/CHANGELOG.md index 97b0e045d3..ea7e972f4d 100644 --- a/packages/pluggableWidgets/image-web/CHANGELOG.md +++ b/packages/pluggableWidgets/image-web/CHANGELOG.md @@ -6,6 +6,12 @@ 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, 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/Image.tsx b/packages/pluggableWidgets/image-web/src/components/Image/Image.tsx index a9c134b5cf..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 { cloneElement, 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"; @@ -61,6 +62,7 @@ export const Image: FunctionComponent = ({ backgroundImageContent }) => { const { lightboxIsOpen, openLightbox, closeLightbox } = useLightboxState(); + const [sizeOnPage, setSizeOnPage] = useState(); const onCloseLightbox = useCallback( event => { @@ -76,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(); } }, @@ -135,7 +139,17 @@ 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/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/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 eb5a5e9d2e..66865cbd7a 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"; @@ -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(); @@ -163,6 +181,75 @@ 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("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 6fbec8e994..eba9e84524 100644 --- a/packages/pluggableWidgets/image-web/src/ui/Image.scss +++ b/packages/pluggableWidgets/image-web/src/ui/Image.scss @@ -44,6 +44,20 @@ top: 50%; left: 50%; transform: translate(-50%, -50%); + + --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: var(--mx-image-viewer-lightbox-max-width); + max-height: var(--mx-image-viewer-lightbox-max-height); + } } .mx-image-viewer.mx-image-background {