Skip to content

Commit fbe3feb

Browse files
add ui
1 parent 6657204 commit fbe3feb

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<script>
2+
import { onMount, tick } from 'svelte';
3+
import * as wurbo from 'wurbo';
4+
5+
// Import wasm component bytes as a url
6+
import wasmURL from '../../../aggregate.wasm?url';
7+
8+
// get imports from +page.svelte
9+
export let importables;
10+
export let load;
11+
12+
/**
13+
* The rendered component as a string of HTML
14+
* @type {string | null}
15+
*/
16+
let renderedHTML;
17+
/**
18+
* The module that loads the WebAssembly component
19+
*
20+
* @type {{ render: (arg0: string) => string | null; listen: () => void; }}
21+
*/
22+
let mod;
23+
24+
onMount(async () => {
25+
// ensure you are in the Browser environment to rollup your WIT Component
26+
// const { load } = await import('rollup-plugin-wit-component');
27+
28+
let listener = new wurbo.Listener();
29+
30+
// get your wasm bytes from your storage source
31+
let wasmBytes = await fetch(wasmURL).then((res) => res.arrayBuffer());
32+
33+
// define the import handles you are giving to your component
34+
let all_importables = [
35+
{ 'component:wurbo/wurbo-in': importables.buildCodeString(listener.namespace) },
36+
{
37+
'component:wallet/[email protected]': importables.buildGetSeedFunc(
38+
'build a test seed from this string'
39+
)
40+
}
41+
];
42+
43+
// load the import handles into the Wasm component and get the ES module returned
44+
mod = await load(wasmBytes, all_importables);
45+
46+
// call `render` with your inputs for the component
47+
let data = {
48+
tag: 'all-content',
49+
val: {
50+
page: { title: 'Use that seed to sign something.' },
51+
input: { placeholder: 'Enter a Signable Message here.' }
52+
}
53+
};
54+
renderedHTML = mod.wurboOut.render(data);
55+
56+
// lisen for events from the component
57+
listener.listen(mod);
58+
});
59+
60+
// Once the HTML is rendered and the module is loaded, we can activate the event emitters
61+
$: if (renderedHTML && mod)
62+
(async () => {
63+
// wait for the DOM to reflect the updates first
64+
await tick();
65+
// once the DOM has our elements loaded, we can activate the event emitters
66+
mod.wurboOut.activate();
67+
})();
68+
</script>
69+
70+
<svelte:head>
71+
<title>Seed Keeper</title>
72+
<script src="https://cdn.tailwindcss.com"></script>
73+
</svelte:head>
74+
<div>
75+
<h1>Aggregate User Interfaces</h1>
76+
{#if renderedHTML}
77+
{@html renderedHTML}
78+
{/if}
79+
</div>

examples/sveltekit/src/routes/importables.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ export function buildCodeString(namespace) {
1010
value: e.target.value,
1111
}
1212
};
13+
// Build nested Contexts, recursively find all ancestor elements with data-slot attributes, wrap child context as value.
14+
// ie) {tag: slotNameGrandParent, value: {tag: slotNameParent, value: {tag: 'input', val: {value: 'foo'}}}}
15+
let el = e.target.closest('[data-slot]');
16+
while (el) {
17+
ctx = { tag: el.dataset.slot, val: ctx };
18+
el = el.closest('[data-slot]');
19+
}
20+
console.log(ctx);
21+
1322
bc.postMessage(window.${namespace}.render(ctx));
1423
});
1524
}`;

0 commit comments

Comments
 (0)