From 5cc06ebf0b4d9562ccd287bf1b1b2c267313a665 Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Tue, 22 Aug 2023 17:10:26 +0200 Subject: [PATCH] Migrate gallery pages to LitElement (#17667) --- gallery/src/components/demo-cards.ts | 30 +++----- gallery/src/components/demo-more-info.js | 93 ----------------------- gallery/src/components/demo-more-info.ts | 93 +++++++++++++++++++++++ gallery/src/components/demo-more-infos.js | 83 -------------------- gallery/src/components/demo-more-infos.ts | 87 +++++++++++++++++++++ gallery/src/ha-demo-options.ts | 47 ++++++++++++ 6 files changed, 236 insertions(+), 197 deletions(-) delete mode 100644 gallery/src/components/demo-more-info.js create mode 100644 gallery/src/components/demo-more-info.ts delete mode 100644 gallery/src/components/demo-more-infos.js create mode 100644 gallery/src/components/demo-more-infos.ts create mode 100644 gallery/src/ha-demo-options.ts diff --git a/gallery/src/components/demo-cards.ts b/gallery/src/components/demo-cards.ts index d2c485e76e..0bb36e3309 100644 --- a/gallery/src/components/demo-cards.ts +++ b/gallery/src/components/demo-cards.ts @@ -1,4 +1,3 @@ -import "@polymer/app-layout/app-toolbar/app-toolbar"; import { html, css, LitElement } from "lit"; import { customElement, property, query, state } from "lit/decorators"; import { applyThemesOnElement } from "../../../src/common/dom/apply_themes_on_element"; @@ -7,6 +6,7 @@ import "../../../src/components/ha-switch"; import { HomeAssistant } from "../../../src/types"; import "./demo-card"; import type { DemoCardConfig } from "./demo-card"; +import "../ha-demo-options"; @customElement("demo-cards") class DemoCards extends LitElement { @@ -20,20 +20,14 @@ class DemoCards extends LitElement { render() { return html` - -
- - - - - - - -
-
+ + + + + + + +
${this.configs.map( @@ -69,12 +63,6 @@ class DemoCards extends LitElement { demo-card { margin: 16px 16px 32px; } - app-toolbar { - background-color: var(--light-primary-color); - } - .filters { - margin-left: 60px; - } ha-formfield { margin-right: 16px; } diff --git a/gallery/src/components/demo-more-info.js b/gallery/src/components/demo-more-info.js deleted file mode 100644 index 9e2fe30b22..0000000000 --- a/gallery/src/components/demo-more-info.js +++ /dev/null @@ -1,93 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag"; -/* eslint-plugin-disable lit */ -import { PolymerElement } from "@polymer/polymer/polymer-element"; -import "../../../src/components/ha-card"; -import "../../../src/dialogs/more-info/more-info-content"; -import "../../../src/state-summary/state-card-content"; - -class DemoMoreInfo extends PolymerElement { - static get template() { - return html` - -
-
- - - - - -
- -
- `; - } - - static get properties() { - return { - hass: Object, - entityId: String, - showConfig: Boolean, - _stateObj: { - type: Object, - computed: "_getState(entityId, hass.states)", - }, - }; - } - - _getState(entityId, states) { - return states[entityId]; - } - - _jsonEntity(stateObj) { - // We are caching some things on stateObj - // (it sucks, we will remove in the future) - const tmp = {}; - Object.keys(stateObj).forEach((key) => { - if (key[0] !== "_") { - tmp[key] = stateObj[key]; - } - }); - return JSON.stringify(tmp, null, 2); - } -} - -customElements.define("demo-more-info", DemoMoreInfo); diff --git a/gallery/src/components/demo-more-info.ts b/gallery/src/components/demo-more-info.ts new file mode 100644 index 0000000000..2aa2ca1336 --- /dev/null +++ b/gallery/src/components/demo-more-info.ts @@ -0,0 +1,93 @@ +import { LitElement, css, html } from "lit"; +import { customElement, property } from "lit/decorators"; +import "../../../src/components/ha-card"; +import "../../../src/dialogs/more-info/more-info-content"; +import "../../../src/state-summary/state-card-content"; +import "../ha-demo-options"; +import { HomeAssistant } from "../../../src/types"; + +@customElement("demo-more-info") +class DemoMoreInfo extends LitElement { + @property() public hass!: HomeAssistant; + + @property() public entityId!: string; + + @property() public showConfig!: boolean; + + render() { + const state = this._getState(this.entityId, this.hass.states); + return html` +
+
+ + + + + +
+ ${this.showConfig ? html`
${this._jsonEntity(state)}
` : ""} +
+ `; + } + + private _getState(entityId, states) { + return states[entityId]; + } + + private _jsonEntity(stateObj) { + // We are caching some things on stateObj + // (it sucks, we will remove in the future) + const tmp = {}; + Object.keys(stateObj).forEach((key) => { + if (key[0] !== "_") { + tmp[key] = stateObj[key]; + } + }); + return JSON.stringify(tmp, null, 2); + } + + static styles = css` + .root { + display: flex; + } + #card { + max-width: 400px; + width: 100vw; + } + ha-card { + width: 352px; + padding: 20px 24px; + } + state-card-content { + display: block; + margin-bottom: 16px; + } + pre { + width: 400px; + margin: 0 16px; + overflow: auto; + color: var(--primary-text-color); + } + @media only screen and (max-width: 800px) { + .root { + flex-direction: column; + } + pre { + margin: 16px 0; + } + } + `; +} + +declare global { + interface HTMLElementTagNameMap { + "demo-more-info": DemoMoreInfo; + } +} diff --git a/gallery/src/components/demo-more-infos.js b/gallery/src/components/demo-more-infos.js deleted file mode 100644 index 26d5fd002f..0000000000 --- a/gallery/src/components/demo-more-infos.js +++ /dev/null @@ -1,83 +0,0 @@ -import "@polymer/app-layout/app-toolbar/app-toolbar"; -import { html } from "@polymer/polymer/lib/utils/html-tag"; -/* eslint-plugin-disable lit */ -import { PolymerElement } from "@polymer/polymer/polymer-element"; -import { applyThemesOnElement } from "../../../src/common/dom/apply_themes_on_element"; -import "../../../src/components/ha-formfield"; -import "../../../src/components/ha-switch"; -import "./demo-more-info"; - -class DemoMoreInfos extends PolymerElement { - static get template() { - return html` - - -
- - - - - - - -
-
-
-
- -
-
- `; - } - - static get properties() { - return { - entities: Array, - hass: Object, - _showConfig: { - type: Boolean, - value: false, - }, - }; - } - - _showConfigToggled(ev) { - this._showConfig = ev.target.checked; - } - - _darkThemeToggled(ev) { - applyThemesOnElement(this.$.container, { themes: {} }, "default", { - dark: ev.target.checked, - }); - } -} - -customElements.define("demo-more-infos", DemoMoreInfos); diff --git a/gallery/src/components/demo-more-infos.ts b/gallery/src/components/demo-more-infos.ts new file mode 100644 index 0000000000..5d7d928620 --- /dev/null +++ b/gallery/src/components/demo-more-infos.ts @@ -0,0 +1,87 @@ +import { LitElement, css, html } from "lit"; +import { customElement, property } from "lit/decorators"; +import { applyThemesOnElement } from "../../../src/common/dom/apply_themes_on_element"; +import "../../../src/components/ha-formfield"; +import "../../../src/components/ha-switch"; +import "./demo-more-info"; +import "../ha-demo-options"; +import { HomeAssistant } from "../../../src/types"; + +@customElement("demo-more-infos") +class DemoMoreInfos extends LitElement { + @property() public hass!: HomeAssistant; + + @property() public entities!: []; + + @property({ attribute: false }) _showConfig: boolean = false; + + render() { + return html` + + + + + + + + +
+
+ ${this.entities.map( + (item) => + html`` + )} +
+
+ `; + } + + static styles = css` + #container { + min-height: calc(100vh - 128px); + background: var(--primary-background-color); + } + .cards { + display: flex; + flex-wrap: wrap; + justify-content: center; + } + demo-more-info { + margin: 16px 16px 32px; + } + ha-formfield { + margin-right: 16px; + } + `; + + _showConfigToggled(ev) { + this._showConfig = ev.target.checked; + } + + _darkThemeToggled(ev) { + applyThemesOnElement( + this.shadowRoot!.querySelector("#container"), + { + default_theme: "default", + default_dark_theme: "default", + themes: {}, + darkMode: false, + theme: "default", + }, + "default", + { + dark: ev.target.checked, + } + ); + } +} + +declare global { + interface HTMLElementTagNameMap { + "demo-more-infos": DemoMoreInfos; + } +} diff --git a/gallery/src/ha-demo-options.ts b/gallery/src/ha-demo-options.ts new file mode 100644 index 0000000000..f3565e7891 --- /dev/null +++ b/gallery/src/ha-demo-options.ts @@ -0,0 +1,47 @@ +import "@material/mwc-drawer"; +import "@material/mwc-top-app-bar-fixed"; +import { html, css, LitElement } from "lit"; +import { customElement } from "lit/decorators"; +import "../../src/components/ha-icon-button"; +import "../../src/managers/notification-manager"; +import { haStyle } from "../../src/resources/styles"; +import "./components/page-description"; + +@customElement("ha-demo-options") +class HaDemoOptions extends LitElement { + render() { + return html``; + } + + static styles = [ + haStyle, + css` + :host { + display: block; + background-color: var(--light-primary-color); + margin-left: 60px + margin-right: 60px; + display: var(--layout-horizontal_-_display); + -ms-flex-direction: var(--layout-horizontal_-_-ms-flex-direction); + -webkit-flex-direction: var( + --layout-horizontal_-_-webkit-flex-direction + ); + flex-direction: var(--layout-horizontal_-_flex-direction); + -ms-flex-align: var(--layout-center_-_-ms-flex-align); + -webkit-align-items: var(--layout-center_-_-webkit-align-items); + align-items: var(--layout-center_-_align-items); + position: relative; + height: 64px; + padding: 0 16px; + pointer-events: none; + font-size: 20px; + } + `, + ]; +} + +declare global { + interface HTMLElementTagNameMap { + "ha-demo-options": HaDemoOptions; + } +}