diff --git a/templates/basic/src/components/code-block.js b/templates/basic/src/components/code-block.js index 76bbf80..a9905d8 100644 --- a/templates/basic/src/components/code-block.js +++ b/templates/basic/src/components/code-block.js @@ -1,19 +1,38 @@ -class CodeBlockComponent extends HTMLElement { - connectedCallback() { - this.trimmed = this.innerHTML.trim(); - const lang = this.getAttribute("language"); - const lineNumbers = this.getAttribute("line-numbers") === "true"; +// @ts-check - console.log(lineNumbers, lang); +/** + * Custom element using a minimal Web Component Base class + * @see https://ayco.io/n/web-component-base + */ +class CodeBlockComponent extends WebComponent { + // initialize props + language; - this.innerHTML = ` -
${
- this.trimmed
- }
- ${trimmed}
`;
}
}
diff --git a/templates/basic/src/components/my-hello-world.js b/templates/basic/src/components/my-hello-world.js
index 873917b..58d1ede 100644
--- a/templates/basic/src/components/my-hello-world.js
+++ b/templates/basic/src/components/my-hello-world.js
@@ -3,15 +3,22 @@
* @see https://ayco.io/n/web-component-base
*/
class MyHelloWorld extends WebComponent {
+ // intialize props
+ dataName = 'World';
+
+ // tell browser which props to cause render
static properties = ["data-name"];
+ // Triggered when the component is connected to the DOM
onInit() {
let count = 0;
+ // initialize event handler
this.onclick = () => {
this.setAttribute("data-name", `Clicked ${++count}x`);
};
}
+ // give readonly template
get template() {
/**
* any attribute/property in kebab-case...
diff --git a/templates/basic/src/components/vanilla-hello-world.js b/templates/basic/src/components/vanilla-hello-world.js
deleted file mode 100644
index 12a1564..0000000
--- a/templates/basic/src/components/vanilla-hello-world.js
+++ /dev/null
@@ -1,18 +0,0 @@
-class HelloWorld extends HTMLElement {
- static get observedAttributes() {
- return ["data-name"];
- }
-
- connectedCallback() {
- let count = 0;
- this.onclick = () => {
- this.setAttribute("data-name", `Clicked ${++count}x`);
- };
- }
-
- attributeChangedCallback(property, previousValue, currentValue) {
- if (property === "data-name" && previousValue !== currentValue) {
- this.innerHTML = ``;
- }
- }
-}
diff --git a/templates/basic/src/pages/index.html b/templates/basic/src/pages/index.html
index ae2c8a3..a316af5 100644
--- a/templates/basic/src/pages/index.html
+++ b/templates/basic/src/pages/index.html
@@ -22,10 +22,9 @@
Here's a vanilla custom element:
-
class HelloWorld extends HTMLElement { static get observedAttributes() { return ["data-name"]; @@ -43,8 +42,7 @@ class HelloWorld extends HTMLElement { this.innerHTML = `<button style="cursor:pointer">Hello ${currentValue}!</button>`; } } -}+}