@superwall/studio 1.4.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 (46) 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/chrome/Toolbar.d.ts +1 -0
  6. package/dist/lib/components/chrome/TopBar.d.ts +1 -0
  7. package/dist/lib/components/icons.d.ts +1 -0
  8. package/dist/lib/components/menus/CompareMenu.d.ts +2 -1
  9. package/dist/lib/components/preview/Canvas.d.ts +18 -0
  10. package/dist/lib/components/preview/DeviceFrame.d.ts +4 -4
  11. package/dist/lib/components/preview/ResizeHandles.d.ts +13 -0
  12. package/dist/lib/index.d.ts +1 -1
  13. package/dist/lib/index.js +2296 -1616
  14. package/dist/lib/lib/canvas.d.ts +27 -0
  15. package/dist/lib/lib/contract.d.ts +7 -1
  16. package/dist/lib/lib/devices.d.ts +9 -0
  17. package/dist/lib/lib/host.d.ts +2 -1
  18. package/dist/lib/lib/presentation.d.ts +23 -0
  19. package/dist/lib/lib/preview.d.ts +1 -0
  20. package/dist/lib/lib/surfaces.d.ts +23 -2
  21. package/dist/lib/studio.css +1 -1
  22. package/package.json +2 -2
  23. package/src/components/DownloadScreenshot.tsx +45 -18
  24. package/src/components/PaywallToasts.tsx +5 -3
  25. package/src/components/chrome/Toolbar.tsx +47 -6
  26. package/src/components/chrome/TopBar.tsx +37 -3
  27. package/src/components/icons.tsx +6 -0
  28. package/src/components/menus/ActionMenu.tsx +1 -1
  29. package/src/components/menus/CompareMenu.tsx +49 -30
  30. package/src/components/preview/Canvas.tsx +426 -0
  31. package/src/components/preview/DeviceFrame.tsx +83 -74
  32. package/src/components/preview/ResizeHandles.tsx +205 -0
  33. package/src/index.ts +2 -0
  34. package/src/lib/canvas.ts +60 -0
  35. package/src/lib/contract.ts +8 -1
  36. package/src/lib/devices.ts +52 -4
  37. package/src/lib/host.ts +7 -2
  38. package/src/lib/presentation.ts +102 -0
  39. package/src/lib/preview.ts +3 -0
  40. package/src/lib/project.ts +43 -1
  41. package/src/lib/surfaces.ts +39 -1
  42. package/src/main.tsx +5 -1
  43. package/src/screens/SurfaceEditor.tsx +215 -52
  44. package/src/styles.css +46 -0
  45. package/dist/assets/index-GB-RwJI6.js +0 -11
  46. package/dist/assets/index-Sz7S5UsH.css +0 -1
@@ -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,4 +1,10 @@
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 = {
@@ -28,6 +34,10 @@ const parseSurface = (value: unknown): StudioSurface | undefined => {
28
34
  const kind: StudioSurfaceKind | undefined =
29
35
  value.kind === "paywall" || value.kind === "funnel" ? value.kind : undefined;
30
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);
31
41
  return {
32
42
  kind,
33
43
  id: value.id,
@@ -36,10 +46,42 @@ const parseSurface = (value: unknown): StudioSurface | undefined => {
36
46
  products: isStringRecord(value.products) ? value.products : {},
37
47
  locales: isStringArray(value.locales) ? value.locales : [],
38
48
  ...(isStringArray(value.platforms) ? { platforms: value.platforms } : {}),
49
+ ...(presentation === undefined ? {} : { presentation }),
39
50
  paywallId: typeof value.paywallId === "string" ? value.paywallId : undefined,
51
+ ...(origin === undefined ? {} : { origin }),
40
52
  };
41
53
  };
42
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
+
43
85
  export const parseProjectPayload = (data: unknown): StudioProjectPayload => {
44
86
  if (!isRecord(data)) {
45
87
  throw new Error("project payload is not an object");
@@ -1,5 +1,8 @@
1
+ import type { StudioPresentation } from "./presentation";
1
2
  import type { SampleDefaults } from "./variables";
2
3
 
4
+ export type { StudioPresentation } from "./presentation";
5
+
3
6
  export type {
4
7
  AppScheme,
5
8
  Samples,
@@ -7,6 +10,7 @@ export type {
7
10
  StudioActionProgress,
8
11
  StudioActionResult,
9
12
  StudioStatus,
13
+ StudioVersion,
10
14
  } from "./contract";
11
15
 
12
16
  export type StudioSurfaceKind = "paywall" | "funnel";
@@ -20,13 +24,47 @@ export type StudioSurface = {
20
24
  routes: string[];
21
25
  locales: string[];
22
26
  platforms?: string[];
27
+ presentation?: StudioPresentation;
23
28
  paywallId?: string;
24
29
  paywallIdentifier?: string;
30
+ origin?: StudioOrigin;
25
31
  };
26
32
 
33
+ export type StudioScreenFramework =
34
+ | "swiftui"
35
+ | "uikit"
36
+ | "compose"
37
+ | "android-views"
38
+ | "react-native"
39
+ | "flutter";
40
+
41
+ export type StudioOrigin =
42
+ | { paywallId: string; url: string }
43
+ | { path: string; framework: StudioScreenFramework; screenshot?: string };
44
+
45
+ export const SCREEN_FRAMEWORK_LABEL: Record<StudioScreenFramework, string> = {
46
+ swiftui: "SwiftUI",
47
+ uikit: "UIKit",
48
+ compose: "Jetpack Compose",
49
+ "android-views": "Android Views",
50
+ "react-native": "React Native",
51
+ flutter: "Flutter",
52
+ };
53
+
54
+ export const isScreenOrigin = (
55
+ origin: StudioOrigin,
56
+ ): origin is Extract<StudioOrigin, { path: string }> => "path" in origin;
57
+
58
+ export const hasOriginal = (surface: StudioSurface): boolean =>
59
+ surface.origin !== undefined &&
60
+ (!isScreenOrigin(surface.origin) || surface.origin.screenshot !== undefined);
61
+
27
62
  export type PreviewUrlBuilder = (surface: StudioSurface) => string;
28
63
 
29
- export type CompareTarget = { type: "live" } | { type: "surface"; surface: StudioSurface };
64
+ export type CompareTarget =
65
+ | { type: "version"; version: number }
66
+ | { type: "surface"; surface: StudioSurface }
67
+ | { type: "origin" };
30
68
 
31
69
  export type StudioSource = {
32
70
  scheme?: string;
package/src/main.tsx CHANGED
@@ -13,7 +13,11 @@ const loadSource = async (): Promise<StudioSource> => {
13
13
  throw new Error(`project data unavailable (${response.status})`);
14
14
  }
15
15
  const payload = parseProjectPayload(await response.json());
16
- return { ...payload, previewUrl: buildPreviewPath, standaloneUrl: surfacePath };
16
+ return {
17
+ ...payload,
18
+ previewUrl: buildPreviewPath,
19
+ standaloneUrl: (surface) => `/preview${surfacePath(surface)}`,
20
+ };
17
21
  };
18
22
 
19
23
  const Failure = ({ error }: { error: unknown }) => (