mirror of
https://github.com/discourse/discourse.git
synced 2025-06-04 11:11:13 +08:00
DEV: Backend support for light/dark mode in color palettes (#30893)
We're embarking on a project for overhauling the color palette and theme systems in Discourse. As part of this project, we're making each color palette include light and dark modes instead of the status quo of requiring 2 separate color palettes to implement light and dark modes. This commit is a first step towards that goal; it adds a code path for generating and serving `color_definitions` stylesheets using the built-in dark variant of a color palette. All of this code path is behind a default-off site setting `use_overhauled_theme_color_palette`, so there's no change in behavior unless the setting is enabled. Internal topic: t/141467.
This commit is contained in:
@ -877,12 +877,20 @@ RSpec.describe ApplicationHelper do
|
||||
describe "#discourse_theme_color_meta_tags" do
|
||||
before do
|
||||
light = Fabricate(:color_scheme)
|
||||
light.color_scheme_colors << ColorSchemeColor.new(name: "header_background", hex: "abcdef")
|
||||
light.color_scheme_colors << ColorSchemeColor.new(
|
||||
name: "header_background",
|
||||
hex: "abcdef",
|
||||
dark_hex: "fedcba",
|
||||
)
|
||||
light.save!
|
||||
helper.request.cookies["color_scheme_id"] = light.id
|
||||
|
||||
dark = Fabricate(:color_scheme)
|
||||
dark.color_scheme_colors << ColorSchemeColor.new(name: "header_background", hex: "defabc")
|
||||
dark.color_scheme_colors << ColorSchemeColor.new(
|
||||
name: "header_background",
|
||||
hex: "defabc",
|
||||
dark_hex: "cbafed",
|
||||
)
|
||||
dark.save!
|
||||
helper.request.cookies["dark_scheme_id"] = dark.id
|
||||
end
|
||||
@ -902,6 +910,17 @@ RSpec.describe ApplicationHelper do
|
||||
<meta name="theme-color" media="all" content="#abcdef">
|
||||
HTML
|
||||
end
|
||||
|
||||
context "when use_overhauled_theme_color_palette setting is true" do
|
||||
before { SiteSetting.use_overhauled_theme_color_palette = true }
|
||||
|
||||
it "renders a light and dark theme-color meta tag using the light and dark palettes of the same color scheme record" do
|
||||
expect(helper.discourse_theme_color_meta_tags).to eq(<<~HTML)
|
||||
<meta name="theme-color" media="(prefers-color-scheme: light)" content="#abcdef">
|
||||
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="#fedcba">
|
||||
HTML
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#discourse_color_scheme_meta_tag" do
|
||||
@ -944,4 +963,27 @@ RSpec.describe ApplicationHelper do
|
||||
HTML
|
||||
end
|
||||
end
|
||||
|
||||
describe "#dark_scheme_id" do
|
||||
fab!(:dark_scheme) { Fabricate(:color_scheme) }
|
||||
fab!(:light_scheme) { Fabricate(:color_scheme) }
|
||||
|
||||
before do
|
||||
helper.request.cookies["color_scheme_id"] = light_scheme.id
|
||||
helper.request.cookies["dark_scheme_id"] = dark_scheme.id
|
||||
end
|
||||
|
||||
it "returns the value set in the dark_scheme_id cookie" do
|
||||
expect(helper.dark_scheme_id).to eq(dark_scheme.id)
|
||||
end
|
||||
|
||||
context "when use_overhauled_theme_color_palette is true" do
|
||||
before { SiteSetting.use_overhauled_theme_color_palette = true }
|
||||
|
||||
it "returns the same value as #scheme_id" do
|
||||
expect(helper.dark_scheme_id).to eq(helper.scheme_id)
|
||||
expect(helper.scheme_id).to eq(light_scheme.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -676,6 +676,73 @@ RSpec.describe Stylesheet::Manager do
|
||||
expect(link).to include("/stylesheets/color_definitions_funky-bunch_#{cs.id}_")
|
||||
end
|
||||
|
||||
it "generates the dark mode of a color scheme when the dark option is specified" do
|
||||
scheme = ColorScheme.create_from_base(name: "Neutral", base_scheme_id: "Neutral")
|
||||
ColorSchemeRevisor.revise(
|
||||
scheme,
|
||||
colors: [{ name: "primary", hex: "CABFAF", dark_hex: "FAFCAB" }],
|
||||
)
|
||||
theme = Fabricate(:theme)
|
||||
manager = manager(theme.id)
|
||||
|
||||
dark_stylesheet =
|
||||
Stylesheet::Manager::Builder.new(
|
||||
target: :color_definitions,
|
||||
theme: theme,
|
||||
color_scheme: scheme,
|
||||
manager: manager,
|
||||
dark: true,
|
||||
).compile
|
||||
light_stylesheet =
|
||||
Stylesheet::Manager::Builder.new(
|
||||
target: :color_definitions,
|
||||
theme: theme,
|
||||
color_scheme: scheme,
|
||||
manager: manager,
|
||||
).compile
|
||||
|
||||
expect(light_stylesheet).to include("--primary: #CABFAF;")
|
||||
expect(light_stylesheet).to include("color_definitions_neutral_#{scheme.id}_#{theme.id}")
|
||||
expect(light_stylesheet).not_to include(
|
||||
"color_definitions_neutral_#{scheme.id}_#{theme.id}_dark",
|
||||
)
|
||||
|
||||
expect(dark_stylesheet).to include("--primary: #FAFCAB;")
|
||||
expect(dark_stylesheet).to include("color_definitions_neutral_#{scheme.id}_#{theme.id}_dark")
|
||||
end
|
||||
|
||||
it "uses the light colors as fallback if the dark scheme doesn't define them" do
|
||||
scheme = ColorScheme.create_from_base(name: "Neutral", base_scheme_id: "Neutral")
|
||||
ColorSchemeRevisor.revise(scheme, colors: [{ name: "primary", hex: "BACFAB", dark_hex: nil }])
|
||||
theme = Fabricate(:theme)
|
||||
manager = manager(theme.id)
|
||||
|
||||
dark_stylesheet =
|
||||
Stylesheet::Manager::Builder.new(
|
||||
target: :color_definitions,
|
||||
theme: theme,
|
||||
color_scheme: scheme,
|
||||
manager: manager,
|
||||
dark: true,
|
||||
).compile
|
||||
light_stylesheet =
|
||||
Stylesheet::Manager::Builder.new(
|
||||
target: :color_definitions,
|
||||
theme: theme,
|
||||
color_scheme: scheme,
|
||||
manager: manager,
|
||||
).compile
|
||||
|
||||
expect(light_stylesheet).to include("--primary: #BACFAB;")
|
||||
expect(light_stylesheet).to include("color_definitions_neutral_#{scheme.id}_#{theme.id}")
|
||||
expect(light_stylesheet).not_to include(
|
||||
"color_definitions_neutral_#{scheme.id}_#{theme.id}_dark",
|
||||
)
|
||||
|
||||
expect(dark_stylesheet).to include("--primary: #BACFAB;")
|
||||
expect(dark_stylesheet).to include("color_definitions_neutral_#{scheme.id}_#{theme.id}_dark")
|
||||
end
|
||||
|
||||
it "updates outputted colors when updating a color scheme" do
|
||||
scheme = ColorScheme.create_from_base(name: "Neutral", base_scheme_id: "Neutral")
|
||||
theme = Fabricate(:theme)
|
||||
@ -905,7 +972,7 @@ RSpec.describe Stylesheet::Manager do
|
||||
|
||||
# Ensure we force compile each theme only once
|
||||
expect(output.scan(/#{child_theme_with_css.name}/).length).to eq(2)
|
||||
expect(StylesheetCache.count).to eq(22) # (3 themes * 2 targets) + 16 color schemes (2 themes * 8 color schemes (7 defaults + 1 theme scheme))
|
||||
expect(StylesheetCache.count).to eq(38) # (3 themes * 2 targets) + 32 color schemes (2 themes * 8 color schemes (7 defaults + 1 theme scheme) * 2 (light and dark mode per scheme))
|
||||
end
|
||||
|
||||
it "generates precompiled CSS - core and themes" do
|
||||
@ -913,7 +980,7 @@ RSpec.describe Stylesheet::Manager do
|
||||
Stylesheet::Manager.precompile_theme_css
|
||||
|
||||
results = StylesheetCache.pluck(:target)
|
||||
expect(results.size).to eq(30) # 11 core targets + 9 theme + 10 color schemes
|
||||
expect(results.size).to eq(46) # 8 core targets + 6 theme + 32 color schemes (light and dark mode per scheme)
|
||||
|
||||
theme_targets.each do |tar|
|
||||
expect(
|
||||
@ -929,7 +996,7 @@ RSpec.describe Stylesheet::Manager do
|
||||
Stylesheet::Manager.precompile_theme_css
|
||||
|
||||
results = StylesheetCache.pluck(:target)
|
||||
expect(results.size).to eq(30) # 11 core targets + 9 theme + 10 color schemes
|
||||
expect(results.size).to eq(46) # 8 core targets + 6 theme + 32 color schemes (light and dark mode per scheme)
|
||||
|
||||
expect(results).to include("color_definitions_#{scheme1.name}_#{scheme1.id}_#{user_theme.id}")
|
||||
expect(results).to include(
|
||||
|
Reference in New Issue
Block a user