Skip to content

A platform for community discussion. Free, open, simple.

UX: add RTL support for new lightbox (#35866)

David Battersby93c47e1d2a20
diff --git a/app/assets/stylesheets/common/base/lightbox.scss b/app/assets/stylesheets/common/base/lightbox.scss
index 5073536a761..9db0220e4d9 100644
--- a/app/assets/stylesheets/common/base/lightbox.scss
+++ b/app/assets/stylesheets/common/base/lightbox.scss
@@ -127,6 +127,29 @@
}
}
+.rtl {
+ .pswp__scroll-wrap,
+ .pswp__bg,
+ .pswp__container,
+ .pswp__item,
+ .pswp__content,
+ .pswp__img,
+ .pswp__zoom-wrap {
+ right: 0;
+ left: unset;
+ }
+
+ .pswp__button--arrow--next {
+ left: 0 !important;
+ right: unset !important;
+ }
+
+ .pswp__button--arrow--prev {
+ left: unset !important;
+ right: 0 !important;
+ }
+}
+
.pswp.pswp--behind-header {
z-index: z("header") - 1;
}
@@ -182,3 +205,18 @@
.pswp__button--image-info svg {
padding: 0.4em;
}
+
+.pswp__custom-counter {
+ height: 30px;
+ margin-top: 15px;
+ margin-inline-start: 20px;
+ font-size: 14px;
+ line-height: 30px;
+ color: var(--pswp-icon-color);
+ text-shadow: 1px 1px 3px var(--pswp-icon-color-secondary);
+ opacity: 0.85;
+}
+
+.pswp--one-slide .pswp__custom-counter {
+ display: none;
+}
diff --git a/frontend/discourse/app/lib/lightbox.js b/frontend/discourse/app/lib/lightbox.js
index bbe59fecd37..54c7bbecee3 100644
--- a/frontend/discourse/app/lib/lightbox.js
+++ b/frontend/discourse/app/lib/lightbox.js
@@ -5,6 +5,7 @@ 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";
+import { isDocumentRTL } from "discourse/lib/text-direction";
import {
escapeExpression,
postRNWebviewMessage,
@@ -30,15 +31,30 @@ export default async function lightbox(elem, siteSettings) {
const { default: PhotoSwipeLightbox } = await import("photoswipe/lightbox");
const isTestEnv = isTesting() || isRailsTesting();
+ const rtl = isDocumentRTL();
+ const items = [...elem.querySelectorAll(SELECTORS.DEFAULT_ITEM_SELECTOR)];
+
+ if (rtl) {
+ items.reverse();
+ }
+
+ items.forEach((el, index) => {
+ el.addEventListener("click", (e) => {
+ e.preventDefault();
+
+ lightboxEl.loadAndOpen(index);
+ });
+ });
+
const lightboxEl = new PhotoSwipeLightbox({
- gallery: elem,
- children: SELECTORS.DEFAULT_ITEM_SELECTOR,
+ dataSource: items,
arrowPrevTitle: i18n("lightbox.previous"),
arrowNextTitle: i18n("lightbox.next"),
closeTitle: i18n("lightbox.close"),
zoomTitle: i18n("lightbox.zoom"),
errorMsg: i18n("lightbox.error"),
showHideAnimationType: isTestEnv ? "none" : "zoom",
+ counter: false,
tapAction,
paddingFn,
pswpModule: async () => await import("photoswipe"),
@@ -63,7 +79,7 @@ export default async function lightbox(elem, siteSettings) {
// adds a custom caption to lightbox
lightboxEl.pswp.ui.registerElement({
name: "caption",
- order: 6,
+ order: 11,
isButton: false,
appendTo: "root",
html: "",
@@ -152,9 +168,25 @@ export default async function lightbox(elem, siteSettings) {
lightboxEl.pswp.element.classList.toggle("pswp--caption-expanded");
},
});
+
+ lightboxEl.pswp.ui.registerElement({
+ name: "custom-counter",
+ order: 6,
+ isButton: false,
+ appendTo: "bar",
+ onInit: (el, pswp) => {
+ pswp.on("change", () => {
+ const total = pswp.getNumItems();
+ const index = rtl ? total - pswp.currIndex : pswp.currIndex + 1;
+ el.textContent = `${index} / ${total}`;
+ });
+ },
+ });
});
- lightboxEl.addFilter("domItemData", (data, el) => {
+ lightboxEl.addFilter("itemData", (data) => {
+ const el = data.element;
+
if (!el) {
return data;
}
diff --git a/spec/system/lightbox_spec.rb b/spec/system/lightbox_spec.rb
index 444f7ed4c56..9e924634867 100644
--- a/spec/system/lightbox_spec.rb
+++ b/spec/system/lightbox_spec.rb
@@ -4,6 +4,8 @@ describe "Lightbox | Photoswipe", type: :system do
fab!(:topic)
fab!(:current_user, :admin)
fab!(:upload_1) { Fabricate(:image_upload, width: 2400, height: 3600) }
+ fab!(:upload_2) { Fabricate(:large_image_upload, width: 2000, height: 1000) }
+ fab!(:upload_3) { Fabricate(:image_upload, width: 800, height: 1200) }
fab!(:post) { Fabricate(:post, topic: topic, raw: "![first image](#{upload_1.url})") }
let(:topic_page) { PageObjects::Pages::Topic.new }
@@ -69,8 +71,6 @@ describe "Lightbox | Photoswipe", type: :system do
end
context "with multiple images" do
- fab!(:upload_2) { Fabricate(:large_image_upload, width: 2000, height: 1000) }
-
before do
post.update(raw: "![first image](#{upload_1.url}) ![second image](#{upload_2.url})")
@@ -97,6 +97,69 @@ describe "Lightbox | Photoswipe", type: :system do
end
end
+ context "when RTL language" do
+ before do
+ SiteSetting.default_locale = "ar"
+ post.update!(
+ raw:
+ "![first image](#{upload_1.url}) ![second image](#{upload_2.url}) ![third image](#{upload_3.url})",
+ )
+ cpp.post_process
+ post.update(cooked: cpp.html)
+ end
+
+ it "supports reversed layout" do
+ topic_page.visit_topic(topic)
+
+ find("#post_1 .lightbox-wrapper:nth-of-type(1) .lightbox").click
+
+ expect(lightbox).to be_visible
+
+ # text is reversed by CSS, so check for correct property
+ expect(get_style(lightbox.counter, "direction")).to eq("rtl")
+
+ expect(lightbox).to have_counter("1 / 3")
+ expect(lightbox).to have_image_source(upload_1)
+ expect(lightbox).to have_caption_title("first image")
+
+ lightbox.prev_button.click
+
+ expect(lightbox).to have_counter("2 / 3")
+ expect(lightbox).to have_image_source(upload_2)
+ expect(lightbox).to have_caption_title("second image")
+
+ lightbox.prev_button.click
+
+ expect(lightbox).to have_counter("3 / 3")
+ expect(lightbox).to have_image_source(upload_3)
+ expect(lightbox).to have_caption_title("third image")
+
+ lightbox.next_button.click
+
+ expect(lightbox).to have_counter("2 / 3")
+ expect(lightbox).to have_image_source(upload_2)
+ expect(lightbox).to have_caption_title("second image")
+
+ send_keys(:right)
+
+ expect(lightbox).to have_counter("1 / 3")
+ expect(lightbox).to have_image_source(upload_1)
+ expect(lightbox).to have_caption_title("first image")
+
+ send_keys(:right)
+
+ expect(lightbox).to have_counter("3 / 3")
+ expect(lightbox).to have_image_source(upload_3)
+ expect(lightbox).to have_caption_title("third image")
+
+ send_keys(:left)
+
+ expect(lightbox).to have_counter("1 / 3")
+ expect(lightbox).to have_image_source(upload_1)
+ expect(lightbox).to have_caption_title("first image")
+ end
+ end
+
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") }
diff --git a/spec/system/page_objects/components/photoswipe.rb b/spec/system/page_objects/components/photoswipe.rb
index 4762e11cedc..ec83672cd99 100644
--- a/spec/system/page_objects/components/photoswipe.rb
+++ b/spec/system/page_objects/components/photoswipe.rb
@@ -13,11 +13,12 @@ module PageObjects
DOWNLOAD_BTN = ".pswp__button--download-image"
ORIGINAL_IMAGE_BTN = ".pswp__button--original-image"
IMAGE_INFO_BTN = ".pswp__button--image-info"
- COUNTER = ".pswp__counter"
+ COUNTER = ".pswp__custom-counter"
CAPTION = ".pswp__caption"
CAPTION_TITLE = ".pswp__caption-title"
CAPTION_DETAILS = ".pswp__caption-details"
UI_VISIBLE = ".pswp--ui-visible"
+ ACTIVE_IMG = ".pswp__item[aria-hidden='false'] .pswp__img"
def initialize
@component = find(SELECTOR)
@@ -47,6 +48,10 @@ module PageObjects
component.find(CLOSE_BTN)
end
+ def counter
+ component.find(COUNTER)
+ end
+
def has_counter?(text)
component.has_css?(COUNTER, text: text)
end
@@ -118,6 +123,10 @@ module PageObjects
def has_no_ui_visible?
page.has_no_css?(UI_VISIBLE)
end
+
+ def has_image_source?(upload)
+ component.has_css?(ACTIVE_IMG + "[src$='#{upload.url}']")
+ end
end
end
end