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

Better resizing support for thermostat card #21120

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions src/panels/lovelace/cards/hui-humidifier-card.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ResizeController } from "@lit-labs/observers/resize-controller";
piitaya marked this conversation as resolved.
Show resolved Hide resolved
import { mdiDotsVertical } from "@mdi/js";
import {
CSSResultGroup,
Expand All @@ -7,7 +8,7 @@
html,
nothing,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { customElement, property, query, state } from "lit/decorators";
piitaya marked this conversation as resolved.
Show resolved Hide resolved
import { styleMap } from "lit/directives/style-map";
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
import { fireEvent } from "../../../common/dom/fire_event";
Expand All @@ -26,6 +27,17 @@

@customElement("hui-humidifier-card")
export class HuiHumidifierCard extends LitElement implements LovelaceCard {
@query(".container") private _container?: HTMLElement;

// @ts-ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the @ts-ignore?

Copy link
Member Author

@piitaya piitaya Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the property is not used. We already have this pattern in other components.

private _resizeController = new ResizeController(this, {
callback: () => {
const height = this._container?.clientHeight;
if (!height) return;
this._container.style.setProperty("--height", `${height}px`);

Check failure on line 37 in src/panels/lovelace/cards/hui-humidifier-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Object is possibly 'undefined'.
},
});
piitaya marked this conversation as resolved.
Show resolved Hide resolved

public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("../editor/config-elements/hui-humidifier-card-editor");
return document.createElement("hui-humidifier-card-editor");
Expand Down Expand Up @@ -126,13 +138,15 @@
return html`
<ha-card>
<p class="title">${name}</p>
<ha-state-control-humidifier-humidity
prevent-interaction-on-scroll
.showCurrentAsPrimary=${this._config.show_current_as_primary}
show-secondary
.hass=${this.hass}
.stateObj=${stateObj}
></ha-state-control-humidifier-humidity>
<div class="container">
<ha-state-control-humidifier-humidity
prevent-interaction-on-scroll
.showCurrentAsPrimary=${this._config.show_current_as_primary}
show-secondary
.hass=${this.hass}
.stateObj=${stateObj}
></ha-state-control-humidifier-humidity>
</div>
<ha-icon-button
class="more-info"
.label=${this.hass!.localize(
Expand All @@ -156,10 +170,15 @@

static get styles(): CSSResultGroup {
return css`
ha-card {
:host {
position: relative;
display: block;
height: 100%;
}
ha-card {
position: relative;
overflow: hidden;
height: 100%;
width: 100%;
padding: 0;
display: flex;
flex-direction: column;
Expand All @@ -178,13 +197,29 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: none;
}

ha-state-control-humidifier-humidity {
width: 100%;
max-width: 344px; /* 12px + 12px + 320px */
padding: 0 12px 12px 12px;
.container {
display: flex;
align-items: center;
justify-content: center;
position: relative;
overflow: hidden;
max-width: 100%;
box-sizing: border-box;
flex: 1;
}

.container:before {
content: "";
display: block;
padding-top: 100%;
}

.container > * {
padding: 8px;
max-width: var(--height, 100%);
}

.more-info {
Expand All @@ -201,6 +236,7 @@

hui-card-features {
width: 100%;
flex: none;
}
`;
}
Expand Down
66 changes: 51 additions & 15 deletions src/panels/lovelace/cards/hui-thermostat-card.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ResizeController } from "@lit-labs/observers/resize-controller";
piitaya marked this conversation as resolved.
Show resolved Hide resolved
import { mdiDotsVertical } from "@mdi/js";
import {
CSSResultGroup,
Expand All @@ -7,7 +8,7 @@
html,
nothing,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { customElement, property, query, state } from "lit/decorators";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider removing unused imports to clean up the code.

While reviewing the imports, ensure that all are necessary for this file. Redundant imports can lead to unnecessary bloat and confusion.

import { styleMap } from "lit/directives/style-map";
import { applyThemesOnElement } from "../../../common/dom/apply_themes_on_element";
import { fireEvent } from "../../../common/dom/fire_event";
Expand All @@ -18,14 +19,25 @@
import { ClimateEntity } from "../../../data/climate";
import "../../../state-control/climate/ha-state-control-climate-temperature";
import { HomeAssistant } from "../../../types";
import "../card-features/hui-card-features";
import { findEntities } from "../common/find-entities";
import { createEntityNotFoundWarning } from "../components/hui-warning";
import "../card-features/hui-card-features";
import { LovelaceCard, LovelaceCardEditor } from "../types";
import { ThermostatCardConfig } from "./types";

@customElement("hui-thermostat-card")
export class HuiThermostatCard extends LitElement implements LovelaceCard {
@query(".container") private _container?: HTMLElement;

// @ts-ignore
private _resizeController = new ResizeController(this, {
callback: () => {
const height = this._container?.clientHeight;
if (!height) return;
this._container.style.setProperty("--height", `${height}px`);

Check failure on line 37 in src/panels/lovelace/cards/hui-thermostat-card.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Object is possibly 'undefined'.
},
});
piitaya marked this conversation as resolved.
Show resolved Hide resolved

public static async getConfigElement(): Promise<LovelaceCardEditor> {
await import("../editor/config-elements/hui-thermostat-card-editor");
return document.createElement("hui-thermostat-card-editor");
Expand Down Expand Up @@ -118,13 +130,15 @@
return html`
<ha-card>
<p class="title">${name}</p>
<ha-state-control-climate-temperature
prevent-interaction-on-scroll
.showCurrentAsPrimary=${this._config.show_current_as_primary}
show-secondary
.hass=${this.hass}
.stateObj=${stateObj}
></ha-state-control-climate-temperature>
<div class="container">
<ha-state-control-climate-temperature
prevent-interaction-on-scroll
.showCurrentAsPrimary=${this._config.show_current_as_primary}
show-secondary
.hass=${this.hass}
.stateObj=${stateObj}
></ha-state-control-climate-temperature>
</div>
<ha-icon-button
class="more-info"
.label=${this.hass!.localize(
Expand All @@ -148,10 +162,15 @@

static get styles(): CSSResultGroup {
return css`
ha-card {
:host {
position: relative;
display: block;
height: 100%;
}
ha-card {
position: relative;
overflow: hidden;
height: 100%;
width: 100%;
padding: 0;
display: flex;
flex-direction: column;
Expand All @@ -170,13 +189,29 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
flex: none;
}

ha-state-control-climate-temperature {
width: 100%;
max-width: 344px; /* 12px + 12px + 320px */
padding: 0 12px 12px 12px;
.container {
display: flex;
align-items: center;
justify-content: center;
position: relative;
overflow: hidden;
max-width: 100%;
box-sizing: border-box;
flex: 1;
}

.container:before {
content: "";
display: block;
padding-top: 100%;
}

.container > * {
padding: 8px;
max-width: var(--height, 100%);
}

.more-info {
Expand All @@ -193,6 +228,7 @@

hui-card-features {
width: 100%;
flex: none;
}
`;
}
Expand Down
Loading