Skip to content

Commit

Permalink
Add support for custom color display in color picker (#22174)
Browse files Browse the repository at this point in the history
Add support for custom color in color picker
  • Loading branch information
piitaya authored Oct 1, 2024
1 parent f9814f3 commit 70d6cce
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/components/ha-color-picker.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { mdiInvertColorsOff, mdiPalette } from "@mdi/js";
import { css, html, LitElement, nothing } from "lit";
import { customElement, property } from "lit/decorators";
import { customElement, property, query } from "lit/decorators";
import { styleMap } from "lit/directives/style-map";
import { computeCssColor, THEME_COLORS } from "../common/color/compute-color";
import { fireEvent } from "../common/dom/fire_event";
import { stopPropagation } from "../common/dom/stop_propagation";
import { LocalizeKeys } from "../common/translations/localize";
import { HomeAssistant } from "../types";
import "./ha-list-item";
import "./ha-select";
import "./ha-md-divider";
import "./ha-select";
import type { HaSelect } from "./ha-select";

@customElement("ha-color-picker")
export class HaColorPicker extends LitElement {
Expand All @@ -32,7 +33,17 @@ export class HaColorPicker extends LitElement {

@property({ type: Boolean }) public disabled = false;

_valueSelected(ev) {
@query("ha-select") private _select!: HaSelect;

connectedCallback(): void {
super.connectedCallback();
// Refresh layout options when the field is connected to the DOM to ensure current value displayed
this._select.layoutOptions();
}

private _valueSelected(ev) {
ev.stopPropagation();
if (!this.isConnected) return;
const value = ev.target.value;
this.value = value === this.defaultColor ? undefined : value;
fireEvent(this, "value-changed", {
Expand All @@ -41,7 +52,13 @@ export class HaColorPicker extends LitElement {
}

render() {
const value = this.value || this.defaultColor;
const value = this.value || this.defaultColor || "";

const isCustom = !(
THEME_COLORS.has(value) ||
value === "none" ||
value === "state"
);

return html`
<ha-select
Expand Down Expand Up @@ -110,6 +127,14 @@ export class HaColorPicker extends LitElement {
</ha-list-item>
`
)}
${isCustom
? html`
<ha-list-item .value=${value} graphic="icon">
${value}
<span slot="graphic">${this.renderColorCircle(value)}</span>
</ha-list-item>
`
: nothing}
</ha-select>
`;
}
Expand Down

0 comments on commit 70d6cce

Please sign in to comment.