-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 visibility option to dashboard cards #20840
Changes from all commits
137a9c3
bf4cbf0
36a6c16
8af457c
3ab3810
4c1494d
bc6cc9f
37f0382
9f0d5bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,141 @@ | ||||||||||
import { PropertyValues, ReactiveElement } from "lit"; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize imports by using - import { PropertyValues, ReactiveElement } from "lit";
- import { customElement, property, state } from "lit/decorators";
- import { MediaQueriesListener } from "../../../common/dom/media_query";
+ import type { PropertyValues, ReactiveElement } from "lit";
+ import type { customElement, property, state } from "lit/decorators";
+ import type { MediaQueriesListener } from "../../../common/dom/media_query"; Also applies to: 2-3, 4-5 Committable suggestion
Suggested change
|
||||||||||
import { customElement, property, state } from "lit/decorators"; | ||||||||||
import { MediaQueriesListener } from "../../../common/dom/media_query"; | ||||||||||
import "../../../components/ha-svg-icon"; | ||||||||||
import { LovelaceCardConfig } from "../../../data/lovelace/config/card"; | ||||||||||
import type { HomeAssistant } from "../../../types"; | ||||||||||
import { computeCardSize } from "../common/compute-card-size"; | ||||||||||
import { | ||||||||||
attachConditionMediaQueriesListeners, | ||||||||||
checkConditionsMet, | ||||||||||
} from "../common/validate-condition"; | ||||||||||
import { createCardElement } from "../create-element/create-card-element"; | ||||||||||
import type { Lovelace, LovelaceCard, LovelaceLayoutOptions } from "../types"; | ||||||||||
|
||||||||||
@customElement("hui-card") | ||||||||||
export class HuiCard extends ReactiveElement { | ||||||||||
@property({ attribute: false }) public hass!: HomeAssistant; | ||||||||||
|
||||||||||
@property({ attribute: false }) public lovelace!: Lovelace; | ||||||||||
|
||||||||||
@state() public _config?: LovelaceCardConfig; | ||||||||||
|
||||||||||
private _element?: LovelaceCard; | ||||||||||
|
||||||||||
private _listeners: MediaQueriesListener[] = []; | ||||||||||
|
||||||||||
protected createRenderRoot() { | ||||||||||
return this; | ||||||||||
} | ||||||||||
|
||||||||||
public disconnectedCallback() { | ||||||||||
super.disconnectedCallback(); | ||||||||||
this._clearMediaQueries(); | ||||||||||
} | ||||||||||
|
||||||||||
public connectedCallback() { | ||||||||||
super.connectedCallback(); | ||||||||||
this._listenMediaQueries(); | ||||||||||
this._updateElement(); | ||||||||||
} | ||||||||||
|
||||||||||
public getCardSize(): number | Promise<number> { | ||||||||||
if (this._element) { | ||||||||||
const size = computeCardSize(this._element); | ||||||||||
return size; | ||||||||||
} | ||||||||||
return 1; | ||||||||||
} | ||||||||||
|
||||||||||
public getLayoutOptions(): LovelaceLayoutOptions { | ||||||||||
const configOptions = this._config?.layout_options ?? {}; | ||||||||||
if (this._element) { | ||||||||||
const cardOptions = this._element.getLayoutOptions?.() ?? {}; | ||||||||||
return { | ||||||||||
...cardOptions, | ||||||||||
...configOptions, | ||||||||||
}; | ||||||||||
} | ||||||||||
return configOptions; | ||||||||||
} | ||||||||||
|
||||||||||
public setConfig(config: LovelaceCardConfig): void { | ||||||||||
if (this._config === config) { | ||||||||||
return; | ||||||||||
} | ||||||||||
this._config = config; | ||||||||||
this._element = createCardElement(config); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
this._element.hass = this.hass; | ||||||||||
this._element.editMode = this.lovelace.editMode; | ||||||||||
|
||||||||||
while (this.lastChild) { | ||||||||||
this.removeChild(this.lastChild); | ||||||||||
} | ||||||||||
this.appendChild(this._element!); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using non-null assertions as they bypass TypeScript's strict null checks. - this.appendChild(this._element!);
+ if (this._element) {
+ this.appendChild(this._element);
+ } Committable suggestion
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
protected update(changedProperties: PropertyValues<typeof this>) { | ||||||||||
super.update(changedProperties); | ||||||||||
|
||||||||||
if (this._element) { | ||||||||||
if (changedProperties.has("hass")) { | ||||||||||
this._element.hass = this.hass; | ||||||||||
} | ||||||||||
if (changedProperties.has("lovelace")) { | ||||||||||
this._element.editMode = this.lovelace.editMode; | ||||||||||
} | ||||||||||
if (changedProperties.has("hass") || changedProperties.has("lovelace")) { | ||||||||||
this._updateElement(); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
private _clearMediaQueries() { | ||||||||||
this._listeners.forEach((unsub) => unsub()); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer using - this._listeners.forEach((unsub) => unsub());
+ for (const unsub of this._listeners) {
+ unsub();
+ } Committable suggestion
Suggested change
|
||||||||||
this._listeners = []; | ||||||||||
} | ||||||||||
|
||||||||||
private _listenMediaQueries() { | ||||||||||
this._clearMediaQueries(); | ||||||||||
if (!this._config?.visibility) { | ||||||||||
return; | ||||||||||
} | ||||||||||
const conditions = this._config.visibility; | ||||||||||
const hasOnlyMediaQuery = | ||||||||||
conditions.length === 1 && | ||||||||||
conditions[0].condition === "screen" && | ||||||||||
!!conditions[0].media_query; | ||||||||||
|
||||||||||
this._listeners = attachConditionMediaQueriesListeners( | ||||||||||
this._config.visibility, | ||||||||||
(matches) => { | ||||||||||
this._updateElement(hasOnlyMediaQuery && matches); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||||||||||
} | ||||||||||
); | ||||||||||
} | ||||||||||
|
||||||||||
private _updateElement(forceVisible?: boolean) { | ||||||||||
if (!this._element) { | ||||||||||
return; | ||||||||||
} | ||||||||||
const visible = | ||||||||||
forceVisible || | ||||||||||
this.lovelace.editMode || | ||||||||||
!this._config?.visibility || | ||||||||||
checkConditionsMet(this._config.visibility, this.hass); | ||||||||||
|
||||||||||
this.style.setProperty("display", visible ? "" : "none"); | ||||||||||
this.toggleAttribute("hidden", !visible); | ||||||||||
if (!visible && this._element.parentElement) { | ||||||||||
this.removeChild(this._element); | ||||||||||
} else if (visible && !this._element.parentElement) { | ||||||||||
this.appendChild(this._element); | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
declare global { | ||||||||||
interface HTMLElementTagNameMap { | ||||||||||
"hui-card": HuiCard; | ||||||||||
} | ||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,8 +1,9 @@ | ||||||||||||||
import { promiseTimeout } from "../../../common/util/promise-timeout"; | ||||||||||||||
import { HuiCard } from "../cards/hui-card"; | ||||||||||||||
import { LovelaceCard, LovelaceHeaderFooter } from "../types"; | ||||||||||||||
Comment on lines
1
to
3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize imports to use type-only imports where appropriate. - import { HuiCard } from "../cards/hui-card";
- import { LovelaceCard, LovelaceHeaderFooter } from "../types";
+ import type { HuiCard } from "../cards/hui-card";
+ import type { LovelaceCard, LovelaceHeaderFooter } from "../types"; Committable suggestion
Suggested change
|
||||||||||||||
|
||||||||||||||
export const computeCardSize = ( | ||||||||||||||
card: LovelaceCard | LovelaceHeaderFooter | ||||||||||||||
card: LovelaceCard | LovelaceHeaderFooter | HuiCard | ||||||||||||||
): number | Promise<number> => { | ||||||||||||||
if (typeof card.getCardSize === "function") { | ||||||||||||||
try { | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,27 +327,13 @@ export function extractMediaQueries(conditions: Condition[]): string[] { | |
|
||
export function attachConditionMediaQueriesListeners( | ||
conditions: Condition[], | ||
hass: HomeAssistant, | ||
onChange: (visibility: boolean) => void | ||
): MediaQueriesListener[] { | ||
// For performance, if there is only one condition and it's a screen condition, set the visibility directly | ||
if ( | ||
conditions.length === 1 && | ||
conditions[0].condition === "screen" && | ||
conditions[0].media_query | ||
) { | ||
const listener = listenMediaQuery(conditions[0].media_query, (matches) => { | ||
onChange(matches); | ||
}); | ||
return [listener]; | ||
} | ||
|
||
const mediaQueries = extractMediaQueries(conditions); | ||
|
||
const listeners = mediaQueries.map((query) => { | ||
const listener = listenMediaQuery(query, () => { | ||
const visibility = checkConditionsMet(conditions, hass); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved |
||
onChange(visibility); | ||
const listener = listenMediaQuery(query, (matches) => { | ||
onChange(matches); | ||
}); | ||
return listener; | ||
}); | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,4 +4,5 @@ export const baseLovelaceCardConfig = object({ | |||||
type: string(), | ||||||
view_layout: any(), | ||||||
layout_options: any(), | ||||||
visibility: any(), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specify a more appropriate type for - visibility: any(),
+ visibility: array(Condition), Committable suggestion
Suggested change
|
||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specify a more appropriate type for
view_layout
.