Skip to content

Commit

Permalink
Fix card not always showing in preview mode.
Browse files Browse the repository at this point in the history
It seems that editMode was recently renamed to preview in the front end:
home-assistant/frontend#21065

Rearranged the logic to toggle the hidden state to track the state
of preview as it changes.
  • Loading branch information
j9brown committed Sep 5, 2024
1 parent 6dd9e6a commit a1aabd6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "decluttering-card",
"version": "1.0.0",
"version": "1.0.3",
"description": "Decluttering Card for Lovelace",
"main": "dist/decluttering-card.js",
"scripts": {
Expand Down
32 changes: 10 additions & 22 deletions src/decluttering-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ abstract class DeclutteringElement extends LitElement {
:host(.child-card-hidden) {
display: none;
}
:host([edit-mode='true']) {
:host([preview]) {
display: block !important;
border: 1px solid var(--primary-color);
}
Expand Down Expand Up @@ -391,8 +391,8 @@ class DeclutteringCardEditor extends LitElement implements LovelaceCardEditor {
@customElement('decluttering-template')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class DeclutteringTemplate extends DeclutteringElement {
@property({ attribute: 'edit-mode', reflect: true }) editMode;
@state() private _previewMode = false;
@property({ type: Boolean, reflect: true }) preview = false;

@state() private _template?: string;

static getConfigElement(): HTMLElement {
Expand Down Expand Up @@ -428,27 +428,15 @@ class DeclutteringTemplate extends DeclutteringElement {
this._setTemplateConfig(config, undefined);
}

async connectedCallback(): Promise<void> {
super.connectedCallback();

this._previewMode = this.parentElement?.localName === 'hui-card-preview';
if (!this.editMode && !this._previewMode) {
this.setAttribute('hidden', '');
} else {
this.removeAttribute('hidden');
}
}

protected render(): TemplateResult | void {
if (this._template) {
if (this._previewMode) return super.render();
if (this.editMode) {
return html`
<div class="badge">${this._template}</div>
${super.render()}
`;
}
if (this.preview) {
this.toggleAttribute('hidden', false);
return html`
<div class="badge">${this._template}</div>
${super.render()}
`;
}
this.toggleAttribute('hidden', true);
return html``;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export interface LovelaceElementConfig {

export interface LovelaceRow extends HTMLElement {
hass?: HomeAssistant;
editMode?: boolean;
setConfig(config: LovelaceRowConfig);
}

Expand Down

0 comments on commit a1aabd6

Please sign in to comment.