@isoloop/preact 0.0.0-next.5aa1a15 → 0.0.0-next.61245db

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.
package/dist/index.d.ts CHANGED
@@ -8,6 +8,12 @@ type IsolateProps = {
8
8
  export declare function defineIsolate(Component: ComponentType<IsolateProps>): IsolateDefinition;
9
9
  /** The isolate context. Throws outside a component that `defineIsolate` rendered. */
10
10
  export declare function useIsolate(): IsolateContext$1;
11
+ /**
12
+ * `ctx.portalRoot`: the container inside this isolate's surface for modals,
13
+ * tooltips, toasts and menus (ADR 0015), for `createPortal` or a library's
14
+ * container option. Throws in a headless isolate.
15
+ */
16
+ export declare function usePortalRoot(): Element;
11
17
  type UseTopicOptions = {
12
18
  /** Default `latest`. `earliest` replays the topic, so a compacted one gives its current value at once. */
13
19
  from?: StartPosition;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BusError, HostCallError, REQUIRES } from "@isoloop/runtime";
2
- import { createContext, h, render } from "preact";
2
+ import { Component, createContext, h, render } from "preact";
3
3
  import { useCallback, useContext, useLayoutEffect, useState } from "preact/hooks";
4
4
  import { routeTopic } from "@isoloop/protocol";
5
5
  // src/context.ts
@@ -10,14 +10,46 @@ function useIsolate() {
10
10
  if (!ctx) throw new Error("useIsolate: not inside a component rendered by defineIsolate()");
11
11
  return ctx;
12
12
  }
13
+ /**
14
+ * `ctx.portalRoot`: the container inside this isolate's surface for modals,
15
+ * tooltips, toasts and menus (ADR 0015), for `createPortal` or a library's
16
+ * container option. Throws in a headless isolate.
17
+ */
18
+ function usePortalRoot() {
19
+ const portal = useIsolate().portalRoot;
20
+ if (!(portal instanceof Element)) throw new Error("usePortalRoot: this isolate has no surface");
21
+ return portal;
22
+ }
13
23
  // src/define.ts
24
+ /**
25
+ * Catches a render error at the isolate's root and reports it as this
26
+ * isolate's own (`ctx.reportError`), so in a pool it crashes only this member
27
+ * (ADR 0015, decision 8). Under a runtime without `reportError` (ADR 0012) the
28
+ * error is rethrown, as before.
29
+ */
30
+ var Boundary = class extends Component {
31
+ state = { failed: false };
32
+ static getDerivedStateFromError() {
33
+ return { failed: true };
34
+ }
35
+ componentDidCatch(error) {
36
+ const { ctx } = this.props;
37
+ if (typeof ctx.reportError === "function") ctx.reportError(error);
38
+ else queueMicrotask(() => {
39
+ throw error;
40
+ });
41
+ }
42
+ render() {
43
+ return this.state.failed ? null : this.props.children;
44
+ }
45
+ };
14
46
  function defineIsolate(Component) {
15
47
  return {
16
48
  requires: REQUIRES,
17
49
  start(ctx) {
18
50
  const container = ctx.root;
19
51
  if (!(container instanceof Element)) throw new Error(`defineIsolate (Preact): "${ctx.id}" is headless; Preact needs a surface`);
20
- render(h(IsolateCtx.Provider, { value: ctx }, h(Component, { config: ctx.config })), container);
52
+ render(h(IsolateCtx.Provider, { value: ctx }, h(Boundary, { ctx }, h(Component, { config: ctx.config }))), container);
21
53
  ctx.onShutdown(() => {
22
54
  render(null, container);
23
55
  });
@@ -75,4 +107,4 @@ function useHost() {
75
107
  function useRoute() {
76
108
  return useTopic(routeTopic, { from: "earliest" });
77
109
  }
78
- export { BusError, HostCallError, defineIsolate, useHost, useIsolate, usePublish, useRequest, useRoute, useTopic };
110
+ export { BusError, HostCallError, defineIsolate, useHost, useIsolate, usePortalRoot, usePublish, useRequest, useRoute, useTopic };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/preact",
3
- "version": "0.0.0-next.5aa1a15",
3
+ "version": "0.0.0-next.61245db",
4
4
  "description": "Preact adapter for Isoloop isolates: defineIsolate(Component) and bus and host hooks.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -23,11 +23,11 @@
23
23
  "preact": "^10 || ^11"
24
24
  },
25
25
  "dependencies": {
26
- "@isoloop/protocol": "0.0.0-next.5aa1a15",
27
- "@isoloop/runtime": "0.0.0-next.5aa1a15"
26
+ "@isoloop/protocol": "0.0.0-next.61245db",
27
+ "@isoloop/runtime": "0.0.0-next.61245db"
28
28
  },
29
29
  "devDependencies": {
30
- "@isoloop/dom": "0.0.0-next.5aa1a15",
30
+ "@isoloop/dom": "0.0.0-next.61245db",
31
31
  "preact10": "npm:preact@10.29.8",
32
32
  "preact11": "npm:preact@11.1.1",
33
33
  "typescript": "7.0.2"