@air-jam/sdk 0.1.1 → 0.1.2

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.
@@ -10,6 +10,19 @@ interface VolumeSettings {
10
10
  getEffectiveVolume: (category: "music" | "sfx") => number;
11
11
  }
12
12
 
13
+ /**
14
+ * Settings sync message from parent (arcade) to child (game) iframe.
15
+ * This allows the arcade overlay to control volume for embedded games.
16
+ */
17
+ interface AirJamSettingsSyncMessage {
18
+ type: "AIRJAM_SETTINGS_SYNC";
19
+ payload: {
20
+ masterVolume: number;
21
+ musicVolume: number;
22
+ sfxVolume: number;
23
+ };
24
+ }
25
+
13
26
  const STORAGE_KEY = "air-jam-volume-settings";
14
27
 
15
28
  // Load initial state from localStorage
@@ -75,3 +88,37 @@ export const useVolumeStore = create<VolumeSettings>((set, get) => ({
75
88
  return masterVolume * categoryVolume;
76
89
  },
77
90
  }));
91
+
92
+ /**
93
+ * Set up parent message listener for child mode (games running in arcade iframe).
94
+ * When the arcade sends AIRJAM_SETTINGS_SYNC messages, apply them to the volume store.
95
+ * This runs once at module load time and is completely transparent to game developers.
96
+ */
97
+ const setupParentSettingsListener = () => {
98
+ if (typeof window === "undefined") return;
99
+
100
+ // Only listen if we're running inside an iframe (child mode)
101
+ const isInIframe = window.parent !== window;
102
+ if (!isInIframe) return;
103
+
104
+ const handleMessage = (event: MessageEvent<AirJamSettingsSyncMessage>) => {
105
+ // Only accept settings sync messages from parent
106
+ if (event.source !== window.parent) return;
107
+ if (event.data?.type !== "AIRJAM_SETTINGS_SYNC") return;
108
+
109
+ const { masterVolume, musicVolume, sfxVolume } = event.data.payload;
110
+
111
+ // Apply settings without saving to localStorage (arcade owns the settings)
112
+ const store = useVolumeStore.getState();
113
+
114
+ // Use setters to trigger AudioManager subscription updates
115
+ if (typeof masterVolume === "number") store.setMasterVolume(masterVolume);
116
+ if (typeof musicVolume === "number") store.setMusicVolume(musicVolume);
117
+ if (typeof sfxVolume === "number") store.setSfxVolume(sfxVolume);
118
+ };
119
+
120
+ window.addEventListener("message", handleMessage);
121
+ };
122
+
123
+ // Initialize the parent settings listener
124
+ setupParentSettingsListener();
@@ -382,7 +382,7 @@ export const AirJamOverlay = (): JSX.Element | null => {
382
382
  size="lg"
383
383
  >
384
384
  <Play className="mr-2 h-5 w-5" />
385
- Start Game
385
+ Play
386
386
  </Button>
387
387
  </div>
388
388
  </div>
@@ -15,17 +15,15 @@ import { useFullscreen } from "../hooks/use-fullscreen";
15
15
  import type { ConnectionStatus, PlayerProfile } from "../protocol";
16
16
  import { cn } from "../utils/cn";
17
17
  import { detectRunMode } from "../utils/mode";
18
+ import { PlayerAvatar } from "./player-avatar";
18
19
  import { QRScannerDialog } from "./qr-scanner-dialog";
19
20
  import { Button } from "./ui/button";
20
- import { PlayerAvatar } from "./player-avatar";
21
- import { VolumeControls } from "./volume-controls";
22
-
23
- type OrientationRequirement = "portrait" | "landscape" | "any";
24
21
 
25
22
  interface ControllerShellProps {
26
23
  roomId?: string | null;
27
24
  connectionStatus: ConnectionStatus;
28
- requiredOrientation?: OrientationRequirement;
25
+ /** Optional forced orientation. If not set, no orientation enforcement is applied. */
26
+ forceOrientation?: "portrait" | "landscape";
29
27
  children: ReactNode;
30
28
  gameState?: "paused" | "playing";
31
29
  onTogglePlayPause?: () => void;
@@ -50,8 +48,10 @@ const describeStatus = (status: ConnectionStatus): string => {
50
48
  }
51
49
  };
52
50
 
53
- const orientationMatches = (required: OrientationRequirement): boolean => {
54
- if (required === "any") return true;
51
+ const orientationMatches = (
52
+ required: "portrait" | "landscape" | undefined,
53
+ ): boolean => {
54
+ if (!required) return true;
55
55
  if (typeof window === "undefined" || !window.matchMedia) return true;
56
56
  const matcher = window.matchMedia(`(orientation: ${required})`);
57
57
  return matcher.matches;
@@ -60,7 +60,7 @@ const orientationMatches = (required: OrientationRequirement): boolean => {
60
60
  export const ControllerShell = ({
61
61
  roomId,
62
62
  connectionStatus,
63
- requiredOrientation = "landscape",
63
+ forceOrientation,
64
64
  children,
65
65
  gameState = "paused",
66
66
  onTogglePlayPause,
@@ -70,7 +70,7 @@ export const ControllerShell = ({
70
70
  }: ControllerShellProps): JSX.Element => {
71
71
  const isChildMode = useMemo(() => detectRunMode() === "platform", []);
72
72
  const [isOrientationOk, setOrientationOk] = useState(() =>
73
- orientationMatches(requiredOrientation),
73
+ orientationMatches(forceOrientation),
74
74
  );
75
75
  const [isScannerOpen, setIsScannerOpen] = useState(false);
76
76
  const { isFullscreen, toggleFullscreen } = useFullscreen();
@@ -103,18 +103,20 @@ export const ControllerShell = ({
103
103
  }, [controllerId, players, connectionStatus]);
104
104
 
105
105
  useEffect(() => {
106
- if (requiredOrientation === "any") {
106
+ if (!forceOrientation) {
107
+ // eslint-disable-next-line react-hooks/set-state-in-effect
108
+ setOrientationOk(true);
107
109
  return;
108
110
  }
109
111
 
110
- const media = window.matchMedia(`(orientation: ${requiredOrientation})`);
112
+ const media = window.matchMedia(`(orientation: ${forceOrientation})`);
111
113
  const handleChange = (): void => setOrientationOk(media.matches);
112
114
  handleChange();
113
115
  media.addEventListener("change", handleChange);
114
116
  return () => media.removeEventListener("change", handleChange);
115
- }, [requiredOrientation]);
117
+ }, [forceOrientation]);
116
118
 
117
- const orientationOk = requiredOrientation === "any" ? true : isOrientationOk;
119
+ const orientationOk = !forceOrientation ? true : isOrientationOk;
118
120
 
119
121
  const statusDotColor = useMemo(() => {
120
122
  switch (connectionStatus) {
@@ -135,28 +137,28 @@ export const ControllerShell = ({
135
137
  return (
136
138
  <div className="dark">
137
139
  <div className="text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden bg-transparent select-none">
138
- <main className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4">
139
- {!orientationOk && (
140
- <div className="bg-background/95 absolute inset-0 z-50 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm">
141
- <p className="text-card-foreground text-xl font-semibold">
142
- Rotate your device
143
- </p>
144
- <p className="text-muted-foreground mt-2 text-sm">
145
- This game is best experienced in {requiredOrientation}{" "}
146
- orientation.
147
- </p>
148
- </div>
149
- )}
150
- <div
151
- className={cn(
152
- "h-full w-full select-none",
153
- !orientationOk && "pointer-events-none opacity-30",
140
+ <main className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4">
141
+ {!orientationOk && (
142
+ <div className="bg-background/95 absolute inset-0 z-50 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm">
143
+ <p className="text-card-foreground text-xl font-semibold">
144
+ Rotate your device
145
+ </p>
146
+ <p className="text-muted-foreground mt-2 text-sm">
147
+ This game is best experienced in {forceOrientation}{" "}
148
+ orientation.
149
+ </p>
150
+ </div>
154
151
  )}
155
- >
156
- {children}
157
- </div>
158
- </main>
159
- </div>
152
+ <div
153
+ className={cn(
154
+ "h-full w-full select-none",
155
+ !orientationOk && "pointer-events-none opacity-30",
156
+ )}
157
+ >
158
+ {children}
159
+ </div>
160
+ </main>
161
+ </div>
160
162
  </div>
161
163
  );
162
164
  }
@@ -164,158 +166,155 @@ export const ControllerShell = ({
164
166
  return (
165
167
  <div className="dark">
166
168
  <div className="bg-background text-foreground relative flex h-dvh w-dvw touch-none flex-col overflow-hidden select-none">
167
- <header className="pointer-events-none sticky top-0 z-50 flex items-center justify-between border-b px-6 py-2">
168
- <div className="pointer-events-auto flex items-center gap-3">
169
- {typeof window !== "undefined" && (currentPlayer || controllerId) && (
170
- <div className="relative" title={describeStatus(connectionStatus)}>
171
- {currentPlayer ? (
172
- <PlayerAvatar player={currentPlayer} size="sm" />
173
- ) : controllerId ? (
174
- <PlayerAvatar
175
- player={{
176
- id: controllerId,
177
- label: "Player",
178
- }}
179
- size="sm"
180
- />
181
- ) : null}
182
- {/* Black border circle - always visible behind */}
183
- <span
184
- className="absolute right-0 bottom-0 h-3 w-3 rounded-full bg-black"
185
- aria-hidden="true"
186
- />
187
- {/* Colored status dot - on top */}
188
- <span
189
- className={cn(
190
- "absolute right-0.5 bottom-0.5 h-2 w-2 rounded-full",
191
- statusDotColor,
192
- connectionStatus === "connected" && "animate-pulse",
193
- )}
194
- aria-label={describeStatus(connectionStatus)}
195
- />
169
+ <header className="pointer-events-none sticky top-0 z-50 flex items-center justify-between border-b px-6 py-2">
170
+ <div className="pointer-events-auto flex items-center gap-3">
171
+ {typeof window !== "undefined" &&
172
+ (currentPlayer || controllerId) && (
173
+ <div
174
+ className="relative"
175
+ title={describeStatus(connectionStatus)}
176
+ >
177
+ {currentPlayer ? (
178
+ <PlayerAvatar player={currentPlayer} size="sm" />
179
+ ) : controllerId ? (
180
+ <PlayerAvatar
181
+ player={{
182
+ id: controllerId,
183
+ label: "Player",
184
+ }}
185
+ size="sm"
186
+ />
187
+ ) : null}
188
+ {/* Black border circle - always visible behind */}
189
+ <span
190
+ className="absolute right-0 bottom-0 h-3 w-3 rounded-full bg-black"
191
+ aria-hidden="true"
192
+ />
193
+ {/* Colored status dot - on top */}
194
+ <span
195
+ className={cn(
196
+ "absolute right-0.5 bottom-0.5 h-2 w-2 rounded-full",
197
+ statusDotColor,
198
+ connectionStatus === "connected" && "animate-pulse",
199
+ )}
200
+ aria-label={describeStatus(connectionStatus)}
201
+ />
202
+ </div>
203
+ )}
204
+ <div>
205
+ <p className="text-muted-foreground text-xs tracking-[0.24em] uppercase">
206
+ Room
207
+ </p>
208
+ <p className="text-foreground text-lg font-semibold">
209
+ {typeof window !== "undefined" ? (roomId ?? "N/A") : "N/A"}
210
+ </p>
196
211
  </div>
197
- )}
198
- <div>
199
- <p className="text-muted-foreground text-xs tracking-[0.24em] uppercase">
200
- Room
201
- </p>
202
- <p className="text-foreground text-lg font-semibold">
203
- {typeof window !== "undefined" ? (roomId ?? "N/A") : "N/A"}
204
- </p>
205
212
  </div>
206
- </div>
207
- <div className="pointer-events-auto flex items-center gap-3">
208
- {customActions}
209
- {onRefresh && roomId && (
210
- <Button
211
- type="button"
212
- variant="outline"
213
- size="icon"
214
- onClick={onRefresh}
215
- disabled={
216
- connectionStatus === "connecting" ||
217
- connectionStatus === "reconnecting"
218
- }
219
- aria-label="Reconnect"
220
- title="Reconnect to room"
221
- >
222
- <RefreshCw
223
- className={cn(
224
- "h-5 w-5",
225
- (connectionStatus === "connecting" ||
226
- connectionStatus === "reconnecting") &&
227
- "animate-spin",
213
+ <div className="pointer-events-auto flex items-center gap-3">
214
+ {customActions}
215
+ {onRefresh && roomId && (
216
+ <Button
217
+ type="button"
218
+ variant="outline"
219
+ size="icon"
220
+ onClick={onRefresh}
221
+ disabled={
222
+ connectionStatus === "connecting" ||
223
+ connectionStatus === "reconnecting"
224
+ }
225
+ aria-label="Reconnect"
226
+ title="Reconnect to room"
227
+ >
228
+ <RefreshCw
229
+ className={cn(
230
+ "h-5 w-5",
231
+ (connectionStatus === "connecting" ||
232
+ connectionStatus === "reconnecting") &&
233
+ "animate-spin",
234
+ )}
235
+ />
236
+ </Button>
237
+ )}
238
+ {onReconnect && (
239
+ <Button
240
+ type="button"
241
+ variant="outline"
242
+ size="icon"
243
+ onClick={() => setIsScannerOpen(true)}
244
+ aria-label="Scan QR code to reconnect"
245
+ title="Scan QR code to reconnect"
246
+ >
247
+ <QrCode className="h-5 w-5" />
248
+ </Button>
249
+ )}
250
+ {onTogglePlayPause && (
251
+ <Button
252
+ type="button"
253
+ variant="outline"
254
+ size="icon"
255
+ onClick={onTogglePlayPause}
256
+ aria-label={gameState === "playing" ? "Pause" : "Play"}
257
+ title={gameState === "playing" ? "Pause" : "Play"}
258
+ >
259
+ {gameState === "playing" ? (
260
+ <Pause className="h-5 w-5" />
261
+ ) : (
262
+ <Play className="h-5 w-5" />
228
263
  )}
229
- />
230
- </Button>
231
- )}
232
- {onReconnect && (
233
- <Button
234
- type="button"
235
- variant="outline"
236
- size="icon"
237
- onClick={() => setIsScannerOpen(true)}
238
- aria-label="Scan QR code to reconnect"
239
- title="Scan QR code to reconnect"
240
- >
241
- <QrCode className="h-5 w-5" />
242
- </Button>
243
- )}
244
- {onTogglePlayPause && (
264
+ </Button>
265
+ )}
245
266
  <Button
246
267
  type="button"
247
268
  variant="outline"
248
269
  size="icon"
249
- onClick={onTogglePlayPause}
250
- aria-label={gameState === "playing" ? "Pause" : "Play"}
251
- title={gameState === "playing" ? "Pause" : "Play"}
270
+ onClick={() => toggleFullscreen()}
271
+ aria-label={isFullscreen ? "Exit fullscreen" : "Enter fullscreen"}
272
+ title={isFullscreen ? "Exit fullscreen" : "Enter fullscreen"}
252
273
  >
253
- {gameState === "playing" ? (
254
- <Pause className="h-5 w-5" />
274
+ {isFullscreen ? (
275
+ <Minimize className="h-5 w-5" />
255
276
  ) : (
256
- <Play className="h-5 w-5" />
277
+ <Maximize className="h-5 w-5" />
257
278
  )}
258
279
  </Button>
259
- )}
260
- <Button
261
- type="button"
262
- variant="outline"
263
- size="icon"
264
- onClick={() => toggleFullscreen()}
265
- aria-label={isFullscreen ? "Exit fullscreen" : "Enter fullscreen"}
266
- title={isFullscreen ? "Exit fullscreen" : "Enter fullscreen"}
267
- >
268
- {isFullscreen ? (
269
- <Minimize className="h-5 w-5" />
270
- ) : (
271
- <Maximize className="h-5 w-5" />
272
- )}
273
- </Button>
274
- </div>
275
- </header>
276
-
277
- {/* Volume Controls - positioned absolutely for mobile */}
278
- <div className="pointer-events-none fixed right-4 bottom-4 z-50">
279
- <div className="pointer-events-auto">
280
- <VolumeControls compact={true} />
281
- </div>
282
- </div>
280
+ </div>
281
+ </header>
283
282
 
284
- <main className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4">
285
- <div className="bg-card/95 absolute inset-0 z-10 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm">
286
- {!orientationOk && (
287
- <div className="bg-background/95 absolute inset-0 z-10 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm">
288
- <p className="text-card-foreground text-xl font-semibold">
289
- Rotate your device
290
- </p>
291
- <p className="text-muted-foreground mt-2 text-sm">
292
- This game is best experienced in {requiredOrientation}{" "}
293
- orientation.
294
- </p>
295
- </div>
296
- )}
297
- <div
298
- className={cn(
299
- "h-full w-full select-none",
300
- !orientationOk && "pointer-events-none opacity-30",
283
+ <main className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden p-2 select-none sm:p-4">
284
+ <div className="bg-card/95 absolute inset-0 z-10 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm">
285
+ {!orientationOk && forceOrientation && (
286
+ <div className="bg-background/95 absolute inset-0 z-10 flex flex-col items-center justify-center p-6 text-center shadow-lg backdrop-blur-sm">
287
+ <p className="text-card-foreground text-xl font-semibold">
288
+ Rotate your device
289
+ </p>
290
+ <p className="text-muted-foreground mt-2 text-sm">
291
+ This game is best experienced in {forceOrientation}{" "}
292
+ orientation.
293
+ </p>
294
+ </div>
301
295
  )}
302
- >
303
- {children}
296
+ <div
297
+ className={cn(
298
+ "h-full w-full select-none",
299
+ !orientationOk && "pointer-events-none opacity-30",
300
+ )}
301
+ >
302
+ {children}
303
+ </div>
304
304
  </div>
305
- </div>
306
- </main>
305
+ </main>
307
306
 
308
- {onReconnect && (
309
- <QRScannerDialog
310
- isOpen={isScannerOpen}
311
- onClose={() => setIsScannerOpen(false)}
312
- onScan={(roomCode) => {
313
- onReconnect(roomCode);
314
- setIsScannerOpen(false);
315
- }}
316
- currentRoomId={roomId}
317
- />
318
- )}
307
+ {onReconnect && (
308
+ <QRScannerDialog
309
+ isOpen={isScannerOpen}
310
+ onClose={() => setIsScannerOpen(false)}
311
+ onScan={(roomCode) => {
312
+ onReconnect(roomCode);
313
+ setIsScannerOpen(false);
314
+ }}
315
+ currentRoomId={roomId}
316
+ />
317
+ )}
319
318
  </div>
320
319
  </div>
321
320
  );
@@ -15,8 +15,8 @@ interface QRScannerDialogProps {
15
15
  /**
16
16
  * Extracts room code from a scanned URL or text.
17
17
  * Supports formats like:
18
- * - http://host/joypad?room=CODE
19
- * - https://host/joypad?room=CODE
18
+ * - http://host/controller?room=CODE
19
+ * - https://host/controller?room=CODE
20
20
  * - Just the room code itself
21
21
  */
22
22
  const extractRoomCode = (text: string): string | null => {
package/src/constants.ts CHANGED
@@ -8,9 +8,10 @@
8
8
  export const DEFAULT_SERVER_PORT = 4000;
9
9
 
10
10
  /**
11
- * Default path for controller endpoints
11
+ * Standard path for controller endpoints.
12
+ * All Air Jam games use this convention - controller is always at {baseUrl}/controller
12
13
  */
13
- export const DEFAULT_CONTROLLER_PATH = "/joypad";
14
+ export const CONTROLLER_PATH = "/controller";
14
15
 
15
16
  /**
16
17
  * Input debounce time in milliseconds
@@ -54,7 +54,7 @@ import {
54
54
  import type { z } from "zod";
55
55
  import { useStore } from "zustand";
56
56
  import { useShallow } from "zustand/react/shallow";
57
- import { DEFAULT_CONTROLLER_PATH, DEFAULT_MAX_PLAYERS } from "../constants";
57
+ import { DEFAULT_MAX_PLAYERS } from "../constants";
58
58
  import { InputManager, type InputConfig } from "../internal/input-manager";
59
59
  import type { ConnectionRole } from "../protocol";
60
60
  import { createAirJamStore, type AirJamStore } from "../state/connection-store";
@@ -73,8 +73,6 @@ export interface AirJamConfig {
73
73
  serverUrl?: string;
74
74
  /** API key for production authentication */
75
75
  apiKey?: string;
76
- /** Path for controller page (default: /joypad) */
77
- controllerPath: string;
78
76
  /** Maximum players allowed in a room */
79
77
  maxPlayers: number;
80
78
  /** Public host for controller URLs (optional) */
@@ -98,7 +96,6 @@ export interface AirJamConfig {
98
96
  * <AirJamProvider
99
97
  * serverUrl="wss://your-server.com"
100
98
  * apiKey="your-api-key"
101
- * controllerPath="/controller"
102
99
  * maxPlayers={4}
103
100
  * input={{
104
101
  * schema: myInputSchema,
@@ -124,12 +121,6 @@ export interface AirJamProviderProps<
124
121
  * Falls back to VITE_AIR_JAM_API_KEY or NEXT_PUBLIC_AIR_JAM_API_KEY environment variables.
125
122
  */
126
123
  apiKey?: string;
127
- /**
128
- * URL path for the controller page on your app.
129
- * Controllers will be directed to this path with ?room=XXXX query parameter.
130
- * @default "/joypad"
131
- */
132
- controllerPath?: string;
133
124
  /**
134
125
  * Maximum number of players allowed in a room.
135
126
  * @default 8
@@ -272,7 +263,7 @@ const getEnvApiKey = (): string | undefined => {
272
263
  * <AirJamProvider>
273
264
  * <Routes>
274
265
  * <Route path="/" element={<HostView />} />
275
- * <Route path="/joypad" element={<ControllerView />} />
266
+ * <Route path="/controller" element={<ControllerView />} />
276
267
  * </Routes>
277
268
  * </AirJamProvider>
278
269
  * );
@@ -309,7 +300,6 @@ export const AirJamProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
309
300
  children,
310
301
  serverUrl,
311
302
  apiKey,
312
- controllerPath = DEFAULT_CONTROLLER_PATH,
313
303
  maxPlayers = DEFAULT_MAX_PLAYERS,
314
304
  publicHost,
315
305
  input,
@@ -319,11 +309,10 @@ export const AirJamProvider = <TSchema extends z.ZodSchema = z.ZodSchema>({
319
309
  () => ({
320
310
  serverUrl: serverUrl ?? getEnvServerUrl(),
321
311
  apiKey: apiKey ?? getEnvApiKey(),
322
- controllerPath,
323
312
  maxPlayers,
324
313
  publicHost,
325
314
  }),
326
- [serverUrl, apiKey, controllerPath, maxPlayers, publicHost],
315
+ [serverUrl, apiKey, maxPlayers, publicHost],
327
316
  );
328
317
 
329
318
  // Create InputManager from input config if provided
@@ -42,7 +42,7 @@ import { detectRunMode } from "../utils/mode";
42
42
  *
43
43
  * @example Basic usage (room from URL)
44
44
  * ```tsx
45
- * // URL: https://yourgame.com/joypad?room=ABCD
45
+ * // URL: https://yourgame.com/controller?room=ABCD
46
46
  * const controller = useAirJamController();
47
47
  * // Automatically joins room ABCD
48
48
  * ```