Created
March 29, 2022 16:48
-
-
Save bobbigmac/bf7f34eff2877709754c04df972d608f to your computer and use it in GitHub Desktop.
Very simple base custom element for 'reactish' rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default class BaseElement extends HTMLElement { | |
| constructor() { | |
| super(); | |
| this.attachShadow({mode: 'open'}); | |
| } | |
| connectedCallback() { | |
| this.setupTemplate(); | |
| this.render() | |
| } | |
| attributeChangedCallback(name, oldValue, newValue) { | |
| //TODO: Diff? | |
| // console.log('changed in base', { name, oldValue, newValue }) | |
| this.setupTemplate(); | |
| this.render(); | |
| } | |
| bindEvents(events) { | |
| Object.keys(events || {}).map(selector => { | |
| Object.keys(events[selector] || {}).map((eventName) => { | |
| Array.from(this.shadowRoot.querySelectorAll(selector)).map(node => { | |
| // console.log('binding', eventName, selector, events[selector][eventName], this, node); | |
| node.addEventListener(eventName, events[selector][eventName].bind(this)); | |
| }); | |
| }); | |
| }); | |
| } | |
| setupTemplate() { | |
| let template = document.querySelector('template.article'); | |
| if(!template) { | |
| template = document.createElement('template'); | |
| template.innerHTML = this.markup(this); | |
| } | |
| this.templateContent = template.content; | |
| } | |
| static get observedAttributes() { | |
| // console.log('observing', this.observe) | |
| return this.observe || []; | |
| } | |
| bindAttribute(key, def) { | |
| if(!Object.hasOwnProperty(this, key)) { | |
| Object.defineProperty(this, key, { | |
| get: function () { | |
| return this.getAttribute(key); | |
| }, | |
| set: function (val) { | |
| this.setAttribute(key, val); | |
| } | |
| }); | |
| this[key] = def; | |
| } | |
| } | |
| render() { | |
| while (this.shadowRoot.firstChild) { | |
| this.shadowRoot.removeChild(this.shadowRoot.lastChild); | |
| } | |
| this.shadowRoot.appendChild( | |
| this.templateContent.cloneNode(true) | |
| ); | |
| this.bindEvents(this.events) | |
| this.onRendered && this.onRendered(); | |
| } | |
| markup(props) { return `<div>BaseElement</div>` } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import BaseElement from './base.js'; | |
| export default class ArticleInnerComponent extends BaseElement { | |
| static observe = ['subtitle', 'counter'] | |
| constructor() { | |
| super(); | |
| this.bindAttribute('counter', 0) | |
| this.bindAttribute('subtitle', 'Default Subtitle') | |
| } | |
| onRendered() { | |
| this.delay = this.delay || (Math.random() * 2000); | |
| // console.log('onRendered', this) | |
| clearTimeout(this.timeout) | |
| this.timeout = setTimeout(() => { | |
| this.counter++; | |
| }, 1000 + this.delay) | |
| } | |
| testClickHandler(event) { | |
| // console.log('Clicked', { event }) | |
| this.counter++; | |
| } | |
| events = { | |
| 'button.test': { | |
| 'click': (event) => this.testClickHandler(event) | |
| } | |
| } | |
| markup(props) { return ` | |
| <style> | |
| .subtitle { | |
| color: green; | |
| } | |
| </style> | |
| <h3 class="subtitle">Subtitle: <slot name="subtitle">${props.subtitle}</slot></h3> | |
| <div><slot name="counter">Inner Counter: ${props.counter}</slot></div> | |
| <button class="test">Test</button> | |
| <slot name="junk"></slot> | |
| `} | |
| } | |
| if(typeof customElements !== 'undefined') { | |
| customElements.define("article-inner", ArticleInnerComponent); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment