diff --git a/.changeset/lightbox-click-beside-scaled-photo.md b/.changeset/lightbox-click-beside-scaled-photo.md new file mode 100644 index 0000000000000..01e54f82bfb0c --- /dev/null +++ b/.changeset/lightbox-click-beside-scaled-photo.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[fix] A mouse click on the dark space beside a Lightbox photo now closes the viewer. Before, the viewer stayed open after a click there within the photo's natural width, or within a wider caption's width. + +@vjeux diff --git a/docs/specs/components/Lightbox/Lightbox.spec.md b/docs/specs/components/Lightbox/Lightbox.spec.md index 64e9028f1e985..60b67cd4d080c 100644 --- a/docs/specs/components/Lightbox/Lightbox.spec.md +++ b/docs/specs/components/Lightbox/Lightbox.spec.md @@ -95,7 +95,6 @@ the requirement it misses and the person it affects. close (FR8) are not built for a mouse, a pen or a trackpad: a drag on a fitted photo does nothing. On a desktop, a click in the viewer outside the photo and a band holding a caption closes it. - Implementation gap: beside a photo scaled down to fit, a click within the band as wide as the photo's natural width stays open. With zoom, a double-click on a caption zooms the photo, and a mouse does not select the caption's text, for one photo or a gallery. - **Double-click listener (FR14).** A mouse's or a pen's double-click reaches the zoom through the stage's own `dblclick` listener, since only the browser diff --git a/packages/core/src/Lightbox/Lightbox.test.tsx b/packages/core/src/Lightbox/Lightbox.test.tsx index 193904aaeb123..a1047820675c2 100644 --- a/packages/core/src/Lightbox/Lightbox.test.tsx +++ b/packages/core/src/Lightbox/Lightbox.test.tsx @@ -140,6 +140,7 @@ function rectAt(left: number, top: number, width: number, height: number) { /** * Lays out a gallery's current page for a mouse's click: its photo's box, * natural width and caption, and the viewer's width. jsdom lays out nothing. + * The photo's laid-out width is its shown width without zoom. */ function layOutPage( page: Element, @@ -164,6 +165,7 @@ function layOutPage( define(img, { getBoundingClientRect: () => photo, offsetHeight: photo.height, + offsetWidth: photo.width, naturalWidth: natural, }); // The photo's own box hugs it without zoom. @@ -837,7 +839,7 @@ describe('Lightbox', () => { }); mousePress(box, {x: 300, y: 200}); expect(onOpenChange).not.toHaveBeenCalled(); - // Beside the photo, past its natural width, the click is the backdrop's. + // Beside the photo, the click is the backdrop's. mousePress(box, {x: 20, y: 200}); expect(onOpenChange).toHaveBeenCalledWith(false); }); @@ -1070,7 +1072,7 @@ describe('Lightbox', () => { ); }); - it("stays open on a mouse click beside a narrow photo, within its caption's width", () => { + it("closes on a mouse click beside a narrow photo, level with it, within its caption's width", () => { const onOpenChange = vi.fn(); render( { caption: rectAt(100, 410, 400, 30), viewer: 1000, }); - // The media is as wide as the caption here, so this point is on it. + // The caption's band sits under the photo. Beside the photo, level + // with it, the click is the backdrop's, even within the caption's width. mousePress(box, {x: 150, y: 250}); - expect(onOpenChange).not.toHaveBeenCalled(); - // Past the caption's width, the click is the backdrop's. - mousePress(box, {x: 50, y: 250}); expect(onOpenChange).toHaveBeenCalledWith(false); }); - it('stays open on a mouse click beside a large photo, level with it, since its media spans the viewer', () => { + it('closes on a mouse click beside a large photo shown smaller, level with it', () => { const onOpenChange = vi.fn(); render( { />, ); const current = document.querySelector('[data-current]')!; - // A photo wider than the viewer, shown at 400 px. + // A photo wider than the viewer, shown at 400 px. The media is the + // shown photo, not its natural width. layOutPage(current, { photo: rectAt(300, 100, 400, 200), natural: 3000, @@ -1117,7 +1118,7 @@ describe('Lightbox', () => { x: 990, y: 200, }); - expect(onOpenChange).not.toHaveBeenCalled(); + expect(onOpenChange).toHaveBeenCalledWith(false); }); it('shows gallery counter', () => { diff --git a/packages/core/src/Lightbox/Lightbox.tsx b/packages/core/src/Lightbox/Lightbox.tsx index a629e25ae9c94..00a176c2d0407 100644 --- a/packages/core/src/Lightbox/Lightbox.tsx +++ b/packages/core/src/Lightbox/Lightbox.tsx @@ -1149,9 +1149,8 @@ export function Lightbox({ ); // Whether a mouse's or a pen's click on the media closes the viewer. It // does when its press or its release lands outside the current page's - // media. That is the fitted photo's rows, as wide as the photo's natural - // width or the caption, up to the viewer's, and the caption's own box. - // While zoomed, the photo's shown box is media too. + // media. That is the fitted photo's laid-out box, the caption's band and + // the caption's own box. While zoomed, the photo's shown box is media too. const isClickOutsideMedia = useCallback((e: ReactMouseEvent) => { const viewer = imageWrapperRef.current; const page = viewer?.querySelector('[data-current]'); @@ -1177,12 +1176,7 @@ export function Lightbox({ const top = boxRect.top + space; const bottom = top + media.offsetHeight; const caption = page.querySelector('[data-lightbox-caption]'); - const natural = - media instanceof HTMLImageElement ? media.naturalWidth : media.videoWidth; - const width = Math.min( - viewer.clientWidth, - Math.max(natural, caption?.offsetWidth ?? 0), - ); + const width = media.offsetWidth; const middle = boxRect.left + box.clientWidth / 2; const shown = media.getBoundingClientRect(); // The caption band can sit apart from the photo. There the media is the diff --git a/packages/core/src/Lightbox/LightboxFinePointer.a11y.browser.spec.ts b/packages/core/src/Lightbox/LightboxFinePointer.a11y.browser.spec.ts index 35ac569c1f34d..0d87a8581d9c9 100644 --- a/packages/core/src/Lightbox/LightboxFinePointer.a11y.browser.spec.ts +++ b/packages/core/src/Lightbox/LightboxFinePointer.a11y.browser.spec.ts @@ -440,18 +440,61 @@ test('a mouse click above the photo closes the viewer with zoom on, as before', }); }); -test('in a gallery, a mouse click beside a large photo, level with it, leaves the viewer open, as on main', async ({ +test('in a gallery, a mouse click beside a large photo shown smaller, level with it, closes the viewer', async ({ page, }) => { - // A photo wider than the viewer: the media spans the viewer's width, so - // a click beside the photo stays on it, as on main. + // A photo wider than the viewer is shown smaller. The media is the shown + // photo, not its natural width, so a click beside it is the backdrop's. const {dialog, current} = await openGallery(page); const shown = await boxOf(current); expect(shown.x).toBeGreaterThan(40); const level = shown.y + shown.height * 0.15; - await page.mouse.click(shown.x / 2, level); - await page.waitForTimeout(400); - await expect(dialog).toBeVisible(); + await expectBackdropClickCloses(page, dialog, {x: shown.x / 2, y: level}); +}); + +test('at 1280 × 800, a mouse click 4 or 20 px beside a gallery photo scaled down to fit closes the viewer, and one on the photo or its caption band keeps it open', async ({ + page, +}) => { + await page.setViewportSize({width: 1280, height: 800}); + // The story's own photo size, which the window scales down to fit. + await page.route('https://picsum.photos/**', async route => + route.fulfill({ + contentType: 'image/svg+xml', + body: photoSvg(1200, 800), + }), + ); + const open = async () => { + await page.goto( + `${storybook.origin}/iframe.html?id=core-lightbox--gallery-with-zoom&viewMode=story`, + ); + await page.getByRole('button', {name: 'Open gallery with zoom'}).click(); + const dialog = page.getByRole('dialog'); + await expect(dialog).toBeVisible(); + const current = dialog.locator('[data-current] img'); + await expect + .poll(async () => + current.evaluate(el => (el as HTMLImageElement).naturalWidth), + ) + .toBe(1200); + await settled(page); + return {dialog, shown: await boxOf(current)}; + }; + let {dialog, shown} = await open(); + expect(shown.width).toBeLessThan(1200); + const level = shown.y + shown.height / 2; + for (const gap of [4, 20]) { + await expectBackdropClickCloses(page, dialog, {x: shown.x - gap, y: level}); + ({dialog, shown} = await open()); + } + expect(await staysOpenAfterClick(page, centre(shown))).toBe(true); + const {band, caption} = await captionBoxes(dialog); + expect(band.x).toBeLessThan(caption.x - 12); + expect( + await staysOpenAfterClick(page, { + x: band.x + 12, + y: caption.y + caption.height / 2, + }), + ).toBe(true); }); test('a mouse click on the gallery counter leaves the viewer open, as on main', async ({ @@ -471,7 +514,7 @@ test('a mouse click on the gallery counter leaves the viewer open, as on main', await expect(dialog).toBeVisible(); }); -test('without zoom, a mouse click at the far edge beside a large gallery photo leaves the viewer open, as on main', async ({ +test('without zoom, a mouse click at the far edge beside a large gallery photo closes the viewer', async ({ page, }) => { await page.setViewportSize({width: 1280, height: 800}); @@ -497,9 +540,10 @@ test('without zoom, a mouse click at the far edge beside a large gallery photo l const shown = await boxOf(current); // The photo leaves dark area at the window's right edge, level with it. expect(1280 - (shown.x + shown.width)).toBeGreaterThan(20); - await page.mouse.click(1276, shown.y + shown.height / 2); - await page.waitForTimeout(400); - await expect(dialog).toBeVisible(); + await expectBackdropClickCloses(page, dialog, { + x: 1276, + y: shown.y + shown.height / 2, + }); }); test('a mouse double-click zooms 2× about the clicked point, and the next zooms back', async ({ @@ -736,23 +780,20 @@ async function staysOpenAfterClick( return page.evaluate(() => document.querySelector('dialog')?.open === true); } -test('a mouse click beside the fitted photo, level with it, leaves the viewer open, as on main', async ({ +test('a mouse click beside the fitted photo, level with it, closes the viewer on either side', async ({ page, }) => { - const {fitted, stageBox} = await openViewer(page); - // The fitted photo leaves dark stage on both sides. Main's media spans the - // viewer's width there, so a click beside the photo does not close. + const {dialog, fitted, stageBox} = await openViewer(page); + // The fitted photo leaves dark stage on both sides, where a click is the + // backdrop's. expect(fitted.x - stageBox.x).toBeGreaterThan(40); const level = fitted.y + fitted.height / 2; - expect(await staysOpenAfterClick(page, {x: stageBox.x + 20, y: level})).toBe( - true, - ); - expect( - await staysOpenAfterClick(page, { - x: fitted.x + fitted.width + 20, - y: level, - }), - ).toBe(true); + await expectBackdropClickCloses(page, dialog, {x: stageBox.x + 20, y: level}); + const again = await openViewer(page); + await expectBackdropClickCloses(page, again.dialog, { + x: again.fitted.x + again.fitted.width + 20, + y: level, + }); }); test('with zoom on, a mouse click on the photo leaves the viewer open and one below it closes, as on main', async ({ @@ -805,7 +846,7 @@ test('a mouse press on the fitted photo released above it closes the viewer, as ).toBe(false); }); -test('without zoom, a mouse click beside the photo leaves the viewer open and one above it closes, as on main', async ({ +test('without zoom, a mouse click beside the photo or above it closes the viewer', async ({ page, }) => { await page.route('https://picsum.photos/**', async route => @@ -836,7 +877,7 @@ test('without zoom, a mouse click beside the photo leaves the viewer open and on x: fitted.x / 2, y: fitted.y + fitted.height / 2, }), - ).toBe(true); + ).toBe(false); await page.setViewportSize({width: 1280, height: 1100}); const tall = await open(); expect(tall.y).toBeGreaterThan(40); @@ -942,46 +983,54 @@ test('with zoom, a mouse double-click on a single photo’s caption zooms the ph ); }); -test('in a gallery with zoom, a mouse click beside a small photo closes past its caption’s width and stays open within it, as on main', async ({ +test('in a gallery with zoom, a mouse click beside a narrow photo, level with it, closes the viewer within its caption’s width or past it', async ({ page, }) => { - // A photo narrower than the viewer: the media is as wide as its caption, - // which is wider than the photo here. + // A photo narrower than its caption. The caption's band sits under the + // photo, so the dark space beside the photo is the backdrop's. await page.setViewportSize({width: 1280, height: 1100}); await page.route('https://picsum.photos/**', async route => route.fulfill({contentType: 'image/svg+xml', body: photoSvg(200, 1200)}), ); - await page.goto( - `${storybook.origin}/iframe.html?id=core-lightbox--gallery-with-zoom&viewMode=story`, - ); - await page.getByRole('button', {name: 'Open gallery with zoom'}).click(); - const dialog = page.getByRole('dialog'); - await expect(dialog).toBeVisible(); - const img = dialog.getByRole('img').first(); - await expect - .poll(async () => img.evaluate(el => (el as HTMLImageElement).naturalWidth)) - .toBe(200); - await settled(page); - const photo = await boxOf(img); - // The current page's own caption. - const caption = await dialog.evaluate(viewer => { - const r = viewer - .querySelector('[data-current] [data-lightbox-caption]') - ?.getBoundingClientRect(); - return r == null ? null : {x: r.x, width: r.width}; - }); - if (caption == null) { - throw new Error('no caption'); - } - const level = photo.y + photo.height / 2; + const open = async () => { + await page.goto( + `${storybook.origin}/iframe.html?id=core-lightbox--gallery-with-zoom&viewMode=story`, + ); + await page.getByRole('button', {name: 'Open gallery with zoom'}).click(); + const dialog = page.getByRole('dialog'); + await expect(dialog).toBeVisible(); + const img = dialog.getByRole('img').first(); + await expect + .poll(async () => + img.evaluate(el => (el as HTMLImageElement).naturalWidth), + ) + .toBe(200); + await settled(page); + // The current page's own caption. + const caption = await dialog.evaluate(viewer => { + const r = viewer + .querySelector('[data-current] [data-lightbox-caption]') + ?.getBoundingClientRect(); + return r == null ? null : {x: r.x, width: r.width}; + }); + if (caption == null) { + throw new Error('no caption'); + } + return {dialog, photo: await boxOf(img), caption}; + }; + const first = await open(); + const level = first.photo.y + first.photo.height / 2; // Within the caption's width, beside the photo. - expect(caption.x).toBeLessThan(photo.x - 10); - expect( - await staysOpenAfterClick(page, {x: (caption.x + photo.x) / 2, y: level}), - ).toBe(true); + expect(first.caption.x).toBeLessThan(first.photo.x - 10); + await expectBackdropClickCloses(page, first.dialog, { + x: (first.caption.x + first.photo.x) / 2, + y: level, + }); // Past the caption's width. - expect(caption.x).toBeGreaterThan(60); - expect(await staysOpenAfterClick(page, {x: caption.x - 30, y: level})).toBe( - false, - ); + const second = await open(); + expect(second.caption.x).toBeGreaterThan(60); + await expectBackdropClickCloses(page, second.dialog, { + x: second.caption.x - 30, + y: level, + }); }); diff --git a/packages/core/src/Lightbox/__tests__/LightboxPagingModes.interaction.browser.spec.ts b/packages/core/src/Lightbox/__tests__/LightboxPagingModes.interaction.browser.spec.ts index f9b8ea0059b6f..261ced00f16a5 100644 --- a/packages/core/src/Lightbox/__tests__/LightboxPagingModes.interaction.browser.spec.ts +++ b/packages/core/src/Lightbox/__tests__/LightboxPagingModes.interaction.browser.spec.ts @@ -619,7 +619,7 @@ test('without zoom, a mouse press beside the photo that is released on it closes } }); -test('without zoom, a mouse click beside a photo narrower than its caption leaves the gallery open, as on main', async ({ +test('without zoom, a mouse click beside a photo narrower than its caption, level with it, closes the gallery', async ({ browser, }) => { const context = await browser.newContext({ @@ -671,9 +671,10 @@ test('without zoom, a mouse click beside a photo narrower than its caption leave ); }, beside), ).toBe(true); + // The caption's band sits under the photo, so this point is the + // backdrop's. await page.mouse.click(beside.x, beside.y); - await frames(page, 40); - await expect(dialog).toBeVisible(); + await expect(dialog).toBeHidden(); } finally { await context.close(); } diff --git a/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts b/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts index 22e3f492cb60d..9c804ccb9e9c6 100644 --- a/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts +++ b/packages/core/src/Lightbox/__tests__/LightboxZoomModes.interaction.browser.spec.ts @@ -322,7 +322,7 @@ test('with a mouse the zoom-in cursor shows over the fitted photo, not over the if (photo == null) { throw new Error('no photo'); } - // The fitted photo leaves dark stage on its left, where a click closes. + // The fitted photo leaves dark stage on its left. expect(photo.x - stage.x).toBeGreaterThan(20); const cursorAt = async (x: number, y: number) => { await page.mouse.move(x, y); @@ -334,6 +334,9 @@ test('with a mouse the zoom-in cursor shows over the fitted photo, not over the expect(await cursorAt(stage.x + 20, y)).toBe('default'); expect(await cursorAt(photo.x + photo.width / 2, y)).toBe('zoom-in'); expect(await cursorAt(stage.x + 20, y)).toBe('default'); + // A click on that dark stage closes the viewer. + await page.mouse.click(stage.x + 20, y); + await expect(page.getByRole('dialog')).toBeHidden(); } finally { await context.close(); } @@ -375,7 +378,7 @@ test('with a mouse a gallery page shows the zoom-in cursor over the fitted photo }; const x = photo.x + photo.width / 2; const y = photo.y + photo.height / 2; - // The fitted photo leaves dark stage on its left, where a click closes. + // The fitted photo leaves dark stage on its left. expect(await cursorAt(photo.x - 20, y)).toBe('default'); expect(await cursorAt(x, y)).toBe('zoom-in'); expect(await cursorAt(photo.x - 20, y)).toBe('default'); @@ -384,6 +387,13 @@ test('with a mouse a gallery page shows the zoom-in cursor over the fitted photo await waitForRest(img); expect((await readTransform(img)).scale).toBeGreaterThan(1); expect(await cursorAt(x, y)).toBe('grab'); + // Back at fit, a click on the dark stage beside the photo closes the + // viewer. + await page.mouse.dblclick(x, y); + await waitForRest(img); + expect((await readTransform(img)).scale).toBe(1); + await page.mouse.click(photo.x - 20, y); + await expect(dialog).toBeHidden(); } finally { await context.close(); }