-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fix consumable feature by adding custom ha-bar component
- Loading branch information
1 parent
870c673
commit 0b674db
Showing
3 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const normalize = (value: number, min: number, max: number): number => { | ||
if (isNaN(value) || isNaN(min) || isNaN(max)) { | ||
// Not a number, return 0 | ||
return 0; | ||
} | ||
if (value > max) return max; | ||
if (value < min) return min; | ||
return value; | ||
}; | ||
|
||
export const getValueInPercentage = (value: number, min: number, max: number): number => { | ||
const newMax = max - min; | ||
const newVal = value - min; | ||
return (100 * newVal) / newMax; | ||
}; | ||
|
||
export const roundWithOneDecimal = (value: number): number => Math.round(value * 10) / 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { css, CSSResultGroup, LitElement, svg, TemplateResult } from "lit"; | ||
import { customElement, property } from "lit/decorators.js"; | ||
import { getValueInPercentage, normalize, roundWithOneDecimal } from "./calculate"; | ||
|
||
@customElement("ha-bar") | ||
export class HaBar extends LitElement { | ||
@property({ type: Number }) public min = 0; | ||
|
||
@property({ type: Number }) public max = 100; | ||
|
||
@property({ type: Number }) public value!: number; | ||
|
||
protected render(): TemplateResult { | ||
const valuePrecentage = roundWithOneDecimal(getValueInPercentage(normalize(this.value, this.min, this.max), this.min, this.max)); | ||
|
||
return svg` | ||
<svg> | ||
<g> | ||
<rect/> | ||
<rect width="${valuePrecentage}%"/> | ||
</g> | ||
</svg> | ||
`; | ||
} | ||
|
||
static get styles(): CSSResultGroup { | ||
return css` | ||
rect { | ||
height: 100%; | ||
} | ||
rect:first-child { | ||
width: 100%; | ||
fill: var(--ha-bar-background-color, var(--secondary-background-color)); | ||
} | ||
rect:last-child { | ||
fill: var(--ha-bar-primary-color, var(--primary-color)); | ||
} | ||
svg { | ||
border-radius: var(--ha-bar-border-radius, 4px); | ||
height: 12px; | ||
width: 100%; | ||
} | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"ha-bar": HaBar; | ||
} | ||
} |