Skip to content

Commit 79f5ac4

Browse files
authored
Refactor height measurement logic in compact-power-card
1 parent 7e1e8da commit 79f5ac4

1 file changed

Lines changed: 51 additions & 9 deletions

File tree

‎compact-power-card.js‎

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,45 @@ class CompactPowerCard extends CompactPowerCardBase {
12111211
}
12121212

12131213
_shouldUseExternalHeight() {
1214-
return Boolean(this.closest("div.card.fit-rows"));
1214+
if (this.closest("div.card.fit-rows")) return true;
1215+
if (this._isInCardEditor()) return false;
1216+
1217+
const rect = this.getBoundingClientRect ? this.getBoundingClientRect() : null;
1218+
const measuredHeight = this._getMeasuredExternalHeight(rect);
1219+
return measuredHeight > 220;
1220+
}
1221+
1222+
_closestComposed(selector) {
1223+
let node = this;
1224+
while (node) {
1225+
if (node instanceof Element && node.matches(selector)) return node;
1226+
const root = node.getRootNode ? node.getRootNode() : null;
1227+
node = node.parentNode || root?.host || null;
1228+
}
1229+
return null;
1230+
}
1231+
1232+
_isInCardEditor() {
1233+
return Boolean(
1234+
this._closestComposed(
1235+
"hui-card-element-editor, hui-dialog-edit-card, hui-card-preview, hui-card-picker, ha-dialog"
1236+
)
1237+
);
1238+
}
1239+
1240+
_getMeasuredExternalHeight(rect = null) {
1241+
const box = rect || (this.getBoundingClientRect ? this.getBoundingClientRect() : null);
1242+
const measuredHeight = this._hostHeight || box?.height || 0;
1243+
if (!measuredHeight) return 0;
1244+
if (this.closest("div.card.fit-rows")) return measuredHeight;
1245+
1246+
const viewportHeight = typeof window !== "undefined" ? window.innerHeight || 0 : 0;
1247+
if (!viewportHeight || !box) return measuredHeight;
1248+
1249+
const scrollY = typeof window !== "undefined" ? window.scrollY || 0 : 0;
1250+
const documentTop = box.top + scrollY;
1251+
const availableHeight = Math.max(0, viewportHeight - Math.max(0, documentTop));
1252+
return availableHeight > 0 ? Math.min(measuredHeight, availableHeight) : measuredHeight;
12151253
}
12161254

12171255
_getLayoutMetrics({ hasPv, hasBattery, hasAnyLabels }) {
@@ -1222,13 +1260,16 @@ class CompactPowerCard extends CompactPowerCardBase {
12221260
const defaultHeight = !useExternalHeight && (!hasPv || !hasBattery) ? 150 : 184;
12231261
const hostRect = this.getBoundingClientRect ? this.getBoundingClientRect() : null;
12241262
const outerWidth = this._hostWidth != null ? this._hostWidth : hostRect?.width || defaultWidth;
1225-
const outerHeight = useExternalHeight
1226-
? this._externalHeight != null
1227-
? this._externalHeight
1228-
: this._hostHeight != null
1229-
? this._hostHeight
1230-
: hostRect?.height || defaultHeight
1231-
: defaultHeight;
1263+
const measuredExternalHeight = this._getMeasuredExternalHeight(hostRect);
1264+
let outerHeight = defaultHeight;
1265+
if (useExternalHeight) {
1266+
outerHeight =
1267+
measuredExternalHeight ||
1268+
this._externalHeight ||
1269+
this._hostHeight ||
1270+
hostRect?.height ||
1271+
defaultHeight;
1272+
}
12321273
const padX = 8; // ha-card left+right padding (4px each)
12331274
const padY = 2; // bottom padding; top is 0
12341275
const baseWidth = Math.max(0, outerWidth - padX);
@@ -1437,8 +1478,9 @@ class CompactPowerCard extends CompactPowerCardBase {
14371478
this._hostWidth = newW;
14381479
this._hostHeight = newH;
14391480
const widthChanged = prevW != null && newW !== prevW;
1481+
const heightChanged = prevH != null && newH !== prevH;
14401482
this._externalHeight = newH;
1441-
if (prevW == null || prevH == null) {
1483+
if (prevW == null || prevH == null || widthChanged || heightChanged) {
14421484
this.requestUpdate();
14431485
}
14441486
}

0 commit comments

Comments
 (0)