Skip to content

Commit 6a45429

Browse files
captbaritoneclaude
andcommitted
Fix scroll offset in contained mode (renderInto)
centerWindows() was unconditionally adding window.scrollX/Y, which incorrectly offset window positions when using renderInto with a scrolled page. Move scroll offset responsibility to callers so each can decide: - contained (renderInto): no scroll offset needed - non-contained (renderWhenReady): add scroll offset to viewport coords - body fallback (centerWindowsInView): add scroll offset only for body Also make parentDomNode required in browserWindowSizeChanged, ensureWindowsAreOnScreen, and centerWindowsInView since the serialized state code (the only caller without a parentDomNode) was removed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f973319 commit 6a45429

1 file changed

Lines changed: 20 additions & 17 deletions

File tree

packages/webamp/js/actionCreators/windows.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,26 @@ export function centerWindowsInContainer(
112112
if (!Selectors.getPositionsAreRelative(getState())) {
113113
return;
114114
}
115-
const { left, top } = contained
116-
? { left: 0, top: 0 }
117-
: container.getBoundingClientRect();
115+
let left = 0;
116+
let top = 0;
117+
if (!contained) {
118+
const rect = container.getBoundingClientRect();
119+
left = rect.left + window.scrollX;
120+
top = rect.top + window.scrollY;
121+
}
118122
const { scrollWidth: width, scrollHeight: height } = container;
119123
dispatch(centerWindows({ left, top, width, height }));
120124
};
121125
}
122126

123-
export function centerWindowsInView(parentDomNode?: HTMLElement): Thunk {
124-
const { width, height } =
125-
parentDomNode === document.body || !parentDomNode
126-
? { width: window.innerWidth, height: window.innerHeight }
127-
: Utils.getElementSize(parentDomNode);
128-
return centerWindows({ left: 0, top: 0, width, height });
127+
export function centerWindowsInView(parentDomNode: HTMLElement): Thunk {
128+
const isBody = parentDomNode === document.body;
129+
const { width, height } = isBody
130+
? { width: window.innerWidth, height: window.innerHeight }
131+
: Utils.getElementSize(parentDomNode);
132+
const left = isBody ? window.scrollX : 0;
133+
const top = isBody ? window.scrollY : 0;
134+
return centerWindows({ left, top, width, height });
129135
}
130136

131137
type Box = {
@@ -155,12 +161,9 @@ export function centerWindows({ left, top, width, height }: Box): Thunk {
155161
const boxHeight = bounding.bottom - bounding.top;
156162
const boxWidth = bounding.right - bounding.left;
157163

158-
const offsetLeft = left + window.scrollX;
159-
const offsetTop = top + window.scrollY;
160-
161164
const move = {
162-
x: Math.ceil(offsetLeft - bounding.left + (width - boxWidth) / 2),
163-
y: Math.ceil(offsetTop - bounding.top + (height - boxHeight) / 2),
165+
x: Math.ceil(left - bounding.left + (width - boxWidth) / 2),
166+
y: Math.ceil(top - bounding.top + (height - boxHeight) / 2),
164167
};
165168

166169
const newPositions = windowsInfo.reduce(
@@ -180,7 +183,7 @@ export function browserWindowSizeChanged(
180183
height: number;
181184
width: number;
182185
},
183-
parentDomNode?: HTMLElement
186+
parentDomNode: HTMLElement
184187
): Thunk {
185188
return (dispatch: Dispatch) => {
186189
dispatch({ type: "BROWSER_WINDOW_SIZE_CHANGED", ...size });
@@ -244,14 +247,14 @@ export function setWindowLayout(layout?: WindowLayout): Thunk {
244247
};
245248
}
246249

247-
export function ensureWindowsAreOnScreen(parentDomNode?: HTMLElement): Thunk {
250+
export function ensureWindowsAreOnScreen(parentDomNode: HTMLElement): Thunk {
248251
return (dispatch, getState) => {
249252
const state = getState();
250253

251254
const windowsInfo = Selectors.getWindowsInfo(state);
252255
const getOpen = Selectors.getWindowOpen(state);
253256
const { height, width } =
254-
parentDomNode === document.body || !parentDomNode
257+
parentDomNode === document.body
255258
? Utils.getWindowSize()
256259
: Utils.getElementSize(parentDomNode);
257260
const bounding = Utils.calculateBoundingBox(

0 commit comments

Comments
 (0)