From e3177c7534e26430404a0cc67073987c9b12f99e Mon Sep 17 00:00:00 2001 From: StoneyDSP Date: Thu, 17 Oct 2024 07:09:51 +0200 Subject: [PATCH] added index to README --- README.md | 135 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 108 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 844e266..7dfa4c7 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ Step by step! - [`'app-component': AppComponent`](#app-component-appcomponent) - [`AppComponent.render()`](#appcomponentrender) - [`AppComponent.render(innerHTML)`](#appcomponentrenderinnerhtml) +- [`AppComponent.setup()`](#appcomponentsetup) +- [`createElement('app-component')`](#createelementapp-component) +- [`App()`](#app) +- [`render(App)`](#renderapp) - [``](#app-component) - [Tips](#tips) - [Further Reading](#further-reading) @@ -162,6 +166,7 @@ class AppComponent extends HTMLElement { ### `CustomElementRegistry` ```ts +// IMPORTANT window.customElements.define('app-component', AppComponent); ``` @@ -170,14 +175,14 @@ window.customElements.define('app-component', AppComponent); ### `'app-component': AppComponent` ```ts -window.customElements.define('app-component', +window.customElements.define('app-component', // <-- wrap the class! class AppComponent extends HTMLElement { constructor() { super(); this.innerHTML = `Your app goes here`; } } -); +); // <-- '.define()' ends here! ``` --- @@ -187,15 +192,17 @@ window.customElements.define('app-component', ### `AppComponent.render()` ```ts -class AppComponent extends HTMLElement { - constructor() { - super(); - this.innerHTML = this.render(); - } - render() { - return `Your app goes here`; +window.customElements.define('app-component', + class AppComponent extends HTMLElement { + constructor() { + super(); + this.innerHTML = this.render(); + } + render() { + return `Your app goes here`; + } } -} +); ``` --- @@ -203,15 +210,17 @@ class AppComponent extends HTMLElement { ### `AppComponent.render(innerHTML)` ```ts -class AppComponent extends HTMLElement { - constructor() { - super(); - this.innerHTML = this.render('Your app goes here'); - } - render(innerHTML: string): string { - return `${innerHTML}`; +window.customElements.define('app-component', + class AppComponent extends HTMLElement { + constructor() { + super(); + this.innerHTML = this.render('Your app goes here'); + } + render(innerHTML: string): string { + return `${innerHTML}`; + } } -} +); ``` --- @@ -219,18 +228,90 @@ class AppComponent extends HTMLElement { ### `AppComponent.setup()` ```ts -class AppComponent extends HTMLElement { - constructor() { - super(); - this.setup(); - } - setup(): void { - this.innerHTML = this.render('Your app goes here'); +window.customElements.define('app-component', + class AppComponent extends HTMLElement { + constructor() { + super(); + this.setup(); + } + setup(): void { + this.innerHTML = this.render('Your app goes here'); + } + render(innerHTML: string): string { + return `${innerHTML}`; + } } - render(innerHTML: string): string { - return `${innerHTML}`; +); +``` + +--- + +### `createElement('app-component')` + +```ts +window.customElements.define('app-component', + class AppComponent extends HTMLElement { + constructor() { + super(); + this.setup(); + } + setup(): void { + this.innerHTML = this.render('Your app goes here'); + } + render(innerHTML: string): string { + return `${innerHTML}`; + } } +); + +const app = document.createElement('app-component'); +``` + +--- + +### `App()` + +```ts +const App = () => { + // define the component + window.customElements.define('app-component', + class AppComponent extends HTMLElement { + constructor() { + super(); + this.setup(); + } + setup(): void { + this.innerHTML = this.render('Your app goes here'); + } + render(innerHTML: string): string { + return `${innerHTML}`; + } + } + ); + // then return it + return document.createElement('app-component'); +} + +// Now we can assign it :) +const app = App(); +``` + +--- + +### `render(App)` + +```ts +// src/index.ts + +import App = require('./App'); + +const render = (element: () => HTMLElement) = { + // ...attaches passed-in element to document } + +// so, pass it our App :) +render(App) + ``` ---