Fix theme setting (#11977)

This commit is contained in:
Bram Kragten 2022-03-08 19:13:08 +01:00 committed by GitHub
parent fc6b594a27
commit db830e9014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 56 additions and 29 deletions

View File

@ -78,6 +78,9 @@ class DemoCards extends LitElement {
ha-formfield { ha-formfield {
margin-right: 16px; margin-right: 16px;
} }
#container {
background-color: var(--primary-background-color);
}
`; `;
} }

View File

@ -121,7 +121,8 @@ export class HassioMain extends SupervisorBaseElement {
this.parentElement, this.parentElement,
this.hass.themes, this.hass.themes,
themeName, themeName,
themeSettings themeSettings,
true
); );
} }
} }

View File

@ -101,13 +101,19 @@ class HaAuthorize extends litLocalizeLiteMixin(LitElement) {
this._fetchAuthProviders(); this._fetchAuthProviders();
if (matchMedia("(prefers-color-scheme: dark)").matches) { if (matchMedia("(prefers-color-scheme: dark)").matches) {
applyThemesOnElement(document.documentElement, { applyThemesOnElement(
default_theme: "default", document.documentElement,
default_dark_theme: null, {
themes: {}, default_theme: "default",
darkMode: true, default_dark_theme: null,
theme: "default", themes: {},
}); darkMode: true,
theme: "default",
},
undefined,
undefined,
true
);
} }
if (!this.redirectUri) { if (!this.redirectUri) {

View File

@ -31,11 +31,12 @@ export const applyThemesOnElement = (
element, element,
themes: HomeAssistant["themes"], themes: HomeAssistant["themes"],
selectedTheme?: string, selectedTheme?: string,
themeSettings?: Partial<HomeAssistant["selectedTheme"]> themeSettings?: Partial<HomeAssistant["selectedTheme"]>,
main?: boolean
) => { ) => {
// If there is no explicitly desired theme provided, we automatically // If there is no explicitly desired theme provided, and the element is the main element we automatically
// use the active one from `themes`. // use the active one from `themes`.
const themeToApply = selectedTheme || themes.theme; const themeToApply = selectedTheme || (main ? themes.theme : undefined);
// If there is no explicitly desired dark mode provided, we automatically // If there is no explicitly desired dark mode provided, we automatically
// use the active one from `themes`. // use the active one from `themes`.
@ -47,7 +48,7 @@ export const applyThemesOnElement = (
let cacheKey = themeToApply; let cacheKey = themeToApply;
let themeRules: Partial<ThemeVars> = {}; let themeRules: Partial<ThemeVars> = {};
if (darkMode) { if (themeToApply && darkMode) {
cacheKey = `${cacheKey}__dark`; cacheKey = `${cacheKey}__dark`;
themeRules = { ...darkStyles }; themeRules = { ...darkStyles };
} }

View File

@ -300,7 +300,9 @@ export const provideHass = (
applyThemesOnElement( applyThemesOnElement(
document.documentElement, document.documentElement,
themes, themes,
selectedTheme!.theme selectedTheme!.theme,
undefined,
true
); );
}, },

View File

@ -101,7 +101,8 @@ class SupervisorErrorScreen extends LitElement {
this.parentElement, this.parentElement,
this.hass.themes, this.hass.themes,
themeName, themeName,
themeSettings themeSettings,
true
); );
} }

View File

@ -133,13 +133,19 @@ class HaOnboarding extends litLocalizeLiteMixin(HassElement) {
import("./particles"); import("./particles");
} }
if (matchMedia("(prefers-color-scheme: dark)").matches) { if (matchMedia("(prefers-color-scheme: dark)").matches) {
applyThemesOnElement(document.documentElement, { applyThemesOnElement(
default_theme: "default", document.documentElement,
default_dark_theme: null, {
themes: {}, default_theme: "default",
darkMode: true, default_dark_theme: null,
theme: "default", themes: {},
}); darkMode: true,
theme: "default",
},
undefined,
undefined,
true
);
} }
} }

View File

@ -38,13 +38,19 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
}); });
mql.addListener((ev) => this._applyTheme(ev.matches)); mql.addListener((ev) => this._applyTheme(ev.matches));
if (!this._themeApplied && mql.matches) { if (!this._themeApplied && mql.matches) {
applyThemesOnElement(document.documentElement, { applyThemesOnElement(
default_theme: "default", document.documentElement,
default_dark_theme: null, {
themes: {}, default_theme: "default",
darkMode: true, default_dark_theme: null,
theme: "default", themes: {},
}); darkMode: true,
theme: "default",
},
undefined,
undefined,
true
);
} }
} }
@ -93,7 +99,8 @@ export default <T extends Constructor<HassBaseEl>>(superClass: T) =>
document.documentElement, document.documentElement,
this.hass.themes, this.hass.themes,
themeName, themeName,
themeSettings themeSettings,
true
); );
if (darkMode !== this.hass.themes.darkMode) { if (darkMode !== this.hass.themes.darkMode) {