From f19fdeacba823a552274591e92c8b64f353c6fa1 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 29 Mar 2023 14:45:52 +0200 Subject: [PATCH] bump @lit-labs/context, fix bug in button card + optimise (#15968) --- package.json | 2 +- src/common/dom/apply_themes_on_element.ts | 8 +- src/panels/lovelace/cards/hui-button-card.ts | 13 ++- src/state/context-mixin.ts | 116 +++++++++---------- yarn.lock | 14 +-- 5 files changed, 76 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index 1c654cdb25..0a22ba8fd3 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "@fullcalendar/list": "6.1.5", "@fullcalendar/timegrid": "6.1.5", "@lezer/highlight": "1.1.4", - "@lit-labs/context": "0.2.0", + "@lit-labs/context": "0.3.0", "@lit-labs/motion": "1.0.3", "@lit-labs/virtualizer": "1.0.1", "@lrnwebcomponents/simple-tooltip": "4.1.0", diff --git a/src/common/dom/apply_themes_on_element.ts b/src/common/dom/apply_themes_on_element.ts index ab80caf54b..7b99678309 100644 --- a/src/common/dom/apply_themes_on_element.ts +++ b/src/common/dom/apply_themes_on_element.ts @@ -93,7 +93,7 @@ export const applyThemesOnElement = ( } // Nothing was changed - if (element._themes?.cacheKey === cacheKey) { + if (element.__themes?.cacheKey === cacheKey) { return; } } @@ -119,7 +119,7 @@ export const applyThemesOnElement = ( } } - if (!element._themes?.keys && !Object.keys(themeRules).length) { + if (!element.__themes?.keys && !Object.keys(themeRules).length) { // No styles to reset, and no styles to set return; } @@ -130,8 +130,8 @@ export const applyThemesOnElement = ( : undefined; // Add previous set keys to reset them, and new theme - const styles = { ...element._themes?.keys, ...newTheme?.styles }; - element._themes = { cacheKey, keys: newTheme?.keys }; + const styles = { ...element.__themes?.keys, ...newTheme?.styles }; + element.__themes = { cacheKey, keys: newTheme?.keys }; // Set and/or reset styles if (element.updateStyles) { diff --git a/src/panels/lovelace/cards/hui-button-card.ts b/src/panels/lovelace/cards/hui-button-card.ts index 0183bc1ba2..94d2fd49ed 100644 --- a/src/panels/lovelace/cards/hui-button-card.ts +++ b/src/panels/lovelace/cards/hui-button-card.ts @@ -232,7 +232,10 @@ export class HuiButtonCard extends LitElement implements LovelaceCard { protected updated(changedProps: PropertyValues): void { super.updated(changedProps); - if (!this._config) { + if (!this._config || !this._themes) { + return; + } + if (!changedProps.has("_themes") && !changedProps.has("_config")) { return; } const oldThemes = changedProps.get("_themes") as @@ -243,10 +246,10 @@ export class HuiButtonCard extends LitElement implements LovelaceCard { | undefined; if ( - !oldThemes || - !oldConfig || - oldThemes !== this._themes || - oldConfig.theme !== this._config.theme + (changedProps.has("_themes") && + (!oldThemes || oldThemes !== this._themes)) || + (changedProps.has("_config") && + (!oldConfig || oldConfig.theme !== this._config.theme)) ) { applyThemesOnElement(this, this._themes, this._config.theme); } diff --git a/src/state/context-mixin.ts b/src/state/context-mixin.ts index 79486e1756..9b8e2a8bb7 100644 --- a/src/state/context-mixin.ts +++ b/src/state/context-mixin.ts @@ -24,66 +24,62 @@ export const contextMixin = >( string, ContextProvider | undefined > = { - states: new ContextProvider( - this, - statesContext, - this.hass ? this.hass.states : this._pendingHass.states - ), - entities: new ContextProvider( - this, - entitiesContext, - this.hass ? this.hass.entities : this._pendingHass.entities - ), - devices: new ContextProvider( - this, - devicesContext, - this.hass ? this.hass.devices : this._pendingHass.devices - ), - areas: new ContextProvider( - this, - areasContext, - this.hass ? this.hass.areas : this._pendingHass.areas - ), - localize: new ContextProvider( - this, - localizeContext, - this.hass ? this.hass.localize : this._pendingHass.localize - ), - locale: new ContextProvider( - this, - localeContext, - this.hass ? this.hass.locale : this._pendingHass.locale - ), - config: new ContextProvider( - this, - configContext, - this.hass ? this.hass.config : this._pendingHass.config - ), - themes: new ContextProvider( - this, - themesContext, - this.hass ? this.hass.themes : this._pendingHass.themes - ), - selectedTheme: new ContextProvider( - this, - selectedThemeContext, - this.hass ? this.hass.selectedTheme : this._pendingHass.selectedTheme - ), - user: new ContextProvider( - this, - userContext, - this.hass ? this.hass.user : this._pendingHass.user - ), - userData: new ContextProvider( - this, - userDataContext, - this.hass ? this.hass.userData : this._pendingHass.userData - ), - panels: new ContextProvider( - this, - panelsContext, - this.hass ? this.hass.panels : this._pendingHass.panels - ), + states: new ContextProvider(this, { + context: statesContext, + initialValue: this.hass ? this.hass.states : this._pendingHass.states, + }), + entities: new ContextProvider(this, { + context: entitiesContext, + initialValue: this.hass + ? this.hass.entities + : this._pendingHass.entities, + }), + devices: new ContextProvider(this, { + context: devicesContext, + initialValue: this.hass ? this.hass.devices : this._pendingHass.devices, + }), + areas: new ContextProvider(this, { + context: areasContext, + initialValue: this.hass ? this.hass.areas : this._pendingHass.areas, + }), + localize: new ContextProvider(this, { + context: localizeContext, + initialValue: this.hass + ? this.hass.localize + : this._pendingHass.localize, + }), + locale: new ContextProvider(this, { + context: localeContext, + initialValue: this.hass ? this.hass.locale : this._pendingHass.locale, + }), + config: new ContextProvider(this, { + context: configContext, + initialValue: this.hass ? this.hass.config : this._pendingHass.config, + }), + themes: new ContextProvider(this, { + context: themesContext, + initialValue: this.hass ? this.hass.themes : this._pendingHass.themes, + }), + selectedTheme: new ContextProvider(this, { + context: selectedThemeContext, + initialValue: this.hass + ? this.hass.selectedTheme + : this._pendingHass.selectedTheme, + }), + user: new ContextProvider(this, { + context: userContext, + initialValue: this.hass ? this.hass.user : this._pendingHass.user, + }), + userData: new ContextProvider(this, { + context: userDataContext, + initialValue: this.hass + ? this.hass.userData + : this._pendingHass.userData, + }), + panels: new ContextProvider(this, { + context: panelsContext, + initialValue: this.hass ? this.hass.panels : this._pendingHass.panels, + }), }; protected _updateHass(obj: Partial) { diff --git a/yarn.lock b/yarn.lock index 6c90d27245..4c7fef9b88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1955,13 +1955,13 @@ __metadata: languageName: node linkType: hard -"@lit-labs/context@npm:0.2.0": - version: 0.2.0 - resolution: "@lit-labs/context@npm:0.2.0" +"@lit-labs/context@npm:0.3.0": + version: 0.3.0 + resolution: "@lit-labs/context@npm:0.3.0" dependencies: "@lit/reactive-element": ^1.5.0 - lit: ^2.5.0 - checksum: 0b3d803ba81683d9650ba384e5f138656ecd52d6a54448535e867e0a0ba0cb23e4526ec52e82ed657e9c3598a103c0e8b164bfe927222467e349fb070c770af3 + lit: ^2.7.0 + checksum: 85c66a67d12d3a9e3753337ef8f5566789ccacbb98b40b01c79db98ea6186c0bcdb5c16b4a27673cd5b4fb807bf3970c376b8c96797a9b20a78b0681f391efdb languageName: node linkType: hard @@ -9425,7 +9425,7 @@ __metadata: "@fullcalendar/timegrid": 6.1.5 "@koa/cors": 4.0.0 "@lezer/highlight": 1.1.4 - "@lit-labs/context": 0.2.0 + "@lit-labs/context": 0.3.0 "@lit-labs/motion": 1.0.3 "@lit-labs/virtualizer": 1.0.1 "@lrnwebcomponents/simple-tooltip": 4.1.0 @@ -11238,7 +11238,7 @@ __metadata: languageName: node linkType: hard -"lit@npm:2.7.0, lit@npm:^2.0.0, lit@npm:^2.0.0-rc.2, lit@npm:^2.2.1, lit@npm:^2.2.8, lit@npm:^2.3.0, lit@npm:^2.5.0": +"lit@npm:2.7.0, lit@npm:^2.0.0, lit@npm:^2.0.0-rc.2, lit@npm:^2.2.1, lit@npm:^2.2.8, lit@npm:^2.3.0, lit@npm:^2.5.0, lit@npm:^2.7.0": version: 2.7.0 resolution: "lit@npm:2.7.0" dependencies: