Skip to content

Commit a14dea4

Browse files
committed
Improve custom elements guide
1 parent 0f392a3 commit a14dea4

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/components_guide_web/controllers/wasm_html/custom_elements.html.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ The element lets you use a WebAssembly instance that renders HTML. It loads your
1010
- a `to_html()` function that returns the memory offset to your built HTML. This will be called on each render.
1111
- an optional `free_all()` function that is called at the start of each render, used to free memory if you need.
1212

13-
If your rendered HTML includes a `<button>` with a `data-action` attribute, then a click listener will be added. The value of this attribute if set to the name of an exported function, will be called every time the button is clicked. The HTML will also be re-rendered for you.
13+
Your WebAssembly instance can also hold internal state in its globals variables and in memory. This allows it to be stateful, rendering different HTML depending on the state.
1414

15-
For example a `<button data-action="increment">Increment counter</button>` will call the `increment()` function you export, plus call your `to_html()` function, allowing you to re-render say a counter from `<output>1</output>` to `<output>2</output>`. Note: you must render the button each time: this allow you to change which buttons are available depending on your internal state.
15+
If your rendered HTML includes a `<button>` with a `data-action` attribute, then a click listener will be added. The value of this attribute if set to the name of an exported function, will be called every time the button is clicked. The HTML will also be re-rendered for you by calling `to_html()` again.
16+
17+
For example a `<button data-action="increment">Increment counter</button>` will call the `increment()` function you export, plus call your `to_html()` function, allowing you to re-render say a counter from `<output>1</output>` to `<output>2</output>`. Note: you must render your buttons every time: this allow you to change which buttons are available depending on your internal state.
1618

1719
### Usage
1820

@@ -66,9 +68,13 @@ async function initWasmHTML(el, wasmModulePromise) {
6668
memoryIO = new MemoryIO(instance.exports);
6769
const { to_html: toHTML, free_all: freeAll } = instance.exports;
6870

71+
// Used to render.
6972
function update() {
73+
// Optionally free.
7074
freeAll?.apply();
75+
// Read the current HTML.
7176
const html = memoryIO.readString(toHTML());
77+
// Replace all HTML inside the custom element.
7278
el.innerHTML = html;
7379
}
7480

@@ -84,6 +90,8 @@ customElements.define("wasm-html", WasmHTML);
8490
8591
## Event listeners
8692
93+
These are some starter event listeners. Any DOM event is possible, it‘s up to you to decide which properties from the event you want and the convention for passing their data to your WebAssembly instance. Usually a specially named exported function makes sense.
94+
8795
```js
8896
function addEventListenersToWasmInstance(instance, update) {
8997
el.addEventListener("click", (event) => {
@@ -132,6 +140,8 @@ function addEventListenersToWasmInstance(instance, update) {
132140
133141
## MemoryIO
134142
143+
This helper class is used to read and write UTF-8 strings from a WebAssembly module’s main memory.
144+
135145
```js
136146
const utf8Encoder = new TextEncoder();
137147
const utf8Decoder = new TextDecoder();

0 commit comments

Comments
 (0)