Skip to content

Commit

Permalink
Allow combining card and row styles
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Feb 27, 2021
1 parent b1be78d commit a99db48
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 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.1",
"version": "3.0.2",
"description": "",
"scripts": {
"build": "rollup -c",
Expand Down
2 changes: 1 addition & 1 deletion src/card-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class CardMod extends LitElement {
this._styleChildren.add(
await applyToElement(
el,
undefined,
`${this.type}-child`,
value,
this.variables,
null,
Expand Down
22 changes: 16 additions & 6 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CardMod } from "./card-mod";
interface ModdedElement extends HTMLElement {
updateComplete?: Promise<void>;
modElement?: ModdedElement;
_cardMod?: CardMod;
_cardMod?: CardMod[];
}

export type Styles = string | Record<string, any>;
Expand All @@ -28,8 +28,21 @@ export async function applyToElement(
await customElements.whenDefined(el.localName);
if (el.updateComplete) await el.updateComplete;

const cardMod = (el._cardMod =
el._cardMod || (document.createElement("card-mod") as CardMod));
if (el._cardMod === undefined) {
el._cardMod = [];
}
let cardMod: CardMod;
for (const cm of el._cardMod) {
if (cm.type === type) {
cardMod = cm;
break;
}
}
if (!cardMod) {
cardMod = document.createElement("card-mod") as CardMod;
cardMod.type = type;
el._cardMod.push(cardMod);
}

const target = el.modElement
? el.modElement
Expand All @@ -38,9 +51,6 @@ export async function applyToElement(
: el;
target.appendChild(cardMod as Node);

if (el.updateComplete) await el.updateComplete;

cardMod.type = type;
cardMod.variables = variables;
cardMod.styles = styles;

Expand Down
36 changes: 36 additions & 0 deletions test/views/5_dom_navigation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,39 @@ cards:
}
entity: alarm_control_panel.alarm
- type: vertical-stack
cards:
- <<: *desc
content: |
## Overriding DOM navigation
First two icons red
Third icon green
- type: entities
entities:
- entity: sun.sun
- entity: sun.sun
- entity: sun.sun
card_mod:
style:
hui-generic-entity-row:
$:
state-badge:
$:
ha-icon:
$: |
ha-svg-icon {
color: green !important;
}
card_mod:
style:
hui-text-entity-row:
$:
hui-generic-entity-row:
$:
state-badge:
$:
ha-icon:
$: |
ha-svg-icon {
color: red;
}

0 comments on commit a99db48

Please sign in to comment.