diff options
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>`; + }; +} |