Skip to content

Commit

Permalink
Hide when child is hidden (conditional-card)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed Apr 16, 2020
1 parent 16e8da9 commit 102d365
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
28 changes: 23 additions & 5 deletions .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ decluttering_templates:
- icon: fire
card:
type: button
name: "[[name]]"
icon: "mdi:[[icon]]"
name: '[[name]]'
icon: 'mdi:[[icon]]'
large_divider:
default:
- opacity: 0.25
Expand All @@ -21,20 +21,30 @@ decluttering_templates:
background-color: var(--secondary-text-color)
height: 1px
margin: 15px auto
opacity: "[[opacity]]"
opacity: '[[opacity]]'
demo_icon:
element:
type: icon
icon: mdi:alert-circle
title: Problem detected!
entity: "[[entity]]"
entity: '[[entity]]'
tap_action:
action: more-info
style:
right: 50%
top: 50%
color: var(--google-red-500)
filter: drop-shadow(black 0 0 1px)
test_hidden:
card:
type: conditional
conditions:
- entity: light.bed_light
state: 'on'
card:
type: entities
entities:
- sun.sun

views:
- cards:
Expand All @@ -44,7 +54,7 @@ views:
- name: This is a test
- type: entities
entities:
- sun.sun
- light.bed_light
- type: custom:decluttering-card
template: large_divider
- sun.sun
Expand All @@ -55,3 +65,11 @@ views:
template: demo_icon
variables:
- entity: sun.sun
- type: horizontal-stack
cards:
- type: custom:decluttering-card
template: my_first_template
variables:
- name: Full width when light Off
- type: custom:decluttering-card
template: test_hidden
22 changes: 21 additions & 1 deletion src/decluttering-card.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LitElement, html, customElement, property, TemplateResult } from 'lit-element';
import { LitElement, html, customElement, property, TemplateResult, css, CSSResult } from 'lit-element';
import { HomeAssistant, getLovelace, createThing, LovelaceCardConfig, LovelaceCard } from 'custom-card-helpers';
import { DeclutteringCardConfig, TemplateConfig } from './types';
import deepReplace from './deep-replace';
Expand Down Expand Up @@ -30,6 +30,24 @@ class DeclutteringCard extends LitElement {
}
}

static get styles(): CSSResult {
return css`
:host(.child-card-hidden) {
display: none;
}
`;
}

protected updated(): void {
this.updateComplete.then(() => {
if (this._card?.style.display === 'none') {
this.className = 'child-card-hidden';
} else if (this.className === 'child-card-hidden') {
this.className = '';
}
});
}

public setConfig(config: DeclutteringCardConfig): void {
if (!config.template) {
throw new Error('Missing template object in your config');
Expand Down Expand Up @@ -91,6 +109,7 @@ class DeclutteringCard extends LitElement {
},
{ once: true },
);
element.id = 'declutter-child';
return element;
}

Expand All @@ -101,6 +120,7 @@ class DeclutteringCard extends LitElement {
): Promise<void> {
const newCard = await this._createCard(config, type);
element.replaceWith(newCard);
this._card = newCard;
return;
}

Expand Down

0 comments on commit 102d365

Please sign in to comment.