Failure to refresh Polymer element attribute detected in lit web component

Recently, I've been diving into the world of the Home Assistant project's usage of Polymer app-layout elements combined with the Lit library. However, I've hit a roadblock where I can't seem to update a public property in my custom component to affect an attribute on a nested Polymer element.

Let me share a snippet of my current code:

import { LitElement, html, css, PropertyValues } from 'lit';
import { customElement, state, query, property } from 'lit/decorators.js';

import "@polymer/app-layout/app-drawer-layout/app-drawer-layout";
import type { AppDrawerLayoutElement } from "@polymer/app-layout/app-drawer-layout/app-drawer-layout";
import "@polymer/app-layout/app-drawer/app-drawer";
import type { AppDrawerElement } from "@polymer/app-layout/app-drawer/app-drawer";

import { listenMediaQuery } from "./common/media-query";
import { toggleAttribute } from "./common/toggle-attribute";


declare global {
    // for fire event
    interface WSDomEvents {
        "ab-toggle-menu": undefined;
    }
}

@customElement('ab-app')
class WsApp extends LitElement {
    static styles = css`
        :host {
            --app-drawer-width: 56px;
        }
        :host([expanded]) {
            --app-drawer-width: 256px;
        }

        button {
            width: 100%;
            height: 56px;
            border: 0;
            background-color: var(--color-accent);
            color: #FFF;
            display: flex;
            align-items: center;
            font-size: var(--font-size-large);
        }

        svg {
            padding: 8px;
            height: 100%;
            width: auto;
        }

        .label {
            display: none;
            padding: 10px;
        }

        :host([expanded]) .label {
            display: inline;
        }

        .ab-sidebar {
            background-color: var(--color-accent)!important;
            height: 100%;
        }
    `;

    @property({ type: Boolean, reflect: true }) public narrow!: boolean;
    @property({ type: Boolean }) public alwaysExpand = false;

    constructor() {
        super();
        listenMediaQuery("(max-width: 870px)", (matches) => {
            this.narrow = matches;
        });
    }

    protected render() {
        const sidebarNarrow = this._sidebarNarrow
        return html`
            <app-drawer-layout
                responsiveWidth="870px"
            >
                <app-drawer
                    class="app-drawer"
                    swipe-open
                    slot="drawer"
                    align="left"
                    ?persistent=${!this.narrow}
                >
                 <!-- buttons and content here -->
            </app-drawer-layout>
        `;
    }

    protected firstUpdated() {

        // method here
    }

    protected updated(changedProps: PropertyValues) {
       // method here
    }

    private toggleSidebar() {
       // method here
    }

    private get _sidebarNarrow() {
        return this.narrow;
    }

    private get drawer(): AppDrawerElement {
       // method here
    }

    private get appLayout(): AppDrawerLayoutElement {
        // method here
    }
}

My challenge lies within ?persistent=${!this.narrow} in my render() function. Despite using this syntax to bind the "persistent" property of the app-drawer Polymer element to my "narrow" property, which is controlled by a media query, the attribute does not get added or removed as expected. The "persistent" attribute continues to toggle at 640px, its default value.

Even setting responsiveWidth="870px" on app-drawer-layout seems to have no impact.

Why isn't this approach working as intended? I followed the guidance for Boolean properties outlined in Lit's guide for Polymer users. While it functions flawlessly in the Home Assistant project, I seem to be overlooking some crucial aspects.

Answer №1

Replace the attribute with the property by adding a . before it:

   .persistent=${!this.narrow}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

There seems to be an issue with executing an imported function from a .ts file within a TSX file in NextJs, resulting

I've encountered an issue that seems to be related to using NextJs with TypeScript. For example: // /pages/index.tsx import _ from 'lodash' export const MyComponent = () => { return ( <ul> { _.map(someArray, ...

Implementing the ternary operator on a nested object field in typescript to enforce type validation

Is there an issue with my code or is this intentional? I want to write something similar to this. type MyDefinition = { salutation: string; value?: { typeOfValue: string; val?: string }; }; function create(name: string, input?: Partial<MyDefin ...

Implementing TypeScript/Angular client generation using Swagger/OpenAPI in the build pipeline

Generating Java/Spring Server-Stubs with swagger-codegen-maven-plugin In my Spring Boot Java project, I utilize the swagger-codegen-maven-plugin to automatically generate the server stubs for Spring MVC controller interfaces from my Swagger 2.0 api.yml fi ...

Version 4.0 of d3 introduces an import statement that provides a __moduleExports wrapper

Having difficulty with an import statement in my D3 4.0 and Ionic2/Angular2 project. I believe the import statement is correct, as everything compiles successfully. import * as d3Request from 'd3-request'; export class HomePage { construc ...

Passing a method from a component to a service in Angular 9

Recently, I've been working on some websocket code that involves sending a message to the server and receiving a reply. The current implementation is functional, but I'm looking to refactor it by encapsulating it within a service and then callin ...

I am encountering an issue with importing modules from the public folder in Next.js when using TypeScript, as I am

I've been running into an issue with importing files in Next.js using TypeScript. I'm trying to use regular imports with custom absolute paths, but I keep getting a module not found error. Oddly enough, my IDE is able to locate the file when I cl ...

Is there a method in TypeScript to make an enum more dynamic by parameterizing it?

I've defined this enum in our codebase. enum EventDesc { EVENT1 = 'event 1', EVENT2 = 'event 2', EVENT3 = 'event 3' } The backend has EVENT1, EVENT2, EVENT3 as event types. On the UI, we display event 1, event 2, a ...

Issue with Angular 18 component not being displayed or identified

Recently, I began building an Angular 18 application and encountered an issue with adding my first component as a standalone. It appears that the app is not recognizing my component properly, as it does not display when added as an HTML tag in my app.compo ...

Ensure that the method is passed a negative number -1 instead of the literal number 1 from an HTML error

This is an example of my HTML code: <button (mousedown)="onMouseDown($event, -1)"></button> Here is the TypeScript code for handling the mouse down event: onMouseDown(e: MouseEvent, direction: 1|-1) { this.compute.emit(direction); ...

Converting a Promise to an Observable in Angular using Typescript

I have a working method that functions as intended: getdata(): Promise<any> { let query = `SELECT * FROM table`; return new Promise((resolve, reject) => { this.db.query(query, (error, rows) => { if(error) reject(error); ...

Setting key-value pairs in TypeScript objects explained

I encountered an issue with setting key/value pairs on a plain object. type getAObjectFn = <K extends string, V>(k: K, v: V) => Record<K, V> const getAObject: getAObjectFn = (k, v) => { return { [k]: v } } console.log(getAObject ...

Deactivate the selection option in Syncfusion NumericTextbox

I have integrated an Angular NumericTextbox component from Syncfusion into my application. A problem we encountered is that when the input is clicked, it automatically gets selected. Is there a way to disable this behavior? Problem: https://gyazo.com/a72b ...

Typescript/Three.js encounters the issue of game objects becoming undefined

Something in my code seems to have broken unexpectedly. I can't figure out why the "Game" object is defined before calling this.render() in the constructor, but becomes undefined in the render method. Before render(), the console shows: Game camera: ...

Reset the select boxes when a button is clicked

I'm currently utilizing Devextreme within my Angular application, and I have three dx-selectbox elements in the component. I am attempting to clear all three dropdown selections when clicking a "clear" button. Unfortunately, I am unable to find a way ...

Error 405: Angular encounters a method not supported while attempting to delete the entity

I have developed an application that performs CRUD operations on a list of entities. However, when attempting to delete an entity, the dialog box does not appear as expected. To start, I have a HttpService serving as the foundation for the CRUD operations ...

Encountering error 2307 "Cannot find module" when using Vue 3 with script setup and TypeScript

I am currently attempting to run unit tests in my Vue3 project using vue/test-utils and jest. Upon running the npm run test script, the test fails due to an error with the import: error TS2307: Cannot find module 'pathtofile/file.vue' I have tr ...

Issue: ReactJS - $npm_package_version variable not functioning properly in build versionIn the current

After trying this suggested solution, unfortunately, it did not work for my specific case. The React application I am working on was initialized with CRA version 5.0.1 and currently has a version of 18.2.0. Additionally, the dotenv version being used is 1 ...

The error encountered states that in the Angular typescript method, the term "x" is not recognized as a

In my codebase, I have an entity named Epic which contains a method called pendingTasks() within a class. import { Solution } from '../solutions.model'; import { PortfolioKanban } from '../kanban/portfolio-kanban.model'; import { Kanban ...

Playwright failing to execute GraphQL tests due to TypeScript configuration problems

I'm facing an issue with my repo where I am running tests using Playwright against a graphQL URL. Despite configuring the tests, there is an error indicating that the environment variable defining the environment cannot be found. The repository in qu ...

The Angular 11 library module has been successfully imported into the consuming app but it is not being utilized

Currently, I am in the process of creating an Angular library that will encompass services, pipes, and directives to be utilized across various Angular projects within my organization. At this point, I have successfully implemented three services within th ...