@hamur/adapter-preact 0.0.0-stage → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +35 -2
  3. package/dist/chunks/browser-index-89d71ade.js +118 -0
  4. package/dist/chunks/browser-index-ezte15pp.js +67 -0
  5. package/dist/chunks/browser-index-hsy7tnbg.js +21 -0
  6. package/dist/chunks/browser-index-k3m2axbw.js +274 -0
  7. package/dist/src/adapter.js +456 -0
  8. package/dist/src/error-page.js +391 -0
  9. package/dist/src/index.js +1506 -0
  10. package/dist/src/keepalive.js +6 -0
  11. package/dist/src/runtime/client.js +96 -0
  12. package/dist/src/runtime/island.js +33 -0
  13. package/dist/src/runtime/server.js +95 -0
  14. package/dist/types/src/activation.d.ts +15 -0
  15. package/dist/types/src/adapter.d.ts +2 -0
  16. package/dist/types/src/async-data.d.ts +13 -0
  17. package/dist/types/src/client-component.d.ts +11 -0
  18. package/dist/types/src/client-fallback.d.ts +2 -0
  19. package/dist/types/src/context.d.ts +81 -0
  20. package/dist/types/src/error-page.d.ts +1 -0
  21. package/dist/types/src/head.d.ts +5 -0
  22. package/dist/types/src/index.d.ts +429 -0
  23. package/dist/types/src/island-html.d.ts +13 -0
  24. package/dist/types/src/island.d.ts +19 -0
  25. package/dist/types/src/jsx-locator.d.ts +6 -0
  26. package/dist/types/src/keepalive.d.ts +26 -0
  27. package/dist/types/src/lazy-hydration.d.ts +12 -0
  28. package/dist/types/src/markdown-page.d.ts +9 -0
  29. package/dist/types/src/markdown.d.ts +12 -0
  30. package/dist/types/src/plugin.d.ts +36 -0
  31. package/dist/types/src/route-boundary.d.ts +41 -0
  32. package/dist/types/src/runtime/client.d.ts +10 -0
  33. package/dist/types/src/runtime/hmr.d.ts +31 -0
  34. package/dist/types/src/runtime/hydration-dev.d.ts +1 -0
  35. package/dist/types/src/runtime/hydration.d.ts +8 -0
  36. package/dist/types/src/runtime/island.d.ts +8 -0
  37. package/dist/types/src/runtime/server.d.ts +11 -0
  38. package/dist/types/src/server-island.d.ts +10 -0
  39. package/dist/types/src/svg.d.ts +10 -0
  40. package/package.json +71 -4
  41. package/src/activation.ts +51 -0
  42. package/src/adapter.ts +35 -0
  43. package/src/async-data.ts +65 -0
  44. package/src/client-component.ts +23 -0
  45. package/src/client-fallback.ts +2 -0
  46. package/src/context.ts +372 -0
  47. package/src/env.d.ts +3 -0
  48. package/src/error-page.ts +1 -0
  49. package/src/head.ts +34 -0
  50. package/src/index.ts +1868 -0
  51. package/src/island-html.ts +58 -0
  52. package/src/island.ts +56 -0
  53. package/src/jsx-locator.ts +199 -0
  54. package/src/keepalive.ts +100 -0
  55. package/src/lazy-hydration.ts +101 -0
  56. package/src/markdown-page.ts +18 -0
  57. package/src/markdown.ts +58 -0
  58. package/src/plugin.ts +231 -0
  59. package/src/route-boundary.ts +51 -0
  60. package/src/runtime/client.ts +85 -0
  61. package/src/runtime/hmr.ts +141 -0
  62. package/src/runtime/hydration-dev.ts +8 -0
  63. package/src/runtime/hydration.ts +7 -0
  64. package/src/runtime/island.ts +37 -0
  65. package/src/runtime/server.ts +102 -0
  66. package/src/server-island.ts +35 -0
  67. package/src/svg.ts +27 -0
  68. package/src/webmcp.d.ts +18 -0
@@ -0,0 +1,35 @@
1
+ import { h } from "preact"
2
+ import type { ComponentChild } from "preact"
3
+ import { useContext, useId, useState } from "preact/hooks"
4
+ import { SERVER_ISLAND_ELEMENT, renderServerIsland, serverIslandProps } from "hamur/adapter"
5
+ import { AppContextContext } from "./context.ts"
6
+ import { settledIsland } from "./island-html.ts"
7
+
8
+ const isServer = typeof window === "undefined"
9
+ const KEPT = { __html: "" }
10
+
11
+ export interface ServerIslandProps<P extends Record<string, unknown> = Record<string, unknown>> {
12
+ /** The name of a component under `app/server-islands/`. */
13
+ name: string
14
+ props?: P
15
+ /** Rendered with the page; replaced by the island's HTML once the browser has it. */
16
+ fallback?: ComponentChild
17
+ }
18
+
19
+ /** A component rendered on the server in its own request; the page holds its fallback, the core swaps the answer in, and Preact never renders or hydrates it. */
20
+ export function ServerIsland<P extends Record<string, unknown> = Record<string, unknown>>({
21
+ name,
22
+ props,
23
+ fallback,
24
+ }: ServerIslandProps<P>): ComponentChild {
25
+ const app = useContext(AppContextContext)
26
+ if (!app) throw new Error("ServerIsland must be rendered inside a Hamur app")
27
+ const id = useId()
28
+ const [kept] = useState(() => !isServer && app.asyncData.hydrating)
29
+ if (isServer)
30
+ return h(SERVER_ISLAND_ELEMENT, settledIsland(renderServerIsland(app, id, name, props).attributes) ?? {}, fallback)
31
+ // While hydrating, the server's element keeps its attributes and nodes: Preact applies no empty innerHTML to it.
32
+ if (kept) return h(SERVER_ISLAND_ELEMENT, { dangerouslySetInnerHTML: KEPT })
33
+ const given = props && Object.keys(props).length ? { props: serverIslandProps(name, props) } : {}
34
+ return h(SERVER_ISLAND_ELEMENT, { name, ...given }, fallback)
35
+ }
package/src/svg.ts ADDED
@@ -0,0 +1,27 @@
1
+ import { svgCode, type SvgCode, type SvgNode, type SvgSource } from "hamur/adapter"
2
+
3
+ function nodeCode(code: SvgCode, node: SvgNode): string {
4
+ if (typeof node === "string" || Array.isArray(node)) return code.value(node)
5
+ const children = node.children.map((child) => nodeCode(code, child))
6
+ return `h(${JSON.stringify(node.tag)}, ${code.props(code.attrs(node))}${children.length ? `, ${children.join(", ")}` : ""})`
7
+ }
8
+
9
+ function module(svg: SvgSource): { contents: string; extension: string } {
10
+ const code = svgCode(svg, { title: "title", props: "props" })
11
+ const children = svg.children.map((child) => nodeCode(code, child))
12
+ const hidden = code.hidden ? `${code.hidden} ? { "aria-hidden": "true" } : {}` : "{}"
13
+ const contents = `import { h } from "preact";
14
+ import { useId } from "preact/hooks";
15
+
16
+ export default function SvgComponent({ title, ...props }) {
17
+ const uid = useId().replace(/[^\\w-]/g, "_");
18
+ return h("svg", { ...${code.props(svg.attrs)}, ...(${hidden}), ...props }, title == null ? null : h("title", null, title)${children.length ? `, ${children.join(", ")}` : ""});
19
+ }
20
+ `
21
+ return { contents, extension: ".js" }
22
+ }
23
+
24
+ export const svgComponent = {
25
+ types: `import("preact").FunctionComponent<import("preact").SVGAttributes<SVGSVGElement> & { title?: string }>`,
26
+ module,
27
+ }
@@ -0,0 +1,18 @@
1
+ import type { Signalish } from "preact"
2
+
3
+ declare module "preact" {
4
+ interface FormHTMLAttributes<T extends EventTarget = HTMLFormElement> {
5
+ toolname?: Signalish<string | undefined>
6
+ tooldescription?: Signalish<string | undefined>
7
+ toolautosubmit?: Signalish<boolean | undefined>
8
+ }
9
+ interface PartialInputHTMLAttributes<T extends EventTarget> {
10
+ toolparamdescription?: Signalish<string | undefined>
11
+ }
12
+ interface PartialSelectHTMLAttributes<T extends EventTarget> {
13
+ toolparamdescription?: Signalish<string | undefined>
14
+ }
15
+ interface TextareaHTMLAttributes<T extends EventTarget = HTMLTextAreaElement> {
16
+ toolparamdescription?: Signalish<string | undefined>
17
+ }
18
+ }