Skip to content

Commit

Permalink
Fix method used to apply styles
Browse files Browse the repository at this point in the history
  • Loading branch information
jcwillox committed Feb 19, 2021
1 parent 36b2856 commit 20f145e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
13 changes: 7 additions & 6 deletions src/modules/generic-card.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extensionEnabled, findConfig, moduleEnabled } from "../utils";
import { applyTheme } from "../styles";
import { applyTheme, mapStyle } from "../styles";
import { NO_CARD_STYLE } from "../const";
import { hass } from "card-tools/src/hass";
import { createModule } from "../module";
Expand All @@ -14,17 +14,18 @@ if (moduleEnabled(MODULE)) {

// ha-card elements have default transition that we need to temporarily disable.
this.style.transition = "none";
this.style.cssText = "";

if (config.no_card === true && extensionEnabled(config, "no_card")) {
Object.assign(this.style, NO_CARD_STYLE);
if (config.canary_theme && extensionEnabled(config, "canary_theme")) {
applyTheme(this, hass().themes, config.canary_theme);
}

if (config.canary_style && extensionEnabled(config, "canary_style")) {
Object.assign(this.style, config.canary_style);
this.style.cssText += mapStyle(config.canary_style);
}

if (config.canary_theme && extensionEnabled(config, "canary_theme")) {
applyTheme(this, hass().themes, config.canary_theme);
if (config.no_card === true && extensionEnabled(config, "no_card")) {
Object.assign(this.style, NO_CARD_STYLE);
}

// flush css.
Expand Down
22 changes: 15 additions & 7 deletions src/modules/generic-entity-row.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { hasTemplate } from "card-tools/src/templates";
import { hass, provideHass } from "card-tools/src/hass";
import { extensionEnabled, moduleEnabled } from "../utils";
import { createModule } from "../module";
import { applyTheme } from "../styles";
import { applyTheme, mapStyle } from "../styles";

const MODULE = "generic-entity-row";
const ELEMENT = "hui-generic-entity-row";
Expand Down Expand Up @@ -44,18 +44,26 @@ if (moduleEnabled(MODULE)) {
}
}

if (
this.config.canary_style &&
extensionEnabled(this.config, "canary_style")
) {
Object.assign(this.style, this.config.canary_style);
}
this.style.cssText = "";

if (
this.config.canary_theme &&
extensionEnabled(this.config, "canary_theme")
) {
applyTheme(this, hass().themes, this.config.canary_theme);
}

if (
this.config.canary_style &&
extensionEnabled(this.config, "canary_style")
) {
if (typeof this.config.canary_style === "string") {
let styleEl = document.createElement("style");
styleEl.innerHTML = this.config.canary_style;
this.appendChild(styleEl);
} else {
this.style.cssText += mapStyle(this.config.canary_style);
}
}
});
}
4 changes: 1 addition & 3 deletions src/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export function coerceObject(data = {}) {

export function applyTheme(element, themes, localTheme) {
if (localTheme in themes.themes) {
for (const [key, value] of Object.entries(themes.themes[localTheme])) {
element.style.setProperty("--" + key, value);
}
element.style.cssText = mapStyle(themes.themes[localTheme], "--");
}
}

0 comments on commit 20f145e

Please sign in to comment.