diff options
author | 2021-03-15 13:22:05 -0400 | |
---|---|---|
committer | 2021-03-15 13:22:05 -0400 | |
commit | af6b029e95e9c98e6fb9c642915d461b8d7f448e (patch) | |
tree | d70972e10884de0363e7ce4dd2a0765dafcf3dc8 /src/frontend/render/preact.ts | |
download | astro-af6b029e95e9c98e6fb9c642915d461b8d7f448e.tar.gz astro-af6b029e95e9c98e6fb9c642915d461b8d7f448e.tar.zst astro-af6b029e95e9c98e6fb9c642915d461b8d7f448e.zip |
initial commit
Diffstat (limited to 'src/frontend/render/preact.ts')
-rw-r--r-- | src/frontend/render/preact.ts | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/frontend/render/preact.ts b/src/frontend/render/preact.ts new file mode 100644 index 000000000..eb588209c --- /dev/null +++ b/src/frontend/render/preact.ts @@ -0,0 +1,27 @@ +import render from 'preact-render-to-string'; +import { h } from 'preact'; +import type { Component } from 'preact'; + +export function __preact_static(PreactComponent: Component) { + return (attrs: Record<string, any>, ...children: any): string => { + let html = render( + h( + PreactComponent as any, // Preact's types seem wrong... + attrs, + children + ) + ); + return html; + }; +} + +export function __preact_dynamic(PreactComponent: Component, importUrl: string, preactUrl: string) { + const placeholderId = `placeholder_${String(Math.random())}`; + return (attrs: Record<string, string>, ...children: any) => { + return `<div id="${placeholderId}"></div><script type="module"> + import {h, render} from '${preactUrl}'; + import Component from '${importUrl}'; + render(h(Component, ${JSON.stringify(attrs)}), document.getElementById('${placeholderId}')); + </script>`; + }; +} |