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 045eb6a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 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
47 changes: 23 additions & 24 deletions src/decluttering-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,6 @@ abstract class DeclutteringElement extends LitElement {
:host(.child-card-hidden) {
display: none;
}
:host([edit-mode='true']) {
display: block !important;
border: 1px solid var(--primary-color);
}
`;
}

Expand Down Expand Up @@ -391,8 +387,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 All @@ -417,6 +413,10 @@ class DeclutteringTemplate extends DeclutteringElement {
margin: 8px;
color: var(--primary-color);
}
:host([preview]) {
display: block !important;
border: 1px solid var(--primary-color);
}
`;
}

Expand All @@ -428,28 +428,27 @@ 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 {
this.setVisibility(!this.preview);
if (this.preview) {
return html`
<div class="badge">${this._template}</div>
${super.render()}
`;
}
return html``;
}

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()}
`;
}
private setVisibility(hidden: boolean): void {
if (this.hasAttribute('hidden') !== hidden) {
this.toggleAttribute('hidden', hidden);
this.dispatchEvent(
new Event('card-visibility-changed', {
bubbles: true,
composed: 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 045eb6a

Please sign in to comment.