Skip to content

Commit 3615f2c

Browse files
committed
Optimize the performance
1 parent 0f8f6fe commit 3615f2c

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"dependencies": {
5555
"custom-card-helpers": "^1.5.0",
5656
"home-assistant-js-websocket": "^5.1.0",
57-
"lit-element": "^2.3.0"
57+
"lit-element": "^2.3.0",
58+
"resize-observer": "^1.0.0"
5859
}
5960
}

src/decluttering-card.ts

+20-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { HomeAssistant, getLovelace, createThing, LovelaceCardConfig, LovelaceCa
33
import { DeclutteringCardConfig, TemplateConfig } from './types';
44
import deepReplace from './deep-replace';
55
import getLovelaceCast from './getLovelaceCast';
6+
import { ResizeObserver } from 'resize-observer';
67
import * as pjson from '../package.json';
78

89
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -19,10 +20,12 @@ console.info(
1920
class DeclutteringCard extends LitElement {
2021
@property() protected _card?: LovelaceCard;
2122

22-
@property() private _hass?: HomeAssistant;
23-
2423
@property() private _config?: LovelaceCardConfig;
2524

25+
private _ro?: ResizeObserver;
26+
27+
private _hass?: HomeAssistant;
28+
2629
set hass(hass: HomeAssistant) {
2730
this._hass = hass;
2831
if (this._card) {
@@ -38,16 +41,20 @@ class DeclutteringCard extends LitElement {
3841
`;
3942
}
4043

41-
protected updated(): void {
44+
protected firstUpdated(): void {
4245
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();
4847
});
4948
}
5049

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+
5158
public setConfig(config: DeclutteringCardConfig): void {
5259
if (!config.template) {
5360
throw new Error('Missing template object in your config');
@@ -64,10 +71,14 @@ class DeclutteringCard extends LitElement {
6471
} else if (templateConfig.card && templateConfig.element) {
6572
throw new Error('You can define a card and an element in the template');
6673
}
74+
this._ro = new ResizeObserver(() => {
75+
this._displayHidden();
76+
});
6777
this._config = deepReplace(config.variables, templateConfig);
6878
const type = templateConfig.card ? 'card' : 'element';
6979
this._createCard(this._config, type).then(card => {
7080
this._card = card;
81+
this._ro?.observe(this._card);
7182
return this._card;
7283
});
7384
}
@@ -121,6 +132,7 @@ class DeclutteringCard extends LitElement {
121132
const newCard = await this._createCard(config, type);
122133
element.replaceWith(newCard);
123134
this._card = newCard;
135+
this._ro?.observe(this._card);
124136
return;
125137
}
126138

0 commit comments

Comments
 (0)