-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert hui-markdown-card to TypeScript/LitElement #1808
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { html, LitElement } from "@polymer/lit-element"; | ||
|
||
import "../../../components/ha-card.js"; | ||
import "../../../components/ha-markdown.js"; | ||
|
||
import { HassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin"; | ||
import { LovelaceCard, LovelaceConfig } from "../types.js"; | ||
|
||
interface Config extends LovelaceConfig { | ||
content: string; | ||
title?: string; | ||
} | ||
|
||
export class HuiMarkdownCard extends HassLocalizeLitMixin(LitElement) | ||
implements LovelaceCard { | ||
protected config?: Config; | ||
|
||
static get properties() { | ||
return { | ||
config: {}, | ||
}; | ||
} | ||
|
||
public getCardSize() { | ||
return this.config!.content.split("\n").length; | ||
} | ||
|
||
public setConfig(config: Config) { | ||
if (!config.content) { | ||
throw new Error("Invalid Configuration: Content Required"); | ||
} | ||
|
||
this.config = config; | ||
} | ||
|
||
protected render() { | ||
if (!this.config) { | ||
return html``; | ||
} | ||
|
||
return html` | ||
${this.renderStyle()} | ||
<ha-card .header="${this.config.title}"> | ||
<ha-markdown .content="${this.config.content}"></ha-markdown> | ||
</ha-card> | ||
`; | ||
} | ||
|
||
private renderStyle() { | ||
return html` | ||
<style> | ||
:host { | ||
@apply --paper-font-body1; | ||
} | ||
ha-markdown { | ||
display: block; | ||
padding: 0 16px 16px; | ||
zsarnett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
padding-top: ${this.config!.title ? '0' : '16px'}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't make stylesheets dynamic, as it means the whole CSS stylesheet has to be re-rendered. I suggest you use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that in that case, you could also use a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! Thanks! I'll put in a PR for this as well: |
||
-ms-user-select: initial; | ||
-webkit-user-select: initial; | ||
-moz-user-select: initial; | ||
} | ||
ha-markdown > *:first-child { | ||
margin-top: 0; | ||
} | ||
ha-markdown > *:last-child { | ||
margin-bottom: 0; | ||
} | ||
ha-markdown a { | ||
color: var(--primary-color); | ||
} | ||
ha-markdown img { | ||
max-width: 100%; | ||
} | ||
</style> | ||
`; | ||
} | ||
} | ||
|
||
declare global { | ||
interface HTMLElementTagNameMap { | ||
"hui-markdown-card": HuiMarkdownCard; | ||
} | ||
} | ||
|
||
customElements.define("hui-markdown-card", HuiMarkdownCard); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why include
HassLocalizeLitMixin
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I just extend
LitElement
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HassLocalizeLitMixin provides a
localize
function for translating text. Since you're not using that,export class HuiMarkdownCard extends LitElement
should be enough, yes.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. I should probably create a new pull to clean up my other conversion as well then: #1801
Thanks