Skip to content

Commit

Permalink
Add dynamic getCardSize function for better HA UI handling (#47)
Browse files Browse the repository at this point in the history
* Add `getCardSize()`

* Add docs and make this function dynamic

* review

* Add spaces
  • Loading branch information
MrBartusek committed Apr 17, 2023
1 parent 1d158ff commit 3e5eab2
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/components/horizonCard/HorizonCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ export class HorizonCard extends LitElement {
this.processLastHass()
}

/**
* called by HASS to properly distribute card in lovelace view. It should return height
* of the card as a number where 1 is equivalent of 50 pixels.
* @see https://developers.home-assistant.io/docs/frontend/custom-ui/custom-card/#api
*/
public getCardSize (): number {
let height = 4 // Smallest possible card (only graph) is roughly 200px

// Each element of card (title, header, content, footer) adds roughly 50px to the height
if (this.config.title && this.config.title.length > 0) {
height += 1
}

if (this.config.fields?.sunrise || this.config.fields?.sunset) {
height += 1
}

if (this.config.fields?.dawn || this.config.fields?.noon || this.config.fields?.dusk) {
height += 1
}

if (this.config.fields?.azimuth || this.config.fields?.elevation) {
height += 1
}

return height
}

// Visual editor disabled because it's broken, see https://developers.home-assistant.io/blog/2022/02/18/paper-elements/
// static getConfigElement (): HTMLElement {
// return document.createElement(HorizonCardEditor.cardType)
Expand Down

0 comments on commit 3e5eab2

Please sign in to comment.