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
|
||||
*/
|
||||
class HelloWorld extends WebComponent {
|
||||
static properties = ["my-name"];
|
||||
static props = {
|
||||
myName: 'World',
|
||||
count: 0,
|
||||
}
|
||||
|
||||
onInit() {
|
||||
let count = 0;
|
||||
this.onclick = () => this.props.myName = `Clicked ${++count}x`;
|
||||
updateLabel(){
|
||||
this.props.myName = `Clicked ${++this.props.count}x`;
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
class MyHelloWorld extends WebComponent {
|
||||
// tell browser which props to cause render
|
||||
static properties = ["my-name"];
|
||||
|
||||
// Triggered when the component is connected to the DOM
|
||||
onInit() {
|
||||
let count = 0;
|
||||
this.onclick = () => this.props.myName = `Clicked ${++count}x`
|
||||
static props = {
|
||||
myName: 'World',
|
||||
count: 0
|
||||
}
|
||||
|
||||
updateLabel() {
|
||||
this.props.myName = `Clicked ${++this.props.count}x`
|
||||
}
|
||||
|
||||
// give readonly 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>
|
||||
<h2>Welcome to {{ project.name }}</h2>
|
||||
<p>
|
||||
Here's a vanilla custom element:
|
||||
<vanilla-hello-world></vanilla-hello-world>
|
||||
Here's an interactive custom element:
|
||||
<my-hello-world></my-hello-world>
|
||||
</p>
|
||||
<code-block language="js">
|
||||
class HelloWorld extends HTMLElement {
|
||||
static get observedAttributes() {
|
||||
return ["my-name"];
|
||||
class MyHelloWorld extends WebComponent {
|
||||
static props = {
|
||||
myName: "World",
|
||||
count: 0
|
||||
}
|
||||
|
||||
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`);
|
||||
};
|
||||
updateLabel() {
|
||||
this.props.myName = `Clicked ${++this.props.count}x`;
|
||||
}
|
||||
|
||||
attributeChangedCallback(property, previousValue, currentValue) {
|
||||
if (property === "my-name" && previousValue !== currentValue) {
|
||||
this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`;
|
||||
}
|
||||
get template() {
|
||||
return html`
|
||||
<button onClick=${() => this.updateLabel()} style="cursor:pointer">
|
||||
Hello ${this.props.myName}!
|
||||
</button>`;
|
||||
}
|
||||
}
|
||||
</code-block>
|
||||
}</code-block>
|
||||
</main>
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue