refactor: add return types; organize code
This commit is contained in:
parent
2affb0697c
commit
74e7c16335
1 changed files with 32 additions and 23 deletions
|
@ -4,7 +4,7 @@
|
||||||
* @author Ayo Ayco <https://ayo.ayco.io>
|
* @author Ayo Ayco <https://ayo.ayco.io>
|
||||||
* @see https://www.npmjs.com/package/web-component-base#readme
|
* @see https://www.npmjs.com/package/web-component-base#readme
|
||||||
* @example
|
* @example
|
||||||
* ```js
|
*
|
||||||
* import WebComponent from "https://unpkg.com/web-component-base/index.js";
|
* import WebComponent from "https://unpkg.com/web-component-base/index.js";
|
||||||
*
|
*
|
||||||
* class HelloWorld extends WebComponent {
|
* class HelloWorld extends WebComponent {
|
||||||
|
@ -20,28 +20,23 @@
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* customElements.define('hello-world', HelloWorld);
|
* customElements.define('hello-world', HelloWorld);
|
||||||
* ```
|
|
||||||
*/
|
*/
|
||||||
export class WebComponent extends HTMLElement {
|
export class WebComponent extends HTMLElement {
|
||||||
/**
|
/**
|
||||||
* Array of strings that tells the browsers which attributes will cause a render
|
* Array of strings that tells the browsers which attributes will cause a render
|
||||||
* @type Array<string>
|
* @type {Array<string>}
|
||||||
*/
|
*/
|
||||||
static properties = [];
|
static properties = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read-only string property that represents how the component will be rendered
|
* Read-only string property that represents how the component will be rendered
|
||||||
* @returns string
|
* @returns {string}
|
||||||
* @see https://www.npmjs.com/package/web-component-base#template-vs-render
|
* @see https://www.npmjs.com/package/web-component-base#template-vs-render
|
||||||
*/
|
*/
|
||||||
get template() {
|
get template() {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
static get observedAttributes() {
|
|
||||||
return this.properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read-only property containing camelCase counterparts of observed attributes.
|
* Read-only property containing camelCase counterparts of observed attributes.
|
||||||
* This works like HTMLElement.dataset except dataset is only for attributes prefixed with `data-`.
|
* This works like HTMLElement.dataset except dataset is only for attributes prefixed with `data-`.
|
||||||
|
@ -65,6 +60,9 @@ export class WebComponent extends HTMLElement {
|
||||||
return this.#props;
|
return this.#props;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {DOMStringMap}
|
||||||
|
*/
|
||||||
#props;
|
#props;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -77,34 +75,44 @@ export class WebComponent extends HTMLElement {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggered after view is initialized. Best for querying DOM nodes that will only exist after render.
|
* Triggered after view is initialized. Best for querying DOM nodes that will only exist after render.
|
||||||
* @returns void
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
afterViewInit() {}
|
afterViewInit() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggered when the component is connected to the DOM. Best for initializing the component like attaching event handlers.
|
* Triggered when the component is connected to the DOM. Best for initializing the component like attaching event handlers.
|
||||||
* @returns void
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
onInit() {}
|
onInit() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggered when the component is disconnected from the DOM. Any initialization done in `onInit` must be undone here.
|
* Triggered when the component is disconnected from the DOM. Any initialization done in `onInit` must be undone here.
|
||||||
* @returns void
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
onDestroy() {}
|
onDestroy() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Triggered when an attribute value changes
|
* Triggered when an attribute value changes
|
||||||
* @typedef {{
|
* @param {{
|
||||||
* property: string,
|
* property: string,
|
||||||
* previousValue: any,
|
* previousValue: any,
|
||||||
* currentValue: any
|
* currentValue: any
|
||||||
* }} Changes
|
* }} changes
|
||||||
* @param {Changes} changes
|
* @returns {void}
|
||||||
* @returns void
|
|
||||||
*/
|
*/
|
||||||
onChanges(changes) {}
|
onChanges(changes) {}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
this.innerHTML = this.template;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* start HTMLElement callbacks
|
||||||
|
*/
|
||||||
|
static get observedAttributes() {
|
||||||
|
return this.properties;
|
||||||
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.onInit();
|
this.onInit();
|
||||||
this.render();
|
this.render();
|
||||||
|
@ -133,10 +141,6 @@ export class WebComponent extends HTMLElement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
|
||||||
this.innerHTML = this.template;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a kebab-cased string into camelCaps
|
* Converts a kebab-cased string into camelCaps
|
||||||
* @param {string} kebab string in kebab-case
|
* @param {string} kebab string in kebab-case
|
||||||
|
@ -148,6 +152,9 @@ export class WebComponent extends HTMLElement {
|
||||||
|
|
||||||
#handler = (el) => ({
|
#handler = (el) => ({
|
||||||
set(obj, prop, newval) {
|
set(obj, prop, newval) {
|
||||||
|
const oldval = obj[prop];
|
||||||
|
console.log(">>> old value:", oldval, typeof oldval);
|
||||||
|
|
||||||
obj[prop] = newval;
|
obj[prop] = newval;
|
||||||
|
|
||||||
const kebabize = (str) =>
|
const kebabize = (str) =>
|
||||||
|
@ -156,13 +163,15 @@ export class WebComponent extends HTMLElement {
|
||||||
($, ofs) => (ofs ? "-" : "") + $.toLowerCase()
|
($, ofs) => (ofs ? "-" : "") + $.toLowerCase()
|
||||||
);
|
);
|
||||||
|
|
||||||
const kebab = kebabize(prop);
|
if (JSON.stringify(oldval) !== newval) {
|
||||||
el.setAttribute(kebab, newval);
|
console.log(oldval, newval);
|
||||||
|
const kebab = kebabize(prop);
|
||||||
|
el.setAttribute(kebab, newval);
|
||||||
|
}
|
||||||
|
|
||||||
// Indicate success
|
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default WebComponent;
|
export default WebComponent; // remove on v2
|
||||||
|
|
Loading…
Reference in a new issue