Skip to content

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

UX: add welcome banner text color setting (#34657)

Yuriy Kurant3d0d277db332
diff --git a/app/assets/javascripts/admin/addon/components/color-input.gjs b/app/assets/javascripts/admin/addon/components/color-input.gjs
index bc5d9af14a9..0fecbc0cebc 100644
--- a/app/assets/javascripts/admin/addon/components/color-input.gjs
+++ b/app/assets/javascripts/admin/addon/components/color-input.gjs
@@ -9,9 +9,10 @@ import TextField from "discourse/components/text-field";
/**
An input field for a color.
- @param hexValue is a reference to the color's hex value.
- @param brightnessValue is a number from 0 to 255 representing the brightness of the color. See ColorSchemeColor.
- @params valid is a boolean indicating if the input field is a valid color.
+ @param {string} hexValue Reference to the color's hex value.
+ @param {number} brightnessValue Number from 0 to 255 representing the brightness of the color. See ColorSchemeColor.
+ @param {boolean} valid If the input field is a valid color.
+ @param {string} [fallbackHexValue] Hex color string to use if hexValue is empty. Optional.
**/
@classNames("color-picker")
@@ -24,9 +25,15 @@ export default class ColorInput extends Component {
return this.onlyHex ? 6 : null;
}
- @computed("hexValue")
- get normalizedHexValue() {
- return this.normalize(this.hexValue);
+ @computed("hexValue", "fallbackHexValue")
+ get hexValueWithFallback() {
+ const { hexValue, fallbackHexValue } = this;
+ return hexValue || (fallbackHexValue ? fallbackHexValue : hexValue);
+ }
+
+ @computed("hexValueWithFallback")
+ get normalizedValue() {
+ return this.normalize(this.hexValueWithFallback);
}
normalize(color) {
@@ -102,7 +109,8 @@ export default class ColorInput extends Component {
<input
class="picker"
type="color"
- value={{this.normalizedHexValue}}
+ value={{this.normalizedValue}}
+ title={{this.normalizedValue}}
{{on "input" this.onPickerInput}}
aria-labelledby={{this.ariaLabelledby}}
/>
diff --git a/app/assets/javascripts/admin/addon/components/site-settings/color.gjs b/app/assets/javascripts/admin/addon/components/site-settings/color.gjs
index 3343fd1cb64..ec3f741a0d2 100644
--- a/app/assets/javascripts/admin/addon/components/site-settings/color.gjs
+++ b/app/assets/javascripts/admin/addon/components/site-settings/color.gjs
@@ -1,6 +1,8 @@
import Component from "@glimmer/component";
import ColorInput from "admin/components/color-input";
+const SETTINGS_WITH_FALLBACK_COLORS = ["welcome_banner_text_color"];
+
function RGBToHex(rgb) {
// Choose correct separator
let sep = rgb.includes(",") ? "," : " ";
@@ -25,6 +27,15 @@ function RGBToHex(rgb) {
}
export default class Color extends Component {
+ get fallbackColor() {
+ if (SETTINGS_WITH_FALLBACK_COLORS.includes(this.args.setting?.setting)) {
+ return getComputedStyle(document.documentElement).getPropertyValue(
+ "--primary"
+ );
+ }
+ return null;
+ }
+
get valid() {
let value = this.args.value.toLowerCase();
@@ -48,6 +59,7 @@ export default class Color extends Component {
<template>
<ColorInput
@hexValue={{readonly @value}}
+ @fallbackHexValue={{this.fallbackColor}}
@valid={{@valid}}
@onlyHex={{false}}
@styleSelection={{false}}
diff --git a/app/assets/javascripts/discourse/app/components/welcome-banner.gjs b/app/assets/javascripts/discourse/app/components/welcome-banner.gjs
index c85d1a70acc..6fde89b0d63 100644
--- a/app/assets/javascripts/discourse/app/components/welcome-banner.gjs
+++ b/app/assets/javascripts/discourse/app/components/welcome-banner.gjs
@@ -130,9 +130,20 @@ export default class WelcomeBanner extends Component {
get bgImgStyle() {
if (this.siteSettings.welcome_banner_image) {
return htmlSafe(
- `background-image: url(${escapeExpression(
+ `background-image:url(${escapeExpression(
this.siteSettings.welcome_banner_image
- )})`
+ )});`
+ );
+ }
+ }
+
+ get textColorStyle() {
+ if (
+ this.siteSettings.welcome_banner_image &&
+ this.siteSettings.welcome_banner_text_color
+ ) {
+ return htmlSafe(
+ `color:${escapeExpression(this.siteSettings.welcome_banner_text_color)};`
);
}
}
@@ -153,7 +164,10 @@ export default class WelcomeBanner extends Component {
class="custom-search-banner-wrap welcome-banner__wrap"
style={{if this.bgImgStyle this.bgImgStyle}}
>
- <div class="welcome-banner__title">
+ <div
+ class="welcome-banner__title"
+ style={{if this.textColorStyle this.textColorStyle}}
+ >
{{htmlSafe this.headerText}}
{{#if this.subheaderText}}
<p class="welcome-banner__subheader">
diff --git a/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs
index ad5d824d111..bdeed3f255f 100644
--- a/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs
+++ b/app/assets/javascripts/discourse/tests/integration/components/color-input-test.gjs
@@ -28,4 +28,49 @@ module("Integration | Component | ColorInput", function (hooks) {
await triggerEvent(".hex-input", "blur");
assert.strictEqual(result, "052e3d", "with 6 digit hex");
});
+
+ module("with fallback hex value provided", function (innerHooks) {
+ innerHooks.beforeEach(function () {
+ this.set("fallbackHexValue", "abcdef");
+ });
+
+ test("with empty hex value", async function (assert) {
+ this.set("hexValue", "");
+
+ await render(
+ <template>
+ <ColorInput
+ @hexValue={{this.hexValue}}
+ @fallbackHexValue={{this.fallbackHexValue}}
+ />
+ </template>
+ );
+
+ assert
+ .dom("input.picker")
+ .hasValue("#abcdef", "uses fallback color when hexValue is empty")
+ .hasAttribute("title", "#abcdef", "sets title to fallback color");
+ });
+
+ test("with hex value", async function (assert) {
+ this.set("hexValue", "123456");
+
+ await render(
+ <template>
+ <ColorInput
+ @hexValue={{this.hexValue}}
+ @fallbackHexValue={{this.fallbackHexValue}}
+ />
+ </template>
+ );
+
+ assert
+ .dom(".hex-input")
+ .hasValue("123456", "ignores fallback if hexValue is provided");
+ assert
+ .dom("input.picker")
+ .hasValue("#123456", "uses hexValue when provided")
+ .hasAttribute("title", "#123456", "uses hexValue when provided");
+ });
+ });
});
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 290e9e4045d..0c2a415edb3 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2730,6 +2730,7 @@ en:
welcome_banner_image: "Set an image to display in the background of the welcome banner."
welcome_banner_location: "Determines where on the page the welcome banner will appear."
welcome_banner_page_visibility: "Determines on which pages the welcome banner will appear."
+ welcome_banner_text_color: "When the 'Welcome banner image' setting has been configured, use this setting to change the banner text color to ensure it remains readable. When left blank or when no image has been uploaded, the 'primary' color from the color palette will be used."
splash_screen: "Displays a temporary loading screen while site assets load"
navigation_menu: "Specify sidebar or header dropdown as the main navigation menu for your site. Sidebar is recommended."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index b7f6c72832f..bb961174e48 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -3671,19 +3671,22 @@ uncategorized:
default: true
themeable: true
area: "interface"
-
welcome_banner_image:
client: true
default: ""
type: upload
area: "interface"
-
+ welcome_banner_text_color:
+ client: true
+ type: color
+ default: ""
+ validator: "CssColorWithBlankValidator"
+ area: "interface"
welcome_banner_location:
client: true
enum: "WelcomeBannerLocation"
default: "above_topic_content"
area: "interface"
-
welcome_banner_page_visibility:
client: true
enum: "WelcomeBannerPageVisibility"
diff --git a/lib/validators/css_color_with_blank_validator.rb b/lib/validators/css_color_with_blank_validator.rb
new file mode 100644
index 00000000000..a65e96dff13
--- /dev/null
+++ b/lib/validators/css_color_with_blank_validator.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+class CssColorWithBlankValidator < CssColorValidator
+ def valid_value?(val)
+ return true if val.blank?
+ !!(val =~ /\A#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\z/ || COLORS.include?(val&.downcase))
+ end
+
+ def error_message
+ I18n.t("site_settings.errors.invalid_css_color")
+ end
+end
diff --git a/plugins/discourse-assign/test/javascripts/acceptance/user-private-messages-assigned-test.js b/plugins/discourse-assign/test/javascripts/acceptance/user-private-messages-assigned-test.js
index 1840e92e2f9..aca9f3b76de 100644
--- a/plugins/discourse-assign/test/javascripts/acceptance/user-private-messages-assigned-test.js
+++ b/plugins/discourse-assign/test/javascripts/acceptance/user-private-messages-assigned-test.js
@@ -29,10 +29,10 @@ acceptance("Discourse Assign | User Private Messages", function (needs) {
await visit("/u/eviltrout/messages");
await click(".messages-dropdown-trigger");
assert
- .dom(".dropdown-menu li:nth-child(3)")
+ .dom(".dropdown-menu li:nth-child(2)")
.hasText(i18n("discourse_assign.assigned"));
- await click(".dropdown-menu li:nth-child(3) .btn");
+ await click(".dropdown-menu li:nth-child(2) .btn");
assert.strictEqual(
currentURL(),
"/u/eviltrout/messages/assigned",
diff --git a/plugins/discourse-subscriptions/assets/javascripts/discourse/components/subscribe-card.gjs b/plugins/discourse-subscriptions/assets/javascripts/discourse/components/subscribe-card.gjs
index 85acc34552c..5abf69781ad 100644
--- a/plugins/discourse-subscriptions/assets/javascripts/discourse/components/subscribe-card.gjs
+++ b/plugins/discourse-subscriptions/assets/javascripts/discourse/components/subscribe-card.gjs
@@ -9,8 +9,7 @@ export default class SubscribeCard extends Component {
}
setCardElementStyles() {
- const root = document.querySelector(":root");
- const computedStyle = getComputedStyle(root);
+ const computedStyle = getComputedStyle(document.documentElement);
const primaryColor = computedStyle.getPropertyValue("--primary");
const placeholderColor =
computedStyle.getPropertyValue("--secondary-medium");
diff --git a/spec/lib/validators/css_color_with_blank_validator_spec.rb b/spec/lib/validators/css_color_with_blank_validator_spec.rb
new file mode 100644
index 00000000000..d74eb67bf15
--- /dev/null
+++ b/spec/lib/validators/css_color_with_blank_validator_spec.rb
@@ -0,0 +1,43 @@
+# frozen_string_literal: true
+
+RSpec.describe CssColorWithBlankValidator do
+ subject(:validator) { described_class.new }
+
+ describe "#valid_value?" do
+ context "when value is blank" do
+ it "accepts nil values" do
+ expect(validator.valid_value?(nil)).to eq(true)
+ end
+
+ it "accepts empty strings" do
+ expect(validator.valid_value?("")).to eq(true)
+ end
+
+ it "accepts strings with only whitespace" do
+ expect(validator.valid_value?(" ")).to eq(true)
+ expect(validator.valid_value?("\t")).to eq(true)
+ expect(validator.valid_value?("\n")).to eq(true)
+ expect(validator.valid_value?(" \t \n ")).to eq(true)
+ end
+ end
+
+ context "when inherits parent validation behavior" do
+ it "still validates valid colors" do
+ expect(validator.valid_value?("#fde")).to eq(true)
+ expect(validator.valid_value?("#000000")).to eq(true)
+ expect(validator.valid_value?("red")).to eq(true)
+ end
+
+ it "still rejects invalid colors" do
+ expect(validator.valid_value?("#g")).to eq(false)
+ expect(validator.valid_value?("invalidcolor")).to eq(false)
+ end
+ end
+ end
+
+ describe "#error_message" do
+ it "returns the correct internationalization key" do
+ expect(validator.error_message).to eq(I18n.t("site_settings.errors.invalid_css_color"))
+ end
+ end
+end
diff --git a/spec/system/page_objects/components/welcome_banner.rb b/spec/system/page_objects/components/welcome_banner.rb
index 4f596f421dd..5e809b06a89 100644
--- a/spec/system/page_objects/components/welcome_banner.rb
+++ b/spec/system/page_objects/components/welcome_banner.rb
@@ -55,11 +55,23 @@ module PageObjects
def has_bg_img?(url)
has_css?(
".welcome-banner.--with-bg-img .custom-search-banner-wrap.welcome-banner__wrap",
- style: "background-image: url(#{url})",
+ style: "background-image:url(#{url});",
visible: :visible,
)
end
+ def has_custom_text_color?(color)
+ has_css?(
+ ".welcome-banner .welcome-banner__title",
+ style: "color:#{color};",
+ visible: :visible,
+ )
+ end
+
+ def has_no_custom_text_color?(color)
+ has_no_css?(".welcome-banner .welcome-banner__title", style: "color:#{color};")
+ end
+
def above_topic_content?
has_css?("#main-outlet > .--location-above-topic-content", visible: :visible)
end
diff --git a/spec/system/welcome_banner_spec.rb b/spec/system/welcome_banner_spec.rb
index a2f5e9e52c1..56af23b59b4 100644
--- a/spec/system/welcome_banner_spec.rb
+++ b/spec/system/welcome_banner_spec.rb
@@ -146,7 +146,7 @@ describe "Welcome banner", type: :system do
end
end
- context "with background image setting" do
+ context "for background image setting" do
fab!(:current_user, :admin)
fab!(:bg_img) { Fabricate(:image_upload, color: "cyan") }
@@ -166,6 +166,22 @@ describe "Welcome banner", type: :system do
visit "/"
expect(banner).to have_bg_img(bg_img.url)
end
+
+ context "for text color setting" do
+ let(:red) { "#ff0000" }
+ before { SiteSetting.welcome_banner_text_color = red }
+
+ it "doesn't set text color without background image" do
+ visit "/"
+ expect(banner).to have_no_custom_text_color(red)
+ end
+
+ it "applies text color if background image is set" do
+ SiteSetting.welcome_banner_image = bg_img
+ visit "/"
+ expect(banner).to have_custom_text_color(red)
+ end
+ end
end
context "with interface location setting" do