Skip to content
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

Merged
merged 4 commits into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions src/panels/lovelace/cards/hui-markdown-card.js

This file was deleted.

86 changes: 86 additions & 0 deletions src/panels/lovelace/cards/hui-markdown-card.ts
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why include HassLocalizeLitMixin?

Copy link
Member Author

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?

Copy link
Contributor

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.

Copy link
Member Author

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

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'};
Copy link
Member

Choose a reason for hiding this comment

The 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 styleMap (example)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that in that case, you could also use a classMap (example), which I think I prefer, as that means we keep all styling inside renderStyle, meaning it can be overridden easily.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-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);
2 changes: 1 addition & 1 deletion src/panels/lovelace/common/create-card-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "../cards/hui-history-graph-card.js";
import "../cards/hui-horizontal-stack-card.js";
import "../cards/hui-iframe-card.ts";
import "../cards/hui-map-card.js";
import "../cards/hui-markdown-card.js";
import "../cards/hui-markdown-card.ts";
import "../cards/hui-media-control-card.js";
import "../cards/hui-picture-card.js";
import "../cards/hui-picture-elements-card";
Expand Down