From eb7f0ec766622ca0c0e1ae237bbeb062e7545093 Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Wed, 17 Mar 2021 13:34:15 -0400 Subject: [PATCH] FIX: In components, fall back to default theme color variables (#12423) Component SCSS compilation should use the current theme's SCSS color variables as a fallback before using the default core colors. This is mostly a backwards-compatibility fix, new themes and components should use CSS custom properties, which offer better support for on-the-fly color scheme changes (dark mode support, etc.). --- lib/stylesheet/importer.rb | 5 ++++- spec/components/stylesheet/manager_spec.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rb index 47f8e709a5d..bf2c1186548 100644 --- a/lib/stylesheet/importer.rb +++ b/lib/stylesheet/importer.rb @@ -135,8 +135,11 @@ module Stylesheet rescue ColorScheme.base_colors end + elsif (@theme_id && theme.color_scheme) + colors = theme.color_scheme.resolved_colors else - colors = (@theme_id && theme.color_scheme) ? theme.color_scheme.resolved_colors : ColorScheme.base_colors + colors = Theme.find_by_id(SiteSetting.default_theme_id)&.color_scheme&.resolved_colors || + ColorScheme.base_colors end colors.each do |n, hex| diff --git a/spec/components/stylesheet/manager_spec.rb b/spec/components/stylesheet/manager_spec.rb index 97ab426f617..53079526d89 100644 --- a/spec/components/stylesheet/manager_spec.rb +++ b/spec/components/stylesheet/manager_spec.rb @@ -415,6 +415,24 @@ describe Stylesheet::Manager do expect { stylesheet.compile }.not_to raise_error end + + it "child theme SCSS includes the default theme's color scheme variables" do + SiteSetting.default_theme_id = theme.id + custom_scheme = ColorScheme.create_from_base(name: "Neutral", base_scheme_id: "Neutral") + ColorSchemeRevisor.revise(custom_scheme, colors: [{ name: "primary", hex: "CC0000" }]) + theme.color_scheme_id = custom_scheme.id + theme.save! + + scss = "body{ border: 2px solid $primary;}" + child.set_field(target: :common, name: "scss", value: scss) + child.save! + + child_theme_manager = Stylesheet::Manager.new(:desktop_theme, child.id) + child_theme_manager.compile(force: true) + + child_css = File.read(child_theme_manager.stylesheet_fullpath) + expect(child_css).to include("body{border:2px solid #c00}") + end end context 'encoded slugs' do