A platform for community discussion. Free, open, simple.
UX: close lightbox on backdrop tap on mobile (#35571)
David Battersby62ed3bed2f4a
diff --git a/frontend/discourse/app/lib/lightbox.js b/frontend/discourse/app/lib/lightbox.jsindex dc08dd12859..ffe830e460c 100644--- a/frontend/discourse/app/lib/lightbox.js+++ b/frontend/discourse/app/lib/lightbox.js@@ -1,7 +1,7 @@import { waitForPromise } from "@ember/test-waiters";import $ from "jquery";import { spinnerHTML } from "discourse/helpers/loading-spinner";-import { isTesting } from "discourse/lib/environment";+import { isRailsTesting, isTesting } from "discourse/lib/environment";import { helperContext } from "discourse/lib/helpers";import { renderIcon } from "discourse/lib/icon-library";import { SELECTORS } from "discourse/lib/lightbox/constants";@@ -28,6 +28,7 @@ export default async function lightbox(elem, siteSettings) {if (siteSettings.experimental_lightbox) {const { default: PhotoSwipeLightbox } = await import("photoswipe/lightbox");+ const isTestEnv = isTesting() || isRailsTesting();const lightboxEl = new PhotoSwipeLightbox({gallery: elem,@@ -37,17 +38,9 @@ export default async function lightbox(elem, siteSettings) {closeTitle: i18n("lightbox.close"),zoomTitle: i18n("lightbox.zoom"),errorMsg: i18n("lightbox.error"),- paddingFn: (viewportSize, itemData) => {- if (viewportSize.x < 1200 || caps.isMobileDevice) {- return { top: 0, bottom: 0, left: 0, right: 0 };- }- return {- top: 20,- bottom: itemData.title ? 75 : 20,- left: 20,- right: 20,- };- },+ showHideAnimationType: isTestEnv ? "none" : "zoom",+ tapAction,+ paddingFn,pswpModule: async () => await import("photoswipe"),appendToEl: isTesting() && document.getElementById("ember-testing"),});@@ -166,6 +159,27 @@ export default async function lightbox(elem, siteSettings) {return data;});+ function tapAction(pt, event) {+ const pswp = lightboxEl.pswp;+ if (event.target.classList.contains("pswp__img")) {+ pswp?.element?.classList.toggle("pswp--ui-visible");+ } else {+ pswp?.close();+ }+ }++ function paddingFn(viewportSize, itemData) {+ if (viewportSize.x < 1200 || caps.isMobileDevice) {+ return { top: 0, bottom: 0, left: 0, right: 0 };+ }+ return {+ top: 20,+ bottom: itemData.title ? 75 : 20,+ left: 20,+ right: 20,+ };+ }+lightboxEl.init();} else {// Magnific lightboxdiff --git a/spec/support/system_helpers.rb b/spec/support/system_helpers.rbindex ebc465d988d..18dfda521c6 100644--- a/spec/support/system_helpers.rb+++ b/spec/support/system_helpers.rb@@ -381,4 +381,8 @@ module SystemHelperspage.driver.with_playwright_page { |pw_page| pw_page.locator(selector) }endend++ def tap_screen_at(x, y)+ page.driver.with_playwright_page { |pw_page| pw_page.touchscreen.tap_point(x, y) }+ endenddiff --git a/spec/system/lightbox_spec.rb b/spec/system/lightbox_spec.rbindex 9ae921fc112..b8d5bfea7af 100644--- a/spec/system/lightbox_spec.rb+++ b/spec/system/lightbox_spec.rb@@ -68,4 +68,40 @@ describe "Lightbox | Photoswipe", type: :system doexpect(lightbox).to have_original_image_buttonendend++ context "when on mobile", mobile: true do+ let(:screen_center_x) { page.evaluate_script("window.innerWidth / 2") }+ let(:screen_center_y) { page.evaluate_script("window.innerHeight / 2") }++ before do+ upload_1.update(width: 400, height: 300)+ cpp.post_process+ post.update(cooked: cpp.html)+ end++ it "tap image to toggle UI" do+ topic_page.visit_topic(topic)+ find("#post_1 a.lightbox").click++ expect(lightbox).to be_visible+ expect(lightbox).to have_ui_visible++ tap_screen_at(screen_center_x, screen_center_y)+ expect(lightbox).to have_no_ui_visible++ tap_screen_at(screen_center_x, screen_center_y)+ expect(lightbox).to have_ui_visible+ end++ it "tap backdrop to close lightbox" do+ topic_page.visit_topic(topic)+ find("#post_1 a.lightbox").click++ expect(lightbox).to be_visible++ tap_screen_at(50, 50)++ expect(lightbox).to be_hidden+ end+ endenddiff --git a/spec/system/page_objects/components/photoswipe.rb b/spec/system/page_objects/components/photoswipe.rbindex e0d14146f09..b2429e16c3f 100644--- a/spec/system/page_objects/components/photoswipe.rb+++ b/spec/system/page_objects/components/photoswipe.rb@@ -16,6 +16,7 @@ module PageObjectsCAPTION = ".pswp__caption"CAPTION_TITLE = ".pswp__caption-title"CAPTION_DETAILS = ".pswp__caption-details"+ UI_VISIBLE = ".pswp--ui-visible"def initialize@component = find(SELECTOR)@@ -92,6 +93,14 @@ module PageObjectsdef has_no_original_image_button?component.has_no_css?(ORIGINAL_IMAGE_BTN)end++ def has_ui_visible?+ page.has_css?(UI_VISIBLE)+ end++ def has_no_ui_visible?+ page.has_no_css?(UI_VISIBLE)+ endendendend