@superwall/studio 1.3.0-beta.3 → 1.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,34 @@
1
+ import * as React from "react";
2
+ /**
3
+ * How the studio is being viewed.
4
+ *
5
+ * - `local` — served by `superwall dev` on a desktop browser.
6
+ * - `device` — the same server, opened on a phone: trimmed chrome, and every
7
+ * card is a real link because touch navigation is browser navigation.
8
+ * - `hosted` — embedded in the dashboard, where the studio is one route inside
9
+ * a larger app rather than the whole page.
10
+ */
11
+ export type StudioMode = "local" | "device" | "hosted";
12
+ /**
13
+ * Where the logo goes.
14
+ *
15
+ * Two mechanisms for one destination: `href` is what the browser opens on a
16
+ * middle or modified click, and `navigate` is what a plain click runs in-app.
17
+ * A hosted studio needs both — following the href there would reload the whole
18
+ * dashboard.
19
+ */
20
+ export type StudioHome = {
21
+ href: string;
22
+ navigate: () => void;
23
+ };
24
+ export type StudioHostValue = {
25
+ mode: StudioMode;
26
+ home: StudioHome;
27
+ };
28
+ export declare const StudioHostProvider: ({ mode, home, children, }: {
29
+ mode: StudioMode;
30
+ home: StudioHome;
31
+ children: React.ReactNode;
32
+ }) => React.JSX.Element;
33
+ export declare const useStudioMode: () => StudioMode;
34
+ export declare const useStudioHome: () => StudioHome;
@@ -33,3 +33,4 @@ export type StudioSource = {
33
33
  };
34
34
  compareHint?: string;
35
35
  };
36
+ export declare const hasActions: (source: StudioSource) => boolean;
@@ -1,9 +1,8 @@
1
1
  import * as React from "react";
2
2
  import { type Samples, type StudioSource, type StudioSurface } from "../lib/surfaces";
3
- export declare const SurfaceEditor: ({ source, surface, samples, onBack, actions, }: {
3
+ export declare const SurfaceEditor: ({ source, surface, samples, actions, }: {
4
4
  source: StudioSource;
5
5
  surface: StudioSurface;
6
6
  samples: Samples;
7
- onBack: () => void;
8
7
  actions?: React.ReactNode;
9
8
  }) => React.JSX.Element;
@@ -1,7 +1,6 @@
1
1
  import * as React from "react";
2
- import type { StudioSource, StudioSurface } from "../lib/surfaces";
3
- export declare const SurfaceList: ({ source, onOpen, device, }: {
2
+ import { type StudioSource, type StudioSurface } from "../lib/surfaces";
3
+ export declare const SurfaceList: ({ source, onOpen, }: {
4
4
  source: StudioSource;
5
5
  onOpen: (surface: StudioSurface) => void;
6
- device?: boolean;
7
6
  }) => React.JSX.Element;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@superwall/studio",
3
- "version": "1.3.0-beta.3",
3
+ "version": "1.3.0",
4
4
  "description": "The Superwall studio: paywall browsing, device preview, and editing surfaces. Shared between `superwall dev` (local studio) and the hosted dashboard.",
5
5
  "keywords": [
6
6
  "paywall",
@@ -56,7 +56,7 @@
56
56
  "clean": "rm -rf dist coverage"
57
57
  },
58
58
  "dependencies": {
59
- "@superwall/host": "1.3.0-beta.3",
59
+ "@superwall/host": "1.3.0",
60
60
  "sonner": "^2.0.7"
61
61
  },
62
62
  "devDependencies": {
package/src/StudioApp.tsx CHANGED
@@ -4,6 +4,7 @@ import { SurfaceEditor } from "./screens/SurfaceEditor";
4
4
  import { SurfaceList } from "./screens/SurfaceList";
5
5
  import { surfacePath } from "./lib/preview";
6
6
  import { fetchSamples } from "./lib/actions";
7
+ import { StudioHostProvider, type StudioMode } from "./lib/studio-host";
7
8
  import type { Samples, StudioSource, StudioSurface } from "./lib/surfaces";
8
9
 
9
10
  const surfaceFromPath = (
@@ -22,10 +23,10 @@ const surfaceFromPath = (
22
23
 
23
24
  export const StudioApp = ({
24
25
  source,
25
- device = false,
26
+ mode = "local",
26
27
  }: {
27
28
  source: StudioSource;
28
- device?: boolean;
29
+ mode?: StudioMode;
29
30
  }) => {
30
31
  const [pathname, setPathname] = React.useState(() => window.location.pathname);
31
32
  const [samples, setSamples] = React.useState<Samples>({});
@@ -47,29 +48,29 @@ export const StudioApp = ({
47
48
  };
48
49
  }, [source.capabilities?.actions]);
49
50
 
50
- const navigate = (path: string) => {
51
+ const homePath = mode === "device" ? "/device" : "/";
52
+
53
+ const navigate = React.useCallback((path: string) => {
51
54
  window.history.pushState(null, "", path);
52
55
  setPathname(path);
53
- };
56
+ }, []);
57
+
58
+ const home = React.useMemo(
59
+ () => ({ href: homePath, navigate: () => navigate(homePath) }),
60
+ [homePath, navigate],
61
+ );
54
62
 
55
63
  const active = surfaceFromPath(source.surfaces, pathname);
56
64
 
57
65
  return (
58
- <StudioShell>
59
- {active ? (
60
- <SurfaceEditor
61
- source={source}
62
- surface={active}
63
- samples={samples}
64
- onBack={() => navigate(device ? "/device" : "/")}
65
- />
66
- ) : (
67
- <SurfaceList
68
- source={source}
69
- device={device}
70
- onOpen={(surface) => navigate(surfacePath(surface))}
71
- />
72
- )}
73
- </StudioShell>
66
+ <StudioHostProvider mode={mode} home={home}>
67
+ <StudioShell>
68
+ {active ? (
69
+ <SurfaceEditor source={source} surface={active} samples={samples} />
70
+ ) : (
71
+ <SurfaceList source={source} onOpen={(surface) => navigate(surfacePath(surface))} />
72
+ )}
73
+ </StudioShell>
74
+ </StudioHostProvider>
74
75
  );
75
76
  };
@@ -1,41 +1,45 @@
1
1
  import * as React from "react";
2
+ import { useStudioHome } from "../../lib/studio-host";
2
3
 
3
4
  export const TopBar = ({
4
5
  title,
5
- onHome,
6
6
  actions,
7
7
  inset = false,
8
8
  label = "Studio",
9
9
  }: {
10
10
  title: string;
11
- onHome: () => void;
12
11
  actions?: React.ReactNode;
13
12
  inset?: boolean;
14
13
  label?: string;
15
- }) => (
16
- <header className="relative z-10 flex h-11 flex-none items-center border-b border-cream/10 bg-ink">
17
- <div
18
- className={`flex h-full w-full items-center px-3 ${inset ? "mx-auto max-w-[1080px] sm:px-0" : ""}`}
19
- >
20
- <a
21
- className="flex items-center gap-3 text-[0.9375rem] font-semibold tracking-[0.01em]"
22
- href="/"
23
- onClick={(event) => {
24
- if (event.metaKey || event.ctrlKey) return;
25
- event.preventDefault();
26
- onHome();
27
- }}
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" : ""}`}
28
21
  >
29
- <span className="whitespace-nowrap">
30
- <em className="text-accent not-italic">$UPER</em>WALL
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}
31
40
  </span>
32
- <span className="h-4 w-px bg-cream/20" />
33
- <span className="font-normal">{label}</span>
34
- </a>
35
- <span className="absolute left-1/2 hidden -translate-x-1/2 text-sm font-medium whitespace-nowrap sm:block">
36
- {title}
37
- </span>
38
- <div className="ml-auto flex items-center gap-2">{actions}</div>
39
- </div>
40
- </header>
41
- );
41
+ <div className="ml-auto flex items-center gap-2">{actions}</div>
42
+ </div>
43
+ </header>
44
+ );
45
+ };
package/src/index.ts CHANGED
@@ -13,6 +13,14 @@ export { Segmented, Select, Switch } from "./components/controls";
13
13
  export { DEFAULT_DEVICE_ID, deviceById, STUDIO_DEVICES } from "./lib/devices";
14
14
  export type { StudioDevice } from "./lib/devices";
15
15
  export { hostStateFor, studioDefaults } from "./lib/host";
16
+ export { hasActions } from "./lib/surfaces";
17
+ export {
18
+ StudioHostProvider,
19
+ useStudioHome,
20
+ useStudioMode,
21
+ type StudioHome,
22
+ type StudioMode,
23
+ } from "./lib/studio-host";
16
24
  export {
17
25
  createHostState,
18
26
  diffHostState,
package/src/lib/host.ts CHANGED
@@ -3,7 +3,6 @@ import {
3
3
  DEFAULT_PARAMS,
4
4
  DEFAULT_USER,
5
5
  defaultDevice,
6
- EXAMPLE_PRODUCT_VARIABLES,
7
6
  type HostProduct,
8
7
  type HostState,
9
8
  type HostVariables,
@@ -63,7 +62,7 @@ export const hostStateFor = ({
63
62
  reference,
64
63
  productId,
65
64
  variables: {
66
- ...(samples.products?.[productId] ?? EXAMPLE_PRODUCT_VARIABLES),
65
+ ...samples.products?.[productId],
67
66
  ...edited.products?.[reference],
68
67
  },
69
68
  }),
@@ -0,0 +1,61 @@
1
+ import * as React from "react";
2
+
3
+ /**
4
+ * How the studio is being viewed.
5
+ *
6
+ * - `local` — served by `superwall dev` on a desktop browser.
7
+ * - `device` — the same server, opened on a phone: trimmed chrome, and every
8
+ * card is a real link because touch navigation is browser navigation.
9
+ * - `hosted` — embedded in the dashboard, where the studio is one route inside
10
+ * a larger app rather than the whole page.
11
+ */
12
+ export type StudioMode = "local" | "device" | "hosted";
13
+
14
+ /**
15
+ * Where the logo goes.
16
+ *
17
+ * Two mechanisms for one destination: `href` is what the browser opens on a
18
+ * middle or modified click, and `navigate` is what a plain click runs in-app.
19
+ * A hosted studio needs both — following the href there would reload the whole
20
+ * dashboard.
21
+ */
22
+ export type StudioHome = {
23
+ href: string;
24
+ navigate: () => void;
25
+ };
26
+
27
+ export type StudioHostValue = {
28
+ mode: StudioMode;
29
+ home: StudioHome;
30
+ };
31
+
32
+ const StudioHostContext = React.createContext<StudioHostValue | undefined>(undefined);
33
+
34
+ const useStudioHost = (): StudioHostValue => {
35
+ const value = React.useContext(StudioHostContext);
36
+ if (!value) {
37
+ throw new Error("Studio components must be rendered inside <StudioHostProvider>.");
38
+ }
39
+ return value;
40
+ };
41
+
42
+ export const StudioHostProvider = ({
43
+ mode,
44
+ home,
45
+ children,
46
+ }: {
47
+ mode: StudioMode;
48
+ home: StudioHome;
49
+ children: React.ReactNode;
50
+ }) => {
51
+ const { href, navigate } = home;
52
+ const value = React.useMemo<StudioHostValue>(
53
+ () => ({ mode, home: { href, navigate } }),
54
+ [mode, href, navigate],
55
+ );
56
+ return <StudioHostContext.Provider value={value}>{children}</StudioHostContext.Provider>;
57
+ };
58
+
59
+ export const useStudioMode = (): StudioMode => useStudioHost().mode;
60
+
61
+ export const useStudioHome = (): StudioHome => useStudioHost().home;
@@ -39,3 +39,5 @@ export type StudioSource = {
39
39
  capabilities?: { actions: boolean };
40
40
  compareHint?: string;
41
41
  };
42
+
43
+ export const hasActions = (source: StudioSource): boolean => source.capabilities?.actions === true;
package/src/main.tsx CHANGED
@@ -4,6 +4,7 @@ import { buildPreviewPath, surfacePath } from "./lib/preview";
4
4
  import { parseProjectPayload } from "./lib/project";
5
5
  import { StudioApp } from "./StudioApp";
6
6
  import "./styles.css";
7
+ import type { StudioMode } from "./lib/studio-host";
7
8
  import type { StudioSource } from "./lib/surfaces";
8
9
 
9
10
  const loadSource = async (): Promise<StudioSource> => {
@@ -30,13 +31,13 @@ const Failure = ({ error }: { error: unknown }) => (
30
31
 
31
32
  const root = createRoot(document.getElementById("root")!);
32
33
 
33
- const device = /^\/device\/?$/.test(window.location.pathname);
34
+ const mode: StudioMode = /^\/device\/?$/.test(window.location.pathname) ? "device" : "local";
34
35
 
35
36
  loadSource()
36
37
  .then((source) => {
37
38
  root.render(
38
39
  <React.StrictMode>
39
- <StudioApp source={source} device={device} />
40
+ <StudioApp source={source} mode={mode} />
40
41
  </React.StrictMode>,
41
42
  );
42
43
  })
@@ -35,6 +35,7 @@ import { hostStateFor, studioDefaults } from "../lib/host";
35
35
  import { hostedUrl } from "../lib/preview";
36
36
  import { ACTION_STEPS, type StudioActionStep, type StudioStepState } from "../lib/steps";
37
37
  import {
38
+ hasActions,
38
39
  type CompareTarget,
39
40
  type Samples,
40
41
  type StudioActionName,
@@ -144,13 +145,11 @@ export const SurfaceEditor = ({
144
145
  source,
145
146
  surface,
146
147
  samples,
147
- onBack,
148
148
  actions,
149
149
  }: {
150
150
  source: StudioSource;
151
151
  surface: StudioSurface;
152
152
  samples: Samples;
153
- onBack: () => void;
154
153
  actions?: React.ReactNode;
155
154
  }) => {
156
155
  const [deviceId, setDeviceId] = React.useState(DEFAULT_DEVICE_ID);
@@ -167,7 +166,7 @@ export const SurfaceEditor = ({
167
166
  const dismissTimer = React.useRef<number | undefined>(undefined);
168
167
  React.useEffect(() => () => window.clearTimeout(dismissTimer.current), []);
169
168
 
170
- const actionsEnabled = source.capabilities?.actions === true;
169
+ const actionsEnabled = hasActions(source);
171
170
  const delegate = useStudioDelegate();
172
171
  const defaults = React.useMemo(studioDefaults, [surface]);
173
172
 
@@ -355,7 +354,6 @@ export const SurfaceEditor = ({
355
354
  title={`${surface.project ?? source.title} / ${surface.id}${
356
355
  surface.label ? ` · ${surface.label}` : ""
357
356
  }`}
358
- onHome={onBack}
359
357
  actions={
360
358
  <>
361
359
  {actions}
@@ -3,26 +3,32 @@ import { OpenInApp } from "../components/OpenInApp";
3
3
  import { SurfaceCard } from "../components/SurfaceCard";
4
4
  import { TopBar } from "../components/chrome/TopBar";
5
5
  import { fetchStatus } from "../lib/actions";
6
- import type { StudioSource, StudioStatus, StudioSurface } from "../lib/surfaces";
6
+ import {
7
+ hasActions,
8
+ type StudioSource,
9
+ type StudioStatus,
10
+ type StudioSurface,
11
+ } from "../lib/surfaces";
7
12
  import { hostStateFor, studioDefaults } from "../lib/host";
8
13
  import { surfacePath } from "../lib/preview";
14
+ import { useStudioMode } from "../lib/studio-host";
9
15
 
10
16
  let lastScroll = 0;
11
17
 
12
18
  export const SurfaceList = ({
13
19
  source,
14
20
  onOpen,
15
- device = false,
16
21
  }: {
17
22
  source: StudioSource;
18
23
  onOpen: (surface: StudioSurface) => void;
19
- device?: boolean;
20
24
  }) => {
25
+ const device = useStudioMode() === "device";
26
+ const actions = hasActions(source);
21
27
  const [statuses, setStatuses] = React.useState<Record<string, StudioStatus>>({});
22
28
  const defaults = React.useMemo(studioDefaults, [source]);
23
29
 
24
30
  React.useEffect(() => {
25
- if (source.capabilities?.actions !== true) return;
31
+ if (!actions) return;
26
32
  let cancelled = false;
27
33
  for (const surface of source.surfaces) {
28
34
  void fetchStatus(surface).then((status) => {
@@ -33,7 +39,7 @@ export const SurfaceList = ({
33
39
  return () => {
34
40
  cancelled = true;
35
41
  };
36
- }, [source]);
42
+ }, [source, actions]);
37
43
 
38
44
  const [queries, setQueries] = React.useState<Record<string, string>>({});
39
45
  const scroller = React.useRef<HTMLDivElement>(null);
@@ -79,7 +85,6 @@ export const SurfaceList = ({
79
85
  <>
80
86
  <TopBar
81
87
  title={source.title}
82
- onHome={() => {}}
83
88
  inset
84
89
  label={device ? "Preview" : undefined}
85
90
  actions={