Skip to content

Commit

Permalink
Even less flashing. FIx #114
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Mar 3, 2021
1 parent 3343450 commit 8360cb5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
4 changes: 2 additions & 2 deletions card-mod.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "card-mod",
"private": true,
"version": "3.0.7",
"version": "3.0.8",
"description": "",
"scripts": {
"build": "rollup -c",
Expand Down
35 changes: 22 additions & 13 deletions src/card-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,29 @@ export class CardMod extends LitElement {
merge_deep(styles, theme_styles);

const styleChildren: Set<CardMod> = new Set();
let thisStyle: any;
const parent = this.parentElement || this.parentNode;
if (!styles["."]) this._styles = "";

if (!styles["."]) thisStyle = "";
for (const [key, value] of Object.entries(styles as object)) {
if (key === ".") {
this._styles = value;
thisStyle = value;
} else {
for (const el of await selectTree(parent, key, true)) {
if (el)
styleChildren.add(
await applyToElement(
el,
`${this.type}-child`,
value,
this.variables,
null,
false
)
const elements = await selectTree(parent, key, true);
if (!elements) continue;
for (const el of elements) {
if (el) {
const child = await applyToElement(
el,
`${this.type}-child`,
value,
this.variables,
null,
false
);
child.refresh();
styleChildren.add(child);
}
}
}
}
Expand All @@ -114,6 +119,9 @@ export class CardMod extends LitElement {
}
this._styleChildren = styleChildren;

if (this._styles === thisStyle) return;
this._styles = thisStyle;

if (this._styles && hasTemplate(this._styles)) {
this._renderer = this._renderer || this._style_rendered.bind(this);
bind_template(this._renderer, this._styles as string, this.variables);
Expand All @@ -126,6 +134,7 @@ export class CardMod extends LitElement {

private async _disconnect() {
this._observer.disconnect();
this._styles = "";
await unbind_template(this._renderer);
}

Expand Down
1 change: 1 addition & 0 deletions src/patch/ha-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ customElements.whenDefined("ha-card").then(() => {
const _update = pn.update;
pn.update = function (changedProperties: any) {
_update.bind(this)(changedProperties);
cardMod.refresh();
this.updateComplete.then(() => {
cardMod.refresh();
});
Expand Down
2 changes: 1 addition & 1 deletion src/theme-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Promise.race(bases).then(() => {
window.setTimeout(() => {
hass().connection.subscribeEvents(() => {
window.setTimeout(refresh_theme, 500);
});
}, "themes_updated");
document
.querySelector("home-assistant")
?.addEventListener("settheme", refresh_theme);
Expand Down
11 changes: 11 additions & 0 deletions test/themes/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test:
card-mod-theme: test
card-mod-card: |
ha-card {
background: url("https://placebear.com/640/480")
}
ha-card.top-level-card {
border: solid 1px var(--secondary-text-color);
background: url("https://placekitten.com/640/480");
}

0 comments on commit 8360cb5

Please sign in to comment.