feat(site, templates): use new WebComponent.io features
This commit is contained in:
parent
1cafa7326f
commit
0f023b1219
5 changed files with 554 additions and 364 deletions
817
package-lock.json
generated
817
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -3,14 +3,19 @@
|
||||||
* @see https://WebComponent.io
|
* @see https://WebComponent.io
|
||||||
*/
|
*/
|
||||||
class HelloWorld extends WebComponent {
|
class HelloWorld extends WebComponent {
|
||||||
static properties = ["my-name"];
|
static props = {
|
||||||
|
myName: 'World',
|
||||||
onInit() {
|
count: 0,
|
||||||
let count = 0;
|
|
||||||
this.onclick = () => this.props.myName = `Clicked ${++count}x`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateLabel(){
|
||||||
|
this.props.myName = `Clicked ${++this.props.count}x`;
|
||||||
|
}
|
||||||
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<button style="cursor:pointer">Hello ${this.props.myName ?? 'World'}!</button>`;
|
return html`
|
||||||
|
<button onClick=${() => this.updateLabel()} style="cursor:pointer">
|
||||||
|
Hello ${this.props.myName}!
|
||||||
|
</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,21 @@
|
||||||
/**
|
/**
|
||||||
* Custom element using a minimal Web Component Base class
|
* Custom element using a minimal base class
|
||||||
* @see https://WebComponent.io
|
* @see https://WebComponent.io
|
||||||
*/
|
*/
|
||||||
class MyHelloWorld extends WebComponent {
|
class MyHelloWorld extends WebComponent {
|
||||||
// tell browser which props to cause render
|
static props = {
|
||||||
static properties = ["my-name"];
|
myName: 'World',
|
||||||
|
count: 0
|
||||||
// Triggered when the component is connected to the DOM
|
}
|
||||||
onInit() {
|
|
||||||
let count = 0;
|
updateLabel() {
|
||||||
this.onclick = () => this.props.myName = `Clicked ${++count}x`
|
this.props.myName = `Clicked ${++this.props.count}x`
|
||||||
}
|
}
|
||||||
|
|
||||||
// give readonly template
|
|
||||||
get template() {
|
get template() {
|
||||||
return `<button style="cursor:pointer">Hello ${this.props.myName ?? 'World'}!</button>`;
|
return html`
|
||||||
|
<button onClick=${() => this.updateLabel()} style="cursor:pointer">
|
||||||
|
Hello ${this.props.myName}!
|
||||||
|
</button>`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
class HelloWorld extends HTMLElement {
|
|
||||||
static get observedAttributes() {
|
|
||||||
return ["my-name"];
|
|
||||||
}
|
|
||||||
|
|
||||||
connectedCallback() {
|
|
||||||
let count = 0;
|
|
||||||
const currentName = this.getAttribute('my-name');
|
|
||||||
|
|
||||||
if (!currentName) {
|
|
||||||
this.setAttribute('my-name', 'World')
|
|
||||||
}
|
|
||||||
|
|
||||||
this.onclick = () => this.setAttribute("my-name", `Clicked ${++count}x`);
|
|
||||||
}
|
|
||||||
|
|
||||||
attributeChangedCallback(property, previousValue, currentValue) {
|
|
||||||
if (property === "my-name" && previousValue !== currentValue) {
|
|
||||||
this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -25,38 +25,32 @@
|
||||||
<main>
|
<main>
|
||||||
<h2>Welcome to {{ project.name }}</h2>
|
<h2>Welcome to {{ project.name }}</h2>
|
||||||
<p>
|
<p>
|
||||||
Here's a vanilla custom element:
|
Here's an interactive custom element:
|
||||||
<vanilla-hello-world></vanilla-hello-world>
|
<my-hello-world></my-hello-world>
|
||||||
</p>
|
</p>
|
||||||
<code-block language="js">
|
<code-block language="js">
|
||||||
class HelloWorld extends HTMLElement {
|
class MyHelloWorld extends WebComponent {
|
||||||
static get observedAttributes() {
|
static props = {
|
||||||
return ["my-name"];
|
myName: "World",
|
||||||
|
count: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
updateLabel() {
|
||||||
let count = 0;
|
this.props.myName = `Clicked ${++this.props.count}x`;
|
||||||
const currentName = this.getAttribute("my-name");
|
|
||||||
|
|
||||||
if (!currentName) {
|
|
||||||
this.setAttribute("my-name", "World")
|
|
||||||
}
|
|
||||||
|
|
||||||
this.onclick = () => {
|
|
||||||
this.setAttribute("my-name", `Clicked ${++count}x`);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
attributeChangedCallback(property, previousValue, currentValue) {
|
get template() {
|
||||||
if (property === "my-name" && previousValue !== currentValue) {
|
return html`
|
||||||
this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`;
|
<button onClick=${() => this.updateLabel()} style="cursor:pointer">
|
||||||
}
|
Hello ${this.props.myName}!
|
||||||
|
</button>`;
|
||||||
}
|
}
|
||||||
}
|
}</code-block>
|
||||||
</code-block>
|
|
||||||
</main>
|
</main>
|
||||||
<my-footer>
|
<my-footer>
|
||||||
<small>A project by <a href="{{ author.url }}">{{ author.name }}</a></small>
|
<small>
|
||||||
|
Learn how to build components easily at <a href="https://WebComponent.io">WebComponent.io</a>
|
||||||
|
</small>
|
||||||
</my-footer>
|
</my-footer>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in a new issue