Metatag theme-color not being defined when using the color palette toggle example #3881
Replies: 4 comments 11 replies
-
Thanks for reporting. The problem with adding the theme color for the multi-color-palette configuration is that there can only be a single theme color, as it is set during the build (inside HTML). When a user switches to the other color, you would end up with non-matching theme colors. This is the reason why it's not set in this configuration setting and what you already observed. Additionally, the theme color must be a hex value, and we set color names in You can use theme extension to add this logic if you feel that it's indispensable for you. |
Beta Was this translation helpful? Give feedback.
-
add extra javascript function syncThemeColor() {
const color = getComputedStyle(document.querySelector("[data-md-component=header]"))['background-color'];
let metaThemeColor = document.querySelector("meta[name=theme-color]");
if (!metaThemeColor) {
metaThemeColor = document.createElement('meta');
metaThemeColor.name = "theme-color";
metaThemeColor.content = color;
document.head.appendChild(metaThemeColor);
}
metaThemeColor.setAttribute("content", color);
}
document.querySelectorAll("input[name='__palette']").forEach(function (button) {
button.addEventListener("change", function () {
syncThemeColor();
})
})
syncThemeColor(); |
Beta Was this translation helpful? Give feedback.
-
I use {% extends "base.html" %}
{% block extrahead %}
{% if config.theme.palette %}
{% set firstPalette = config.theme.palette[0] %}
{% if firstPalette.primary %}
{% import "partials/palette.html" as map %}
{% set primary = map.primary(
firstPalette.primary | replace(" ", "-") | lower
) %}
<meta name="theme-color" content="{{ primary }}" />
{% endif %}
{% endif %}
{% endblock %} |
Beta Was this translation helpful? Give feedback.
-
The issue is fixed in 944180d. The theme color is now automatically set from JavaScript and always kept in sync with the header. The changes are on the v9 branch, progress is tracked in #4714: RPReplay_Final1670777188.MP4 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If I just have a static theme colour the correct metatags are generated for every page. However, if you use the colour palette toggle example, no theme colour is generated.
I understand why switching theme colour on the fly based on cached info in the browser could present issues, but it would be nice if the theme colour could be hard set, or if it used the default scheme's primary colour.
Beta Was this translation helpful? Give feedback.
All reactions