From 2841369d3dbb56f434d741abdf056eb9c9eb7ee6 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 8 Oct 2021 10:48:39 -0700 Subject: [PATCH] Extract black/white row into component (#10212) * Extract black/white row into component * Remove unused import --- .../src/components/demo-black-white-row.ts | 122 ++++++++++++++ gallery/src/demos/demo-ha-form.ts | 151 +++--------------- 2 files changed, 143 insertions(+), 130 deletions(-) create mode 100644 gallery/src/components/demo-black-white-row.ts diff --git a/gallery/src/components/demo-black-white-row.ts b/gallery/src/components/demo-black-white-row.ts new file mode 100644 index 0000000000..002d7510c2 --- /dev/null +++ b/gallery/src/components/demo-black-white-row.ts @@ -0,0 +1,122 @@ +import { html, LitElement, css, TemplateResult } from "lit"; +import { customElement, property } from "lit/decorators"; +import { applyThemesOnElement } from "../../../src/common/dom/apply_themes_on_element"; + +@customElement("demo-black-white-row") +class DemoBlackWhiteRow extends LitElement { + @property() title!: string; + + @property() value!: any; + + protected render(): TemplateResult { + return html` +
+
+ +
+ +
+
+ Submit +
+
+
+
+ +
+ +
+
+ Submit +
+
+
${JSON.stringify(this.value, undefined, 2)}
+
+
+ `; + } + + firstUpdated(changedProps) { + super.firstUpdated(changedProps); + applyThemesOnElement( + this.shadowRoot!.querySelector(".dark"), + { + default_theme: "default", + default_dark_theme: "default", + themes: {}, + darkMode: false, + }, + "default", + { dark: true } + ); + } + + static styles = css` + .row { + display: flex; + } + .content { + padding: 50px 0; + background-color: var(--primary-background-color); + } + .light { + flex: 1; + padding-left: 50px; + padding-right: 50px; + box-sizing: border-box; + } + .light ha-card { + margin-left: auto; + } + .dark { + display: flex; + flex: 1; + padding-left: 50px; + box-sizing: border-box; + flex-wrap: wrap; + } + ha-card { + width: 400px; + } + pre { + width: 300px; + margin: 0 16px 0; + overflow: auto; + color: var(--primary-text-color); + } + .card-actions { + display: flex; + flex-direction: row-reverse; + border-top: none; + } + @media only screen and (max-width: 1500px) { + .light { + flex: initial; + } + } + @media only screen and (max-width: 1000px) { + .light, + .dark { + padding: 16px; + } + .row, + .dark { + flex-direction: column; + } + ha-card { + margin: 0 auto; + width: 100%; + max-width: 400px; + } + pre { + margin: 16px auto; + } + } + `; +} + +declare global { + interface HTMLElementTagNameMap { + "demo-black-white-row": DemoBlackWhiteRow; + } +} diff --git a/gallery/src/demos/demo-ha-form.ts b/gallery/src/demos/demo-ha-form.ts index 38266cd602..5dc69ece51 100644 --- a/gallery/src/demos/demo-ha-form.ts +++ b/gallery/src/demos/demo-ha-form.ts @@ -1,12 +1,11 @@ /* eslint-disable lit/no-template-arrow */ import "@material/mwc-button"; -import { LitElement, TemplateResult, css, html } from "lit"; +import { LitElement, TemplateResult, html } from "lit"; import { customElement } from "lit/decorators"; import { computeInitialHaFormData } from "../../../src/components/ha-form/compute-initial-ha-form-data"; -import "../../../src/components/ha-card"; -import { applyThemesOnElement } from "../../../src/common/dom/apply_themes_on_element"; import type { HaFormSchema } from "../../../src/components/ha-form/types"; import "../../../src/components/ha-form/ha-form"; +import "../components/demo-black-white-row"; const SCHEMAS: { title: string; @@ -235,138 +234,30 @@ class DemoHaForm extends LitElement { return html` ${SCHEMAS.map((info, idx) => { const translations = info.translations || {}; - const computeLabel = (schema) => - translations[schema.name] || schema.name; - const computeError = (error) => translations[error] || error; - return html` -
-
- -
- { - this.data[idx] = e.detail.value; - this.requestUpdate(); - }} - > -
-
- Submit -
-
-
-
- -
- { - this.data[idx] = e.detail.value; - this.requestUpdate(); - }} - > -
-
- Submit -
-
-
${JSON.stringify(this.data[idx], undefined, 2)}
-
-
+ + ${["light", "dark"].map( + (slot) => html` + translations[error] || error} + .computeLabel=${(schema) => + translations[schema.name] || schema.name} + @value-changed=${(e) => { + this.data[idx] = e.detail.value; + this.requestUpdate(); + }} + > + ` + )} + `; })} `; } - - firstUpdated(changedProps) { - super.firstUpdated(changedProps); - this.shadowRoot!.querySelectorAll(".dark").forEach((el) => { - applyThemesOnElement( - el, - { - default_theme: "default", - default_dark_theme: "default", - themes: {}, - darkMode: false, - }, - "default", - { dark: true } - ); - }); - } - - static styles = css` - .row { - display: flex; - } - .content { - padding: 50px 0; - background-color: var(--primary-background-color); - } - .light { - flex: 1; - padding-left: 50px; - padding-right: 50px; - box-sizing: border-box; - } - .light ha-card { - margin-left: auto; - } - .dark { - display: flex; - flex: 1; - padding-left: 50px; - box-sizing: border-box; - flex-wrap: wrap; - } - ha-card { - width: 400px; - } - pre { - width: 300px; - margin: 0 16px 0; - overflow: auto; - color: var(--primary-text-color); - } - .card-actions { - display: flex; - flex-direction: row-reverse; - border-top: none; - } - @media only screen and (max-width: 1500px) { - .light { - flex: initial; - } - } - @media only screen and (max-width: 1000px) { - .light, - .dark { - padding: 16px; - } - .row, - .dark { - flex-direction: column; - } - ha-card { - margin: 0 auto; - width: 100%; - max-width: 400px; - } - pre { - margin: 16px auto; - } - } - `; } declare global {