I have built a native web component called <expanding-textarea>
which extends the functionality of <textarea>
. I am able to set text content of the component like <expanding-textarea>content goes here</expanding-textarea>
but not when I try to set the content dynamically by using the component within a LitElement. The below is a minimal example which populates the text correctly for <textarea>${item.text}</textarea>
within the repeat directive, but not as is.
<body>
<my-element></my-element>
<script type="module">
const template = document.createElement("template");
template.innerHTML = `<textarea></textarea>`;
class ExpandingTextarea extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.appendChild(template.content.cloneNode(true));
const textarea = this.shadowRoot.querySelector("textarea");
textarea.textContent = this.textContent;
}
}
customElements.define("expanding-textarea", ExpandingTextarea);
import { LitElement, html } from "lit-element";
import { repeat } from "lit-html/directives/repeat.js";
class MyElement extends LitElement {
static get properties() {
return {
list: { type: Array },
};
}
constructor() {
super();
this.list = [
{ id: "1", text: "hello" },
{ id: "2", text: "hi" },
{ id: "3", text: "cool" },
];
}
render() {
return html`
${repeat(
this.list,
item => item.id,
item => html`<expanding-textarea>${item.text}</expanding-textarea>`
)}
`;
}
}
customElements.define("my-element", MyElement);
</script>
</body>