Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Automatically detect user contrast setting

Kabiru Mwenjace689f9386db
diff --git a/app/views/layouts/base.html.erb b/app/views/layouts/base.html.erb
index 5dcb558d85f..6363f71536c 100644
--- a/app/views/layouts/base.html.erb
+++ b/app/views/layouts/base.html.erb
@@ -46,10 +46,10 @@ See COPYRIGHT and LICENSE files for more details.
// Apply system theme before body renders to prevent flickering
// This duplicates logic from AutoThemeSwitcher controller for immediate execution
(function() {
- const theme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
+ const colorModePreference = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
const body = document.body;
- switch (theme) {
+ switch (colorModePreference) {
case 'dark':
body.setAttribute('data-color-mode', 'dark');
body.setAttribute('data-dark-theme', 'dark');
@@ -57,9 +57,11 @@ See COPYRIGHT and LICENSE files for more details.
break;
case 'light':
body.setAttribute('data-color-mode', 'light');
- body.setAttribute('data-light-theme', 'light');
+ body.setAttribute('data-light-theme', window.matchMedia('(prefers-contrast: more)').matches ? 'light_high_contrast' : 'light');
body.removeAttribute('data-dark-theme');
break;
+ default: // Do nothing
+ break;
}
})();
<% end %>
diff --git a/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts b/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts
index 241f8eab87a..b67c2d6068a 100644
--- a/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts
+++ b/frontend/src/stimulus/controllers/auto-theme-switcher.controller.ts
@@ -31,7 +31,7 @@
import { Controller } from '@hotwired/stimulus';
import { useMatchMedia } from 'stimulus-use';
-export type OpTheme = 'light' | 'dark';
+export type OpTheme = 'light' | 'light_high_contrast' | 'dark';
export default class AutoThemeSwitcher extends Controller {
static values = {
@@ -69,11 +69,15 @@ export default class AutoThemeSwitcher extends Controller {
break;
case 'light':
body.setAttribute('data-color-mode', 'light');
- body.setAttribute('data-light-theme', 'light');
+ body.setAttribute('data-light-theme', this.increaseContrast ? 'light_high_contrast' : 'light');
body.removeAttribute('data-dark-theme');
break;
default: // Do nothing
break;
}
}
+
+ private get increaseContrast():boolean {
+ return window.matchMedia('(prefers-contrast: more)').matches;
+ }
}
diff --git a/spec/features/my/interface_spec.rb b/spec/features/my/interface_spec.rb
index 03980411247..eb0338ff2a7 100644
--- a/spec/features/my/interface_spec.rb
+++ b/spec/features/my/interface_spec.rb
@@ -47,6 +47,11 @@ RSpec.describe "My account Interface settings",
expect(page).to have_css("body[data-color-mode='light'][data-light-theme='light']")
+ select "Light high contrast", from: "Colour mode"
+ click_on "Update look and feel"
+
+ expect(page).to have_css("body[data-color-mode='light'][data-light-theme='light_high_contrast']")
+
select "Dark", from: "Colour mode"
click_on "Update look and feel"
@@ -59,15 +64,25 @@ RSpec.describe "My account Interface settings",
end
describe "Automatic (match OS colour mode)" do
- context "with OS in dark mode", driver: :chrome_dark_mode do
- it "syncs with OS colour mode" do
- click_on "Interface"
+ def set_automatic_mode_with_reload
+ click_on "Interface"
- select "Dark", from: "Colour mode"
- click_on "Update look and feel"
+ select "Automatic (match OS colour mode)", from: "Colour mode"
+ click_on "Update look and feel"
+ end
+ context "with OS in dark mode", driver: :chrome_dark_mode do
+ it "syncs with OS colour mode" do
+ set_automatic_mode_with_reload
expect(page).to have_css("body[data-color-mode='dark'][data-dark-theme='dark']")
end
end
+
+ context "with OS contrasting mode", driver: :firefox_light_high_contrast do
+ it "syncs with OS colour mode" do
+ set_automatic_mode_with_reload
+ expect(page).to have_css("body[data-color-mode='light'][data-light-theme='light_high_contrast']")
+ end
+ end
end
end
diff --git a/spec/support/browsers/firefox.rb b/spec/support/browsers/firefox.rb
index fc0b2a58956..7b5b68e65e3 100644
--- a/spec/support/browsers/firefox.rb
+++ b/spec/support/browsers/firefox.rb
@@ -60,3 +60,8 @@ end
register_firefox "en"
# Register german locale for custom field decimal test
register_firefox "de"
+
+register_firefox "en", name: :firefox_light_high_contrast do |profile, _options|
+ # 2 = more, 1 = less, 0 = no-preference, 3 = forced-colors only
+ profile["layout.css.prefers-contrast.content-override"] = 2
+end