@superwall/studio 1.2.0 → 1.3.0-beta.4

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 (74) hide show
  1. package/README.md +27 -14
  2. package/dist/assets/index-CEz-HnIH.js +11 -0
  3. package/dist/index.html +1 -1
  4. package/dist/lib/StudioApp.d.ts +4 -4
  5. package/dist/lib/components/PaywallToasts.d.ts +3 -0
  6. package/dist/lib/components/SurfaceCard.d.ts +4 -2
  7. package/dist/lib/components/{TopBar.d.ts → chrome/TopBar.d.ts} +1 -2
  8. package/dist/lib/components/icons.d.ts +0 -2
  9. package/dist/lib/components/{ActionMenu.d.ts → menus/ActionMenu.d.ts} +2 -1
  10. package/dist/lib/components/{CompareMenu.d.ts → menus/CompareMenu.d.ts} +1 -1
  11. package/dist/lib/components/{VariablesPanel.d.ts → panels/VariablesPanel.d.ts} +3 -3
  12. package/dist/lib/components/{DeviceChrome.d.ts → preview/DeviceChrome.d.ts} +1 -1
  13. package/dist/lib/components/{DeviceFrame.d.ts → preview/DeviceFrame.d.ts} +5 -2
  14. package/dist/lib/components/preview/HostedIframe.d.ts +7 -0
  15. package/dist/lib/index.d.ts +14 -6
  16. package/dist/lib/index.js +1490 -998
  17. package/dist/lib/lib/actions.d.ts +1 -1
  18. package/dist/lib/lib/contract.d.ts +33 -0
  19. package/dist/lib/lib/devices.d.ts +1 -1
  20. package/dist/lib/lib/host.d.ts +15 -0
  21. package/dist/lib/lib/notices.d.ts +9 -0
  22. package/dist/lib/lib/preview.d.ts +2 -4
  23. package/dist/lib/lib/project.d.ts +2 -1
  24. package/dist/lib/lib/steps.d.ts +5 -0
  25. package/dist/lib/lib/studio-host.d.ts +34 -0
  26. package/dist/lib/lib/surfaces.d.ts +36 -0
  27. package/dist/lib/lib/variables.d.ts +12 -0
  28. package/dist/lib/products.js +26 -12
  29. package/dist/lib/screens/SurfaceEditor.d.ts +2 -3
  30. package/dist/lib/screens/SurfaceList.d.ts +2 -3
  31. package/package.json +5 -1
  32. package/src/StudioApp.tsx +25 -28
  33. package/src/components/PaywallToasts.tsx +162 -86
  34. package/src/components/SurfaceCard.tsx +9 -2
  35. package/src/components/chrome/TopBar.tsx +45 -0
  36. package/src/components/icons.tsx +0 -13
  37. package/src/components/{ActionMenu.tsx → menus/ActionMenu.tsx} +3 -3
  38. package/src/components/{CompareMenu.tsx → menus/CompareMenu.tsx} +2 -2
  39. package/src/components/{SidePanel.tsx → panels/SidePanel.tsx} +1 -1
  40. package/src/components/{VariablesPanel.tsx → panels/VariablesPanel.tsx} +4 -4
  41. package/src/components/{DeviceChrome.tsx → preview/DeviceChrome.tsx} +1 -1
  42. package/src/components/{DeviceFrame.tsx → preview/DeviceFrame.tsx} +14 -55
  43. package/src/components/preview/HostedIframe.tsx +46 -0
  44. package/src/index.ts +32 -12
  45. package/src/lib/actions.ts +1 -1
  46. package/src/lib/contract.ts +30 -0
  47. package/src/lib/devices.ts +5 -4
  48. package/src/lib/host.ts +79 -0
  49. package/src/lib/notices.ts +63 -0
  50. package/src/lib/preview.ts +8 -40
  51. package/src/lib/products.ts +20 -1
  52. package/src/lib/project.ts +2 -1
  53. package/src/lib/steps.ts +20 -0
  54. package/src/lib/studio-host.tsx +61 -0
  55. package/src/lib/surfaces.ts +43 -0
  56. package/src/lib/time.ts +1 -2
  57. package/src/lib/variables.ts +13 -0
  58. package/src/main.tsx +6 -5
  59. package/src/screens/SurfaceEditor.tsx +79 -70
  60. package/src/screens/SurfaceList.tsx +27 -12
  61. package/src/vite-env.d.ts +1 -0
  62. package/dist/assets/index-CVKQ82YR.js +0 -11
  63. package/dist/lib/lib/events.d.ts +0 -27
  64. package/dist/lib/lib/types.d.ts +0 -77
  65. package/src/components/TopBar.tsx +0 -41
  66. package/src/lib/events.ts +0 -86
  67. package/src/lib/types.ts +0 -87
  68. /package/dist/lib/components/{StudioShell.d.ts → chrome/StudioShell.d.ts} +0 -0
  69. /package/dist/lib/components/{Toolbar.d.ts → chrome/Toolbar.d.ts} +0 -0
  70. /package/dist/lib/components/{MissingProducts.d.ts → panels/MissingProducts.d.ts} +0 -0
  71. /package/dist/lib/components/{SidePanel.d.ts → panels/SidePanel.d.ts} +0 -0
  72. /package/src/components/{StudioShell.tsx → chrome/StudioShell.tsx} +0 -0
  73. /package/src/components/{Toolbar.tsx → chrome/Toolbar.tsx} +0 -0
  74. /package/src/components/{MissingProducts.tsx → panels/MissingProducts.tsx} +0 -0
@@ -1,5 +1,6 @@
1
1
  import * as React from "react";
2
2
  import { toast, Toaster } from "sonner";
3
+ import type { HostDelegate } from "@superwall/host";
3
4
  import {
4
5
  AlertTriangleIcon,
5
6
  BellIcon,
@@ -13,7 +14,7 @@ import {
13
14
  StarIcon,
14
15
  UserIcon,
15
16
  } from "./icons";
16
- import { isBridgeMessage, noticeFor, STUDIO_CHANNEL, type EventNotice } from "../lib/events";
17
+ import { noticeFor, type EventNotice, type PaywallMessageName } from "../lib/notices";
17
18
 
18
19
  const TINT: Record<EventNotice["tone"], string> = {
19
20
  info: "text-cream/45",
@@ -21,7 +22,7 @@ const TINT: Record<EventNotice["tone"], string> = {
21
22
  warning: "text-red",
22
23
  };
23
24
 
24
- const ICON: Record<string, () => React.ReactElement> = {
25
+ const ICON: Partial<Record<PaywallMessageName, () => React.ReactElement>> = {
25
26
  purchase: CartIcon,
26
27
  transaction_complete: CheckCircleIcon,
27
28
  freeTrial_start: CheckCircleIcon,
@@ -29,6 +30,8 @@ const ICON: Record<string, () => React.ReactElement> = {
29
30
  transaction_fail: AlertTriangleIcon,
30
31
  close: CloseIcon,
31
32
  restore: RotateIcon,
33
+ restore_complete: CheckCircleIcon,
34
+ restore_fail: AlertTriangleIcon,
32
35
  open_url: ExternalIcon,
33
36
  open_url_external: ExternalIcon,
34
37
  open_deep_link: ExternalIcon,
@@ -59,7 +62,7 @@ const Toast = ({
59
62
  children,
60
63
  actions,
61
64
  }: React.PropsWithChildren<{
62
- action: string;
65
+ action: PaywallMessageName;
63
66
  tone: EventNotice["tone"];
64
67
  actions?: React.ReactNode;
65
68
  }>) => {
@@ -92,67 +95,170 @@ const Notice = ({ notice }: { notice: EventNotice }) => (
92
95
  </Toast>
93
96
  );
94
97
 
95
- const OUTCOME: Record<string, { action: string; tone: EventNotice["tone"]; label: string }> = {
96
- completed: { action: "transaction_complete", tone: "success", label: "purchase completed." },
97
- abandoned: { action: "transaction_abandon", tone: "warning", label: "purchase abandoned." },
98
- granted: { action: "request_permission", tone: "success", label: "granted." },
99
- denied: { action: "request_permission", tone: "warning", label: "denied." },
100
- success: { action: "request_callback", tone: "success", label: "succeeded." },
101
- failure: { action: "request_callback", tone: "warning", label: "failed." },
102
- };
103
-
104
- const Outcome = ({ outcome, subject }: { outcome: string; subject: string }) => {
105
- const spec = OUTCOME[outcome] ?? OUTCOME.completed;
106
- return (
107
- <Toast action={spec?.action ?? "purchase"} tone={spec?.tone ?? "info"}>
108
- <Pill>{subject}</Pill> {spec?.label}
109
- </Toast>
110
- );
111
- };
112
-
113
- const ASK: Record<string, { action: string; verb: string; no: string; yes: string }> = {
114
- purchase: { action: "purchase", verb: "purchase", no: "Abandon", yes: "Complete" },
115
- permission: { action: "request_permission", verb: "permission", no: "Deny", yes: "Grant" },
116
- callback: { action: "request_callback", verb: "callback", no: "Fail", yes: "Succeed" },
98
+ type Choice<T> = {
99
+ label: string;
100
+ value: T;
101
+ primary?: boolean;
102
+ action: PaywallMessageName;
103
+ tone: EventNotice["tone"];
104
+ done: string;
117
105
  };
118
106
 
119
- const OUTCOMES: Record<string, readonly [string, string]> = {
120
- purchase: ["abandoned", "completed"],
121
- permission: ["denied", "granted"],
122
- callback: ["failure", "success"],
123
- };
107
+ const Outcome = <T,>({ choice, subject }: { choice: Choice<T>; subject: string }) => (
108
+ <Toast action={choice.action} tone={choice.tone}>
109
+ <Pill>{subject}</Pill> {choice.done}
110
+ </Toast>
111
+ );
124
112
 
125
- const Ask = ({
126
- kind,
113
+ const Ask = <T,>({
114
+ action,
115
+ verb,
127
116
  subject,
117
+ choices,
128
118
  onAnswer,
129
119
  }: {
130
- kind: string;
120
+ action: PaywallMessageName;
121
+ verb: string;
131
122
  subject: string;
132
- onAnswer: (outcome: string) => void;
133
- }) => {
134
- const spec = ASK[kind] ?? ASK.purchase;
135
- const [no, yes] = OUTCOMES[kind] ?? ["abandoned", "completed"];
123
+ choices: readonly Choice<T>[];
124
+ onAnswer: (choice: Choice<T>) => void;
125
+ }) => (
126
+ <Toast
127
+ action={action}
128
+ tone="info"
129
+ actions={choices.map((choice) => (
130
+ <button
131
+ key={choice.label}
132
+ type="button"
133
+ className={choice.primary ? "sw-btn sw-btn--primary" : "sw-btn"}
134
+ onClick={() => onAnswer(choice)}
135
+ >
136
+ {choice.label}
137
+ </button>
138
+ ))}
139
+ >
140
+ Tap triggered a <Pill>{verb}</Pill> for <Pill>{subject}</Pill>.
141
+ </Toast>
142
+ );
136
143
 
137
- return (
138
- <Toast
139
- action={spec?.action ?? "purchase"}
140
- tone="info"
141
- actions={
142
- <>
143
- <button type="button" className="sw-btn" onClick={() => onAnswer(no)}>
144
- {spec?.no}
145
- </button>
146
- <button type="button" className="sw-btn sw-btn--primary" onClick={() => onAnswer(yes)}>
147
- {spec?.yes}
148
- </button>
149
- </>
150
- }
151
- >
152
- Tap triggered a <Pill>{spec?.verb}</Pill> for <Pill>{subject}</Pill>.
153
- </Toast>
154
- );
155
- };
144
+ const PURCHASE: readonly Choice<"completed" | "abandoned" | "failed">[] = [
145
+ {
146
+ label: "Abandon",
147
+ value: "abandoned",
148
+ action: "transaction_abandon",
149
+ tone: "warning",
150
+ done: "purchase abandoned.",
151
+ },
152
+ {
153
+ label: "Fail",
154
+ value: "failed",
155
+ action: "transaction_fail",
156
+ tone: "warning",
157
+ done: "purchase failed.",
158
+ },
159
+ {
160
+ label: "Complete",
161
+ value: "completed",
162
+ primary: true,
163
+ action: "transaction_complete",
164
+ tone: "success",
165
+ done: "purchase completed.",
166
+ },
167
+ ];
168
+
169
+ const RESTORE: readonly Choice<"restored" | "failed">[] = [
170
+ {
171
+ label: "Nothing",
172
+ value: "failed",
173
+ action: "restore_fail",
174
+ tone: "warning",
175
+ done: "nothing to restore.",
176
+ },
177
+ {
178
+ label: "Restore",
179
+ value: "restored",
180
+ primary: true,
181
+ action: "restore_complete",
182
+ tone: "success",
183
+ done: "restored.",
184
+ },
185
+ ];
186
+
187
+ const PERMISSION: readonly Choice<"granted" | "denied">[] = [
188
+ {
189
+ label: "Deny",
190
+ value: "denied",
191
+ action: "request_permission",
192
+ tone: "warning",
193
+ done: "denied.",
194
+ },
195
+ {
196
+ label: "Grant",
197
+ value: "granted",
198
+ primary: true,
199
+ action: "request_permission",
200
+ tone: "success",
201
+ done: "granted.",
202
+ },
203
+ ];
204
+
205
+ const CALLBACK: readonly Choice<"success" | "failure">[] = [
206
+ { label: "Fail", value: "failure", action: "request_callback", tone: "warning", done: "failed." },
207
+ {
208
+ label: "Succeed",
209
+ value: "success",
210
+ primary: true,
211
+ action: "request_callback",
212
+ tone: "success",
213
+ done: "succeeded.",
214
+ },
215
+ ];
216
+
217
+ let asked = 0;
218
+
219
+ const ask = <T,>(
220
+ action: PaywallMessageName,
221
+ verb: string,
222
+ subject: string,
223
+ choices: readonly Choice<T>[],
224
+ ): Promise<T> =>
225
+ new Promise((resolve) => {
226
+ const id = `${action}-${(asked += 1)}`;
227
+ toast.custom(
228
+ () => (
229
+ <Ask
230
+ action={action}
231
+ verb={verb}
232
+ subject={subject}
233
+ choices={choices}
234
+ onAnswer={(choice) => {
235
+ toast.dismiss(id);
236
+ toast.custom(() => <Outcome choice={choice} subject={subject} />);
237
+ resolve(choice.value);
238
+ }}
239
+ />
240
+ ),
241
+ { id, duration: Number.POSITIVE_INFINITY },
242
+ );
243
+ });
244
+
245
+ export const createStudioDelegate = (): HostDelegate => ({
246
+ event: (message) => {
247
+ const notice = noticeFor(message);
248
+ if (notice) toast.custom(() => <Notice notice={notice} />);
249
+ },
250
+ purchase: (product) => ask("purchase", "purchase", product.reference, PURCHASE),
251
+ restore: () => ask("restore", "restore", "purchases", RESTORE),
252
+ close: () => {},
253
+ openUrl: () => {},
254
+ custom: () => {},
255
+ customPlacement: () => {},
256
+ permission: (type) => ask("request_permission", "permission", type, PERMISSION),
257
+ callback: (name) =>
258
+ ask("request_callback", "callback", name, CALLBACK).then((status) => ({ status })),
259
+ });
260
+
261
+ export const useStudioDelegate = (): HostDelegate => React.useMemo(createStudioDelegate, []);
156
262
 
157
263
  export const PaywallToasts = ({
158
264
  surfaceKey,
@@ -165,36 +271,6 @@ export const PaywallToasts = ({
165
271
  toast.dismiss();
166
272
  }, [surfaceKey]);
167
273
 
168
- React.useEffect(() => {
169
- const onMessage = (event: MessageEvent) => {
170
- if (!isBridgeMessage(event.data)) return;
171
-
172
- if (event.data.type === "ask") {
173
- const { id, kind, subject } = event.data;
174
- const answer = (outcome: string) => {
175
- (event.source as Window | null)?.postMessage(
176
- { channel: STUDIO_CHANNEL, type: "answer", id, outcome },
177
- "*",
178
- );
179
- toast.dismiss(id);
180
- toast.custom(() => <Outcome outcome={outcome} subject={subject} />);
181
- };
182
- toast.custom(() => <Ask kind={kind} subject={subject} onAnswer={answer} />, {
183
- id,
184
- duration: Number.POSITIVE_INFINITY,
185
- });
186
- return;
187
- }
188
-
189
- if (event.data.type !== "event") return;
190
- const notice = noticeFor(event.data.message);
191
- if (notice) toast.custom(() => <Notice notice={notice} />);
192
- };
193
-
194
- window.addEventListener("message", onMessage);
195
- return () => window.removeEventListener("message", onMessage);
196
- }, []);
197
-
198
274
  return (
199
275
  <Toaster
200
276
  position="top-center"
@@ -1,7 +1,9 @@
1
1
  import * as React from "react";
2
+ import { silentDelegate, type HostState } from "@superwall/host";
3
+ import { HostedIframe } from "./preview/HostedIframe";
2
4
  import { surfacePath } from "../lib/preview";
3
5
  import { timeAgo } from "../lib/time";
4
- import type { StudioStatus, StudioSurface } from "../lib/types";
6
+ import type { StudioStatus, StudioSurface } from "../lib/surfaces";
5
7
 
6
8
  const PREVIEW_WIDTH = 390;
7
9
  const PREVIEW_HEIGHT = 844;
@@ -27,18 +29,21 @@ export const SurfaceCard = ({
27
29
  surface,
28
30
  status,
29
31
  previewSrc,
32
+ hostState,
30
33
  onOpen,
31
34
  href,
32
35
  }: {
33
36
  surface: StudioSurface;
34
37
  status?: StudioStatus;
35
38
  previewSrc: string;
39
+ hostState: HostState;
36
40
  onOpen: () => void;
37
41
  href?: string;
38
42
  }) => {
39
43
  const previewRef = React.useRef<HTMLDivElement>(null);
40
44
  const width = useElementWidth(previewRef);
41
45
  const scale = width > 0 ? width / PREVIEW_WIDTH : 0;
46
+ const delegate = React.useMemo(silentDelegate, []);
42
47
 
43
48
  return (
44
49
  <a
@@ -55,7 +60,9 @@ export const SurfaceCard = ({
55
60
  ref={previewRef}
56
61
  >
57
62
  {scale > 0 && (
58
- <iframe
63
+ <HostedIframe
64
+ hostState={hostState}
65
+ delegate={delegate}
59
66
  className="pointer-events-none block origin-top-left border-0 bg-white"
60
67
  title={`${surface.project ? `${surface.project}/` : ""}${surface.id} preview`}
61
68
  src={previewSrc}
@@ -0,0 +1,45 @@
1
+ import * as React from "react";
2
+ import { useStudioHome } from "../../lib/studio-host";
3
+
4
+ export const TopBar = ({
5
+ title,
6
+ actions,
7
+ inset = false,
8
+ label = "Studio",
9
+ }: {
10
+ title: string;
11
+ actions?: React.ReactNode;
12
+ inset?: boolean;
13
+ label?: string;
14
+ }) => {
15
+ const home = useStudioHome();
16
+
17
+ return (
18
+ <header className="relative z-10 flex h-11 flex-none items-center border-b border-cream/10 bg-ink">
19
+ <div
20
+ className={`flex h-full w-full items-center px-3 ${inset ? "mx-auto max-w-[1080px] sm:px-0" : ""}`}
21
+ >
22
+ <a
23
+ className="flex items-center gap-3 text-[0.9375rem] font-semibold tracking-[0.01em]"
24
+ href={home.href}
25
+ onClick={(event) => {
26
+ if (event.button !== 0) return;
27
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
28
+ event.preventDefault();
29
+ home.navigate();
30
+ }}
31
+ >
32
+ <span className="whitespace-nowrap">
33
+ <em className="text-accent not-italic">$UPER</em>WALL
34
+ </span>
35
+ <span className="h-4 w-px bg-cream/20" />
36
+ <span className="font-normal">{label}</span>
37
+ </a>
38
+ <span className="absolute left-1/2 hidden -translate-x-1/2 text-sm font-medium whitespace-nowrap sm:block">
39
+ {title}
40
+ </span>
41
+ <div className="ml-auto flex items-center gap-2">{actions}</div>
42
+ </div>
43
+ </header>
44
+ );
45
+ };
@@ -6,12 +6,6 @@ const Icon = ({ children }: React.PropsWithChildren) => (
6
6
  </svg>
7
7
  );
8
8
 
9
- export const ChevronLeftIcon = () => (
10
- <Icon>
11
- <path d="m15 18-6-6 6-6" />
12
- </Icon>
13
- );
14
-
15
9
  export const RotateIcon = () => (
16
10
  <Icon>
17
11
  <path d="M21 12a9 9 0 1 1-2.64-6.36" />
@@ -40,13 +34,6 @@ export const MoonIcon = () => (
40
34
  </Icon>
41
35
  );
42
36
 
43
- export const ResetIcon = () => (
44
- <Icon>
45
- <path d="M3 12a9 9 0 1 0 2.64-6.36" />
46
- <path d="M3 3v6h6" />
47
- </Icon>
48
- );
49
-
50
37
  export const SlidersIcon = () => (
51
38
  <Icon>
52
39
  <path d="M21 4h-7M10 4H3M21 12h-9M8 12H3M21 20h-5M12 20H3" />
@@ -1,12 +1,12 @@
1
1
  import * as React from "react";
2
- import { ChevronDownIcon } from "./icons";
2
+ import { ChevronDownIcon } from "../icons";
3
3
  import {
4
4
  ACTION_STEPS,
5
5
  STEP_LABELS,
6
- type StudioActionName,
7
6
  type StudioActionStep,
8
7
  type StudioStepState,
9
- } from "../lib/types";
8
+ } from "../../lib/steps";
9
+ import { type StudioActionName } from "../../lib/surfaces";
10
10
 
11
11
  const ACTIONS: readonly { name: StudioActionName; label: string; hint: string }[] = [
12
12
  { name: "push", label: "Push", hint: "New version, nothing live changes" },
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
- import { ColumnsIcon } from "./icons";
3
- import type { CompareTarget } from "../lib/types";
2
+ import { ColumnsIcon } from "../icons";
3
+ import type { CompareTarget } from "../../lib/surfaces";
4
4
 
5
5
  export type CompareOption = {
6
6
  key: string;
@@ -1,5 +1,5 @@
1
1
  import * as React from "react";
2
- import { CloseIcon } from "./icons";
2
+ import { CloseIcon } from "../icons";
3
3
 
4
4
  export const SidePanel = ({
5
5
  open,
@@ -1,6 +1,6 @@
1
1
  import * as React from "react";
2
- import { ChevronDownIcon } from "./icons";
3
- import type { PreviewOverrides } from "../lib/types";
2
+ import { ChevronDownIcon } from "../icons";
3
+ import type { VariableOverrides } from "../../lib/variables";
4
4
 
5
5
  export type VariableGroup = "user" | "device" | "params" | "products";
6
6
 
@@ -157,9 +157,9 @@ export const VariablesPanel = ({
157
157
  groups,
158
158
  onChange,
159
159
  }: {
160
- overrides: PreviewOverrides;
160
+ overrides: VariableOverrides;
161
161
  groups: VariableGroups;
162
- onChange: (overrides: PreviewOverrides) => void;
162
+ onChange: (overrides: VariableOverrides) => void;
163
163
  }) => {
164
164
  const update = (group: VariableGroup, path: string, value: unknown) => {
165
165
  if (group !== "products") {
@@ -1,4 +1,4 @@
1
- import type { DeviceChromeSpec } from "../lib/devices";
1
+ import type { DeviceChromeSpec } from "../../lib/devices";
2
2
 
3
3
  const CellularIcon = () => (
4
4
  <svg width="19" height="12" viewBox="0 0 19 12" fill="currentColor" aria-hidden="true">
@@ -1,13 +1,13 @@
1
1
  import * as React from "react";
2
+ import type { HostDelegate, HostState } from "@superwall/host";
2
3
  import { DeviceChrome } from "./DeviceChrome";
3
- import type { DeviceChromeSpec } from "../lib/devices";
4
+ import { HostedIframe } from "./HostedIframe";
5
+ import type { DeviceChromeSpec } from "../../lib/devices";
4
6
 
5
7
  const FRAME_MARGIN = 96;
6
8
 
7
9
  const SWAP_DELAY_MS = 250;
8
10
 
9
- type PreviewWindow = Window & { __swPreviewApply?: (search: string) => void };
10
-
11
11
  const useFitScale = (
12
12
  container: React.RefObject<HTMLElement | null>,
13
13
  width: number,
@@ -63,32 +63,18 @@ export const DevicePlaceholder = ({
63
63
  };
64
64
 
65
65
  type Frame = {
66
- mountSrc: string;
67
- currentSrc: string;
66
+ src: string;
68
67
  generation: number;
69
68
  ready: boolean;
70
69
  };
71
70
 
72
- const instantApply = (node: HTMLIFrameElement | undefined, from: string, to: string): boolean => {
73
- try {
74
- const base = window.location.origin;
75
- const fromUrl = new URL(from, base);
76
- const toUrl = new URL(to, base);
77
- if (fromUrl.origin !== toUrl.origin || fromUrl.pathname !== toUrl.pathname) return false;
78
- const apply = (node?.contentWindow as PreviewWindow | null | undefined)?.__swPreviewApply;
79
- if (typeof apply !== "function") return false;
80
- apply(toUrl.search);
81
- return true;
82
- } catch {
83
- return false;
84
- }
85
- };
86
-
87
71
  export const DeviceFrame = ({
88
72
  width,
89
73
  height,
90
74
  title,
91
75
  src,
76
+ hostState,
77
+ delegate,
92
78
  stageRef,
93
79
  reloadKey = 0,
94
80
  chrome,
@@ -99,6 +85,8 @@ export const DeviceFrame = ({
99
85
  height: number;
100
86
  title: string;
101
87
  src: string;
88
+ hostState: HostState;
89
+ delegate: HostDelegate;
102
90
  stageRef: React.RefObject<HTMLElement | null>;
103
91
  reloadKey?: number;
104
92
  chrome?: DeviceChromeSpec;
@@ -108,43 +96,16 @@ export const DeviceFrame = ({
108
96
  const scale = useFitScale(stageRef, width, height);
109
97
 
110
98
  const generation = React.useRef(0);
111
- const [frames, setFrames] = React.useState<Frame[]>([
112
- { mountSrc: src, currentSrc: src, generation: 0, ready: true },
113
- ]);
114
- const framesRef = React.useRef(frames);
115
- framesRef.current = frames;
116
- const nodes = React.useRef(new Map<number, HTMLIFrameElement>());
99
+ const [frames, setFrames] = React.useState<Frame[]>([{ src, generation: 0, ready: true }]);
117
100
 
118
101
  const mounted = React.useRef(false);
119
- const lastReload = React.useRef(reloadKey);
120
102
  React.useEffect(() => {
121
103
  if (!mounted.current) {
122
104
  mounted.current = true;
123
105
  return;
124
106
  }
125
- const forced = reloadKey !== lastReload.current;
126
- lastReload.current = reloadKey;
127
-
128
- if (!forced) {
129
- const visible = framesRef.current.findLast((frame) => frame.ready);
130
- const node = visible ? nodes.current.get(visible.generation) : undefined;
131
- if (visible && instantApply(node, visible.currentSrc, src)) {
132
- setFrames((existing) =>
133
- existing.map((frame) =>
134
- frame.generation === visible.generation ? { ...frame, currentSrc: src } : frame,
135
- ),
136
- );
137
- return;
138
- }
139
- }
140
-
141
107
  generation.current += 1;
142
- const next: Frame = {
143
- mountSrc: src,
144
- currentSrc: src,
145
- generation: generation.current,
146
- ready: false,
147
- };
108
+ const next: Frame = { src, generation: generation.current, ready: false };
148
109
  setFrames((existing) => [...existing.filter((frame) => frame.ready).slice(-1), next]);
149
110
  }, [src, reloadKey]);
150
111
 
@@ -172,17 +133,15 @@ export const DeviceFrame = ({
172
133
  style={{ width: width * scale, height: height * scale, borderRadius: radius }}
173
134
  >
174
135
  {frames.map((frame) => (
175
- <iframe
136
+ <HostedIframe
176
137
  key={frame.generation}
177
- ref={(node) => {
178
- if (node) nodes.current.set(frame.generation, node);
179
- else nodes.current.delete(frame.generation);
180
- }}
138
+ hostState={hostState}
139
+ delegate={delegate}
181
140
  className={`absolute inset-0 origin-top-left border-0 bg-white transition-opacity duration-150 ${
182
141
  frame === visible ? "opacity-100" : "opacity-0"
183
142
  }`}
184
143
  title={title}
185
- src={frame.mountSrc}
144
+ src={frame.src}
186
145
  width={width}
187
146
  height={height}
188
147
  style={{ transform: `scale(${scale})` }}
@@ -0,0 +1,46 @@
1
+ import * as React from "react";
2
+ import {
3
+ createWindowHost,
4
+ diffHostState,
5
+ type HostDelegate,
6
+ type HostState,
7
+ type PaywallHost,
8
+ } from "@superwall/host";
9
+
10
+ type Hosted = { host: PaywallHost; detach: () => void; sent: HostState };
11
+
12
+ const useHostedWindow = (hostState: HostState, delegate: HostDelegate) => {
13
+ const hosted = React.useRef<Hosted | undefined>(undefined);
14
+ const initial = React.useRef({ hostState, delegate });
15
+
16
+ const ref = React.useCallback((node: HTMLIFrameElement | null) => {
17
+ hosted.current?.detach();
18
+ hosted.current = undefined;
19
+ if (!node?.contentWindow) return;
20
+ const { host, detach } = createWindowHost({
21
+ target: node.contentWindow,
22
+ state: initial.current.hostState,
23
+ delegate: initial.current.delegate,
24
+ });
25
+ hosted.current = { host, detach, sent: initial.current.hostState };
26
+ }, []);
27
+
28
+ React.useEffect(() => {
29
+ initial.current = { hostState, delegate };
30
+ const current = hosted.current;
31
+ if (!current) return;
32
+ current.host.update(diffHostState(current.sent, hostState));
33
+ current.sent = hostState;
34
+ }, [hostState, delegate]);
35
+
36
+ return ref;
37
+ };
38
+
39
+ export type HostedIframeProps = React.ComponentPropsWithoutRef<"iframe"> & {
40
+ hostState: HostState;
41
+ delegate: HostDelegate;
42
+ };
43
+
44
+ export const HostedIframe = ({ hostState, delegate, ...iframe }: HostedIframeProps) => (
45
+ <iframe {...iframe} ref={useHostedWindow(hostState, delegate)} />
46
+ );