summaryrefslogtreecommitdiff
path: root/src/frontend/render/preact.ts
blob: 3b9e1e6d8c8f3c3a327d87e2701526bb6d640856 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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>`;
  };
}