@superwall/studio 1.3.0 → 1.5.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.
Files changed (62) hide show
  1. package/README.md +2 -2
  2. package/dist/assets/index-Bi2YHYVJ.js +11 -0
  3. package/dist/assets/index-DrsTCwot.css +1 -0
  4. package/dist/index.html +2 -2
  5. package/dist/lib/components/DownloadScreenshot.d.ts +6 -0
  6. package/dist/lib/components/PreviewOnDevice.d.ts +6 -0
  7. package/dist/lib/components/Tooltip.d.ts +5 -0
  8. package/dist/lib/components/chrome/Toolbar.d.ts +1 -0
  9. package/dist/lib/components/chrome/TopBar.d.ts +1 -0
  10. package/dist/lib/components/icons.d.ts +1 -0
  11. package/dist/lib/components/menus/CompareMenu.d.ts +2 -1
  12. package/dist/lib/components/preview/Canvas.d.ts +18 -0
  13. package/dist/lib/components/preview/DeviceFrame.d.ts +4 -4
  14. package/dist/lib/components/preview/ResizeHandles.d.ts +13 -0
  15. package/dist/lib/index.d.ts +5 -1
  16. package/dist/lib/index.js +2924 -1229
  17. package/dist/lib/lib/actions.d.ts +1 -0
  18. package/dist/lib/lib/canvas.d.ts +27 -0
  19. package/dist/lib/lib/contract.d.ts +7 -1
  20. package/dist/lib/lib/devices.d.ts +9 -0
  21. package/dist/lib/lib/host.d.ts +2 -1
  22. package/dist/lib/lib/notify.d.ts +4 -0
  23. package/dist/lib/lib/presentation.d.ts +23 -0
  24. package/dist/lib/lib/preview.d.ts +1 -0
  25. package/dist/lib/lib/project.d.ts +1 -0
  26. package/dist/lib/lib/scheme.d.ts +11 -0
  27. package/dist/lib/lib/surfaces.d.ts +26 -2
  28. package/dist/lib/studio.css +1 -1
  29. package/package.json +3 -2
  30. package/src/components/DownloadScreenshot.tsx +133 -0
  31. package/src/components/OpenInApp.tsx +5 -25
  32. package/src/components/PaywallToasts.tsx +5 -3
  33. package/src/components/PreviewOnDevice.tsx +133 -0
  34. package/src/components/SurfaceCard.tsx +1 -1
  35. package/src/components/Tooltip.tsx +27 -0
  36. package/src/components/chrome/Toolbar.tsx +47 -6
  37. package/src/components/chrome/TopBar.tsx +37 -3
  38. package/src/components/icons.tsx +6 -0
  39. package/src/components/menus/ActionMenu.tsx +8 -8
  40. package/src/components/menus/CompareMenu.tsx +49 -30
  41. package/src/components/panels/MissingProducts.tsx +1 -1
  42. package/src/components/preview/Canvas.tsx +426 -0
  43. package/src/components/preview/DeviceFrame.tsx +83 -74
  44. package/src/components/preview/ResizeHandles.tsx +205 -0
  45. package/src/index.ts +6 -0
  46. package/src/lib/actions.ts +7 -0
  47. package/src/lib/canvas.ts +60 -0
  48. package/src/lib/contract.ts +8 -1
  49. package/src/lib/devices.ts +52 -4
  50. package/src/lib/host.ts +7 -2
  51. package/src/lib/notify.tsx +31 -0
  52. package/src/lib/presentation.ts +102 -0
  53. package/src/lib/preview.ts +3 -0
  54. package/src/lib/project.ts +45 -1
  55. package/src/lib/scheme.ts +31 -0
  56. package/src/lib/surfaces.ts +42 -1
  57. package/src/main.tsx +5 -1
  58. package/src/screens/SurfaceEditor.tsx +219 -52
  59. package/src/screens/SurfaceList.tsx +2 -2
  60. package/src/styles.css +89 -8
  61. package/dist/assets/index-CEz-HnIH.js +0 -11
  62. package/dist/assets/index-_HCp6gCi.css +0 -1
@@ -0,0 +1,205 @@
1
+ import * as React from "react";
2
+
3
+ export type FrameSize = { width: number; height: number };
4
+
5
+ type Horizontal = "left" | "right";
6
+ type Vertical = "top" | "bottom";
7
+ type Handle = {
8
+ id: string;
9
+ label: string;
10
+ x?: Horizontal;
11
+ y?: Vertical;
12
+ className: string;
13
+ cursor: string;
14
+ };
15
+
16
+ const HANDLES: readonly Handle[] = [
17
+ {
18
+ id: "top",
19
+ label: "Resize height from the top",
20
+ y: "top",
21
+ className: "-top-2 left-0 h-4 w-full",
22
+ cursor: "ns-resize",
23
+ },
24
+ {
25
+ id: "bottom",
26
+ label: "Resize height",
27
+ y: "bottom",
28
+ className: "-bottom-2 left-0 h-4 w-full",
29
+ cursor: "ns-resize",
30
+ },
31
+ {
32
+ id: "left",
33
+ label: "Resize width from the left",
34
+ x: "left",
35
+ className: "top-0 -left-2 h-full w-4",
36
+ cursor: "ew-resize",
37
+ },
38
+ {
39
+ id: "right",
40
+ label: "Resize width",
41
+ x: "right",
42
+ className: "top-0 -right-2 h-full w-4",
43
+ cursor: "ew-resize",
44
+ },
45
+ {
46
+ id: "top-left",
47
+ label: "Resize from the top left",
48
+ x: "left",
49
+ y: "top",
50
+ className: "-top-2 -left-2 h-5 w-5",
51
+ cursor: "nwse-resize",
52
+ },
53
+ {
54
+ id: "top-right",
55
+ label: "Resize from the top right",
56
+ x: "right",
57
+ y: "top",
58
+ className: "-top-2 -right-2 h-5 w-5",
59
+ cursor: "nesw-resize",
60
+ },
61
+ {
62
+ id: "bottom-left",
63
+ label: "Resize from the bottom left",
64
+ x: "left",
65
+ y: "bottom",
66
+ className: "-bottom-2 -left-2 h-5 w-5",
67
+ cursor: "nesw-resize",
68
+ },
69
+ {
70
+ id: "bottom-right",
71
+ label: "Resize",
72
+ x: "right",
73
+ y: "bottom",
74
+ className: "-right-2 -bottom-2 h-5 w-5",
75
+ cursor: "nwse-resize",
76
+ },
77
+ ];
78
+
79
+ type Drag = {
80
+ pointerId: number;
81
+ handle: Handle;
82
+ from: { x: number; y: number };
83
+ start: FrameSize;
84
+ last: FrameSize;
85
+ release: () => void;
86
+ };
87
+
88
+ export const ResizeHandles = ({
89
+ size,
90
+ scale,
91
+ clamp,
92
+ onResize,
93
+ onDragStart,
94
+ onShift,
95
+ }: {
96
+ size: FrameSize;
97
+ scale: number;
98
+ clamp: (value: number, fallback: number) => number;
99
+ onResize: (next: FrameSize) => void;
100
+ onDragStart?: () => void;
101
+ onShift?: (dx: number, dy: number) => void;
102
+ }) => {
103
+ const drag = React.useRef<Drag | null>(null);
104
+
105
+ const end = React.useCallback(() => {
106
+ drag.current?.release();
107
+ drag.current = null;
108
+ }, []);
109
+
110
+ React.useEffect(() => end, [end]);
111
+
112
+ const onPointerDown = (handle: Handle) => (event: React.PointerEvent<HTMLDivElement>) => {
113
+ if (event.pointerType === "mouse" && event.button !== 0) return;
114
+ event.stopPropagation();
115
+ event.preventDefault();
116
+ const element = event.currentTarget;
117
+ try {
118
+ element.setPointerCapture(event.pointerId);
119
+ } catch {
120
+ return;
121
+ }
122
+ end();
123
+ const pointerId = event.pointerId;
124
+ const released = (release: PointerEvent) => {
125
+ if (release.pointerId === pointerId) end();
126
+ };
127
+ const finish = () => end();
128
+ window.addEventListener("pointerup", released);
129
+ window.addEventListener("pointercancel", released);
130
+ window.addEventListener("blur", finish);
131
+ element.addEventListener("lostpointercapture", finish);
132
+ drag.current = {
133
+ pointerId,
134
+ handle,
135
+ from: { x: event.clientX, y: event.clientY },
136
+ start: size,
137
+ last: size,
138
+ release: () => {
139
+ window.removeEventListener("pointerup", released);
140
+ window.removeEventListener("pointercancel", released);
141
+ window.removeEventListener("blur", finish);
142
+ element.removeEventListener("lostpointercapture", finish);
143
+ },
144
+ };
145
+ onDragStart?.();
146
+ };
147
+
148
+ const onPointerMove = (event: React.PointerEvent<HTMLDivElement>) => {
149
+ const current = drag.current;
150
+ if (!current || current.pointerId !== event.pointerId) return;
151
+ event.stopPropagation();
152
+ if (event.pointerType === "mouse" && event.buttons === 0) {
153
+ end();
154
+ return;
155
+ }
156
+ const { handle, start } = current;
157
+ const dx = (event.clientX - current.from.x) / scale;
158
+ const dy = (event.clientY - current.from.y) / scale;
159
+ const next: FrameSize = {
160
+ width:
161
+ handle.x === "right"
162
+ ? clamp(Math.round(start.width + dx), start.width)
163
+ : handle.x === "left"
164
+ ? clamp(Math.round(start.width - dx), start.width)
165
+ : start.width,
166
+ height:
167
+ handle.y === "bottom"
168
+ ? clamp(Math.round(start.height + dy), start.height)
169
+ : handle.y === "top"
170
+ ? clamp(Math.round(start.height - dy), start.height)
171
+ : start.height,
172
+ };
173
+ const shiftX = handle.x === "left" ? -(next.width - current.last.width) * scale : 0;
174
+ const shiftY = handle.y === "top" ? -(next.height - current.last.height) * scale : 0;
175
+ current.last = next;
176
+ onResize(next);
177
+ if (shiftX !== 0 || shiftY !== 0) onShift?.(shiftX, shiftY);
178
+ };
179
+
180
+ const onPointerUp = (event: React.PointerEvent<HTMLDivElement>) => {
181
+ if (drag.current?.pointerId !== event.pointerId) return;
182
+ event.stopPropagation();
183
+ end();
184
+ };
185
+
186
+ return (
187
+ <>
188
+ {HANDLES.map((handle) => (
189
+ <div
190
+ key={handle.id}
191
+ role="separator"
192
+ aria-label={handle.label}
193
+ aria-orientation={handle.y && !handle.x ? "horizontal" : "vertical"}
194
+ data-testid={`resize-${handle.id}`}
195
+ className={`absolute z-10 touch-none select-none ${handle.className}`}
196
+ style={{ cursor: handle.cursor }}
197
+ onPointerDown={onPointerDown(handle)}
198
+ onPointerMove={onPointerMove}
199
+ onPointerUp={onPointerUp}
200
+ onPointerCancel={onPointerUp}
201
+ />
202
+ ))}
203
+ </>
204
+ );
205
+ };
package/src/index.ts CHANGED
@@ -7,6 +7,9 @@ export { DeviceFrame } from "./components/preview/DeviceFrame";
7
7
  export { HostedIframe } from "./components/preview/HostedIframe";
8
8
  export { PaywallToasts, createStudioDelegate, useStudioDelegate } from "./components/PaywallToasts";
9
9
  export { StudioShell } from "./components/chrome/StudioShell";
10
+ export { DownloadScreenshot } from "./components/DownloadScreenshot";
11
+ export { PreviewOnDevice } from "./components/PreviewOnDevice";
12
+ export { Tooltip } from "./components/Tooltip";
10
13
  export { TopBar } from "./components/chrome/TopBar";
11
14
  export { VariablesPanel } from "./components/panels/VariablesPanel";
12
15
  export { Segmented, Select, Switch } from "./components/controls";
@@ -14,6 +17,7 @@ export { DEFAULT_DEVICE_ID, deviceById, STUDIO_DEVICES } from "./lib/devices";
14
17
  export type { StudioDevice } from "./lib/devices";
15
18
  export { hostStateFor, studioDefaults } from "./lib/host";
16
19
  export { hasActions } from "./lib/surfaces";
20
+ export { devServerLink } from "./lib/scheme";
17
21
  export {
18
22
  StudioHostProvider,
19
23
  useStudioHome,
@@ -40,9 +44,11 @@ export type {
40
44
  StudioActionName,
41
45
  StudioActionProgress,
42
46
  StudioActionResult,
47
+ StudioPresentation,
43
48
  StudioSource,
44
49
  StudioStatus,
45
50
  StudioSurface,
46
51
  StudioSurfaceKind,
52
+ StudioVersion,
47
53
  } from "./lib/surfaces";
48
54
  export type { SampleDefaults, VariableOverrides } from "./lib/variables";
@@ -68,6 +68,13 @@ export const fetchStatus = async (surface: StudioSurface): Promise<StudioStatus>
68
68
  }
69
69
  };
70
70
 
71
+ export const fetchScreenshot = async (surface: StudioSurface): Promise<string | undefined> => {
72
+ const response = await fetch(`/__studio/screenshot${surfacePath(surface)}`);
73
+ const value = (await response.json()) as { url?: string; unreachable?: string };
74
+ if (value.unreachable !== undefined) throw new Error(value.unreachable);
75
+ return value.url;
76
+ };
77
+
71
78
  export const fetchSamples = async (): Promise<Samples> => {
72
79
  try {
73
80
  const response = await fetch("/__studio/samples.json");
@@ -0,0 +1,60 @@
1
+ export type CanvasTransform = { scale: number; x: number; y: number };
2
+
3
+ export type Size = { width: number; height: number };
4
+
5
+ export const MIN_SCALE = 0.1;
6
+ export const MAX_SCALE = 4;
7
+ export const FIT_INSET = { top: 48, right: 48, bottom: 48, left: 48 };
8
+
9
+ const STEP_FACTOR = 1.25;
10
+
11
+ export const clampScale = (scale: number): number =>
12
+ Math.min(MAX_SCALE, Math.max(MIN_SCALE, scale));
13
+
14
+ export const fitTransform = (content: Size, viewport: Size): CanvasTransform => {
15
+ if (content.width <= 0 || content.height <= 0 || viewport.width <= 0 || viewport.height <= 0) {
16
+ return { scale: 1, x: 0, y: 0 };
17
+ }
18
+ const room = insetRoom(viewport);
19
+ const scale = clampScale(Math.min(1, room.width / content.width, room.height / content.height));
20
+ return centered(scale, content, viewport);
21
+ };
22
+
23
+ const insetRoom = (viewport: Size): Size => ({
24
+ width: viewport.width - FIT_INSET.left - FIT_INSET.right,
25
+ height: viewport.height - FIT_INSET.top - FIT_INSET.bottom,
26
+ });
27
+
28
+ export const centered = (scale: number, content: Size, viewport: Size): CanvasTransform => {
29
+ const room = insetRoom(viewport);
30
+ return {
31
+ scale,
32
+ x: FIT_INSET.left + (room.width - content.width * scale) / 2,
33
+ y: FIT_INSET.top + (room.height - content.height * scale) / 2,
34
+ };
35
+ };
36
+
37
+ export const zoomAt = (
38
+ transform: CanvasTransform,
39
+ factor: number,
40
+ point: { x: number; y: number },
41
+ ): CanvasTransform => {
42
+ const scale = clampScale(transform.scale * factor);
43
+ const ratio = scale / transform.scale;
44
+ return {
45
+ scale,
46
+ x: point.x - (point.x - transform.x) * ratio,
47
+ y: point.y - (point.y - transform.y) * ratio,
48
+ };
49
+ };
50
+
51
+ export const panBy = (transform: CanvasTransform, dx: number, dy: number): CanvasTransform => ({
52
+ ...transform,
53
+ x: transform.x + dx,
54
+ y: transform.y + dy,
55
+ });
56
+
57
+ export const stepScale = (scale: number, direction: 1 | -1): number =>
58
+ clampScale(Math.round((direction === 1 ? scale * STEP_FACTOR : scale / STEP_FACTOR) * 100) / 100);
59
+
60
+ export const percent = (scale: number): string => `${Math.round(scale * 100)}%`;
@@ -6,10 +6,17 @@ export type StudioActionResult =
6
6
 
7
7
  export type StudioActionProgress = (step: string, state: "start" | "done") => void;
8
8
 
9
+ export type StudioVersion = {
10
+ version: number;
11
+ url: string;
12
+ live: boolean;
13
+ pushedAt?: string;
14
+ };
15
+
9
16
  export type StudioStatus = {
10
17
  paywallId?: string;
11
18
  identifier?: string;
12
- liveUrl?: string;
19
+ versions?: StudioVersion[];
13
20
  active?: boolean;
14
21
  updatedAt?: string;
15
22
  unreachable?: string;
@@ -3,6 +3,7 @@ export type DeviceChromeSpec = {
3
3
  cutout?: "island" | "punch";
4
4
  statusBarHeight: number;
5
5
  homeIndicator: boolean;
6
+ navigationBarHeight?: number;
6
7
  };
7
8
 
8
9
  export type StudioDevice = {
@@ -14,6 +15,7 @@ export type StudioDevice = {
14
15
  platform: "iOS" | "Android";
15
16
  deviceModel: string;
16
17
  osVersion: string;
18
+ interfaceType?: string;
17
19
  };
18
20
  chrome?: DeviceChromeSpec;
19
21
  };
@@ -64,7 +66,13 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
64
66
  width: 412,
65
67
  height: 915,
66
68
  device: { platform: "Android", deviceModel: "Pixel 10", osVersion: "16" },
67
- chrome: { radius: 28, cutout: "punch", statusBarHeight: 48, homeIndicator: true },
69
+ chrome: {
70
+ radius: 28,
71
+ cutout: "punch",
72
+ statusBarHeight: 24,
73
+ homeIndicator: true,
74
+ navigationBarHeight: 24,
75
+ },
68
76
  },
69
77
  {
70
78
  id: "galaxy-s25",
@@ -72,14 +80,20 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
72
80
  width: 360,
73
81
  height: 780,
74
82
  device: { platform: "Android", deviceModel: "SM-S931B", osVersion: "15" },
75
- chrome: { radius: 26, cutout: "punch", statusBarHeight: 40, homeIndicator: true },
83
+ chrome: {
84
+ radius: 26,
85
+ cutout: "punch",
86
+ statusBarHeight: 24,
87
+ homeIndicator: true,
88
+ navigationBarHeight: 24,
89
+ },
76
90
  },
77
91
  {
78
92
  id: "ipad-mini",
79
93
  name: "iPad mini",
80
94
  width: 744,
81
95
  height: 1133,
82
- device: { platform: "iOS", deviceModel: "iPad14,1", osVersion: "26.0" },
96
+ device: { platform: "iOS", deviceModel: "iPad14,1", osVersion: "26.0", interfaceType: "ipad" },
83
97
  chrome: { radius: 21, statusBarHeight: 24, homeIndicator: true },
84
98
  },
85
99
  {
@@ -87,7 +101,7 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
87
101
  name: "iPad Pro 11″",
88
102
  width: 834,
89
103
  height: 1194,
90
- device: { platform: "iOS", deviceModel: "iPad16,3", osVersion: "26.0" },
104
+ device: { platform: "iOS", deviceModel: "iPad16,3", osVersion: "26.0", interfaceType: "ipad" },
91
105
  chrome: { radius: 18, statusBarHeight: 24, homeIndicator: true },
92
106
  },
93
107
  {
@@ -100,6 +114,40 @@ export const STUDIO_DEVICES: readonly [StudioDevice, ...StudioDevice[]] = [
100
114
 
101
115
  export const DEFAULT_DEVICE_ID = "iphone-17";
102
116
 
117
+ export const isWebOnly = (platforms: readonly string[] | undefined): boolean =>
118
+ platforms !== undefined &&
119
+ platforms.length > 0 &&
120
+ platforms.every((platform) => platform.toLowerCase() === "web");
121
+
122
+ export const chromeFor = (device: StudioDevice, web: boolean): DeviceChromeSpec | undefined =>
123
+ web ? undefined : device.chrome;
124
+
125
+ const BROWSER_CHROME = { iOS: 84, Android: 104, tablet: 66, desktop: 52 };
126
+
127
+ export const webViewportFor = (device: StudioDevice): { width: number; height: number } => {
128
+ const statusBar = device.chrome?.statusBarHeight ?? 0;
129
+ const bars =
130
+ device.device === undefined
131
+ ? BROWSER_CHROME.desktop
132
+ : device.width >= 600
133
+ ? BROWSER_CHROME.tablet
134
+ : BROWSER_CHROME[device.device.platform];
135
+ return { width: device.width, height: device.height - statusBar - bars };
136
+ };
137
+
138
+ const DESKTOP_HOST = {
139
+ platform: "iOS",
140
+ deviceModel: "Mac",
141
+ osVersion: "26.0",
142
+ interfaceType: "mac",
143
+ };
144
+
145
+ export const deviceVariablesFor = (
146
+ device: StudioDevice,
147
+ web: boolean,
148
+ ): Record<string, unknown> | undefined =>
149
+ web ? { ...device.device, platform: "web" } : (device.device ?? DESKTOP_HOST);
150
+
103
151
  export const RESPONSIVE_ID = "responsive";
104
152
  export const RESPONSIVE_DEFAULT = { width: 393, height: 852 };
105
153
 
package/src/lib/host.ts CHANGED
@@ -27,6 +27,11 @@ export const studioDefaults = (): SampleDefaults => ({
27
27
  params: DEFAULT_PARAMS,
28
28
  });
29
29
 
30
+ export const previewDevice = (device: HostVariables): HostVariables => {
31
+ const { publicApiKey: _key, ...rest } = device;
32
+ return rest;
33
+ };
34
+
30
35
  export const hostStateFor = ({
31
36
  surface,
32
37
  defaults,
@@ -37,7 +42,7 @@ export const hostStateFor = ({
37
42
  locale,
38
43
  edited,
39
44
  }: HostInputs): HostState => {
40
- const device: HostVariables = {
45
+ const device: HostVariables = previewDevice({
41
46
  ...defaults.device,
42
47
  ...sourceSamples?.device,
43
48
  ...samples.device,
@@ -45,7 +50,7 @@ export const hostStateFor = ({
45
50
  interfaceStyle: theme === "dark" ? "Dark" : "Light",
46
51
  ...(locale ? { deviceLocale: locale, deviceLanguageCode: locale.split(/[-_]/)[0] } : {}),
47
52
  ...edited.device,
48
- };
53
+ });
49
54
  const user: HostVariables = {
50
55
  ...defaults.user,
51
56
  ...sourceSamples?.user,
@@ -0,0 +1,31 @@
1
+ import * as React from "react";
2
+ import { toast } from "sonner";
3
+ import { AlertTriangleIcon, CheckCircleIcon, InfoCircleIcon } from "../components/icons";
4
+
5
+ type Tone = "info" | "success" | "warning";
6
+
7
+ const TINT: Record<Tone, string> = {
8
+ info: "text-cream/45",
9
+ success: "text-accent",
10
+ warning: "text-red",
11
+ };
12
+
13
+ const ICON: Record<Tone, () => React.ReactElement> = {
14
+ info: InfoCircleIcon,
15
+ success: CheckCircleIcon,
16
+ warning: AlertTriangleIcon,
17
+ };
18
+
19
+ export const notify = (message: React.ReactNode, tone: Tone = "info") => {
20
+ const Icon = ICON[tone];
21
+ toast.custom(() => (
22
+ <div className="bg-panel font-sans w-[344px] rounded border border-cream/15 p-3">
23
+ <div className="grid grid-cols-[14px_1fr] items-start gap-x-2.5">
24
+ <span className={`flex h-5 items-center justify-center ${TINT[tone]}`}>
25
+ <Icon />
26
+ </span>
27
+ <span className="text-[0.8125rem] leading-5 font-medium text-cream">{message}</span>
28
+ </div>
29
+ </div>
30
+ ));
31
+ };
@@ -0,0 +1,102 @@
1
+ export type StudioPresentation = {
2
+ style: "fullscreen" | "modal" | "push" | "noAnimation" | "drawer" | "popup";
3
+ width?: number;
4
+ height?: number;
5
+ cornerRadius?: number;
6
+ };
7
+
8
+ export type PresentationRect = {
9
+ x: number;
10
+ y: number;
11
+ width: number;
12
+ height: number;
13
+ radius: { top: number; bottom: number };
14
+ underlay: boolean;
15
+ };
16
+
17
+ const DEFAULT_RADIUS = 15;
18
+ const DRAWER_DEFAULT_HEIGHT = 70;
19
+ const POPUP_DEFAULT = { width: 80, height: 60 };
20
+ const TABLET_MIN_WIDTH = 600;
21
+
22
+ export const modalHeightPercent = (width: number, height: number): number => {
23
+ const offset = 0.1 * (width / height) + 0.035;
24
+ return Math.round((100 - offset * 100) * 10) / 10;
25
+ };
26
+
27
+ export const presentationRect = (
28
+ presentation: StudioPresentation | undefined,
29
+ frame: { width: number; height: number },
30
+ ): PresentationRect | undefined => {
31
+ if (!presentation) return undefined;
32
+ const radius = presentation.cornerRadius ?? DEFAULT_RADIUS;
33
+ const tablet = Math.min(frame.width, frame.height) >= TABLET_MIN_WIDTH;
34
+ const centered = (widthPercent: number, heightPercent: number): PresentationRect => {
35
+ const width = Math.round((frame.width * widthPercent) / 100);
36
+ const height = Math.round((frame.height * heightPercent) / 100);
37
+ return {
38
+ x: Math.round((frame.width - width) / 2),
39
+ y: Math.round((frame.height - height) / 2),
40
+ width,
41
+ height,
42
+ radius: { top: radius, bottom: radius },
43
+ underlay: false,
44
+ };
45
+ };
46
+ const bottom = (heightPercent: number, underlay: boolean): PresentationRect => {
47
+ const height = Math.round((frame.height * heightPercent) / 100);
48
+ return {
49
+ x: 0,
50
+ y: frame.height - height,
51
+ width: frame.width,
52
+ height,
53
+ radius: { top: radius, bottom: 0 },
54
+ underlay,
55
+ };
56
+ };
57
+
58
+ switch (presentation.style) {
59
+ case "modal":
60
+ return tablet
61
+ ? centered(70, 80)
62
+ : bottom(modalHeightPercent(frame.width, frame.height), true);
63
+ case "drawer":
64
+ return bottom(presentation.height ?? DRAWER_DEFAULT_HEIGHT, false);
65
+ case "popup":
66
+ return centered(
67
+ presentation.width ?? POPUP_DEFAULT.width,
68
+ presentation.height ?? POPUP_DEFAULT.height,
69
+ );
70
+ default:
71
+ return undefined;
72
+ }
73
+ };
74
+
75
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
76
+ typeof value === "object" && value !== null && !Array.isArray(value);
77
+
78
+ const number = (value: unknown): number | undefined =>
79
+ typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined;
80
+
81
+ const STYLES: Record<string, StudioPresentation["style"]> = {
82
+ FULLSCREEN: "fullscreen",
83
+ MODAL: "modal",
84
+ PUSH: "push",
85
+ NO_ANIMATION: "noAnimation",
86
+ DRAWER: "drawer",
87
+ POPUP: "popup",
88
+ };
89
+
90
+ export const parsePresentation = (value: unknown): StudioPresentation | undefined => {
91
+ if (!isRecord(value) || typeof value.type !== "string") return undefined;
92
+ const style = STYLES[value.type];
93
+ if (!style) return undefined;
94
+ return {
95
+ style,
96
+ ...(number(value.width) === undefined ? {} : { width: number(value.width) }),
97
+ ...(number(value.height) === undefined ? {} : { height: number(value.height) }),
98
+ ...(number(value.corner_radius) === undefined
99
+ ? {}
100
+ : { cornerRadius: number(value.corner_radius) }),
101
+ };
102
+ };
@@ -8,6 +8,9 @@ export const hostedUrl = (url: string): string => {
8
8
  return `${url}${url.includes("?") ? "&" : "?"}transport=editor`;
9
9
  };
10
10
 
11
+ export const originalUrl = (surface: StudioSurface): string =>
12
+ `/__studio/original${surfacePath(surface)}`;
13
+
11
14
  export const buildPreviewPath: PreviewUrlBuilder = (surface) =>
12
15
  hostedUrl(`/preview${surfacePath(surface)}`);
13
16
 
@@ -1,9 +1,16 @@
1
- import type { StudioSurface, StudioSurfaceKind } from "./surfaces";
1
+ import { parsePresentation } from "./presentation";
2
+ import type {
3
+ StudioOrigin,
4
+ StudioScreenFramework,
5
+ StudioSurface,
6
+ StudioSurfaceKind,
7
+ } from "./surfaces";
2
8
  import type { SampleDefaults } from "./variables";
3
9
 
4
10
  export type StudioProjectPayload = {
5
11
  title: string;
6
12
  scheme?: string;
13
+ networkOrigin?: string;
7
14
  surfaces: StudioSurface[];
8
15
  samples: SampleDefaults;
9
16
  diagnostics: string[];
@@ -27,6 +34,10 @@ const parseSurface = (value: unknown): StudioSurface | undefined => {
27
34
  const kind: StudioSurfaceKind | undefined =
28
35
  value.kind === "paywall" || value.kind === "funnel" ? value.kind : undefined;
29
36
  if (!kind || typeof value.id !== "string") return undefined;
37
+ const presentation = parsePresentation(
38
+ isRecord(value.settings) ? value.settings.presentation_style : undefined,
39
+ );
40
+ const origin = parseOrigin(value.origin);
30
41
  return {
31
42
  kind,
32
43
  id: value.id,
@@ -35,10 +46,42 @@ const parseSurface = (value: unknown): StudioSurface | undefined => {
35
46
  products: isStringRecord(value.products) ? value.products : {},
36
47
  locales: isStringArray(value.locales) ? value.locales : [],
37
48
  ...(isStringArray(value.platforms) ? { platforms: value.platforms } : {}),
49
+ ...(presentation === undefined ? {} : { presentation }),
38
50
  paywallId: typeof value.paywallId === "string" ? value.paywallId : undefined,
51
+ ...(origin === undefined ? {} : { origin }),
39
52
  };
40
53
  };
41
54
 
55
+ const isFileName = (value: unknown): value is string =>
56
+ typeof value === "string" && value !== "" && !/[\\/]/.test(value) && !value.startsWith(".");
57
+
58
+ const SCREEN_FRAMEWORKS: ReadonlySet<string> = new Set([
59
+ "swiftui",
60
+ "uikit",
61
+ "compose",
62
+ "android-views",
63
+ "react-native",
64
+ "flutter",
65
+ ]);
66
+
67
+ const parseOrigin = (value: unknown): StudioOrigin | undefined => {
68
+ if (!isRecord(value)) return undefined;
69
+ if (typeof value.paywallId === "string" && typeof value.url === "string") {
70
+ return { paywallId: value.paywallId, url: value.url };
71
+ }
72
+ if (
73
+ typeof value.path === "string" &&
74
+ typeof value.framework === "string" &&
75
+ SCREEN_FRAMEWORKS.has(value.framework)
76
+ ) {
77
+ const framework = value.framework as StudioScreenFramework;
78
+ return isFileName(value.screenshot)
79
+ ? { path: value.path, framework, screenshot: value.screenshot }
80
+ : { path: value.path, framework };
81
+ }
82
+ return undefined;
83
+ };
84
+
42
85
  export const parseProjectPayload = (data: unknown): StudioProjectPayload => {
43
86
  if (!isRecord(data)) {
44
87
  throw new Error("project payload is not an object");
@@ -53,6 +96,7 @@ export const parseProjectPayload = (data: unknown): StudioProjectPayload => {
53
96
  return {
54
97
  title: typeof data.title === "string" ? data.title : "superwall",
55
98
  scheme: typeof data.scheme === "string" ? data.scheme : undefined,
99
+ networkOrigin: typeof data.networkOrigin === "string" ? data.networkOrigin : undefined,
56
100
  surfaces,
57
101
  diagnostics: isStringArray(data.diagnostics) ? data.diagnostics : [],
58
102
  samples: {