diff --git a/src/card.ts b/src/card.ts index 7117b263..41f2215d 100644 --- a/src/card.ts +++ b/src/card.ts @@ -10,6 +10,7 @@ import { HomeAssistant, LovelaceCardEditor, getLovelace } from 'custom-card-help import { StyleInfo, styleMap } from 'lit/directives/style-map.js'; import { customElement, property, query, state } from 'lit/decorators.js'; import { classMap } from 'lit/directives/class-map.js'; +import { createRef, ref, Ref } from 'lit/directives/ref.js'; import screenfull from 'screenfull'; import { throttle } from 'lodash-es'; import { until } from 'lit/directives/until.js'; @@ -150,14 +151,9 @@ export class FrigateCard extends LitElement { @state() protected _conditionState?: ConditionState; - @query('frigate-card-menu') - protected _menu!: FrigateCardMenu; - - @query('frigate-card-elements') - protected _elements?: FrigateCardElements; - - @query('frigate-card-image') - protected _image?: FrigateCardImage; + protected _refMenu: Ref = createRef(); + protected _refElements: Ref = createRef(); + protected _refImage: Ref = createRef(); // user interaction timer ("screensaver" functionality, return to default // view after user interaction). @@ -195,14 +191,14 @@ export class FrigateCard extends LitElement { // to update, without necessarily re-rendering the entire card (re-rendering // is expensive). if (this._hass) { - if (this._menu) { - this._menu.hass = this._hass; + if (this._refMenu.value) { + this._refMenu.value.hass = this._hass; } - if (this._elements) { - this._elements.hass = this._hass; + if (this._refElements.value) { + this._refElements.value.hass = this._hass; } - if (this._image) { - this._image.hass = this._hass; + if (this._refImage.value) { + this._refImage.value.hass = this._hass; } } @@ -434,7 +430,9 @@ export class FrigateCard extends LitElement { if (!this._dynamicMenuButtons.includes(button)) { this._dynamicMenuButtons = [...this._dynamicMenuButtons, button]; } - this._menu.buttons = this._getMenuButtons(); + if (this._refMenu.value) { + this._refMenu.value.buttons = this._getMenuButtons(); + } } /** @@ -445,7 +443,9 @@ export class FrigateCard extends LitElement { this._dynamicMenuButtons = this._dynamicMenuButtons.filter( (button) => button != target, ); - this._menu.buttons = this._getMenuButtons(); + if (this._refMenu.value) { + this._refMenu.value.buttons = this._getMenuButtons(); + } } /** @@ -888,7 +888,7 @@ export class FrigateCard extends LitElement { // This is a rare code path: this would only be used if someone has a // menu toggle action configured outside of the menu itself (e.g. // picture elements). - this._menu.toggleMenu(); + this._refMenu.value?.toggleMenu(); break; case 'camera_select': const camera = frigateCardAction.camera; @@ -1025,6 +1025,7 @@ export class FrigateCard extends LitElement { protected _renderMenu(): TemplateResult | void { return html` = createRef() ; /** * Set the Home Assistant object. */ set hass(hass: HomeAssistant) { - if (this._core) { - this._core.hass = hass; + if (this._refCore.value) { + this._refCore.value.hass = hass; } this._hass = hass; } @@ -283,6 +279,7 @@ export class FrigateCardElementsConditional extends LitElement { protected render(): TemplateResult | void { if (fetchStateAndEvaluateCondition(this, this._config.conditions)) { return html` diff --git a/src/components/image.ts b/src/components/image.ts index 621a0aa9..28d1c0de 100644 --- a/src/components/image.ts +++ b/src/components/image.ts @@ -7,7 +7,8 @@ import { unsafeCSS, } from 'lit'; import { HomeAssistant } from 'custom-card-helpers'; -import { customElement, property, query, state } from 'lit/decorators.js'; +import { createRef, ref, Ref } from 'lit/directives/ref.js'; +import { customElement, property, state } from 'lit/decorators.js'; import { CachedValueController } from '../cached-value-controller.js'; import { CameraConfig, ImageViewConfig } from '../types.js'; @@ -40,8 +41,7 @@ export class FrigateCardImage extends LitElement { @state() protected _imageConfig?: ImageViewConfig; - @query('img') - protected _image?: HTMLImageElement; + protected _refImage: Ref = createRef() ; protected _cachedValueController?: CachedValueController; protected _boundVisibilityHandler = this._visibilityHandler.bind(this); @@ -144,7 +144,7 @@ export class FrigateCardImage extends LitElement { * Handle document visibility changes. */ protected _visibilityHandler(): void { - if (!this._image) { + if (!this._refImage.value) { return; } if (document.visibilityState === 'hidden') { @@ -196,8 +196,8 @@ export class FrigateCardImage extends LitElement { * Force the img element to the stock image. */ protected _forceStockImage(): void { - if (this._image) { - this._image.src = defaultImage; + if (this._refImage.value) { + this._refImage.value.src = defaultImage; } } @@ -205,6 +205,7 @@ export class FrigateCardImage extends LitElement { const src = this._cachedValueController?.value; return src ? html` { dispatchMediaShowEvent(this, ev);