Use theme mode property for ha-map (#20606)

* Use theme mode property for ha-map

* Use theme mode in ha-location-editor

---------

Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
Paul Bottein 2024-04-24 14:28:33 +02:00 committed by GitHub
parent 4c9c52d27d
commit 4e97e3763e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 36 deletions

View File

@ -19,7 +19,7 @@ import { customElement, property, query, state } from "lit/decorators";
import memoizeOne from "memoize-one"; import memoizeOne from "memoize-one";
import { fireEvent } from "../../common/dom/fire_event"; import { fireEvent } from "../../common/dom/fire_event";
import type { LeafletModuleType } from "../../common/dom/setup-leaflet-map"; import type { LeafletModuleType } from "../../common/dom/setup-leaflet-map";
import type { HomeAssistant } from "../../types"; import type { HomeAssistant, ThemeMode } from "../../types";
import "../ha-input-helper-text"; import "../ha-input-helper-text";
import "./ha-map"; import "./ha-map";
import type { HaMap } from "./ha-map"; import type { HaMap } from "./ha-map";
@ -61,7 +61,8 @@ export class HaLocationsEditor extends LitElement {
@property({ type: Number }) public zoom = 16; @property({ type: Number }) public zoom = 16;
@property({ type: Boolean }) public darkMode = false; @property({ attribute: "theme-mode", type: String })
public themeMode: ThemeMode = "auto";
@state() private _locationMarkers?: Record<string, Marker | Circle>; @state() private _locationMarkers?: Record<string, Marker | Circle>;
@ -133,7 +134,7 @@ export class HaLocationsEditor extends LitElement {
.layers=${this._getLayers(this._circles, this._locationMarkers)} .layers=${this._getLayers(this._circles, this._locationMarkers)}
.zoom=${this.zoom} .zoom=${this.zoom}
.autoFit=${this.autoFit} .autoFit=${this.autoFit}
?forceDarkMode=${this.darkMode} .themeMode=${this.themeMode}
></ha-map> ></ha-map>
${this.helper ${this.helper
? html`<ha-input-helper-text>${this.helper}</ha-input-helper-text>` ? html`<ha-input-helper-text>${this.helper}</ha-input-helper-text>`

View File

@ -1,32 +1,32 @@
import { isToday } from "date-fns";
import type { import type {
Circle, Circle,
CircleMarker, CircleMarker,
LatLngTuple,
LatLngExpression, LatLngExpression,
LatLngTuple,
Layer, Layer,
Map, Map,
Marker, Marker,
Polyline, Polyline,
} from "leaflet"; } from "leaflet";
import { isToday } from "date-fns"; import { CSSResultGroup, PropertyValues, ReactiveElement, css } from "lit";
import { css, CSSResultGroup, PropertyValues, ReactiveElement } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { formatDateTime } from "../../common/datetime/format_date_time";
import {
formatTimeWeekday,
formatTimeWithSeconds,
} from "../../common/datetime/format_time";
import { import {
LeafletModuleType, LeafletModuleType,
setupLeafletMap, setupLeafletMap,
} from "../../common/dom/setup-leaflet-map"; } from "../../common/dom/setup-leaflet-map";
import {
formatTimeWithSeconds,
formatTimeWeekday,
} from "../../common/datetime/format_time";
import { formatDateTime } from "../../common/datetime/format_date_time";
import { computeStateDomain } from "../../common/entity/compute_state_domain"; import { computeStateDomain } from "../../common/entity/compute_state_domain";
import { computeStateName } from "../../common/entity/compute_state_name"; import { computeStateName } from "../../common/entity/compute_state_name";
import { loadPolyfillIfNeeded } from "../../resources/resize-observer.polyfill"; import { loadPolyfillIfNeeded } from "../../resources/resize-observer.polyfill";
import { HomeAssistant } from "../../types"; import { HomeAssistant, ThemeMode } from "../../types";
import { isTouch } from "../../util/is_touch";
import "../ha-icon-button"; import "../ha-icon-button";
import "./ha-entity-marker"; import "./ha-entity-marker";
import { isTouch } from "../../util/is_touch";
const getEntityId = (entity: string | HaMapEntity): string => const getEntityId = (entity: string | HaMapEntity): string =>
typeof entity === "string" ? entity : entity.entity_id; typeof entity === "string" ? entity : entity.entity_id;
@ -69,9 +69,8 @@ export class HaMap extends ReactiveElement {
@property({ type: Boolean }) public fitZones = false; @property({ type: Boolean }) public fitZones = false;
@property({ type: Boolean }) public forceDarkMode = false; @property({ attribute: "theme-mode", type: String })
public themeMode: ThemeMode = "auto";
@property({ type: Boolean }) public forceLightMode = false;
@property({ type: Number }) public zoom = 14; @property({ type: Number }) public zoom = 14;
@ -156,8 +155,7 @@ export class HaMap extends ReactiveElement {
} }
if ( if (
!changedProps.has("forceDarkMode") && !changedProps.has("themeMode") &&
!changedProps.has("forceLightMode") &&
(!changedProps.has("hass") || (!changedProps.has("hass") ||
(oldHass && oldHass.themes?.darkMode === this.hass.themes?.darkMode)) (oldHass && oldHass.themes?.darkMode === this.hass.themes?.darkMode))
) { ) {
@ -166,14 +164,18 @@ export class HaMap extends ReactiveElement {
this._updateMapStyle(); this._updateMapStyle();
} }
private get _darkMode() {
return (
this.themeMode === "dark" ||
(this.themeMode === "auto" && Boolean(this.hass.themes.darkMode))
);
}
private _updateMapStyle(): void { private _updateMapStyle(): void {
const darkMode =
!this.forceLightMode &&
(this.forceDarkMode || (this.hass.themes.darkMode ?? false));
const map = this.renderRoot.querySelector("#map"); const map = this.renderRoot.querySelector("#map");
map!.classList.toggle("dark", darkMode); map!.classList.toggle("dark", this._darkMode);
map!.classList.toggle("forced-dark", this.forceDarkMode); map!.classList.toggle("forced-dark", this.themeMode === "dark");
map!.classList.toggle("forced-light", this.forceLightMode); map!.classList.toggle("forced-light", this.themeMode === "light");
} }
private async _loadMap(): Promise<void> { private async _loadMap(): Promise<void> {
@ -403,13 +405,7 @@ export class HaMap extends ReactiveElement {
"--dark-primary-color" "--dark-primary-color"
); );
const className = this.forceLightMode const className = this._darkMode ? "dark" : "light";
? "light"
: this.forceDarkMode
? "dark"
: this.hass.themes.darkMode
? "dark"
: "light";
for (const entity of this.entities) { for (const entity of this.entities) {
const stateObj = hass.states[getEntityId(entity)]; const stateObj = hass.states[getEntityId(entity)];

View File

@ -41,7 +41,7 @@ import type { HomeAssistant } from "../types";
import { onBoardingStyles } from "./styles"; import { onBoardingStyles } from "./styles";
const AMSTERDAM: [number, number] = [52.3731339, 4.8903147]; const AMSTERDAM: [number, number] = [52.3731339, 4.8903147];
const mql = matchMedia("(prefers-color-scheme: dark)"); const darkMql = matchMedia("(prefers-color-scheme: dark)");
const LOCATION_MARKER_ID = "location"; const LOCATION_MARKER_ID = "location";
@customElement("onboarding-location") @customElement("onboarding-location")
@ -199,7 +199,7 @@ class OnboardingLocation extends LitElement {
this._highlightedMarker this._highlightedMarker
)} )}
zoom="14" zoom="14"
.darkMode=${mql.matches} .themeMode=${darkMql.matches ? "dark" : "light"}
.disabled=${this._working} .disabled=${this._working}
@location-updated=${this._locationChanged} @location-updated=${this._locationChanged}
@marker-clicked=${this._markerClicked} @marker-clicked=${this._markerClicked}

View File

@ -159,6 +159,9 @@ class HuiMapCard extends LitElement implements LovelaceCard {
? false ? false
: this.hass.themes.darkMode; : this.hass.themes.darkMode;
const themeMode =
this._config.theme_mode || (this._config.dark_mode ? "dark" : "auto");
return html` return html`
<ha-card id="card" .header=${this._config.title}> <ha-card id="card" .header=${this._config.title}>
<div id="root"> <div id="root">
@ -169,9 +172,7 @@ class HuiMapCard extends LitElement implements LovelaceCard {
.paths=${this._getHistoryPaths(this._config, this._stateHistory)} .paths=${this._getHistoryPaths(this._config, this._stateHistory)}
.autoFit=${this._config.auto_fit || false} .autoFit=${this._config.auto_fit || false}
.fitZones=${this._config.fit_zones} .fitZones=${this._config.fit_zones}
?forceDarkMode=${this._config.theme_mode === "dark" || .themeMode=${themeMode}
this._config.dark_mode}
?forceLightMode=${this._config.theme_mode === "light"}
interactiveZones interactiveZones
renderPassive renderPassive
></ha-map> ></ha-map>