Skip to content

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

FEATURE: enhance screenshot support for themes with dark/light modes (#36422)

Yuriy Kurantdb10d595e742
diff --git a/app/assets/stylesheets/common/components/theme-card.scss b/app/assets/stylesheets/common/components/theme-card.scss
index 93935e79d71..8c7658f730a 100644
--- a/app/assets/stylesheets/common/components/theme-card.scss
+++ b/app/assets/stylesheets/common/components/theme-card.scss
@@ -91,10 +91,11 @@
&__image-wrapper {
width: 100%;
height: 160px;
- overflow: hidden;
+ overflow-x: auto;
+ overflow-y: hidden;
display: flex;
align-items: center;
- justify-content: center;
+ scroll-snap-type: x mandatory;
border-bottom: 1px solid var(--content-border-color);
border-top-left-radius: var(--d-border-radius);
border-top-right-radius: var(--d-border-radius);
@@ -108,9 +109,10 @@
&__image {
width: 100%;
- height: 100%;
+ flex-shrink: 0;
object-fit: cover;
object-position: top left;
+ scroll-snap-align: start;
}
.ember-checkbox {
diff --git a/app/models/theme.rb b/app/models/theme.rb
index ad8bbb1b673..c3a2a673169 100644
--- a/app/models/theme.rb
+++ b/app/models/theme.rb
@@ -402,9 +402,21 @@ class Theme < ActiveRecord::Base
end
end
- def screenshot_url
+ def screenshot_dark_url
theme_fields
- .find { |field| field.type_id == ThemeField.types[:theme_screenshot_upload_var] }
+ .find do |field|
+ field.type_id == ThemeField.types[:theme_screenshot_upload_var] &&
+ field.name == "screenshot_dark"
+ end
+ &.upload_url
+ end
+
+ def screenshot_light_url
+ theme_fields
+ .find do |field|
+ field.type_id == ThemeField.types[:theme_screenshot_upload_var] &&
+ field.name == "screenshot_light"
+ end
&.upload_url
end
diff --git a/app/serializers/theme_index_serializer.rb b/app/serializers/theme_index_serializer.rb
index c1b714b48b9..ed7dc3c39f3 100644
--- a/app/serializers/theme_index_serializer.rb
+++ b/app/serializers/theme_index_serializer.rb
@@ -1,7 +1,13 @@
# frozen_string_literal: true
class ThemeIndexSerializer < BasicThemeSerializer
- attributes :user_selectable, :screenshot_url, :remote_theme_id, :enabled?, :supported?, :system
+ attributes :user_selectable,
+ :screenshot_dark_url,
+ :screenshot_light_url,
+ :remote_theme_id,
+ :enabled?,
+ :supported?,
+ :system
has_one :color_scheme, serializer: ColorSchemeSerializer, embed: :object
has_one :remote_theme, serializer: RemoteThemeSerializer, embed: :objects
diff --git a/app/serializers/theme_serializer.rb b/app/serializers/theme_serializer.rb
index 92cf66c4337..a334e8fdbbf 100644
--- a/app/serializers/theme_serializer.rb
+++ b/app/serializers/theme_serializer.rb
@@ -15,7 +15,8 @@ class ThemeSerializer < BasicThemeSerializer
:enabled?,
:disabled_at,
:theme_fields,
- :screenshot_url,
+ :screenshot_dark_url,
+ :screenshot_light_url,
:system
has_one :color_scheme, serializer: ColorSchemeSerializer, embed: :object
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 5357ed006f5..9f8c104b53c 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -7151,6 +7151,8 @@ en:
create_name: "Name"
save: "Save"
long_title: "Amend colors, CSS and HTML contents of your site"
+ dark_mode: "%{themeName} dark mode"
+ light_mode: "%{themeName} light mode"
edit: "Edit"
edit_confirm: "This is a remote theme, if you edit CSS/HTML your changes will be erased next time you update the theme."
update_confirm: "These local changes will be erased by the update. Are you sure you want to continue?"
diff --git a/frontend/discourse/admin/components/themes-grid-card.gjs b/frontend/discourse/admin/components/themes-grid-card.gjs
index 009a3ffa8e9..5f6b3bfb746 100644
--- a/frontend/discourse/admin/components/themes-grid-card.gjs
+++ b/frontend/discourse/admin/components/themes-grid-card.gjs
@@ -11,6 +11,7 @@ import icon from "discourse/helpers/d-icon";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { removeValueFromArray } from "discourse/lib/array-tools";
import getURL from "discourse/lib/get-url";
+import { and } from "discourse/truth-helpers";
import { i18n } from "discourse-i18n";
import ThemesGridPlaceholder from "./themes-grid-placeholder";
@@ -175,17 +176,32 @@ export default class ThemeCard extends Component {
data-theme-id={{@theme.id}}
>
<:content>
-
<div class="theme-card__image-wrapper">
- {{#if @theme.screenshot_url}}
+ {{#if @theme.screenshot_light_url}}
<img
class="theme-card__image"
- src={{@theme.screenshot_url}}
- alt={{@theme.name}}
+ src={{@theme.screenshot_light_url}}
+ alt={{i18n
+ "admin.customize.theme.light_mode"
+ }}
/>
- {{else}}
- <ThemesGridPlaceholder @theme={{@theme}} />
{{/if}}
+ {{#if @theme.screenshot_dark_url}}
+ <img
+ class="theme-card__image"
+ src={{@theme.screenshot_dark_url}}
+ alt={{i18n
+ "admin.customize.theme.dark_mode"
+ }}
+ />
+ {{/if}}
+ {{#unless
+ (and @theme.screenshot_dark_url @theme.screenshot_light_url)
+ }}
+ <ThemesGridPlaceholder @theme={{@theme}} />
+ {{/unless}}
</div>
<div class="theme-card__content">
<a class="theme-card__title" href={{this.editUrl}}>{{@theme.name}}</a>
diff --git a/frontend/discourse/tests/acceptance/admin-customize-themes-show-test.gjs b/frontend/discourse/tests/acceptance/admin-customize-themes-show-test.gjs
index 9769cf7a467..d8045a37a42 100644
--- a/frontend/discourse/tests/acceptance/admin-customize-themes-show-test.gjs
+++ b/frontend/discourse/tests/acceptance/admin-customize-themes-show-test.gjs
@@ -53,7 +53,7 @@ acceptance("Admin - Customize - Themes - Show", function (needs) {
type_id: 5,
},
],
- screenshot_url:
+ screenshot_light_url:
"/uploads/default/original/1X/f8a61b9a0bfac672daec9e401787812f8c5e28df.png",
system: true,
color_scheme: null,
diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rb
index c775fb7707c..8f98d3e103a 100644
--- a/spec/models/theme_spec.rb
+++ b/spec/models/theme_spec.rb
@@ -1676,21 +1676,29 @@ HTML
end
end
- describe "#screenshot_url" do
- it "returns nil when no screenshot is set" do
- expect(theme.screenshot_url).to be_nil
+ describe "#screenshot_light_url & #screenshot_dark_url" do
+ it "returns nil when no screenshots are set" do
+ expect(theme.screenshot_light_url).to be_nil
+ expect(theme.screenshot_dark_url).to be_nil
end
- it "returns the upload URL when screenshot is set" do
+ it "returns the upload URL when screenshots are set" do
upload = UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(-1)
theme.set_field(
target: :common,
- name: "screenshot",
+ name: "screenshot_dark",
+ upload_id: upload.id,
+ type: :theme_screenshot_upload_var,
+ )
+ theme.set_field(
+ target: :common,
+ name: "screenshot_light",
upload_id: upload.id,
type: :theme_screenshot_upload_var,
)
theme.save!
- expect(theme.screenshot_url).to eq(upload.url)
+ expect(theme.screenshot_light_url).to eq(upload.url)
+ expect(theme.screenshot_dark_url).to eq(upload.url)
end
end
diff --git a/spec/requests/admin/config/customize_controller_spec.rb b/spec/requests/admin/config/customize_controller_spec.rb
index f635f332670..eef077ff0da 100644
--- a/spec/requests/admin/config/customize_controller_spec.rb
+++ b/spec/requests/admin/config/customize_controller_spec.rb
@@ -35,11 +35,11 @@ RSpec.describe Admin::Config::CustomizeController do
expect(theme_response["color_scheme"]["id"]).to eq(color_scheme.id)
end
- it "includes screenshot_url in response" do
+ it "includes screenshot_light_url in response" do
upload = UploadCreator.new(file_from_fixtures("logo.png"), "logo.png").create_for(-1)
theme1.set_field(
target: :common,
- name: "screenshot",
+ name: "screenshot_light",
upload_id: upload.id,
type: :theme_screenshot_upload_var,
)
@@ -50,7 +50,7 @@ RSpec.describe Admin::Config::CustomizeController do
expect(response.status).to eq(200)
theme_response = response.parsed_body["themes"].find { |t| t["id"] == theme1.id }
- expect(theme_response["screenshot_url"]).to eq(upload.url)
+ expect(theme_response["screenshot_light_url"]).to eq(upload.url)
end
end
diff --git a/spec/serializers/theme_index_serializer_spec.rb b/spec/serializers/theme_index_serializer_spec.rb
index ba4f8b49c83..55b8ae068bb 100644
--- a/spec/serializers/theme_index_serializer_spec.rb
+++ b/spec/serializers/theme_index_serializer_spec.rb
@@ -10,7 +10,7 @@ RSpec.describe ThemeIndexSerializer do
:theme_field,
theme:,
target_id: Theme.targets[:common],
- name: "screenshot",
+ name: "screenshot_light",
upload_id: screenshot_upload.id,
type_id: ThemeField.types[:theme_screenshot_upload_var],
)
@@ -26,8 +26,8 @@ RSpec.describe ThemeIndexSerializer do
expect(json[:user_selectable]).to eq(true)
end
- it "includes screenshot_url attribute" do
- expect(json[:screenshot_url]).to eq(screenshot_upload.url)
+ it "includes screenshot_light_url attribute" do
+ expect(json[:screenshot_light_url]).to eq(screenshot_upload.url)
end
it "includes color_scheme relationship when present" do
diff --git a/spec/serializers/theme_serializer_spec.rb b/spec/serializers/theme_serializer_spec.rb
index 2423520d2c8..6f77f322fc9 100644
--- a/spec/serializers/theme_serializer_spec.rb
+++ b/spec/serializers/theme_serializer_spec.rb
@@ -37,44 +37,46 @@ RSpec.describe ThemeSerializer do
end
end
- describe "screenshot_url" do
+ describe "screenshot_light_url" do
fab!(:theme)
let(:serialized) { ThemeSerializer.new(theme.reload).as_json[:theme] }
- it "should include screenshot_url when there is a theme field with screenshot upload type" do
+ it "should include screenshot_light_url when there is a theme field with screenshot upload type and name 'screenshot_light'" do
Fabricate(
:theme_field,
theme: theme,
type_id: ThemeField.types[:theme_screenshot_upload_var],
- name: "theme_screenshot_1",
+ name: "screenshot_light",
upload: Fabricate(:upload),
)
- expect(serialized[:screenshot_url]).to be_present
+ expect(serialized[:screenshot_light_url]).to be_present
end
- it "should not include screenshot_url when there is no theme field with screenshot upload type" do
- expect(serialized[:screenshot_url]).to be_nil
+ it "should not include screenshot_light_url/screenshot_dark_url when there is no theme fields of screenshot upload type with names 'screenshot_light' or 'screenshot_dark'" do
+ expect(serialized[:screenshot_light_url]).to be_nil
+ expect(serialized[:screenshot_dark_url]).to be_nil
end
- it "should handle multiple screenshot fields and use the first one" do
+ it "should handle dark/light screenshot fields and use both" do
first_upload = Fabricate(:upload)
second_upload = Fabricate(:upload)
Fabricate(
:theme_field,
theme: theme,
type_id: ThemeField.types[:theme_screenshot_upload_var],
- name: "theme_screenshot_1",
+ name: "screenshot_light",
upload: first_upload,
)
Fabricate(
:theme_field,
theme: theme,
type_id: ThemeField.types[:theme_screenshot_upload_var],
- name: "theme_screenshot_2",
+ name: "screenshot_dark",
upload: second_upload,
)
- expect(serialized[:screenshot_url]).to eq(first_upload.url)
+ expect(serialized[:screenshot_light_url]).to eq(first_upload.url)
+ expect(serialized[:screenshot_dark_url]).to eq(second_upload.url)
end
end