|
14 | 14 | * You should have received a copy of the GNU Lesser General Public License along with @cldn/components. |
15 | 15 | * If not, see <https://www.gnu.org/licenses/>. |
16 | 16 | */ |
17 | | -import {NodeComponent} from "./NodeComponent.js"; |
| 17 | +import {DocumentComponent, NodeComponent} from "./index.js"; |
18 | 18 |
|
19 | 19 | /** |
20 | 20 | * Non-readonly non-method keys |
@@ -127,11 +127,21 @@ export abstract class ElementComponent<T extends Element> extends NodeComponent< |
127 | 127 | } |
128 | 128 |
|
129 | 129 | /** |
130 | | - * Set inner HTML |
| 130 | + * Append HTML. |
| 131 | + * |
| 132 | + * Any components (provided as `${...}` inside the HTML string literal) |
| 133 | + * remain fully functional and are appended (not just as HTML). |
| 134 | + * @example |
| 135 | + * component.html`<div>${new Component("button") |
| 136 | + * .text("Click me") |
| 137 | + * .on("click", () => console.log("clicked")) |
| 138 | + * }</div>` |
| 139 | + * // Event listeners etc. are preserved. |
| 140 | + * // Note the lack of parentheses. |
| 141 | + * @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals Template literals (Template strings) - MDN} |
131 | 142 | */ |
132 | | - public html(html: string) { |
133 | | - this.node.innerHTML = html; |
134 | | - return this; |
| 143 | + public html(strings: TemplateStringsArray, ...components: NodeComponent<any>[]): this { |
| 144 | + return this.append(DocumentComponent.tag(strings, ...components)); |
135 | 145 | } |
136 | 146 |
|
137 | 147 | /** |
@@ -167,6 +177,11 @@ export abstract class ElementComponent<T extends Element> extends NodeComponent< |
167 | 177 | return super.on(type as any, listener, c as any); |
168 | 178 | } |
169 | 179 |
|
| 180 | + public override empty() { |
| 181 | + this.node.replaceChildren(); |
| 182 | + return this; |
| 183 | + } |
| 184 | + |
170 | 185 | /** |
171 | 186 | * Get this component's outer HTML |
172 | 187 | */ |
|
0 commit comments