diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index e0a0d22a1ef..c5e4ed7812b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -678,6 +678,20 @@ module ApplicationHelper result.html_safe end + def discourse_color_scheme_meta_tag + scheme = + if dark_scheme_id == -1 + # no automatic client-side switching + dark_color_scheme? ? "dark" : "light" + else + # auto-switched based on browser setting + "light dark" + end + <<~HTML.html_safe + + HTML + end + def dark_color_scheme? return false if scheme_id.blank? ColorScheme.find_by_id(scheme_id)&.is_dark? diff --git a/app/views/layouts/_head.html.erb b/app/views/layouts/_head.html.erb index 5f16e291241..ed74c484671 100644 --- a/app/views/layouts/_head.html.erb +++ b/app/views/layouts/_head.html.erb @@ -7,6 +7,7 @@ <%- end %> <%= discourse_theme_color_meta_tags %> +<%= discourse_color_scheme_meta_tag %> <%- if Discourse.base_path.present? %> diff --git a/app/views/layouts/finish_installation.html.erb b/app/views/layouts/finish_installation.html.erb index ecc82f04158..1dee3c3535d 100644 --- a/app/views/layouts/finish_installation.html.erb +++ b/app/views/layouts/finish_installation.html.erb @@ -2,7 +2,6 @@ <%= discourse_stylesheet_link_tag 'wizard', theme_id: nil %> <%= discourse_color_scheme_stylesheets %> - <%= render partial: "layouts/head" %> <%= t 'wizard.title' %> diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 561005784b7..568e0248986 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -903,4 +903,45 @@ RSpec.describe ApplicationHelper do HTML end end + + describe "#discourse_color_scheme_meta_tag" do + fab!(:color_scheme) + + before { SiteSetting.default_dark_mode_color_scheme_id = -1 } + + it "renders a 'light' color-scheme if no dark scheme is set and the current scheme is light" do + ColorSchemeRevisor.revise( + color_scheme, + colors: [{ name: "primary", hex: "333333" }, { name: "secondary", hex: "DDDDDD" }], + ) + + helper.request.cookies["color_scheme_id"] = color_scheme.id + + expect(helper.discourse_color_scheme_meta_tag).to eq(<<~HTML) + + HTML + end + + it "renders a 'dark' color-scheme if no dark scheme is set and the default scheme is dark" do + ColorSchemeRevisor.revise( + color_scheme, + colors: [{ name: "primary", hex: "F8F8F8" }, { name: "secondary", hex: "232323" }], + ) + @scheme_id = color_scheme.id + + expect(helper.discourse_color_scheme_meta_tag).to eq(<<~HTML) + + HTML + end + + it "renders a 'light dark' color-scheme if a dark scheme is set" do + dark = Fabricate(:color_scheme) + dark.save! + helper.request.cookies["dark_scheme_id"] = dark.id + + expect(helper.discourse_color_scheme_meta_tag).to eq(<<~HTML) + + HTML + end + end end