@superwall/studio 2.2.1 → 2.3.0

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.
@@ -0,0 +1,275 @@
1
+ import * as React from "react";
2
+ import { BROWSER_BAR, type BrowserKind } from "../../lib/devices";
3
+
4
+ const SITE = "superwall.com";
5
+
6
+ const BAR_BACKGROUND = "#1c1c1e";
7
+
8
+ const DARK_GLASS: React.CSSProperties = {
9
+ background: "rgba(255, 255, 255, 0.12)",
10
+ backdropFilter: "blur(40px) saturate(180%)",
11
+ WebkitBackdropFilter: "blur(40px) saturate(180%)",
12
+ border: "0.5px solid rgba(255, 255, 255, 0.15)",
13
+ boxShadow: "0 2px 12px rgba(0, 0, 0, 0.1)",
14
+ };
15
+
16
+ const LIGHT_GLASS: React.CSSProperties = {
17
+ background: "rgba(255, 255, 255, 0.45)",
18
+ backdropFilter: "blur(40px) saturate(180%)",
19
+ WebkitBackdropFilter: "blur(40px) saturate(180%)",
20
+ border: "0.5px solid rgba(255, 255, 255, 0.5)",
21
+ boxShadow: "0 2px 16px rgba(0, 0, 0, 0.08)",
22
+ };
23
+
24
+ const PHONE_DARK_GLASS: React.CSSProperties = {
25
+ ...DARK_GLASS,
26
+ background: "rgba(60, 60, 64, 0.55)",
27
+ };
28
+
29
+ const Glyph = ({
30
+ size,
31
+ weight = 2,
32
+ className,
33
+ fill = "none",
34
+ children,
35
+ }: React.PropsWithChildren<{
36
+ size: number;
37
+ weight?: number;
38
+ className?: string;
39
+ fill?: string;
40
+ }>) => (
41
+ <svg
42
+ width={size}
43
+ height={size}
44
+ viewBox="0 0 24 24"
45
+ fill={fill}
46
+ stroke={fill === "none" ? "currentColor" : undefined}
47
+ strokeWidth={weight}
48
+ strokeLinecap="round"
49
+ strokeLinejoin="round"
50
+ className={`shrink-0 ${className ?? ""}`}
51
+ aria-hidden="true"
52
+ >
53
+ {children}
54
+ </svg>
55
+ );
56
+
57
+ const PageSearch = ({ size }: { size: number }) => (
58
+ <Glyph size={size}>
59
+ <path d="M19.1213 20.1213C17.9497 21.2929 16.0503 21.2929 14.8787 20.1213C13.7071 18.9497 13.7071 17.0503 14.8787 15.8787C16.0503 14.7071 17.9497 14.7071 19.1213 15.8787C20.2929 17.0503 20.2929 18.9497 19.1213 20.1213Z" />
60
+ <path d="M19 11V6C19 4.34315 17.6569 3 16 3H7C5.34315 3 4 4.34315 4 6V18C4 19.6569 5.34315 21 7 21H10.5" />
61
+ <path d="M8 15H9" />
62
+ <path d="M8 7H15" />
63
+ <path d="M8 11H11" />
64
+ <path d="M19.1211 20.1211L20.9998 21.9998" />
65
+ </Glyph>
66
+ );
67
+
68
+ const Reload = ({ size }: { size: number }) => (
69
+ <Glyph size={size} weight={2.5}>
70
+ <path d="M21 12a9 9 0 1 1-9-9c2.52 0 4.93 1 6.74 2.74L21 8" />
71
+ <path d="M21 3v5h-5" />
72
+ </Glyph>
73
+ );
74
+
75
+ const Back = ({ size, className }: { size: number; className?: string }) => (
76
+ <Glyph size={size} weight={2.5} className={className}>
77
+ <path d="m15 18-6-6 6-6" />
78
+ </Glyph>
79
+ );
80
+
81
+ const Forward = ({ size, className }: { size: number; className?: string }) => (
82
+ <Glyph size={size} weight={2.5} className={className}>
83
+ <path d="m9 18 6-6-6-6" />
84
+ </Glyph>
85
+ );
86
+
87
+ const Sidebar = ({ size }: { size: number }) => (
88
+ <Glyph size={size}>
89
+ <rect width="18" height="18" x="3" y="3" rx="2" />
90
+ <path d="M9 3v18" />
91
+ </Glyph>
92
+ );
93
+
94
+ const Share = ({ size }: { size: number }) => (
95
+ <Glyph size={size}>
96
+ <path d="M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8" />
97
+ <polyline points="16 6 12 2 8 6" />
98
+ <line x1="12" x2="12" y1="2" y2="15" />
99
+ </Glyph>
100
+ );
101
+
102
+ const NewTab = ({ size }: { size: number }) => (
103
+ <Glyph size={size} weight={2.5}>
104
+ <path d="M12 5v14" />
105
+ <path d="M5 12h14" />
106
+ </Glyph>
107
+ );
108
+
109
+ const Tabs = ({ size }: { size: number }) => (
110
+ <Glyph size={size}>
111
+ <rect width="14" height="14" x="8" y="8" rx="2" ry="2" />
112
+ <path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
113
+ </Glyph>
114
+ );
115
+
116
+ const More = ({ size }: { size: number }) => (
117
+ <Glyph size={size} fill="currentColor">
118
+ <circle cx="12" cy="5" r="2" />
119
+ <circle cx="12" cy="12" r="2" />
120
+ <circle cx="12" cy="19" r="2" />
121
+ </Glyph>
122
+ );
123
+
124
+ const Pill = ({
125
+ glass,
126
+ height,
127
+ grow = false,
128
+ padding,
129
+ gap,
130
+ children,
131
+ }: React.PropsWithChildren<{
132
+ glass: React.CSSProperties;
133
+ height: number;
134
+ grow?: boolean;
135
+ padding: number;
136
+ gap?: number;
137
+ }>) => (
138
+ <div
139
+ className={`flex items-center ${grow ? "min-w-0 flex-1" : "shrink-0 justify-center"}`}
140
+ style={{
141
+ height,
142
+ minWidth: height,
143
+ borderRadius: height / 2,
144
+ paddingLeft: padding,
145
+ paddingRight: padding,
146
+ gap,
147
+ ...glass,
148
+ }}
149
+ >
150
+ {children}
151
+ </div>
152
+ );
153
+
154
+ const Address = ({ size, fontSize, color }: { size: number; fontSize: number; color: string }) => (
155
+ <>
156
+ <PageSearch size={size} />
157
+ <span
158
+ className="min-w-0 flex-1 truncate text-center font-medium"
159
+ style={{ color, fontSize, letterSpacing: -0.2 }}
160
+ >
161
+ {SITE}
162
+ </span>
163
+ <Reload size={size - 2} />
164
+ </>
165
+ );
166
+
167
+ const DesktopTopBar = () => (
168
+ <div
169
+ className="absolute inset-x-0 top-0 flex items-center gap-2 text-white"
170
+ data-testid="browser-top-bar"
171
+ style={{
172
+ height: BROWSER_BAR.desktop,
173
+ background: BAR_BACKGROUND,
174
+ paddingLeft: 10,
175
+ paddingRight: 10,
176
+ }}
177
+ >
178
+ <div className="mx-1 flex shrink-0 gap-2">
179
+ <span className="size-3 rounded-full bg-[#FF5F56]" />
180
+ <span className="size-3 rounded-full bg-[#FFBD2E]" />
181
+ <span className="size-3 rounded-full bg-[#27C93F]" />
182
+ </div>
183
+ <Pill glass={DARK_GLASS} height={36} padding={10}>
184
+ <Sidebar size={18} />
185
+ </Pill>
186
+ <Pill glass={DARK_GLASS} height={36} padding={8} gap={4}>
187
+ <Back size={18} />
188
+ <Forward size={18} className="text-white/30" />
189
+ </Pill>
190
+ <Pill glass={DARK_GLASS} height={36} padding={12} grow>
191
+ <Address size={16} fontSize={14} color="rgba(255,255,255,0.85)" />
192
+ </Pill>
193
+ <Pill glass={DARK_GLASS} height={36} padding={10} gap={12}>
194
+ <Share size={16} />
195
+ <NewTab size={16} />
196
+ <Tabs size={16} />
197
+ </Pill>
198
+ </div>
199
+ );
200
+
201
+ const TabletTopBar = ({
202
+ height,
203
+ statusBar,
204
+ right,
205
+ }: {
206
+ height: number;
207
+ statusBar: number;
208
+ right: number;
209
+ }) => (
210
+ <div
211
+ className="absolute left-0 flex flex-col text-white"
212
+ data-testid="browser-top-bar"
213
+ style={{ top: statusBar, right, height: height - statusBar, background: BAR_BACKGROUND }}
214
+ >
215
+ <div className="flex flex-1 items-center gap-2 px-3">
216
+ <Pill glass={DARK_GLASS} height={40} padding={0}>
217
+ <Sidebar size={20} />
218
+ </Pill>
219
+ <Pill glass={DARK_GLASS} height={40} padding={0}>
220
+ <Back size={20} />
221
+ </Pill>
222
+ <Pill glass={DARK_GLASS} height={40} padding={12} grow>
223
+ <Address size={18} fontSize={15} color="rgba(255,255,255,0.85)" />
224
+ </Pill>
225
+ <Pill glass={DARK_GLASS} height={40} padding={12} gap={14}>
226
+ <Share size={18} />
227
+ <NewTab size={18} />
228
+ <Tabs size={18} />
229
+ </Pill>
230
+ </div>
231
+ </div>
232
+ );
233
+
234
+ const PhoneBottomBar = ({ dark }: { dark: boolean }) => {
235
+ const glass = dark ? PHONE_DARK_GLASS : LIGHT_GLASS;
236
+ return (
237
+ <div
238
+ className={`absolute inset-x-0 bottom-0 flex justify-center ${dark ? "text-white" : "text-black"}`}
239
+ data-testid="browser-bottom-bar"
240
+ style={{ height: BROWSER_BAR.phone }}
241
+ >
242
+ <div className="flex w-full items-center gap-2.5 px-4" style={{ height: 44, marginTop: 6 }}>
243
+ <Pill glass={glass} height={44} padding={0}>
244
+ <Back size={24} />
245
+ </Pill>
246
+ <Pill glass={glass} height={44} padding={14} grow>
247
+ <Address size={20} fontSize={16} color={dark ? "#ffffff" : "#000000"} />
248
+ </Pill>
249
+ <Pill glass={glass} height={44} padding={0}>
250
+ <More size={20} />
251
+ </Pill>
252
+ </div>
253
+ </div>
254
+ );
255
+ };
256
+
257
+ export const BrowserChrome = ({
258
+ kind,
259
+ top,
260
+ statusBar,
261
+ right = 0,
262
+ theme = "light",
263
+ }: {
264
+ kind: BrowserKind;
265
+ top: number;
266
+ statusBar: number;
267
+ right?: number;
268
+ theme?: "light" | "dark";
269
+ }) => (
270
+ <div className="pointer-events-none absolute inset-0" data-testid={`browser-chrome-${kind}`}>
271
+ {kind === "desktop" && <DesktopTopBar />}
272
+ {kind === "tablet" && <TabletTopBar height={top} statusBar={statusBar} right={right} />}
273
+ {kind === "phone" && <PhoneBottomBar dark={theme === "dark"} />}
274
+ </div>
275
+ );
@@ -1,8 +1,9 @@
1
1
  import * as React from "react";
2
2
  import type { HostDelegate, HostState } from "@superwall/host";
3
+ import { BrowserChrome } from "./BrowserChrome";
3
4
  import { DeviceChrome, duoLayout } from "./DeviceChrome";
4
5
  import { HostedIframe } from "./HostedIframe";
5
- import type { DeviceChromeSpec } from "../../lib/devices";
6
+ import type { BrowserLayout, DeviceChromeSpec } from "../../lib/devices";
6
7
  import { presentationRect, type StudioPresentation } from "../../lib/presentation";
7
8
 
8
9
  const SWAP_DELAY_MS = 250;
@@ -42,6 +43,7 @@ export const DeviceFrame = ({
42
43
  presentation,
43
44
  landscape = false,
44
45
  theme = "light",
46
+ browser,
45
47
  }: {
46
48
  width: number;
47
49
  height: number;
@@ -54,6 +56,7 @@ export const DeviceFrame = ({
54
56
  presentation?: StudioPresentation;
55
57
  landscape?: boolean;
56
58
  theme?: "light" | "dark";
59
+ browser?: BrowserLayout;
57
60
  }) => {
58
61
  const generation = React.useRef(0);
59
62
  const [frames, setFrames] = React.useState<Frame[]>([{ src, generation: 0, ready: true }]);
@@ -82,13 +85,13 @@ export const DeviceFrame = ({
82
85
 
83
86
  const visible = frames.findLast((frame) => frame.ready);
84
87
 
85
- const sheet = presentationRect(presentation, { width, height });
86
- const navigationBar = landscape ? 0 : (chrome?.navigationBarHeight ?? 0);
88
+ const sheet = browser ? undefined : presentationRect(presentation, { width, height });
89
+ const navigationBar = landscape || browser ? 0 : (chrome?.navigationBarHeight ?? 0);
87
90
  const page = sheet ?? {
88
91
  x: 0,
89
- y: 0,
92
+ y: browser?.top ?? 0,
90
93
  width,
91
- height: height - navigationBar,
94
+ height: height - navigationBar - (browser ? browser.top + browser.bottom : 0),
92
95
  radius: { top: 0, bottom: 0 },
93
96
  };
94
97
  const dark = theme === "dark";
@@ -102,7 +105,13 @@ export const DeviceFrame = ({
102
105
  <div
103
106
  className="relative flex-none overflow-hidden bg-background outline outline-foreground/20"
104
107
  data-testid="device-frame"
105
- style={{ width, height, borderRadius: radius }}
108
+ data-browser={browser ? browser.kind : undefined}
109
+ style={{
110
+ width,
111
+ height,
112
+ borderRadius: radius,
113
+ background: browser ? (dark ? "#000000" : "#ffffff") : undefined,
114
+ }}
106
115
  >
107
116
  {sheet && (
108
117
  <div
@@ -176,6 +185,15 @@ export const DeviceFrame = ({
176
185
  />
177
186
  </div>
178
187
  )}
188
+ {browser && (
189
+ <BrowserChrome
190
+ kind={browser.kind}
191
+ top={browser.top}
192
+ statusBar={browser.statusBar}
193
+ right={browser.right}
194
+ theme={theme}
195
+ />
196
+ )}
179
197
  </div>
180
198
  );
181
199
  };
@@ -158,17 +158,62 @@ export const isWebOnly = (platforms: readonly string[] | undefined): boolean =>
158
158
  export const chromeFor = (device: StudioDevice, web: boolean): DeviceChromeSpec | undefined =>
159
159
  web ? undefined : device.chrome;
160
160
 
161
- const BROWSER_CHROME = { iOS: 84, Android: 104, tablet: 66, desktop: 52 };
161
+ export type BrowserKind = "phone" | "tablet" | "desktop";
162
162
 
163
- export const webViewportFor = (device: StudioDevice): { width: number; height: number } => {
163
+ export type BrowserLayout = {
164
+ kind: BrowserKind;
165
+ top: number;
166
+ bottom: number;
167
+ statusBar: number;
168
+ right: number;
169
+ };
170
+
171
+ export const BROWSER_BAR = { phone: 84, tablet: 66, desktop: 52 } as const;
172
+
173
+ const ANDROID_BROWSER_BARS = { phone: 104, tablet: 66 } as const;
174
+
175
+ const isTablet = (device: StudioDevice) => device.width >= 600;
176
+
177
+ const browserKindOf = (device: StudioDevice): BrowserKind | undefined => {
178
+ if (device.device === undefined) return "desktop";
179
+ if (device.device.platform !== "iOS") return undefined;
180
+ return isTablet(device) ? "tablet" : "phone";
181
+ };
182
+
183
+ export const browserLayoutFor = (
184
+ device: StudioDevice,
185
+ { teleported, landscape = false }: { teleported: boolean; landscape?: boolean },
186
+ ): BrowserLayout | undefined => {
187
+ const kind = browserKindOf(device);
188
+ if (kind === undefined) return undefined;
164
189
  const statusBar = device.chrome?.statusBarHeight ?? 0;
190
+ if (kind === "tablet") {
191
+ const safeArea = safeAreaFor(device, landscape);
192
+ const top = Math.max(statusBar, safeArea?.top ?? 0);
193
+ return {
194
+ kind,
195
+ top: top + BROWSER_BAR.tablet,
196
+ bottom: 0,
197
+ statusBar: top,
198
+ right: safeArea?.right ?? 0,
199
+ };
200
+ }
201
+ if (kind === "desktop")
202
+ return { kind, top: BROWSER_BAR.desktop, bottom: 0, statusBar: 0, right: 0 };
203
+ if (teleported) return { kind, top: 0, bottom: 0, statusBar, right: 0 };
204
+ return landscape
205
+ ? undefined
206
+ : { kind, top: statusBar, bottom: BROWSER_BAR.phone, statusBar, right: 0 };
207
+ };
208
+
209
+ export const webViewportFor = (device: StudioDevice): { width: number; height: number } => {
210
+ const layout = browserLayoutFor(device, { teleported: false });
165
211
  const bars =
166
- device.device === undefined
167
- ? BROWSER_CHROME.desktop
168
- : device.width >= 600
169
- ? BROWSER_CHROME.tablet
170
- : BROWSER_CHROME[device.device.platform];
171
- return { width: device.width, height: device.height - statusBar - bars };
212
+ layout === undefined
213
+ ? (device.chrome?.statusBarHeight ?? 0) +
214
+ ANDROID_BROWSER_BARS[isTablet(device) ? "tablet" : "phone"]
215
+ : layout.top + layout.bottom;
216
+ return { width: device.width, height: device.height - bars };
172
217
  };
173
218
 
174
219
  const DESKTOP_HOST = {
@@ -78,6 +78,15 @@ const periodCounts = (period: string, count: number): Record<string, number> | u
78
78
  };
79
79
  };
80
80
 
81
+ const periodLabels = (period: string, count: number): { period: string; periodly: string } => {
82
+ if (count === 1 || !PERIOD_UNITS[period]) {
83
+ return { period, periodly: PERIODLY[period] ?? `${period}ly` };
84
+ }
85
+ if (period === "month" && count === 3) return { period: "quarter", periodly: "quarterly" };
86
+ const label = `${count} ${period}s`;
87
+ return { period: label, periodly: `every ${label}` };
88
+ };
89
+
81
90
  export const productVariables = (product: DashboardProduct): Record<string, unknown> => {
82
91
  const variables: Record<string, unknown> = {};
83
92
  if (product.identifier) variables.identifier = product.identifier;
@@ -96,8 +105,7 @@ export const productVariables = (product: DashboardProduct): Record<string, unkn
96
105
  if (!period) return variables;
97
106
 
98
107
  const count = product.subscription?.period_count ?? 1;
99
- variables.period = period;
100
- variables.periodly = PERIODLY[period] ?? `${period}ly`;
108
+ Object.assign(variables, periodLabels(period, count));
101
109
  Object.assign(variables, periodCounts(period, count) ?? {});
102
110
 
103
111
  const perYear = (PER_YEAR[period] ?? 1) / count;
@@ -0,0 +1,85 @@
1
+ import * as React from "react";
2
+ import type { TOutgoingMessage } from "@superwall/host";
3
+
4
+ const ROUTE_QUERY_KEY = "sw_nav";
5
+
6
+ export type PreviewPlace =
7
+ | { where: "app"; teleported: boolean }
8
+ | { where: "browser"; teleported: true; route?: string; webDepth?: number };
9
+
10
+ export type TeleportChange = "teleported" | "returned";
11
+
12
+ export const IN_APP: PreviewPlace = { where: "app", teleported: false };
13
+
14
+ const routeOf = (url: string): string | undefined => {
15
+ try {
16
+ return new URL(url).searchParams.get(ROUTE_QUERY_KEY) ?? undefined;
17
+ } catch {
18
+ return undefined;
19
+ }
20
+ };
21
+
22
+ export const teleportStep = (
23
+ place: PreviewPlace,
24
+ message: TOutgoingMessage,
25
+ ): { place: PreviewPlace; change?: TeleportChange } => {
26
+ if (message.event_name === "open_url_external" && message.teleport === true) {
27
+ const route = routeOf(message.url);
28
+ return {
29
+ place: { where: "browser", teleported: true, ...(route === undefined ? {} : { route }) },
30
+ change: "teleported",
31
+ };
32
+ }
33
+ if (place.where !== "browser") return { place };
34
+ if (message.event_name === "close") return { place: { where: "app", teleported: true } };
35
+ if (message.event_name !== "page_view") return { place };
36
+ if (message.type === "entry") return { place: { where: "app", teleported: true } };
37
+ if (place.webDepth === undefined) {
38
+ return { place: { ...place, route: message.page_name, webDepth: message.flow_position } };
39
+ }
40
+ if (message.flow_position < place.webDepth) {
41
+ return { place: { where: "app", teleported: true }, change: "returned" };
42
+ }
43
+ return { place: { ...place, route: message.page_name } };
44
+ };
45
+
46
+ export type TeleportTracker = {
47
+ getSnapshot: () => PreviewPlace;
48
+ subscribe: (listener: () => void) => () => void;
49
+ feed: (message: TOutgoingMessage) => TeleportChange | undefined;
50
+ reset: () => void;
51
+ };
52
+
53
+ export const createTeleportTracker = (): TeleportTracker => {
54
+ let place: PreviewPlace = IN_APP;
55
+ const listeners = new Set<() => void>();
56
+ const set = (next: PreviewPlace) => {
57
+ if (next === place) return;
58
+ place = next;
59
+ for (const listener of listeners) listener();
60
+ };
61
+ return {
62
+ getSnapshot: () => place,
63
+ subscribe: (listener) => {
64
+ listeners.add(listener);
65
+ return () => {
66
+ listeners.delete(listener);
67
+ };
68
+ },
69
+ feed: (message) => {
70
+ const step = teleportStep(place, message);
71
+ set(step.place);
72
+ return step.change;
73
+ },
74
+ reset: () => set(IN_APP),
75
+ };
76
+ };
77
+
78
+ export const usePreviewPlace = (
79
+ resetKey: string,
80
+ ): { tracker: TeleportTracker; place: PreviewPlace } => {
81
+ const tracker = React.useMemo(createTeleportTracker, []);
82
+ React.useEffect(() => tracker.reset(), [tracker, resetKey]);
83
+ const place = React.useSyncExternalStore(tracker.subscribe, tracker.getSnapshot);
84
+ return { tracker, place };
85
+ };