Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ResizeObserver to EntityRow #9837

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/panels/lovelace/entity-rows/hui-input-number-entity-row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
import { customElement, property, state } from "lit/decorators";
import { computeStateDisplay } from "../../../common/entity/compute_state_display";
import { computeRTLDirection } from "../../../common/util/compute_rtl";
import { debounce } from "../../../common/util/debounce";
import "../../../components/ha-slider";
import { UNAVAILABLE_STATES } from "../../../data/entity";
import { setValue } from "../../../data/input_text";
import { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import { installResizeObserver } from "../common/install-resize-observer";
import "../components/hui-generic-entity-row";
import { createEntityNotFoundWarning } from "../components/hui-warning";
import { EntityConfig, LovelaceRow } from "./types";
Expand All @@ -29,6 +31,8 @@ class HuiInputNumberEntityRow extends LitElement implements LovelaceRow {

private _updated?: boolean;

private _resizeObserver?: ResizeObserver;

public setConfig(config: EntityConfig): void {
if (!config) {
throw new Error("Invalid configuration");
Expand All @@ -41,13 +45,19 @@ class HuiInputNumberEntityRow extends LitElement implements LovelaceRow {
if (this._updated && !this._loaded) {
this._initialLoad();
}
this._attachObserver();
}

public disconnectedCallback(): void {
this._resizeObserver?.disconnect();
}

protected firstUpdated(): void {
this._updated = true;
if (this.isConnected && !this._loaded) {
this._initialLoad();
}
this._attachObserver();
}

protected shouldUpdate(changedProps: PropertyValues): boolean {
Expand Down Expand Up @@ -120,6 +130,9 @@ class HuiInputNumberEntityRow extends LitElement implements LovelaceRow {

static get styles(): CSSResultGroup {
return css`
:host {
display: block;
}
.flex {
display: flex;
align-items: center;
Expand All @@ -146,13 +159,30 @@ class HuiInputNumberEntityRow extends LitElement implements LovelaceRow {
private async _initialLoad(): Promise<void> {
this._loaded = true;
await this.updateComplete;
const element = this.shadowRoot!.querySelector(".state") as HTMLElement;
this._measureCard();
}

if (!element || !this.parentElement) {
private _measureCard() {
if (!this.isConnected) {
return;
}
const element = this.shadowRoot!.querySelector(".state") as HTMLElement;
if (!element) {
return;
}
element.hidden = this.clientWidth <= 300;
}

element.hidden = this.parentElement.clientWidth <= 300;
private async _attachObserver(): Promise<void> {
if (!this._resizeObserver) {
await installResizeObserver();
this._resizeObserver = new ResizeObserver(
debounce(() => this._measureCard(), 250, false)
);
}
if (this.isConnected) {
this._resizeObserver.observe(this);
}
}

private get _inputElement(): { value: string } {
Expand Down