Skip to content

Commit 29ccb77

Browse files
committed
feat: render resolved string
1 parent a202457 commit 29ccb77

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"imports": {
2323
"@b-fuze/deno-dom": "jsr:@b-fuze/deno-dom@^0.1.47",
24-
"@std/assert": "jsr:@std/assert@^0.226.0"
24+
"@std/assert": "jsr:@std/assert@^0.226.0",
25+
"@std/async": "jsr:@std/async@^0.224.2"
2526
}
2627
}

deno.lock

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mod.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface Context<EL = HTMLElement> {
4747
/** The component type */
4848
export type Component<EL extends HTMLElement> = (
4949
ctx: Context<EL>,
50-
) => string | undefined | void;
50+
) => string | undefined | void | PromiseLike<void | string>;
5151

5252
/** The event handler type */
5353
export type EventHandler = (e: Event) => void;
@@ -195,6 +195,12 @@ export function register<EL extends HTMLElement>(
195195
const html = component(context);
196196
if (typeof html === "string") {
197197
el.innerHTML = html;
198+
} else if (html && typeof html.then === "function") {
199+
html.then((html) => {
200+
if (typeof html === "string") {
201+
el.innerHTML = html;
202+
}
203+
});
198204
}
199205
}
200206
};

test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2022 Yoshiya Hinosawa. All rights reserved. MIT license.
22

33
import { assert, assertEquals, assertExists, assertThrows } from "@std/assert";
4+
import { delay } from "@std/async";
45
import "./dom_polyfill.ts";
56
import { type Context, mount, register, unmount } from "./mod.ts";
67

@@ -393,3 +394,14 @@ Deno.test("Returned string from Component is rendered", () => {
393394
register(Component, name);
394395
assertEquals(queryByClass(name).innerHTML, "<p>hello</p>");
395396
});
397+
398+
Deno.test("Resolved string from Component is rendered as innerHTML", async () => {
399+
const name = randomName();
400+
document.body.innerHTML = `<div class="${name}"><div>`;
401+
function Component() {
402+
return Promise.resolve("<p>hello</p>");
403+
}
404+
register(Component, name);
405+
await delay(100);
406+
assertEquals(queryByClass(name).innerHTML, "<p>hello</p>");
407+
});

0 commit comments

Comments
 (0)