Skip to content

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

Add site setting to pick dark mode color scheme (#10390)

Penar Musaraj9c9aa217269e
diff --git a/app/models/color_scheme_setting.rb b/app/models/color_scheme_setting.rb
new file mode 100644
index 00000000000..4003348b0e8
--- /dev/null
+++ b/app/models/color_scheme_setting.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class ColorSchemeSetting < EnumSiteSetting
+
+ def self.valid_value?(val)
+ val == -1 || ColorScheme.find_by_id(val)
+ end
+
+ def self.values
+ values = [{ name: I18n.t("site_settings.dark_mode_none"), value: -1 }]
+ ColorScheme.all.map do |c|
+ values << { name: c.name, value: c.id }
+ end
+ values
+ end
+
+end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 8e6beb2f6a6..65afb842e2a 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1834,6 +1834,8 @@ en:
desktop_category_page_style: "Visual style for the /categories page."
category_colors: "A list of hexadecimal color values allowed for categories."
category_style: "Visual style for category badges."
+ default_dark_mode_color_scheme_id: "The color scheme used when in dark mode."
+ dark_mode_none: "None"
max_image_size_kb: "The maximum image upload size in kB. This must be configured in nginx (client_max_body_size) / apache or proxy as well. Images larger than this and smaller than client_max_body_size will be resized to fit on upload."
max_attachment_size_kb: "The maximum attachment files upload size in kB. This must be configured in nginx (client_max_body_size) / apache or proxy as well."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index a325968330c..e4b8b3f28a3 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -247,7 +247,8 @@ basic:
hidden: true
default_dark_mode_color_scheme_id:
default: -1
- hidden: true
+ type: enum
+ enum: "ColorSchemeSetting"
client: true
relative_date_duration:
client: true
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index 96245121d55..33b23e5fa75 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -364,8 +364,10 @@ describe ApplicationHelper do
expect(cs_stylesheets.scan("stylesheets/color_definitions").size).to eq(2)
end
- it 'fails gracefully when the dark color scheme ID is set but missing' do
- SiteSetting.default_dark_mode_color_scheme_id = -5
+ it 'handles a missing dark color scheme gracefully' do
+ scheme = ColorScheme.create!(name: "pyramid")
+ SiteSetting.default_dark_mode_color_scheme_id = scheme.id
+ scheme.destroy!
cs_stylesheets = helper.discourse_color_scheme_stylesheets
expect(cs_stylesheets).to include("stylesheets/color_definitions")