@superwall/studio 1.1.1 → 1.3.0-beta.3
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/README.md +61 -11
- package/dist/assets/index-DyLb5fiB.js +11 -0
- package/dist/assets/index-_HCp6gCi.css +1 -0
- package/dist/index.html +2 -2
- package/dist/lib/StudioApp.d.ts +6 -0
- package/dist/lib/components/OpenInApp.d.ts +5 -0
- package/dist/lib/components/PaywallToasts.d.ts +8 -0
- package/dist/lib/components/SurfaceCard.d.ts +11 -0
- package/dist/lib/components/chrome/StudioShell.d.ts +4 -0
- package/dist/lib/components/chrome/Toolbar.d.ts +6 -0
- package/dist/lib/components/chrome/TopBar.d.ts +8 -0
- package/dist/lib/components/controls.d.ts +24 -0
- package/dist/lib/components/icons.d.ts +18 -0
- package/dist/lib/components/menus/ActionMenu.d.ts +14 -0
- package/dist/lib/components/menus/CompareMenu.d.ts +13 -0
- package/dist/lib/components/panels/MissingProducts.d.ts +8 -0
- package/dist/lib/components/panels/SidePanel.d.ts +6 -0
- package/dist/lib/components/panels/VariablesPanel.d.ts +13 -0
- package/dist/lib/components/preview/DeviceChrome.d.ts +12 -0
- package/dist/lib/components/preview/DeviceFrame.d.ts +21 -0
- package/dist/lib/components/preview/HostedIframe.d.ts +7 -0
- package/dist/lib/icon.svg +5 -0
- package/dist/lib/index.d.ts +20 -0
- package/dist/lib/index.js +2268 -0
- package/dist/lib/lib/actions.d.ts +4 -0
- package/dist/lib/lib/contract.d.ts +33 -0
- package/dist/lib/lib/devices.d.ts +26 -0
- package/dist/lib/lib/host.d.ts +15 -0
- package/dist/lib/lib/notices.d.ts +9 -0
- package/dist/lib/lib/preview.d.ts +5 -0
- package/dist/lib/lib/products.d.ts +16 -0
- package/dist/lib/lib/project.d.ts +13 -0
- package/dist/lib/lib/steps.d.ts +5 -0
- package/dist/lib/lib/surfaces.d.ts +35 -0
- package/dist/lib/lib/time.d.ts +1 -0
- package/dist/lib/lib/variables.d.ts +12 -0
- package/dist/lib/products.js +65 -0
- package/dist/lib/screens/SurfaceEditor.d.ts +9 -0
- package/dist/lib/screens/SurfaceList.d.ts +7 -0
- package/dist/lib/studio.css +1 -0
- package/package.json +19 -4
- package/src/StudioApp.tsx +8 -11
- package/src/components/PaywallToasts.tsx +162 -85
- package/src/components/SurfaceCard.tsx +9 -2
- package/src/components/chrome/StudioShell.tsx +12 -0
- package/src/components/icons.tsx +0 -13
- package/src/components/{ActionMenu.tsx → menus/ActionMenu.tsx} +4 -4
- package/src/components/{CompareMenu.tsx → menus/CompareMenu.tsx} +3 -3
- package/src/components/{SidePanel.tsx → panels/SidePanel.tsx} +1 -1
- package/src/components/{VariablesPanel.tsx → panels/VariablesPanel.tsx} +4 -4
- package/src/components/{DeviceChrome.tsx → preview/DeviceChrome.tsx} +1 -1
- package/src/components/{DeviceFrame.tsx → preview/DeviceFrame.tsx} +14 -55
- package/src/components/preview/HostedIframe.tsx +46 -0
- package/src/index.ts +26 -6
- package/src/lib/actions.ts +1 -1
- package/src/lib/contract.ts +30 -0
- package/src/lib/devices.ts +5 -4
- package/src/lib/host.ts +80 -0
- package/src/lib/notices.ts +63 -0
- package/src/lib/preview.ts +11 -33
- package/src/lib/products.ts +116 -0
- package/src/lib/project.ts +2 -1
- package/src/lib/steps.ts +20 -0
- package/src/lib/surfaces.ts +41 -0
- package/src/lib/time.ts +1 -2
- package/src/lib/variables.ts +13 -0
- package/src/main.tsx +3 -3
- package/src/screens/SurfaceEditor.tsx +97 -67
- package/src/screens/SurfaceList.tsx +17 -7
- package/src/styles.css +8 -6
- package/src/vite-env.d.ts +1 -0
- package/dist/assets/index-CVKGrx00.js +0 -11
- package/dist/assets/index-D4RIpSbg.css +0 -1
- package/src/lib/events.ts +0 -77
- package/src/lib/types.ts +0 -85
- /package/src/components/{Toolbar.tsx → chrome/Toolbar.tsx} +0 -0
- /package/src/components/{TopBar.tsx → chrome/TopBar.tsx} +0 -0
- /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 {
|
|
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<
|
|
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:
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
113
|
+
const Ask = <T,>({
|
|
114
|
+
action,
|
|
115
|
+
verb,
|
|
127
116
|
subject,
|
|
117
|
+
choices,
|
|
128
118
|
onAnswer,
|
|
129
119
|
}: {
|
|
130
|
-
|
|
120
|
+
action: PaywallMessageName;
|
|
121
|
+
verb: string;
|
|
131
122
|
subject: string;
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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,35 +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
|
-
const notice = noticeFor(event.data.message);
|
|
190
|
-
if (notice) toast.custom(() => <Notice notice={notice} />);
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
window.addEventListener("message", onMessage);
|
|
194
|
-
return () => window.removeEventListener("message", onMessage);
|
|
195
|
-
}, []);
|
|
196
|
-
|
|
197
274
|
return (
|
|
198
275
|
<Toaster
|
|
199
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/
|
|
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
|
-
<
|
|
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,12 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
export const StudioShell = ({
|
|
4
|
+
children,
|
|
5
|
+
position = "fixed",
|
|
6
|
+
}: React.PropsWithChildren<{ position?: "fixed" | "absolute" }>) => (
|
|
7
|
+
<div
|
|
8
|
+
className={`sw-studio bg-ink text-cream ${position} inset-0 flex flex-col font-sans text-[0.9375rem] tracking-[-0.01em] antialiased`}
|
|
9
|
+
>
|
|
10
|
+
{children}
|
|
11
|
+
</div>
|
|
12
|
+
);
|
package/src/components/icons.tsx
CHANGED
|
@@ -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 "
|
|
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 "
|
|
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" },
|
|
@@ -116,7 +116,7 @@ export const ActionMenu = ({
|
|
|
116
116
|
React.useEffect(() => {
|
|
117
117
|
if (!open) return undefined;
|
|
118
118
|
const close = (event: MouseEvent) => {
|
|
119
|
-
if (
|
|
119
|
+
if (menuRef.current && !event.composedPath().includes(menuRef.current)) setOpen(false);
|
|
120
120
|
};
|
|
121
121
|
window.addEventListener("mousedown", close);
|
|
122
122
|
return () => window.removeEventListener("mousedown", close);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import { ColumnsIcon } from "
|
|
3
|
-
import type { CompareTarget } from "
|
|
2
|
+
import { ColumnsIcon } from "../icons";
|
|
3
|
+
import type { CompareTarget } from "../../lib/surfaces";
|
|
4
4
|
|
|
5
5
|
export type CompareOption = {
|
|
6
6
|
key: string;
|
|
@@ -52,7 +52,7 @@ export const CompareMenu = ({
|
|
|
52
52
|
React.useEffect(() => {
|
|
53
53
|
if (!open) return undefined;
|
|
54
54
|
const close = (event: MouseEvent) => {
|
|
55
|
-
if (
|
|
55
|
+
if (menuRef.current && !event.composedPath().includes(menuRef.current)) setOpen(false);
|
|
56
56
|
};
|
|
57
57
|
window.addEventListener("mousedown", close);
|
|
58
58
|
return () => window.removeEventListener("mousedown", close);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from "react";
|
|
2
|
-
import { ChevronDownIcon } from "
|
|
3
|
-
import type {
|
|
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:
|
|
160
|
+
overrides: VariableOverrides;
|
|
161
161
|
groups: VariableGroups;
|
|
162
|
-
onChange: (overrides:
|
|
162
|
+
onChange: (overrides: VariableOverrides) => void;
|
|
163
163
|
}) => {
|
|
164
164
|
const update = (group: VariableGroup, path: string, value: unknown) => {
|
|
165
165
|
if (group !== "products") {
|
|
@@ -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
|
|
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
|
-
|
|
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
|
-
<
|
|
136
|
+
<HostedIframe
|
|
176
137
|
key={frame.generation}
|
|
177
|
-
|
|
178
|
-
|
|
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.
|
|
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
|
+
);
|