From 7cfef1b9c8de31c7d8645db3dbe4973a0ccc1dd3 Mon Sep 17 00:00:00 2001 From: Ayo Date: Sun, 17 Sep 2023 02:05:12 +0200 Subject: [PATCH] feat: onInit hook --- README.md | 54 +++++++++++++++++++++++++++++---------------- WebComponent.mjs | 6 +++++ demo/SimpleText.mjs | 8 +++---- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 7326db5..4f428f0 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,17 @@ -# Web Component Base - +Web Component Base +--- This is a very minimal base class for creating reactive custom elements easily. When you extend the `WebComponent` class for your component, you only have to define the `template()` and `observedAttributes()`, and changes in any attribute value will automatically cause the UI to render. The result is a reactive UI on attribute changes. -Optionally, you can -1. define a method [`onChanges`](#hooks) that gets triggered when an attribute value changed +## Table of Contents +1. [Installation](#installation) +1. [Usage](#usage) +1. [Life-Cycle Hooks](#life-cycle-hooks) + 1. [`onInit`](#oninit) - the component is connected to the DOM + 1. [`onChanges`](#onchanges) - an attribute value changed ## Installation @@ -64,28 +68,40 @@ The result is a reactive UI that updates on attribute changes: UI showing feeling toward Web Components changing from SAD to EXCITED -### Hooks +## Life-Cycle Hooks -Currently, you can define behavior when an attribute value changes by defining a method `onChanges` in your component: +Define behavior when certain events in the component's life cycle is triggered by providing hook methods + +### onInit() +- gets triggered when the component is connected to the DOM ```js import WebComponent from "../index.mjs"; -export class HelloWorld extends WebComponent { - name = "World"; - emotion = "excited"; - - static get observedAttributes() { - return ["name", "emotion"]; - } - - onChanges({ previousValue, currentValue }) { - console.log(">>> changed", { previousValue, currentValue }); +class ClickableText extends WebComponent { + onInit() { + this.onclick = () => console.log(">>> click!"); } get template() { - return ` -

Hello ${this.name}${this.emotion === "sad" ? ". 😭" : "! 🙌"}

`; + return `Click me!`; } } -``` \ No newline at end of file +``` + +### onChanges() +- gets triggered when an attribute value changed + +```js +import WebComponent from "../index.mjs"; + +class ClickableText extends WebComponent { + onChanges({prev, curr}) { + console.log('>>> something changed', prev, curr) + } + + get template() { + return `Click me!`; + } +} +``` diff --git a/WebComponent.mjs b/WebComponent.mjs index 3ef10c5..78fbf92 100644 --- a/WebComponent.mjs +++ b/WebComponent.mjs @@ -5,6 +5,11 @@ export class WebComponent extends HTMLElement { return ""; } + /** + * triggered when the component is connected to the DOM + */ + onInit() {} + /** * triggered when an attribute value changed */ @@ -12,6 +17,7 @@ export class WebComponent extends HTMLElement { connectedCallback() { this.render(); + this.onInit(); } attributeChangedCallback(property, previousValue, currentValue) { diff --git a/demo/SimpleText.mjs b/demo/SimpleText.mjs index b866872..afc6506 100644 --- a/demo/SimpleText.mjs +++ b/demo/SimpleText.mjs @@ -2,14 +2,12 @@ import WebComponent from "../index.mjs"; class SimpleText extends WebComponent { - greeting = "Hello"; - - static get observedAttributes() { - return ["greeting"]; + onInit() { + this.onclick = () => console.log(">>> click!"); } get template() { - return `

Simple text ${this.greeting}

`; + return `Click me!`; } }