Skip to content

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

FEATURE: Color mode preference (#33875)

Osama Sayeghd01526f91597
diff --git a/app/assets/javascripts/discourse/app/components/interface-color-selector.gjs b/app/assets/javascripts/discourse/app/components/interface-color-selector.gjs
index 60733f8611d..cd41c1f689a 100644
--- a/app/assets/javascripts/discourse/app/components/interface-color-selector.gjs
+++ b/app/assets/javascripts/discourse/app/components/interface-color-selector.gjs
@@ -34,7 +34,7 @@ export default class InterfaceColorSelector extends Component {
@action
switchToAuto(dMenu) {
- this.interfaceColor.removeColorModeOverride();
+ this.interfaceColor.useAutoMode();
dMenu.close();
}
@@ -45,6 +45,7 @@ export default class InterfaceColorSelector extends Component {
@identifier="interface-color-selector"
@animated={{false}}
class="interface-color-selector icon"
+ data-current-mode={{this.interfaceColor.colorMode}}
>
<:content as |dMenu|>
<DropdownMenu as |dropdown|>
diff --git a/app/assets/javascripts/discourse/app/controllers/preferences/interface.js b/app/assets/javascripts/discourse/app/controllers/preferences/interface.js
index ad3cd7c1c15..e9f07ec1232 100644
--- a/app/assets/javascripts/discourse/app/controllers/preferences/interface.js
+++ b/app/assets/javascripts/discourse/app/controllers/preferences/interface.js
@@ -1,3 +1,4 @@
+import { tracked } from "@glimmer/tracking";
import Controller, { inject as controller } from "@ember/controller";
import { action, computed } from "@ember/object";
import { not, reads } from "@ember/object/computed";
@@ -10,6 +11,7 @@ import {
updateColorSchemeCookie,
} from "discourse/lib/color-scheme-picker";
import { propertyEqual } from "discourse/lib/computed";
+import { INTERFACE_COLOR_MODES } from "discourse/lib/constants";
import discourseComputed from "discourse/lib/decorators";
import {
currentThemeId,
@@ -36,15 +38,18 @@ const TEXT_SIZES = ["smallest", "smaller", "normal", "larger", "largest"];
const TITLE_COUNT_MODES = ["notifications", "contextual"];
export default class InterfaceController extends Controller {
+ @service interfaceColor;
@service session;
@controller("preferences") preferencesController;
+ @tracked selectedInterfaceColorModeId = null;
currentThemeId = currentThemeId();
previewingColorScheme = false;
selectedDarkColorSchemeId = null;
makeColorSchemeDefault = true;
@propertyEqual("model.id", "currentUser.id") canPreviewColorScheme;
+ @propertyEqual("model.id", "currentUser.id") isViewingOwnProfile;
subpageTitle = i18n("user.preferences_nav.interface");
@reads("userSelectableColorSchemes.length") showColorSchemeSelector;
@@ -80,6 +85,7 @@ export default class InterfaceController extends Controller {
"color_scheme_id",
"dark_scheme_id",
"bookmark_auto_delete_preference",
+ "interface_color_mode",
];
if (makeThemeDefault) {
@@ -251,6 +257,41 @@ export default class InterfaceController extends Controller {
return darkSchemes && darkSchemes.length > minToShow;
}
+ get interfaceColorModes() {
+ return [
+ {
+ id: INTERFACE_COLOR_MODES.AUTO,
+ name: i18n("user.color_schemes.interface_modes.auto"),
+ },
+ {
+ id: INTERFACE_COLOR_MODES.LIGHT,
+ name: i18n("user.color_schemes.interface_modes.light"),
+ },
+ {
+ id: INTERFACE_COLOR_MODES.DARK,
+ name: i18n("user.color_schemes.interface_modes.dark"),
+ },
+ ];
+ }
+
+ get selectedInterfaceColorMode() {
+ if (this.selectedInterfaceColorModeId) {
+ return this.selectedInterfaceColorModeId;
+ }
+ if (this.isViewingOwnProfile) {
+ if (this.interfaceColor.colorModeIsAuto) {
+ return INTERFACE_COLOR_MODES.AUTO;
+ }
+ if (this.interfaceColor.colorModeIsLight) {
+ return INTERFACE_COLOR_MODES.LIGHT;
+ }
+ if (this.interfaceColor.colorModeIsDark) {
+ return INTERFACE_COLOR_MODES.DARK;
+ }
+ }
+ return this.model.user_option.interface_color_mode;
+ }
+
getSelectedColorSchemeId() {
if (!this.session.userColorSchemeId) {
return;
@@ -290,6 +331,12 @@ export default class InterfaceController extends Controller {
this.set("model.user_option.color_scheme_id", null);
} else if (this.makeColorSchemeDefault) {
this.set("model.user_option.color_scheme_id", this.selectedColorSchemeId);
+ if (this.selectedInterfaceColorModeId) {
+ this.set(
+ "model.user_option.interface_color_mode",
+ this.selectedInterfaceColorModeId
+ );
+ }
}
if (this.showDarkModeToggle) {
@@ -349,6 +396,20 @@ export default class InterfaceController extends Controller {
}
}
+ if (this.selectedInterfaceColorModeId) {
+ if (this.isViewingOwnProfile) {
+ const modeId = this.selectedInterfaceColorModeId;
+ if (modeId === INTERFACE_COLOR_MODES.AUTO) {
+ this.interfaceColor.useAutoMode();
+ } else if (modeId === INTERFACE_COLOR_MODES.LIGHT) {
+ this.interfaceColor.forceLightMode();
+ } else if (modeId === INTERFACE_COLOR_MODES.DARK) {
+ this.interfaceColor.forceDarkMode();
+ }
+ }
+ this.selectedInterfaceColorModeId = null;
+ }
+
this.homeChanged();
if (this.themeId && this.themeId !== this.currentThemeId) {
@@ -380,10 +441,10 @@ export default class InterfaceController extends Controller {
loadColorScheme(colorSchemeId) {
this.setProperties({
selectedColorSchemeId: colorSchemeId,
- previewingColorScheme: this.canPreviewColorScheme,
+ previewingColorScheme: this.isViewingOwnProfile,
});
- if (!this.canPreviewColorScheme) {
+ if (!this.isViewingOwnProfile) {
return;
}
@@ -405,10 +466,10 @@ export default class InterfaceController extends Controller {
loadDarkColorScheme(colorSchemeId) {
this.setProperties({
selectedDarkColorSchemeId: colorSchemeId,
- previewingColorScheme: this.canPreviewColorScheme,
+ previewingColorScheme: this.isViewingOwnProfile,
});
- if (!this.canPreviewColorScheme) {
+ if (!this.isViewingOwnProfile) {
return;
}
@@ -422,11 +483,18 @@ export default class InterfaceController extends Controller {
}
}
+ @action
+ selectColorMode(modeId) {
+ this.selectedInterfaceColorModeId = modeId;
+ this.set("previewingColorScheme", this.isViewingOwnProfile);
+ }
+
@action
undoColorSchemePreview() {
this.setProperties({
selectedColorSchemeId: this.session.userColorSchemeId,
selectedDarkColorSchemeId: this.session.userDarkSchemeId,
+ selectedInterfaceColorModeId: null,
previewingColorScheme: false,
});
const darkStylesheet = document.querySelector("link#cs-preview-dark"),
diff --git a/app/assets/javascripts/discourse/app/lib/constants.js b/app/assets/javascripts/discourse/app/lib/constants.js
index d52b05ad802..2d9beb085f9 100644
--- a/app/assets/javascripts/discourse/app/lib/constants.js
+++ b/app/assets/javascripts/discourse/app/lib/constants.js
@@ -106,3 +106,9 @@ export const API_KEY_SCOPE_MODES = ["global", "read_only", "granular"];
export const INVITE_DESCRIPTION_MAX_LENGTH = 100;
export const USER_OPTION_COMPOSITION_MODES = { markdown: 0, rich: 1 };
+
+export const INTERFACE_COLOR_MODES = {
+ AUTO: 1,
+ LIGHT: 2,
+ DARK: 3,
+};
diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.js
index b22b1736111..11a46e29176 100644
--- a/app/assets/javascripts/discourse/app/models/user.js
+++ b/app/assets/javascripts/discourse/app/models/user.js
@@ -13,7 +13,10 @@ import { isEmpty } from "@ember/utils";
import { Promise } from "rsvp";
import { ajax } from "discourse/lib/ajax";
import { url } from "discourse/lib/computed";
-import { USER_OPTION_COMPOSITION_MODES } from "discourse/lib/constants";
+import {
+ INTERFACE_COLOR_MODES,
+ USER_OPTION_COMPOSITION_MODES,
+} from "discourse/lib/constants";
import cookie, { removeCookie } from "discourse/lib/cookie";
import discourseComputed from "discourse/lib/decorators";
import deprecated from "discourse/lib/deprecated";
@@ -107,6 +110,7 @@ let userOptionFields = [
"email_previous_replies",
"color_scheme_id",
"dark_scheme_id",
+ "interface_color_mode",
"dynamic_favicon",
"enable_quoting",
"enable_smart_lists",
@@ -1299,6 +1303,24 @@ export default class User extends RestModel.extend(Evented) {
trackedTags(trackedTags, watchedTags, watchingFirstPostTags) {
return [...trackedTags, ...watchedTags, ...watchingFirstPostTags];
}
+
+ get prefersLightColor() {
+ return (
+ this.user_option?.interface_color_mode === INTERFACE_COLOR_MODES.LIGHT
+ );
+ }
+
+ get prefersDarkColor() {
+ return (
+ this.user_option?.interface_color_mode === INTERFACE_COLOR_MODES.DARK
+ );
+ }
+
+ get prefersAutoColor() {
+ return (
+ this.user_option?.interface_color_mode === INTERFACE_COLOR_MODES.AUTODARK
+ );
+ }
}
User.reopenClass({
diff --git a/app/assets/javascripts/discourse/app/services/interface-color.js b/app/assets/javascripts/discourse/app/services/interface-color.js
index feb5ab2d174..b1a91303393 100644
--- a/app/assets/javascripts/discourse/app/services/interface-color.js
+++ b/app/assets/javascripts/discourse/app/services/interface-color.js
@@ -1,24 +1,26 @@
import { tracked } from "@glimmer/tracking";
import Service, { service } from "@ember/service";
-import cookie, { removeCookie } from "discourse/lib/cookie";
+import cookie from "discourse/lib/cookie";
const COOKIE_NAME = "forced_color_mode";
-const DARK = "dark";
-const LIGHT = "light";
+const DARK_VALUE_FOR_COOKIE = "dark";
+const LIGHT_VALUE_FOR_COOKIE = "light";
+const AUTO_VALUE_FOR_COOKIE = "auto";
export default class InterfaceColor extends Service {
@service appEvents;
+ @service currentUser;
@service siteSettings;
@service session;
- @tracked forcedColorMode;
+ @tracked colorMode;
get lightModeForced() {
- return this.selectorAvailable && this.forcedColorMode === LIGHT;
+ return this.selectorAvailable && this.colorModeIsLight;
}
get darkModeForced() {
- return this.selectorAvailable && this.forcedColorMode === DARK;
+ return this.selectorAvailable && this.colorModeIsDark;
}
get selectorAvailable() {
@@ -27,6 +29,18 @@ export default class InterfaceColor extends Service {
);
}
+ get colorModeIsLight() {
+ return this.colorMode === LIGHT_VALUE_FOR_COOKIE;
+ }
+
+ get colorModeIsDark() {
+ return this.colorMode === DARK_VALUE_FOR_COOKIE;
+ }
+
+ get colorModeIsAuto() {
+ return this.colorMode === AUTO_VALUE_FOR_COOKIE;
+ }
+
get selectorAvailableInSidebar() {
return (
this.selectorAvailable &&
@@ -46,18 +60,26 @@ export default class InterfaceColor extends Service {
return;
}
- const forcedColorMode = cookie(COOKIE_NAME);
+ const cookieValue = cookie(COOKIE_NAME);
- if (forcedColorMode === LIGHT) {
+ if (cookieValue === AUTO_VALUE_FOR_COOKIE) {
+ this.useAutoMode({ adjustStylesheets: false });
+ } else if (cookieValue === LIGHT_VALUE_FOR_COOKIE) {
this.forceLightMode({ flipStylesheets: false });
- } else if (forcedColorMode === DARK) {
+ } else if (cookieValue === DARK_VALUE_FOR_COOKIE) {
this.forceDarkMode({ flipStylesheets: false });
+ } else if (this.currentUser?.prefersAutoColor) {
+ this.colorMode = AUTO_VALUE_FOR_COOKIE;
+ } else if (this.currentUser?.prefersLightColor) {
+ this.colorMode = LIGHT_VALUE_FOR_COOKIE;
+ } else if (this.currentUser?.prefersDarkColor) {
+ this.colorMode = DARK_VALUE_FOR_COOKIE;
}
}
forceLightMode({ flipStylesheets = true } = {}) {
- this.forcedColorMode = LIGHT;
- cookie(COOKIE_NAME, LIGHT, {
+ this.colorMode = LIGHT_VALUE_FOR_COOKIE;
+ cookie(COOKIE_NAME, LIGHT_VALUE_FOR_COOKIE, {
path: "/",
expires: 365,
});
@@ -70,12 +92,12 @@ export default class InterfaceColor extends Service {
darkStylesheet.media = "none";
}
}
- this.appEvents.trigger("interface-color:changed", LIGHT);
+ this.appEvents.trigger("interface-color:changed", LIGHT_VALUE_FOR_COOKIE);
}
forceDarkMode({ flipStylesheets = true } = {}) {
- this.forcedColorMode = DARK;
- cookie(COOKIE_NAME, DARK, {
+ this.colorMode = DARK_VALUE_FOR_COOKIE;
+ cookie(COOKIE_NAME, DARK_VALUE_FOR_COOKIE, {
path: "/",
expires: 365,
});
@@ -88,23 +110,34 @@ export default class InterfaceColor extends Service {
darkStylesheet.media = "all";
}
}
- this.appEvents.trigger("interface-color:changed", DARK);
+ this.appEvents.trigger("interface-color:changed", DARK_VALUE_FOR_COOKIE);
}
- removeColorModeOverride() {
- this.forcedColorMode = null;
- removeCookie(COOKIE_NAME, { path: "/" });
+ useAutoMode({ adjustStylesheets = true } = {}) {
+ this.colorMode = AUTO_VALUE_FOR_COOKIE;
+ cookie(COOKIE_NAME, AUTO_VALUE_FOR_COOKIE, {
+ path: "/",
+ expires: 365,
+ });
- const lightStylesheet = this.#lightColorsStylesheet();
- if (lightStylesheet) {
- lightStylesheet.media = "(prefers-color-scheme: light)";
- this.appEvents.trigger("interface-color:changed", LIGHT);
- }
+ if (adjustStylesheets) {
+ const lightStylesheet = this.#lightColorsStylesheet();
+ if (lightStylesheet) {
+ lightStylesheet.media = "(prefers-color-scheme: light)";
+ this.appEvents.trigger(
+ "interface-color:changed",
+ LIGHT_VALUE_FOR_COOKIE
+ );
+ }
- const darkStylesheet = this.#darkColorsStylesheet();
- if (darkStylesheet) {
- darkStylesheet.media = "(prefers-color-scheme: dark)";
- this.appEvents.trigger("interface-color:changed", DARK);
+ const darkStylesheet = this.#darkColorsStylesheet();
+ if (darkStylesheet) {
+ darkStylesheet.media = "(prefers-color-scheme: dark)";
+ this.appEvents.trigger(
+ "interface-color:changed",
+ DARK_VALUE_FOR_COOKIE
+ );
+ }
}
}
diff --git a/app/assets/javascripts/discourse/app/templates/preferences/interface.gjs b/app/assets/javascripts/discourse/app/templates/preferences/interface.gjs
index c05546408a1..1c278e7d3c5 100644
--- a/app/assets/javascripts/discourse/app/templates/preferences/interface.gjs
+++ b/app/assets/javascripts/discourse/app/templates/preferences/interface.gjs
@@ -80,6 +80,18 @@ export default RouteTemplate(
/>
</div>
</div>
+ <div class="control-subgroup interface-color-mode">
+ <div class="instructions">{{i18n
+ "user.color_schemes.interface_mode"
+ }}</div>
+ <div class="controls">
+ <ComboBox
+ @content={{@controller.interfaceColorModes}}
+ @value={{@controller.selectedInterfaceColorMode}}
+ @onChange={{@controller.selectColorMode}}
+ />
+ </div>
+ </div>
{{/if}}
</div>
{{#if @controller.previewingColorScheme}}
@@ -100,7 +112,7 @@ export default RouteTemplate(
{{/if}}
{{#if @controller.showDarkColorSchemeSelector}}
<div class="instructions">
- {{i18n "user.color_schemes.dark_instructions"}}
+ {{i18n "user.color_schemes.interface_mode_instructions"}}
</div>
{{/if}}
</fieldset>
diff --git a/app/assets/stylesheets/common/base/user.scss b/app/assets/stylesheets/common/base/user.scss
index 0ecf0f213af..b1cbb4d9392 100644
--- a/app/assets/stylesheets/common/base/user.scss
+++ b/app/assets/stylesheets/common/base/user.scss
@@ -751,7 +751,8 @@
}
}
- .light-color-scheme {
+ .light-color-scheme,
+ .dark-color-scheme {
margin-right: 1em;
}
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 56c70b68eb5..f3a2896755a 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -759,13 +759,21 @@ module ApplicationHelper
end
def forced_light_mode?
- InterfaceColorSelectorSetting.enabled? && cookies[:forced_color_mode] == "light" &&
- !dark_color_scheme?
+ return false if dark_color_scheme?
+
+ cookie = cookies[:forced_color_mode]
+ return cookie == "light" if cookie.present?
+
+ !!(current_user&.user_option&.light_mode_forced?)
end
def forced_dark_mode?
- InterfaceColorSelectorSetting.enabled? && cookies[:forced_color_mode] == "dark" &&
- dark_scheme_id != -1
+ return false if dark_scheme_id == -1
+
+ cookie = cookies[:forced_color_mode]
+ return cookie == "dark" if cookie.present?
+
+ !!(current_user&.user_option&.dark_mode_forced?)
end
def light_color_hex_for_name(name)
diff --git a/app/models/user_option.rb b/app/models/user_option.rb
index 5c58e0bf0cb..376da56a41d 100644
--- a/app/models/user_option.rb
+++ b/app/models/user_option.rb
@@ -1,6 +1,10 @@
# frozen_string_literal: true
class UserOption < ActiveRecord::Base
+ AUTO_MODE = 1
+ LIGHT_MODE = 2
+ DARK_MODE = 3
+
HOMEPAGES = {
# -1 => reserved for "custom homepage"
1 => "latest",
@@ -116,6 +120,14 @@ class UserOption < ActiveRecord::Base
last_redirected_to_top_at.present?
end
+ def light_mode_forced?
+ interface_color_mode == LIGHT_MODE
+ end
+
+ def dark_mode_forced?
+ interface_color_mode == DARK_MODE
+ end
+
def update_last_redirected_to_top!
key = "user:#{id}:update_last_redirected_to_top"
delay = SiteSetting.active_user_rate_limit_secs
@@ -272,6 +284,7 @@ end
# hide_profile :boolean default(FALSE), not null
# hide_profile_and_presence :boolean default(FALSE), not null
# include_tl0_in_digests :boolean default(FALSE)
+# interface_color_mode :integer default(1), not null
# last_redirected_to_top_at :datetime
# like_notification_frequency :integer default(1), not null
# mailing_list_mode :boolean default(FALSE), not null
diff --git a/app/serializers/current_user_option_serializer.rb b/app/serializers/current_user_option_serializer.rb
index f2564d4c666..5d629d61278 100644
--- a/app/serializers/current_user_option_serializer.rb
+++ b/app/serializers/current_user_option_serializer.rb
@@ -23,7 +23,8 @@ class CurrentUserOptionSerializer < ApplicationSerializer
:treat_as_new_topic_start_date,
:sidebar_link_to_filtered_list,
:sidebar_show_count_of_new_items,
- :composition_mode
+ :composition_mode,
+ :interface_color_mode
def likes_notifications_disabled
object.likes_notifications_disabled?
diff --git a/app/serializers/user_option_serializer.rb b/app/serializers/user_option_serializer.rb
index 7e2253e27f5..7f9db9225f4 100644
--- a/app/serializers/user_option_serializer.rb
+++ b/app/serializers/user_option_serializer.rb
@@ -44,7 +44,8 @@ class UserOptionSerializer < ApplicationSerializer
:sidebar_show_count_of_new_items,
:watched_precedence_over_muted,
:topics_unread_when_closed,
- :composition_mode
+ :composition_mode,
+ :interface_color_mode
def auto_track_topics_after_msecs
object.auto_track_topics_after_msecs || SiteSetting.default_other_auto_track_topics_after_msecs
diff --git a/app/services/user_updater.rb b/app/services/user_updater.rb
index f98167132b8..124b2160932 100644
--- a/app/services/user_updater.rb
+++ b/app/services/user_updater.rb
@@ -28,6 +28,7 @@ class UserUpdater
enable_defer
color_scheme_id
dark_scheme_id
+ interface_color_mode
dynamic_favicon
automatically_unpin_topics
digest_after_minutes
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index cb333354ea2..a172de7e4af 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -1543,7 +1543,7 @@ en:
description: "Skip new user onboarding tips and badges"
reset_seen_user_tips: "Show user tips again"
theme_default_on_all_devices: "Make this the default theme on all my devices"
- color_scheme_default_on_all_devices: "Set default color palette on all my devices"
+ color_scheme_default_on_all_devices: "Set default color palette and mode on all my devices"
color_scheme: "Color Palette"
color_schemes:
default_description: "Theme default"
@@ -1553,6 +1553,12 @@ en:
regular: "Regular"
dark: "Dark mode"
default_dark_scheme: "(site default)"
+ interface_mode: "Mode"
+ interface_mode_instructions: "‘Auto’ uses your system’s preferences for light or dark mode."
+ interface_modes:
+ auto: "Auto"
+ light: "Light"
+ dark: "Dark"
dark_mode: "Dark Mode"
dark_mode_enable: "Enable automatic dark mode color palette"
text_size_default_on_all_devices: "Make this the default text size on all my devices"
diff --git a/db/migrate/20250723122719_add_interface_color_mode_to_user_options.rb b/db/migrate/20250723122719_add_interface_color_mode_to_user_options.rb
new file mode 100644
index 00000000000..7016e3bb535
--- /dev/null
+++ b/db/migrate/20250723122719_add_interface_color_mode_to_user_options.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddInterfaceColorModeToUserOptions < ActiveRecord::Migration[8.0]
+ def change
+ add_column :user_options, :interface_color_mode, :integer, null: false, default: 1
+ end
+end
diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rake
index dd26447c877..48001f2d35f 100644
--- a/lib/tasks/javascript.rake
+++ b/lib/tasks/javascript.rake
@@ -191,6 +191,12 @@ task "javascript:update_constants" => :environment do
export const INVITE_DESCRIPTION_MAX_LENGTH = #{Invite::DESCRIPTION_MAX_LENGTH};
export const USER_OPTION_COMPOSITION_MODES = #{UserOption.composition_mode_types.to_json};
+
+ export const INTERFACE_COLOR_MODES = {
+ AUTO: #{UserOption::AUTO_MODE},
+ LIGHT: #{UserOption::LIGHT_MODE},
+ DARK: #{UserOption::DARK_MODE},
+ };
JS
pretty_notifications = Notification.types.map { |n| " #{n[0]}: #{n[1]}," }.join("\n")
diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb
index 07d493d6e94..607656ff004 100644
--- a/spec/helpers/application_helper_spec.rb
+++ b/spec/helpers/application_helper_spec.rb
@@ -1057,4 +1057,174 @@ RSpec.describe ApplicationHelper do
end
end
end
+
+ describe "#forced_light_mode?" do
+ fab!(:user)
+
+ context "when the user preference in the database is set to light" do
+ before do
+ user.user_option.update!(interface_color_mode: UserOption::LIGHT_MODE)
+ helper.stubs(:current_user).returns(user)
+ end
+
+ it "returns true if the forced_color_mode cookie is set to `light`" do
+ helper.request.cookies["forced_color_mode"] = "light"
+ expect(helper.forced_light_mode?).to eq(true)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `dark`" do
+ helper.request.cookies["forced_color_mode"] = "dark"
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `auto`" do
+ helper.request.cookies["forced_color_mode"] = "auto"
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+
+ it "returns true if the forced_color_mode cookie is not set" do
+ helper.request.cookies["forced_color_mode"] = nil
+ expect(helper.forced_light_mode?).to eq(true)
+ end
+ end
+
+ context "when the user preference in the database is set to dark" do
+ before do
+ user.user_option.update!(interface_color_mode: UserOption::DARK_MODE)
+ helper.stubs(:current_user).returns(user)
+ end
+
+ it "returns true if the forced_color_mode cookie is set to `light`" do
+ helper.request.cookies["forced_color_mode"] = "light"
+ expect(helper.forced_light_mode?).to eq(true)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `dark`" do
+ helper.request.cookies["forced_color_mode"] = "dark"
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `auto`" do
+ helper.request.cookies["forced_color_mode"] = "auto"
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+
+ it "returns false if the forced_color_mode cookie is not set" do
+ helper.request.cookies["forced_color_mode"] = nil
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+ end
+
+ context "when the user preference in the database is set to auto" do
+ before do
+ user.user_option.update!(interface_color_mode: UserOption::AUTO_MODE)
+ helper.stubs(:current_user).returns(user)
+ end
+
+ it "returns true if the forced_color_mode cookie is set to `light`" do
+ helper.request.cookies["forced_color_mode"] = "light"
+ expect(helper.forced_light_mode?).to eq(true)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `dark`" do
+ helper.request.cookies["forced_color_mode"] = "dark"
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `auto`" do
+ helper.request.cookies["forced_color_mode"] = "auto"
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+
+ it "returns false if the forced_color_mode cookie is not set" do
+ helper.request.cookies["forced_color_mode"] = nil
+ expect(helper.forced_light_mode?).to eq(false)
+ end
+ end
+ end
+
+ describe "#forced_dark_mode?" do
+ fab!(:user)
+
+ context "when the user preference in the database is set to light" do
+ before do
+ user.user_option.update!(interface_color_mode: UserOption::LIGHT_MODE)
+ helper.stubs(:current_user).returns(user)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `light`" do
+ helper.request.cookies["forced_color_mode"] = "light"
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+
+ it "returns true if the forced_color_mode cookie is set to `dark`" do
+ helper.request.cookies["forced_color_mode"] = "dark"
+ expect(helper.forced_dark_mode?).to eq(true)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `auto`" do
+ helper.request.cookies["forced_color_mode"] = "auto"
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+
+ it "returns true if the forced_color_mode cookie is not set" do
+ helper.request.cookies["forced_color_mode"] = nil
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+ end
+
+ context "when the user preference in the database is set to dark" do
+ before do
+ user.user_option.update!(interface_color_mode: UserOption::DARK_MODE)
+ helper.stubs(:current_user).returns(user)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `light`" do
+ helper.request.cookies["forced_color_mode"] = "light"
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+
+ it "returns true if the forced_color_mode cookie is set to `dark`" do
+ helper.request.cookies["forced_color_mode"] = "dark"
+ expect(helper.forced_dark_mode?).to eq(true)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `auto`" do
+ helper.request.cookies["forced_color_mode"] = "auto"
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+
+ it "returns true if the forced_color_mode cookie is not set" do
+ helper.request.cookies["forced_color_mode"] = nil
+ expect(helper.forced_dark_mode?).to eq(true)
+ end
+ end
+
+ context "when the user preference in the database is set to auto" do
+ before do
+ user.user_option.update!(interface_color_mode: UserOption::AUTO_MODE)
+ helper.stubs(:current_user).returns(user)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `light`" do
+ helper.request.cookies["forced_color_mode"] = "light"
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+
+ it "returns true if the forced_color_mode cookie is set to `dark`" do
+ helper.request.cookies["forced_color_mode"] = "dark"
+ expect(helper.forced_dark_mode?).to eq(true)
+ end
+
+ it "returns false if the forced_color_mode cookie is set to `auto`" do
+ helper.request.cookies["forced_color_mode"] = "auto"
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+
+ it "returns false if the forced_color_mode cookie is not set" do
+ helper.request.cookies["forced_color_mode"] = nil
+ expect(helper.forced_dark_mode?).to eq(false)
+ end
+ end
+ end
end
diff --git a/spec/requests/api/schemas/json/user_get_response.json b/spec/requests/api/schemas/json/user_get_response.json
index c2c94d65fe9..46fa73feeb3 100644
--- a/spec/requests/api/schemas/json/user_get_response.json
+++ b/spec/requests/api/schemas/json/user_get_response.json
@@ -818,6 +818,9 @@
},
"composition_mode": {
"type": "integer"
+ },
+ "interface_color_mode": {
+ "type": "integer"
}
},
"required": [
@@ -856,7 +859,8 @@
"title_count_mode",
"timezone",
"skip_new_user_tips",
- "topics_unread_when_closed"
+ "topics_unread_when_closed",
+ "interface_color_mode"
]
}
},
diff --git a/spec/system/page_objects/components/interface_color_selector.rb b/spec/system/page_objects/components/interface_color_selector.rb
index 55e3a79217a..b987026e197 100644
--- a/spec/system/page_objects/components/interface_color_selector.rb
+++ b/spec/system/page_objects/components/interface_color_selector.rb
@@ -23,6 +23,18 @@ module PageObjects
find(container_selector).find(SELECTOR).click
end
+ def has_light_as_current_mode?
+ has_css?(SELECTOR + "[data-current-mode='light']")
+ end
+
+ def has_dark_as_current_mode?
+ has_css?(SELECTOR + "[data-current-mode='dark']")
+ end
+
+ def has_auto_as_current_mode?
+ has_css?(SELECTOR + "[data-current-mode='auto']")
+ end
+
def light_option
find("#{SELECTOR}__light-option")
end
diff --git a/spec/system/page_objects/pages/user_preferences_interface.rb b/spec/system/page_objects/pages/user_preferences_interface.rb
index 5e56cdc9a00..3b98a7a75d3 100644
--- a/spec/system/page_objects/pages/user_preferences_interface.rb
+++ b/spec/system/page_objects/pages/user_preferences_interface.rb
@@ -24,6 +24,18 @@ module PageObjects
page.find('.dark-mode input[type="checkbox"]')
end
+ def color_mode_dropdown
+ PageObjects::Components::SelectKit.new(".interface-color-mode .select-kit")
+ end
+
+ def default_palette_and_mode_for_all_devices_checkbox
+ find(".color-scheme-checkbox input[type='checkbox']")
+ end
+
+ def has_no_default_palette_and_mode_for_all_devices_checkbox?
+ has_no_css?(".color-scheme-checkbox input[type='checkbox']")
+ end
+
def save_changes
click_button "Save Changes"
expect(page).to have_content(I18n.t("js.saved"))
diff --git a/spec/system/user_page/user_preferences_interface_spec.rb b/spec/system/user_page/user_preferences_interface_spec.rb
index aa389453ee4..7cd9eeb8c10 100644
--- a/spec/system/user_page/user_preferences_interface_spec.rb
+++ b/spec/system/user_page/user_preferences_interface_spec.rb
@@ -86,4 +86,198 @@ describe "User preferences | Interface", type: :system do
end
end
end
+
+ describe "the color mode selector" do
+ let(:interface_color_mode) { PageObjects::Components::InterfaceColorMode.new }
+ let(:mode_selector_in_sidebar) do
+ PageObjects::Components::InterfaceColorSelector.new(".sidebar-footer-actions")
+ end
+
+ before { SiteSetting.interface_color_selector = "sidebar_footer" }
+
+ context "when changing own preferences" do
+ it "can change the color mode for the current device only" do
+ user_preferences_interface_page.visit(user)
+
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::AUTO_MODE,
+ ),
+ "the default value should be auto mode"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.auto"),
+ ),
+ "the default value should be auto mode"
+
+ user_preferences_interface_page.color_mode_dropdown.expand
+ user_preferences_interface_page.color_mode_dropdown.select_row_by_value(
+ UserOption::DARK_MODE,
+ )
+
+ user_preferences_interface_page.default_palette_and_mode_for_all_devices_checkbox.click
+ expect do user_preferences_interface_page.save_changes end.to not_change {
+ user.user_option.reload.interface_color_mode
+ }
+ expect(user.user_option.interface_color_mode).to eq(UserOption::AUTO_MODE),
+ "the user option in the database doesn't change"
+
+ expect(interface_color_mode).to have_dark_mode_forced, "the interface switches to dark mode"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should show dark mode as selected"
+
+ page.refresh
+
+ expect(interface_color_mode).to have_dark_mode_forced,
+ "the interface should still be in dark mode after a page refresh"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should still show dark mode as selected"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::DARK_MODE,
+ ),
+ "the dropdown should still have dark mode selected after a page refresh"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.dark"),
+ ),
+ "the dropdown should still have dark mode selected after a page refresh"
+ end
+
+ it "can change the default color mode for all devices" do
+ user.user_option.update!(interface_color_mode: UserOption::LIGHT_MODE)
+ user_preferences_interface_page.visit(user)
+
+ expect(interface_color_mode).to have_light_mode_forced,
+ "the interface should be in light mode"
+ user_preferences_interface_page.color_mode_dropdown.expand
+ user_preferences_interface_page.color_mode_dropdown.select_row_by_value(
+ UserOption::DARK_MODE,
+ )
+
+ expect do user_preferences_interface_page.save_changes end.to change {
+ user.user_option.reload.interface_color_mode
+ }.to(UserOption::DARK_MODE)
+
+ expect(interface_color_mode).to have_dark_mode_forced, "the interface switches to dark mode"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should show dark mode as selected"
+
+ page.refresh
+
+ expect(interface_color_mode).to have_dark_mode_forced,
+ "the interface should still be in dark mode after a page refresh"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should still show dark mode as selected"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::DARK_MODE,
+ ),
+ "the dropdown should still have dark mode selected after a page refresh"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.dark"),
+ ),
+ "the dropdown should still have dark mode selected after a page refresh"
+ end
+
+ it "updates the selected color mode in preferences when the color mode is changed in the sidebar" do
+ user_preferences_interface_page.visit(user)
+
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::AUTO_MODE,
+ ),
+ "the default value should be auto mode"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.auto"),
+ ),
+ "the default value should be auto mode"
+
+ mode_selector_in_sidebar.expand
+ mode_selector_in_sidebar.dark_option.click
+
+ expect(interface_color_mode).to have_dark_mode_forced, "the interface switches to dark mode"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should show dark mode as selected"
+
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::DARK_MODE,
+ ),
+ "the dropdown should now have dark mode selected"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.dark"),
+ ),
+ "the dropdown should now have dark mode selected"
+
+ page.refresh
+
+ expect(interface_color_mode).to have_dark_mode_forced,
+ "the interface should still be in dark mode after a page refresh"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should still show dark mode as selected after a page refresh"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::DARK_MODE,
+ ),
+ "the dropdown should still have dark mode selected after a page refresh"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.dark"),
+ ),
+ "the dropdown should still have dark mode selected after a page refresh"
+ end
+ end
+
+ context "when changing another user's preferences as an admin" do
+ fab!(:admin)
+
+ before do
+ sign_in(admin)
+ admin.user_option.update!(interface_color_mode: UserOption::DARK_MODE)
+ user.user_option.update!(interface_color_mode: UserOption::LIGHT_MODE)
+ end
+
+ it "doesn't affect the viewing admin preferences and changes the target user's default preference for all devices" do
+ user_preferences_interface_page.visit(user)
+
+ expect(interface_color_mode).to have_dark_mode_forced,
+ "the interface should be in dark mode for the admin"
+
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::LIGHT_MODE,
+ ),
+ "the dropdown should have light mode selected for the target user"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.light"),
+ ),
+ "the dropdown should have light mode selected for the target user"
+
+ user_preferences_interface_page.color_mode_dropdown.expand
+ user_preferences_interface_page.color_mode_dropdown.select_row_by_value(
+ UserOption::AUTO_MODE,
+ )
+ expect(
+ user_preferences_interface_page,
+ ).to have_no_default_palette_and_mode_for_all_devices_checkbox,
+ "the checkbox for applying the color mode to the current device is not present when editing another user's preferences"
+
+ expect do user_preferences_interface_page.save_changes end.to change {
+ user.user_option.reload.interface_color_mode
+ }.to(UserOption::AUTO_MODE).and not_change { admin.user_option.reload.interface_color_mode }
+
+ expect(interface_color_mode).to have_dark_mode_forced,
+ "the interface should still be in dark mode for the admin"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should still show dark mode as selected for the admin"
+
+ page.refresh
+
+ expect(interface_color_mode).to have_dark_mode_forced,
+ "the interface should still be in dark mode for the admin after a page refresh"
+ expect(mode_selector_in_sidebar).to have_dark_as_current_mode,
+ "the selector in the sidebar should still show dark mode as selected for the admin after a page refresh"
+
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_value(
+ UserOption::AUTO_MODE,
+ ),
+ "the dropdown should still have auto mode selected after a page refresh"
+ expect(user_preferences_interface_page.color_mode_dropdown).to have_selected_name(
+ I18n.t("js.user.color_schemes.interface_modes.auto"),
+ ),
+ "the dropdown should still have auto mode selected after a page refresh"
+ end
+ end
+ end
end