Skip to content

Commit 15cfeda

Browse files
author
James Hill
committed
Added WebComponent instructions to README
1 parent 8af7a38 commit 15cfeda

4 files changed

Lines changed: 55 additions & 6 deletions

File tree

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
A super fast and lightweight (**971bytes** gzipped) utility function to build HTML node trees, and stateful functional components. Can be used either directly via the `h` function, or from transpiled `JSX`.
1+
A super fast and lightweight (**971bytes** gzipped) utility function to build HTML node trees and stateful functional components. Can be used either directly via the `h` function, or from transpiled `JSX`.
2+
3+
Due to it's size, `hypnode` is a useful companion utility for writing _WebComponents_, simplifying `HTML` structures, element references and state management. See section entitled "WebComponents" for an example.
24

35
# Getting Started
46

@@ -220,6 +222,45 @@ function Button({ buttonText }) {
220222

221223
You provide mutations to your state via the `setState` function, this accepts a _new_ value you wish to assign. Whenever this function is called, the component will be re-rendered.
222224

225+
## WebComponents
226+
227+
Creating and managing complex HTML structures using _WebComponents_ can become tricky as their size increases. `hypnode` simplifies this by incorporating a familiar component and state pattern into your custom elements. You can find more info on _WebComponents_ [here](https://www.webcomponents.org/introduction). As `hypnode` is fast and lightweight, _WebComponents_ can be more easily shared between projects without significant dependency overhead.
228+
229+
A quick example can be found below:
230+
231+
```
232+
// myComponent.tsx (TypeScript + JSX)
233+
234+
import { h, useState, State } from 'hypnode';
235+
...
236+
class MyComponent extends HTMLElement {
237+
private state: State<number>;
238+
239+
public connectedCallback() {
240+
const Button = () => this.renderButton();
241+
242+
this.appendChild(<Button />);
243+
}
244+
245+
private renderButton = () => {
246+
const [counter] = (this.state = useState(0));
247+
248+
return (
249+
<div class="my-component">
250+
<p>Counter: {counter}</p>
251+
<button onClick={this.onClick}>Click Here</button>
252+
</div>
253+
);
254+
}
255+
256+
private onClick = (ev: Event) => {
257+
const [counter, setState] = this.state;
258+
259+
setState(counter + 1);
260+
}
261+
}
262+
```
263+
223264
# SSR
224265

225266
`hypnode` provides a fallback Virtual `DOM` representation for server side rendering (universal rendering). You can use this output to generate a complete `HTML` representation of your app. This output depends on a `window.document` reference being undefined, if it is, `hypnode` will return a virtual `DOM` object. A prebuilt utility that can convert this into an `HTML` string can be found here: [`hypnode-server`](https://github.com/jhdevuk/hypnode-server)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hypnode",
3-
"version": "1.1.6",
3+
"version": "1.1.7",
44
"description": "",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IAttrs } from './attributes';
2-
import { useState, setElement, setIndex } from './useState';
2+
import { State, useState, setElement, setIndex } from './useState';
33
import { IVNode, virtualDom } from './virtualDom';
44

55
/* -----------------------------------
@@ -215,4 +215,4 @@ function render(root: HTMLElement, output: HTMLElement) {
215215
*
216216
* -------------------------------- */
217217

218-
export { Hypnode, Tag, IVNode, h, useState, render };
218+
export { Hypnode, Tag, State, IVNode, h, useState, render };

src/useState.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ interface IContext {
2323
};
2424
}
2525

26+
/* -----------------------------------
27+
*
28+
* IState
29+
*
30+
* -------------------------------- */
31+
32+
type State<T> = [T, (value: T) => void];
33+
2634
/* -----------------------------------
2735
*
2836
* Variables
@@ -39,7 +47,7 @@ let callRender: number = null;
3947
*
4048
* -------------------------------- */
4149

42-
function useState(initial: any) {
50+
function useState<T>(initial: T): State<T> {
4351
let index = callIndex;
4452
let state = initial;
4553

@@ -128,4 +136,4 @@ function reRender(index: number) {
128136
*
129137
* -------------------------------- */
130138

131-
export { useState, setIndex, setElement };
139+
export { State, useState, setIndex, setElement };

0 commit comments

Comments
 (0)