From 03a21d551935f454abf4654458384f5d335fac47 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Mon, 23 Oct 2023 09:58:20 +0200 Subject: [PATCH] Only calculate opening direction when we are opening the datepicker (#18313) --- src/components/ha-date-range-picker.ts | 41 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/components/ha-date-range-picker.ts b/src/components/ha-date-range-picker.ts index 7e63a1ddbc..966d429d40 100644 --- a/src/components/ha-date-range-picker.ts +++ b/src/components/ha-date-range-picker.ts @@ -19,7 +19,7 @@ import { PropertyValues, TemplateResult, } from "lit"; -import { customElement, property } from "lit/decorators"; +import { customElement, property, state } from "lit/decorators"; import { calcDate } from "../common/datetime/calc_date"; import { firstWeekdayIndex } from "../common/datetime/first_weekday"; import { formatDate } from "../common/datetime/format_date"; @@ -58,19 +58,15 @@ export class HaDateRangePicker extends LitElement { @property({ type: Boolean }) private minimal = false; - @property() private _openingDirection = "right"; + @property() public openingDirection?: "right" | "left" | "center" | "inline"; + + @state() private _calcedOpeningDirection?: + | "right" + | "left" + | "center" + | "inline"; protected willUpdate(changedProps: PropertyValues) { - // set dialog opening direction based on position - const datePickerPosition = this.getBoundingClientRect().x; - if (datePickerPosition > (2 * window.innerWidth) / 3) { - this._openingDirection = "left"; - } else if (datePickerPosition < window.innerWidth / 3) { - this._openingDirection = "right"; - } else { - this._openingDirection = "center"; - } - if ( (!this.hasUpdated && this.ranges === undefined) || (changedProps.has("hass") && @@ -158,10 +154,11 @@ export class HaDateRangePicker extends LitElement { start-date=${this.startDate} end-date=${this.endDate} ?ranges=${this.ranges !== false} - opening-direction=${this._openingDirection} + opening-direction=${this.openingDirection || + this._calcedOpeningDirection} first-day=${firstWeekdayIndex(this.hass.locale)} > -
+
${!this.minimal ? html` (2 * window.innerWidth) / 3) { + opens = "left"; + } else if (datePickerPosition < window.innerWidth / 3) { + opens = "right"; + } else { + opens = "center"; + } + this._calcedOpeningDirection = opens; + } + } + static get styles(): CSSResultGroup { return css` ha-svg-icon {