feat: allow string | Node | (string | Node)[]
This commit is contained in:
parent
bce526f17e
commit
eeb0b1ef59
3 changed files with 22 additions and 4 deletions
|
@ -13,7 +13,25 @@ export class HelloWorld extends WebComponent {
|
||||||
const label = this.props.count ? `Clicked ${this.props.count}` : "World";
|
const label = this.props.count ? `Clicked ${this.props.count}` : "World";
|
||||||
const emote = this.props.emotion === "sad" ? ". 😭" : "! 🙌";
|
const emote = this.props.emotion === "sad" ? ". 😭" : "! 🙌";
|
||||||
|
|
||||||
return `<button id="btn">Hello ${label}${emote}</button>`;
|
const button = document.createElement("button");
|
||||||
|
button.innerText = `Hello ${label}${emote}`;
|
||||||
|
const paragraph = document.createElement("p");
|
||||||
|
paragraph.innerText = "Oh what, dynamic DOM?";
|
||||||
|
|
||||||
|
return [button, paragraph, "no way"];
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (typeof this.template === "string") {
|
||||||
|
this.innerHTML = this.template;
|
||||||
|
} else if (this.template instanceof Node) {
|
||||||
|
this.replaceChildren(this.template);
|
||||||
|
} else if (
|
||||||
|
Array.isArray(this.template) &&
|
||||||
|
this.template.every((t) => t instanceof Node || typeof t === "string")
|
||||||
|
) {
|
||||||
|
this.replaceChildren(...this.template);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
"pub": "npm run clean && npm run build && cd ./dist && npm publish --access public",
|
"pub": "npm run clean && npm run build && cd ./dist && npm publish --access public",
|
||||||
"publish:patch": "npm version patch && npm run pub",
|
"publish:patch": "npm version patch && npm run pub",
|
||||||
"publish:minor": "npm version minor && npm run pub",
|
"publish:minor": "npm version minor && npm run pub",
|
||||||
"check:size": "npm run build && size-limit ./dist/WebComponent.min.js"
|
"check:size": "npm run build && size-limit ./dist/WebComponent.js"
|
||||||
},
|
},
|
||||||
"repository": "https://git.sr.ht/~ayoayco/web-component-base",
|
"repository": "https://git.sr.ht/~ayoayco/web-component-base",
|
||||||
"homepage": "https://git.sr.ht/~ayoayco/web-component-base#readme",
|
"homepage": "https://git.sr.ht/~ayoayco/web-component-base#readme",
|
||||||
|
|
|
@ -30,7 +30,7 @@ export class WebComponent extends HTMLElement {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read-only string property that represents how the component will be rendered
|
* Read-only string property that represents how the component will be rendered
|
||||||
* @returns {string}
|
* @returns {string | Node | (string | Node)[]}
|
||||||
* @see https://www.npmjs.com/package/web-component-base#template-vs-render
|
* @see https://www.npmjs.com/package/web-component-base#template-vs-render
|
||||||
*/
|
*/
|
||||||
get template() {
|
get template() {
|
||||||
|
@ -96,7 +96,7 @@ export class WebComponent extends HTMLElement {
|
||||||
onChanges(changes) {}
|
onChanges(changes) {}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
this.innerHTML = this.template;
|
if (typeof this.template === "string") this.innerHTML = this.template;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue