Skip to content

Commit e93c8ac

Browse files
saviotxcpb8010
authored andcommitted
feat: allow custom popup positioning
1 parent c2d3d0c commit e93c8ac

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

packages/sdk/src/communicator/PopupCommunicator.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,29 @@ import type { Communicator, Message } from "./index.js";
44
export interface PopupConfigMessage extends Message {
55
event: "PopupLoaded" | "PopupUnload";
66
}
7+
type PositionCalculator = (width: number, height: number) => { left: number; top: number };
78

89
export class PopupCommunicator implements Communicator {
910
private readonly url: URL;
1011
private popup: Window | null = null;
1112
private listeners = new Map<(_: MessageEvent) => void, { reject: (_: Error) => void }>();
1213

13-
constructor(url: string) {
14+
private readonly width: number;
15+
private readonly height: number;
16+
private readonly calculatePosition?: PositionCalculator;
17+
18+
constructor(
19+
url: string,
20+
options?: {
21+
width?: number;
22+
height?: number;
23+
calculatePosition?: PositionCalculator;
24+
},
25+
) {
1426
this.url = new URL(url);
27+
this.width = options?.width ?? 420;
28+
this.height = options?.height ?? 600;
29+
this.calculatePosition = options?.calculatePosition;
1530
}
1631

1732
postMessage = async (message: Message) => {
@@ -64,14 +79,18 @@ export class PopupCommunicator implements Communicator {
6479
};
6580

6681
openPopup = () => {
67-
const width = 420;
68-
const height = 600;
82+
const width = this.width;
83+
const height = this.height;
6984

7085
const url = new URL(this.url.toString());
7186
url.searchParams.set("origin", window.location.origin);
7287

73-
const left = (window.innerWidth - width) / 2 + window.screenX;
74-
const top = (window.innerHeight - height) / 2 + window.screenY;
88+
const { left, top } = this.calculatePosition
89+
? this.calculatePosition(width, height)
90+
: {
91+
left: (window.innerWidth - width) / 2 + window.screenX,
92+
top: (window.innerHeight - height) / 2 + window.screenY,
93+
};
7594

7695
const popup = window.open(
7796
url,

0 commit comments

Comments
 (0)