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
-    }
-
+ // tell browser which props to cause render + static properties = ["language"]; + + // Triggered after view is initialized + afterViewInit() { + /** + * Provides autocompletion on IDEs when @ts-check is enabled on first line + * @type {HTMLPreElement} */ + const pre = this.querySelector("#pre"); + + // scoped style for pre block + pre.style.background = "#f5f2f0"; + pre.style.padding = "1em"; + pre.style.margin = "1em 0"; + pre.style.fontSize = "1.25em"; + pre.style.overflow = "auto"; + } + + // readonly template + get template() { + const trimmed = this.innerHTML.trim(); + return ` +
${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 @@

Welcome to {{ name }}

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>`;
     }
   }
-}
+}