|
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. |
2 | 4 |
|
3 | 5 | # Getting Started |
4 | 6 |
|
@@ -220,6 +222,45 @@ function Button({ buttonText }) { |
220 | 222 |
|
221 | 223 | 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. |
222 | 224 |
|
| 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 | + |
223 | 264 | # SSR |
224 | 265 |
|
225 | 266 | `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) |
|
0 commit comments