Skip to content

Commit 102d365

Browse files
committed
Hide when child is hidden (conditional-card)
1 parent 16e8da9 commit 102d365

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

.devcontainer/ui-lovelace.yaml

+23-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ decluttering_templates:
1010
- icon: fire
1111
card:
1212
type: button
13-
name: "[[name]]"
14-
icon: "mdi:[[icon]]"
13+
name: '[[name]]'
14+
icon: 'mdi:[[icon]]'
1515
large_divider:
1616
default:
1717
- opacity: 0.25
@@ -21,20 +21,30 @@ decluttering_templates:
2121
background-color: var(--secondary-text-color)
2222
height: 1px
2323
margin: 15px auto
24-
opacity: "[[opacity]]"
24+
opacity: '[[opacity]]'
2525
demo_icon:
2626
element:
2727
type: icon
2828
icon: mdi:alert-circle
2929
title: Problem detected!
30-
entity: "[[entity]]"
30+
entity: '[[entity]]'
3131
tap_action:
3232
action: more-info
3333
style:
3434
right: 50%
3535
top: 50%
3636
color: var(--google-red-500)
3737
filter: drop-shadow(black 0 0 1px)
38+
test_hidden:
39+
card:
40+
type: conditional
41+
conditions:
42+
- entity: light.bed_light
43+
state: 'on'
44+
card:
45+
type: entities
46+
entities:
47+
- sun.sun
3848

3949
views:
4050
- cards:
@@ -44,7 +54,7 @@ views:
4454
- name: This is a test
4555
- type: entities
4656
entities:
47-
- sun.sun
57+
- light.bed_light
4858
- type: custom:decluttering-card
4959
template: large_divider
5060
- sun.sun
@@ -55,3 +65,11 @@ views:
5565
template: demo_icon
5666
variables:
5767
- entity: sun.sun
68+
- type: horizontal-stack
69+
cards:
70+
- type: custom:decluttering-card
71+
template: my_first_template
72+
variables:
73+
- name: Full width when light Off
74+
- type: custom:decluttering-card
75+
template: test_hidden

src/decluttering-card.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LitElement, html, customElement, property, TemplateResult } from 'lit-element';
1+
import { LitElement, html, customElement, property, TemplateResult, css, CSSResult } from 'lit-element';
22
import { HomeAssistant, getLovelace, createThing, LovelaceCardConfig, LovelaceCard } from 'custom-card-helpers';
33
import { DeclutteringCardConfig, TemplateConfig } from './types';
44
import deepReplace from './deep-replace';
@@ -30,6 +30,24 @@ class DeclutteringCard extends LitElement {
3030
}
3131
}
3232

33+
static get styles(): CSSResult {
34+
return css`
35+
:host(.child-card-hidden) {
36+
display: none;
37+
}
38+
`;
39+
}
40+
41+
protected updated(): void {
42+
this.updateComplete.then(() => {
43+
if (this._card?.style.display === 'none') {
44+
this.className = 'child-card-hidden';
45+
} else if (this.className === 'child-card-hidden') {
46+
this.className = '';
47+
}
48+
});
49+
}
50+
3351
public setConfig(config: DeclutteringCardConfig): void {
3452
if (!config.template) {
3553
throw new Error('Missing template object in your config');
@@ -91,6 +109,7 @@ class DeclutteringCard extends LitElement {
91109
},
92110
{ once: true },
93111
);
112+
element.id = 'declutter-child';
94113
return element;
95114
}
96115

@@ -101,6 +120,7 @@ class DeclutteringCard extends LitElement {
101120
): Promise<void> {
102121
const newCard = await this._createCard(config, type);
103122
element.replaceWith(newCard);
123+
this._card = newCard;
104124
return;
105125
}
106126

0 commit comments

Comments
 (0)