@@ -3,6 +3,7 @@ import { HomeAssistant, getLovelace, createThing, LovelaceCardConfig, LovelaceCa
3
3
import { DeclutteringCardConfig , TemplateConfig } from './types' ;
4
4
import deepReplace from './deep-replace' ;
5
5
import getLovelaceCast from './getLovelaceCast' ;
6
+ import { ResizeObserver } from 'resize-observer' ;
6
7
import * as pjson from '../package.json' ;
7
8
8
9
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -19,10 +20,12 @@ console.info(
19
20
class DeclutteringCard extends LitElement {
20
21
@property ( ) protected _card ?: LovelaceCard ;
21
22
22
- @property ( ) private _hass ?: HomeAssistant ;
23
-
24
23
@property ( ) private _config ?: LovelaceCardConfig ;
25
24
25
+ private _ro ?: ResizeObserver ;
26
+
27
+ private _hass ?: HomeAssistant ;
28
+
26
29
set hass ( hass : HomeAssistant ) {
27
30
this . _hass = hass ;
28
31
if ( this . _card ) {
@@ -38,16 +41,20 @@ class DeclutteringCard extends LitElement {
38
41
` ;
39
42
}
40
43
41
- protected updated ( ) : void {
44
+ protected firstUpdated ( ) : void {
42
45
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
- }
46
+ this . _displayHidden ( ) ;
48
47
} ) ;
49
48
}
50
49
50
+ protected _displayHidden ( ) : void {
51
+ if ( this . _card ?. style . display === 'none' ) {
52
+ this . classList . add ( 'child-card-hidden' ) ;
53
+ } else if ( this . classList . contains ( 'child-card-hidden' ) ) {
54
+ this . classList . remove ( 'child-card-hidden' ) ;
55
+ }
56
+ }
57
+
51
58
public setConfig ( config : DeclutteringCardConfig ) : void {
52
59
if ( ! config . template ) {
53
60
throw new Error ( 'Missing template object in your config' ) ;
@@ -64,10 +71,14 @@ class DeclutteringCard extends LitElement {
64
71
} else if ( templateConfig . card && templateConfig . element ) {
65
72
throw new Error ( 'You can define a card and an element in the template' ) ;
66
73
}
74
+ this . _ro = new ResizeObserver ( ( ) => {
75
+ this . _displayHidden ( ) ;
76
+ } ) ;
67
77
this . _config = deepReplace ( config . variables , templateConfig ) ;
68
78
const type = templateConfig . card ? 'card' : 'element' ;
69
79
this . _createCard ( this . _config , type ) . then ( card => {
70
80
this . _card = card ;
81
+ this . _ro ?. observe ( this . _card ) ;
71
82
return this . _card ;
72
83
} ) ;
73
84
}
@@ -121,6 +132,7 @@ class DeclutteringCard extends LitElement {
121
132
const newCard = await this . _createCard ( config , type ) ;
122
133
element . replaceWith ( newCard ) ;
123
134
this . _card = newCard ;
135
+ this . _ro ?. observe ( this . _card ) ;
124
136
return ;
125
137
}
126
138
0 commit comments